3
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>
4
  • 2
    Plase explain what exactly is your question Commented Aug 8, 2010 at 8:54
  • 2
    What do you want to do exactly? Commented Aug 8, 2010 at 8:54
  • 2
    It should indeed increase! From php documentation: mysql_fetch_array - Returns an array that corresponds to the fetched row and moves the internal data pointer ahead. Note that since you fetch a row both in the while condition, and inside the while loop itself, you will move two rows for each iteration... Commented Aug 8, 2010 at 9:01
  • Your question has nothing to do with pointers, it's only about "how to manage html-tables inside a loop" Commented Aug 8, 2010 at 11:15

6 Answers 6

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

0

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

1 Comment

use mysql_data_seek() to move to the item and use mysql_fetch_assoc() to get the result. see php.net/manual/en/function.mysql-data-seek.php or you can execute mysql_fetch_array() twice (not recommend)
0

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.

Comments

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

Comments

-1

Maybe you want mysql_data_seek?

1 Comment

@monkey_boys, The only thing mysql_data_seek is touch the internal pointer. I'm not sure what you're after...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.