Jump to content

Help me to display mysql data in website like as i want!


GFXUniverse

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>

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.