Tutorial :Exists any alternative more beautiful at this loop?



Question:

$i=0;  while ($row=mysql_fetch_assoc()) {  if ($i==0) echo "First"  $i++;  }  

Access directly to mysqli pointer? a php class like's Iterator?

Thanks.


Solution:1

I’d use this:

if ($row = mysql_fetch_assoc()) {      // process first item      while ($row = mysql_fetch_assoc()) {          // process following items      }  }  


Solution:2

$first = true;  while ($row=mysql_fetch_assoc()) {     if ($first) echo "First"     $first = false;  }  


Solution:3

If you're trying to use the first element for something special, then maybe something like:

$row=mysql_fetch_assoc();  //do stuff to first row  do {      //do stuff to all rows (including the first)  } while ($row=mysql_fetch_assoc());  

Otherwise I have no idea what the question is and I'm not a PHP guy...


Solution:4

Do not have any method for know exactly position in all moment?

for example java iterator implements next or hasNext(), if !hasNext() the item is the last.


Solution:5

well, if i understand your question correctly, you want the first row and then iterate through the others:

$row = mysql_fetch_assoc(); // $row contains first row    while ($row = mysql_fetch_assoc()) { // loop through the rest of the rows    }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »