for loop table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
jayson.ph
Forum Contributor
Posts: 165
Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:

for loop table

Post by jayson.ph »

Hi all,

i have a table for loop with 20rows and 14columns and i have only 1 record here my db mysql. and now how to display a 1 record into 20rows that i set into loop. the 20rows that i set into loop it should be only 1record should be displayed, the rest 19 rows are empty?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: for loop table

Post by social_experiment »

The value that determines the amount of times the for loop is executed should be the amount of rows in your database.

Code: Select all

<?php
 $rows = mysql_num_rows($sql);

 for($i=0; $i<=$rows; $i++) { // loop will now execute until $i is equal to $rows
 // show data
 }
?>
Hope this helps :)
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
jayson.ph
Forum Contributor
Posts: 165
Joined: Mon Jan 02, 2012 9:20 am
Location: MP
Contact:

Re: for loop table

Post by jayson.ph »

thank for quick reply,
<?php
$rows = mysql_num_rows($sql);

for($i=0; $i<=$rows; $i++) { // loop will now execute until $i is equal to $rows
// show data
}
?>
and yes it work but no empty rows are happen?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: for loop table

Post by social_experiment »

Do you want to spread the single row over 20 rows?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: for loop table

Post by Celauran »

You want 20 rows no matter what?

Code: Select all

$i = 0;
foreach ($result as $row)
{
    // display results
    $i++;
}

for ($i; $i < 20; $i++)
{
    // empty row
}
Post Reply