Jump to content

Recommended Posts

Hi, guyz, this is what m loking for,

 

for example, i have a table in mysql database with only one column

and this is what i usually do to display data

(m not writing the actual code, just typing simple words)

 

<table>
<?php
while ($row = mysql_fetch_array($sql))
{
?>
<tr>
   <td> 
        <?php echo $row['record']; ?><br> 
   </td>
</tr>
<?php
}
?>
</table>

 

Result:

Recored1

Recored2

Recored3

.

.so on

 

But I want to display data in table like this

s1t56t.jpg

 

in rows, i mean, first 4 records must be displayed in first row using 4 colums, (and only 4 records in a row)

 

Please help me!

 

<table>
<?php
$rowcount = 0;
while ($row = mysql_fetch_array($sql))
{
if ($rowcount % 4 == 0)
{
echo '<tr>';
}
?>
   <td>
        <?php echo $row['record']; ?><br>
   </td>
<?php
if ($rowcount % 4 == 0)
{
echo '</tr>';
}
$rowcount++;
}
?>
</table>

no working :(

look the Result is:

 

157f7tf.jpg

 

my exact code:

 

<table border="1" align="center" cellpadding="5" >
<?php
$rowcount = 0;
while($rows = mysql_fetch_array($result)) 
{
if ($rowcount % 4 == 0)
{
echo '<tr>';
}
?>
   <td width="100" height="50">
        <?php echo $rows['name']; ?><br>
   </td>
<?php
if ($rowcount % 4 == 0)
{
echo '</tr>';
}
$rowcount++;
}
?>
</table>

with the help of joel24's code i tried to modify this code little bit, and it worked now!!! :)

 

here is the code

<table border="0" align="center" cellpadding="5" >
<?php
$tr = 1;
$td = 4;
while($rows = mysql_fetch_array($result)) 
{
if ($tr == 1)
{
echo '<tr>';
}
?>
   <td>
        <?php echo $rows['name']; ?><br>
   </td>
<?php
if ($tr == 4)
{
echo '</tr>';
$tr = 1;
} 
else 
{
$rowcount++;
}
}
?>
</table>

 

with table border = 1, it looks %100 correct only if database fills all the cells in the table otherwise i have to keep the table border = 0.

thats fine.

 

Thanks man for giving me little time,

and guyz please tell me if any other improvement needed.

Sorry the old code was incorrect. this one is correct

 

<table border="0" align="center" cellpadding="5" >
<?php
$$columns= 4;
$r = 1;
while($rows = mysql_fetch_array($result)) 
{
if ($tr == 1)
{
echo '<tr>';
}
?>
   <td>
        <?php echo $rows['name']; ?><br>
   </td>
<?php
if ($tr == $columns)
   {
   echo '</tr>';
   $tr = 1;
   } 
else 
   {
   $tr++;
   }
}
?>
</table>

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.