Tutorial :How to increase mysql_fetch_array pointer?



Question:

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))         $row = mysql_fetch_array($result, MYSQL_ASSOC) //it's not increase ?  }  

i want increase two time in each loop?

for

<table>  <td>**1 times**</td><td>**1 times**</td>  </table>  


Solution:1

I think the documentation is very clear. http://hu.php.net/manual/en/function.mysql-fetch-array.php or http://hu.php.net/manual/en/function.mysql-fetch-assoc.php.

    mysql_connect("localhost", "mysql_user", "mysql_password") or      die("Could not connect: " . mysql_error());  mysql_select_db("mydb");    $result = mysql_query("SELECT id, name FROM mytable");    while ($row = mysql_fetch_array($result, MYSQL_NUM)) {      printf("ID: %s  Name: %s", $row[0], $row[1]);        }    mysql_free_result($result);  


Solution:2

You don't need that. If you want to print your data formatted in 2 columns, select it all into array and then use that array for the formatted output.

one of possible solutions

<?php  //collect data into array  $data = array();  while ($row = mysql_fetch_assoc($result)) {    $data[] = $row;  }  //and here goes template part  ?>  <html>  <? $data = array_chunk($data, 2) ?>  <table>  <? foreach ($data as $chunk): ?>    <tr>  <? foreach ($chunk as $row): ?>      <td><?=$row['name']?></td>  <? endforeach ?>    </tr>  <? endforeach ?>  </table>  


Solution:3

it WILL increase the pointer by 1 step...

therefore:

 while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {        // logic here     }  

will perform the loop and logic


Solution:4

Why not do something like this:

$max_row = 100;//mysql_num_rows($result);  for($idx = 0; $idx < $max_row; $idx+=2){  if (!mysql_data_seek($result, $i)) {      echo "Cannot seek to row $i: " . mysql_error() . "\n";          continue;      }        if (!($row = mysql_fetch_assoc($result))) {          continue;      }        //do what you want to do here...  }  

if you have 10 records, it will echo row 0, 2, 4, 6, 8.


Solution:5

<?php  $counter=0;    while ($row = mysql_fetch_array($result, MYSQL_ASSOC))  {         if ($counter % 2)         {             echo '<table><tr>'; // start table         }           echo '<td>' . $row['foo'] . '</td>'; // echo result           if (!$counter % 2)         {             echo "</tr></table>"; // end table         }         $counter++;  }  ?>  


Solution:6

Maybe you want mysql_data_seek?


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