Help with limiting table rows please

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.

In your loop you can just have a counter and everytime that counter hits three add in the appropriate tags.

[php]<?php
$count = 0;
while(…)
{
if($count < 4)
{

<?php echo $myinfo; ?>
$count++;
}
else
{
?>


<?php echo $myinfo; ?>
<?php
$count = 0;
}
}
?>[/php]

Something along those lines should get ya what ya need.

thanks very much ill try it when i get back from work later

I want to display three pieces of information to each table column as shown below:

Item 1 Item 1 Item 1
Item 2 Item 2 Item 2
Item 3 Item 3 Item 3

But still when the amount of results displayed gets to count = 3 I want It to crwate a news row of information again with 3 pieces of information for each column.

Can anybody help me with this please as I really cant figure it out and I need it for most of my websites.

Thanks in advance

Sry i don’t understand why the solution prowided by Ragster00 does not work for u?
have u tryed to write some code based on Ragster00’s example?
could u post is and tell us more specific what’s still missing?

Here is the code I have used to get the result below:

[code]$query = “SELECT * FROM news”;

$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());

$count = 0;
?>

<?php

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

If($count <3)
{
?>

<?php $count++; }else{ ?> <?php $count = 0; }

// End of while statement
}
?>

<?php echo $row['profile']; ?>
<?php echo $row['profile']; ?>
[/code]

Here is the result I get:

Midnight Racer Midnight Racer Midnight Racer
Midnight Racer

Here is the result I want to get to:

Midnight Racer Midnight Racer Midnight Racer
Profile 1 Profile 2 Profile 3
Midnight Racer
Profile 4

As you can see above the loop works to display one piece of information then start a new row but I want to display the first row of information then start a new row to display the profile information. Then when 3 results have been displayed start a new row and display the 4th result onwards with the profile underneath as shown above.

Hopefully this explains what i am trying to do I hope somebody will be able to let me know how I can do this as I have tried a veriety of things but it never looks how I want it.

first of all: delete the line
$row = mysql_fetch_array($result) or die(mysql_error());
otherwise the first result will be missing

where is the data “Profile 1, Profile 2, …” stored in?

profile is stored in the mysql table news under the heading profile hope you can help

“Midnight Racer” is the text stored in ur database in the table news in the field profile, isn’t it?
So whats does the dummy text “Profile 1” stand for. please give an example.

could u please tell us about ur database strucktur?

I know what u want but how to do it depends.

The most general way is to collect the data with a while loop in two arrays.
And then inside the else put 2 foreach loops to echo them out.

But if u wanna echo big data or if u need a second mysqlquery there are better ways.

Id (Int) (auto Increment)
dateins (date)
Profile (Varchar) (45)
Msg (varchar) (45)

I would like to display as the following:

Midnight Racer Cool Racer
24/07/2007 25/07/2007
This is my car spec. This is the next car spec.

The next results…

I would like to display the results as show above but when It displays three results it needs to create a new table row and display the next 3 results etc…

If you need any more information please let me know

Ok… so you want your table formatted as such:


Midnight Racer | Cool Racer

24/07/2007 | 25/07/2007

This is my car spec. | This is the next car spec

Is this correct?

And is this how the information corrolates to the database?


Profile
dateins

Msg

i wound go for the solution mentiont by me already:

  1. collect the 3 rows insite an array:
    [php]
    $threerows=array();
    while($row=mysql_query() and $count<3)
    $threerows[]=$row;
    if(!count($threerows)) break;
    [/php]

  2. build all of the tablerows:
    [php]
    echo ‘

    ’;
    foreach($threerows as $onerow)
    echo ‘’.$onerow[‘profile’].’’;
    echo ‘’;

echo ‘

’;
foreach($threerows as $onerow)
echo ‘’.$onerow[‘dateins’].’’;
echo ‘’;

echo ‘

’;
foreach($threerows as $onerow)
echo ‘’.$onerow[‘Msg’].’’;
echo ‘’;
[/php]

of cause u don’t have 2 use an array. but as u use then in a diffrten order u have to store them somewhere.

when using that solution I get the following error:

Warning: Wrong parameter count for mysql_query() line 16

this is line 16

while($row=mysql_query() and $count<3)

any ideas why this is happening

Hi ithink i have worked out a way to do this but not sure how to put it in code:

I want to create an array with the value of the current results
then for each result that is less than 4 print out

dateins

then the same for the rest of the results

Is this right and can you tell me how to do this in php code

Hi, This post is very informative, however I would like some specific information. If someone can help me then please send me a private message. Best Regards,

Sponsor our Newsletter | Privacy Policy | Terms of Service