Jump to content

[SOLVED] print five rows even if records are less than 5


sayedsohail

Recommended Posts

Hi,

 

I wish to print five rows even if the fetched records are less than 5.

 

here is my code:

 

?><table><tr>

<?
while(list($id, $name) = mysql_fetch_array($sql)) 
{ 

print "<td><input type ='radio' value= '$id'></td>";
print"<td>$name</td></tr>";

}
echo "</table>";

Do you mean that you want to repeat the list if need be? e.g.

 

Query returns:

 

id    |    name

 

1          bob

2          steve

3          jill

 

You want the table to print out as

 

<table>

  <tr>

    <td><input type ='radio' value= '1'></td>

    <td>bob</td>

</tr>

 

<tr>

    <td><input type ='radio' value= '2'></td>

    <td>steve</td>

</tr>

 

<tr>

    <td><input type ='radio' value= '3'></td>

    <td>jill</td>

</tr>

 

<tr>

    <td><input type ='radio' value= '1'></td>

    <td>bob</td>

</tr>

 

<tr>

    <td><input type ='radio' value= '2'></td>

    <td>steve</td>

</tr>

 

</table>

 

Is this what your after?

 

Nate

i am able to repeat the list from sql statement, I wish to print blank tr td even if the records are less than 5 from my sql statement.  someone has suggested code below, but its giving an error

 

<?php 
for($i=0;$i<5;++$i) 
{ 
if while(list($id, $name) = mysql_fetch_array($sql)) 
{ 
  echo "<tr><td><input type ='radio' value= '$id'></td>"; 
  echo "<td>$name</td></tr>"; 
}    else { 
  echo '<tr><td></td><td></td></tr>';      
} 
} 
?> 

it was close

<?php 
for($i=0;$i<5;++$i) 
{ 
if (list($id, $name) = mysql_fetch_row($sql)) 
{ 
  echo "<tr><td><input type ='radio' value= '$id'></td>"; 
  echo "<td>$name</td></tr>"; 
}    
else { 
  echo '<tr><td> </td><td> </td></tr>';      
} 
} 
?> 

tak a look at the structure of this script, im sure you will find an answer:

 

define ("NUMCOLS",4);

$res = mysql_query("SELECT name, cover FROM movies ORDER BY id DESC LIMIT 4");

$count = 0;
echo "<TABLE>";
while (list($name, $cover) = mysql_fetch_row($res)) {

    if ($count % NUMCOLS == 0) echo "<TR>\n";  # new row

    echo "<TD>$name<br>$cover</TD>\n";
    $count++;

    if ($count % NUMCOLS == 0) echo "</TR>\n";  # end row
}

[color=red]# end row if not already ended

if ($count % NUMCOLS != 0) {
   while ($count++ % NUMCOLS) echo "<td> </td>";
   echo "</TR>\n";
}
echo "</TABLE>";[/color]

 

in red is what you after!

 

nita

Thank you barand me bad, it worked perfectly with little changes

 

for($p=0;$p<3;++$p)

{

if(list($id, $name, $address_1, $p_code, $city, $l_line, $mobile) = mysql_fetch_array($sql))

 

{  echo "<tr><td><input type ='radio' value= '$id'></td>";

  echo "<td>$name</td>";

  echo "<td>$address_1</td>";

  echo "<td>$p_code</td>";

  echo "<td>$city</td>";

  echo "<td>$l_line</td>";

  echo "<td>$mobile</td></tr>";  } 

 

else

{  echo "<tr><td colspan='7' align='center'> </td></tr>"; }

 

}

 

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.