[英]How to display MySQL results side by side
我過去曾使用以下代碼顯示MySQL結果,但現在需要更改腳本以並排顯示表。
示例:當前,它在格式化的表中將每個結果顯示在另一個表中,如下所示
dj1: info
dj2: info
dj3: info
dj4: info
我需要更改代碼,以便結果的格式如下
dj1: info dj2: info
dj3: info dj4: info
我已經嘗試過,但是似乎無法讓它做我想要的事情。
這是代碼。
<?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>
我對php很陌生,我嘗試過的所有方法都沒有用。
誰能幫我編輯上面的代碼來實現這一目標?
如果我沒有解釋清楚,請告訴我。
現在所有人都在工作,謝謝大家花時間提供幫助。
感謝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>
您要顯示哪個結果?
您可以做的是,可以在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>
您也可以使用HTML“浮動”
<div style="width:500px;">
<?php
while($rows = mysql_fetch_array($result)){
echo '<div style="float:left; width:250px;">'.$rows['name'].'</div>';
}
?>
</div>
您需要在表中有兩列,並使用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);
};
這樣,每一行將有2列,並且您將數據交替輸出到每一列。
同樣,您在第一列之前開始新的行<tr>
在第二列之后結束行</tr>
您需要了解html float屬性
嘗試使用這個
<div style='1000px;'>
<div style='float:left; width:500px;'>Your Data</div>
<div style='float:left; width:500px;'>Your Data</div>
</div>
您可以將上述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.