[英]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. 我过去曾使用以下代码显示MySQL结果,但现在需要更改脚本以并排显示表。
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. 我对php很陌生,我尝试过的所有方法都没有用。
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. 现在所有人都在工作,谢谢大家花时间提供帮助。
Updated code below thanks to danishgoel:) 感谢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>
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: 您可以做的是,可以在while循环中放置一个额外的变量,例如:
<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' 您也可以使用HTML“浮动”
<div style="width:500px;">
<?php
while($rows = mysql_fetch_array($result)){
echo '<div style="float:left; width:250px;">'.$rows['name'].'</div>';
}
?>
</div>
You need to have two columns in your table and use the while loop like this: 您需要在表中有两列,并使用while循环,如下所示:
<?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. 这样,每一行将有2列,并且您将数据交替输出到每一列。
Also you start a new row <tr>
before first column and end the row </tr>
after 2nd column 同样,您在第一列之前开始新的行
<tr>
在第二列之后结束行</tr>
You need to understand the html float property 您需要了解html float属性
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包裹在您的PHP打印和循环中,例如:
<div style='1000px;'>
<?php
while ($row = mysql_fetch_assoc($rs_result)){
print "<div style='float:left; width:500px;'>{$row['handle']}</div>";
}
?>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.