How to display MySQL results side by side
I have used the following code to display MySQL results in the past but now need to change the script to display the table side by side.
Example: It currently displays each result in a formatted table one under another like so
dj1: info
dj2: info
dj3: info
dj4: info
and i need to change the code so the results are formatted like this
dj1: info dj2: info
dj3: info dj4: info
I have tried, but just cant seem to get it to do what I want.
Here is the code.
<?php
$connection = mysql_connect("localhost","myuser","pass");
if (!$connection)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydb", $connection);
$link = mysql_query("SELECT name FROM djs") or die(mysql_error());
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 3;
$sql = "SELECT * FROM djs ORDER BY name ASC LIMIT $start_from, 3";
$rs_result = mysql_query ($sql,$connection);
?>
<table align="center" bordercolor="#CC0033">
<?php
while ($row = mysql_fetch_assoc($rs_result)) {
?>
<tr>
<td height="164" align="center" valign="middle" bgcolor="#333333">
<table width="526" border="0" cellpadding="0" cellspacing="0" bgcolor="#1F1F1F">
<tr>
<td width="117" rowspan="5"><img src="/take2<?php echo $row["photo1"]; ?>" width="110" height="136" /></td>
<td width="10"> </td>
<td width="138" bgcolor="#202020"><span class="style11">Name</span></td>
<td width="340" bgcolor="#202020"><span class="style13"><? echo $row["name"]; ?></span></td>
</tr>
<tr>
<td> </td>
<td bgcolor="#202020"><span class="style11">Dj Name</span></td>
<td bgcolor="#202020"><span class="style13"><? echo $row["handle"]; ?></span></td>
</tr>
<tr>
<td> </td>
<td bgcolor="#202020"><span class="style11">Email</span></td>
<td bgcolor="#202020"><span class="style13"><? echo $row["email"]; ?></span></td>
</tr>
<tr>
<td> </td>
<td bgcolor="#202020"><span class="style11">Profile</span></td>
<td bgcolor="#202020"><span class="style13"><? echo $row["profile"]; ?></span></td>
</tr>
<tr>
<td> </td>
<td bgcolor="#202020"><?php
echo '<td><a href="profile.php?id=' . $row["id"] . '">View More</a></td>';
?></td>
<td bgcolor="#202020"> </td>
</tr>
</table>
</td>
</tr>
<?php
};
?>
</table>
<div align="center">
<?php
$sql = "SELECT COUNT(name) FROM djs";
$rs_result = mysql_query($sql,$connection);
$row = mysql_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / 3);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='djs.php?page=".$i."'>".$i."</a> ";
};
?>
</div>
I am quite new to php and all the things i have tried have not worked.
Could anyone help me edit the code above to achieve this?If I have not explained it clear enough let me know.
All working now, thanks everyone for taking the time to help.
Updat开发者_开发知识库ed code below thanks to danishgoel:)
<?php
$connection = mysql_connect("localhost","myuser","pass");
if (!$connection)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydb", $connection);
$link = mysql_query("SELECT name FROM djs") or die(mysql_error());
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 3;
$sql = "SELECT * FROM djs ORDER BY name ASC LIMIT $start_from, 3";
$rs_result = mysql_query ($sql,$connection);
?>
<table align="center" bordercolor="#CC0033">
<?php
$column = 0;
while ($row = mysql_fetch_assoc($rs_result)) {
if($column == 0)
echo '<tr>';
?>
<td width="999" height="159" bgcolor="#333333">
<table width="400" border="0" cellpadding="0" cellspacing="0" bgcolor="#1F1F1F">
<tr>
<td width="117" rowspan="5"><img src="/take2<?php echo $row["photo1"]; ?>" width="110" height="136" /></td>
<td width="10"> </td>
<td width="138" bgcolor="#202020"><span class="style11">Name</span></td>
<td width="340" bgcolor="#202020"><span class="style13"><? echo $row["name"]; ?></span></td>
</tr>
<tr>
<td> </td>
<td bgcolor="#202020"><span class="style11">Dj Name</span></td>
<td bgcolor="#202020"><span class="style13"><? echo $row["handle"]; ?></span></td>
</tr>
<tr>
<td> </td>
<td bgcolor="#202020"><span class="style11">Email</span></td>
<td bgcolor="#202020"><span class="style13"><? echo $row["email"]; ?></span></td>
</tr>
<tr>
<td> </td>
<td bgcolor="#202020"> </td>
<td bgcolor="#202020"> </td>
</tr>
<tr>
<td> </td>
<td bgcolor="#202020"> </td>
<td bgcolor="#202020"> </td>
</tr>
</table>
</td>
<?php
if($column == 1)
echo '</tr>';
$column = ($column == 0 ? 1 : 0);
}; ?>
</table>
You need to have two columns in your table and use the while loop like this:
<?php
$column = 0;
while ($row = mysql_fetch_assoc($rs_result)) {
if($column == 0)
echo '<tr>';
?>
<td height="164" align="center" valign="middle" bgcolor="#333333">
<!-- Your data output -->
</td>
<?php
if($column == 1)
echo '</tr>';
$column = ($column == 0 ? 1 : 0);
};
This way each row will have 2 columns and you output your data alternatingly to each column.
Also you start a new row <tr>
before first column and end the row </tr>
after 2nd column
You need to understand the html float property
try using this
<div style='1000px;'>
<div style='float:left; width:500px;'>Your Data</div>
<div style='float:left; width:500px;'>Your Data</div>
</div>
You can wrap the above div around your PHP prints and loops, such as:
<div style='1000px;'>
<?php
while ($row = mysql_fetch_assoc($rs_result)){
print "<div style='float:left; width:500px;'>{$row['handle']}</div>";
}
?>
</div>
which one is the result you want to show?
what you can do is you can put an extra variable outsite your while loop something like this:
<table>
<tr>
<?php
$i=1;
while($rows = mysql_fetch_array($result)){
echo '<td>'.$rows['name'].'</td>';
if($i %2 == 0) { echo '</tr><tr>'; } // this will create the line separator by inserting a row when the $i is dividable by 2
}
?>
</tr>
</table>
you can also do this using HTML 'float'
<div style="width:500px;">
<?php
while($rows = mysql_fetch_array($result)){
echo '<div style="float:left; width:250px;">'.$rows['name'].'</div>';
}
?>
</div>
精彩评论