Jump to content

help please


djrichwz

Recommended Posts

Hi I am trying to display items from a mysql database in a table but I need it to start a new table row each time multiples of 3 rows of the database table have been displayed like the following example:

 

item 1  item 2  item3

 

item 4  item 5  item 6

 

Does anybody know how I can make this work using php.

Link to comment
Share on other sites

<?php
  print '<table cellspacing="0" cellpadding="2" border="1">';
  $query=mysql_query("SELECT items FROM table");
  while ($row=mysql_fetch_assoc($query)) {
    print '<tr><td>'.$row[0].'</td><td>'.$row[1].'</td><td>'.$row[2].'</td></tr>';
  }
  print '</table>';
?>

Link to comment
Share on other sites

Hi I tried the code Below with my database table inserted instead and it did not show any of my results have you any idea why this wouldnt work.

 

I want to display each row of my table until it gets to three rows then start a new table row.

 

Hope somebody can help?

 

<?php

  print '<table cellspacing="0" cellpadding="2" border="1">';

  $query=mysql_query("SELECT msg FROM news");

  while ($row=mysql_fetch_assoc($query)) {

    print '<tr><td>'.$row[0].'</td><td>'.$row[1].'</td><td>'.$row[2].'</td></tr>';

  }

  print '</table>';

?>

Link to comment
Share on other sites

I tried to figure out your problem using a nested for loop. I was not able to get it to work since my inside loop only works once. However this is in the direction of what you need. Perhaps you can work from this. If someone else can figure out why the inner loop only works once, then this will work. I would definately like to know.

 

To test this just change:

 

$total_table_rows = ceil($total_results / $max_rows);

to

$total_table_rows = 15; (or any number you like)

 

<?php
$max_colums = 3;  //Set the number of columns you want in your table
$total_results = mysql_result($result,0);     //Get the number of results from your query
$total_table_rows = ceil($total_results / $max_rows);  //Figure out how many rows needed in table
?>
<table>
<?php
for($i = 1; $i <= $total_table_rows; $i++)
{

$from = (($i * $max_rows) - $max_rows);     
echo "<tr><td>$i</td>"; //Show which table row it is

for ( $j = $from; $j < $max_rows ; $j++) 
{
echo "<td>result: $j</td>";
}

echo "</tr>\n";
}

?>
</table>

Link to comment
Share on other sites

I don't know where djrichwz is at, but im still curious about this.

 

Heres what the output of my "solution" looks like:

<table border="1">
<tr><td>1</td><td>result: 0</td><td>result: 1</td><td>result: 2</td></tr>
<tr><td>2</td></tr>
<tr><td>3</td></tr>
<tr><td>4</td></tr>
<tr><td>5</td></tr>
</table>

 

It is still stopping after only working once.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.