View Single Post
  #2 (permalink)  
Old 12-17-03, 04:31 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
loops are something useful in PHP
try to use FOR .. that's how is can be used :
PHP Code:

$names=array('NeverMind','tsb','Ryan');

$num=count($names);

for ( 
$i=1$i<$num$i++ ) {
  
$counter=$i-1
  
echo"$i$names[$counter] .";

and if you want to use it when you fetch data from databases like MySQL e.g. you need to make sure that mysql_fetch_array() is inside the loop not outside it.. like this :
PHP Code:

$fetch=mysql_query("SELECT * FROM table")or

die(
'couldn\'t excute query: '.mysql_error());

$num=mysql_num_rows($fetch);

for ( 
$i=1$i<=$num$i++ ) {
  
$data=mysql_fetch_array($fetch);
  
//we put it inside loop not outside it,
  //otherwise it will print the same data through the loop
  
  
echo $data['ID'], $data['name']; 

BTW if you want increment of a variable by 1 , you could use $var++ ..
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]

Last edited by NeverMind; 12-17-03 at 04:38 AM.
Reply With Quote