开发者

How to increase mysql_fetch_array pointer?

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&开发者_运维问答gt;**1 times**</td>
</table>


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);


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>


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


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.


<?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++;
}
?>


Maybe you want mysql_data_seek?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜