Jump to content

mysql loop


project3

Recommended Posts

Im having trouble figuring out an easy way to do this.

 

What i am doing is looping trough mysql results.

 

<table>

 

results

 

<table>

 

what i want to do is in the table I want to have three results in each row.

 

how do i go about this?

 

I thought about puting a counter and doing an if statement on the number to put the <tr></tr>

but the only problem is there could be 500 results so I can't really put 500 if statements in there

 

any help would be great.

Link to comment
https://forums.phpfreaks.com/topic/95913-mysql-loop/
Share on other sites

try something like this:

 

<?php
// assuming query is above
$counter = 0;

while($arr = msyql_fetch_assoc($run)){
  extract($arr);
  $counter++;
  
  // if divisible by 3
  if($counter % 3 == 0){
    echo "<tr>";
  }

// display td
  echo "<td>$result</td>";

  // if divisible by 3
  if($counter % 3 == 0){
    echo "</tr>";
  }
}
?>

 

I haven't tested this and I'm very tired, but something like this I think would work.

Link to comment
https://forums.phpfreaks.com/topic/95913-mysql-loop/#findComment-491057
Share on other sites

try something like this:

 

<?php
// assuming query is above
$counter = 0;

while($arr = msyql_fetch_assoc($run)){
  extract($arr);
  $counter++;
  
  // if divisible by 3
  if($counter % 3 == 0){
    echo "<tr>";
  }

// display td
  echo "<td>$result</td>";

  // if divisible by 3
  if($counter % 3 == 0){
    echo "</tr>";
  }
}
?>

 

I haven't tested this and I'm very tired, but something like this I think would work.

 

I ended up doing something very similar. i just would start the counter over after three and have it set to do <tr> at one and </tr> at three.

 

Thanks very much for your reply

Link to comment
https://forums.phpfreaks.com/topic/95913-mysql-loop/#findComment-491066
Share on other sites

p2grace,

 

move the $counter++ to just before the second test for $counter%3

 

<?php
// assuming query is above
$counter = 0;

while($arr = msyql_fetch_assoc($run)){
  extract($arr);
  
  // if divisible by 3
  if($counter % 3 == 0){
    echo "<tr>";
  }

// display td
  echo "<td>$result</td>";

  $counter++;
  // if divisible by 3
  if($counter % 3 == 0){
    echo "</tr>";
  }
}
?>

Link to comment
https://forums.phpfreaks.com/topic/95913-mysql-loop/#findComment-491083
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.