如何并排显示MySQL结果

时间:2011-09-15 10:12:59

标签: php mysql html-table

我以前使用以下代码显示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">&nbsp;</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>&nbsp;</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>&nbsp;</td>
            <td bgcolor="#202020"><span class="style11">Email</span></td>
            <td bgcolor="#202020"><span class="style13"><? echo $row["email"]; ?></span></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td bgcolor="#202020"><span class="style11">Profile</span></td>
            <td bgcolor="#202020"><span class="style13"><? echo $row["profile"]; ?></span></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td bgcolor="#202020"><?php
echo '<td><a href="profile.php?id=' . $row["id"] . '">View More</a></td>';
?></td>
            <td bgcolor="#202020">&nbsp;</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">&nbsp;</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>&nbsp;</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>&nbsp;</td>
            <td bgcolor="#202020"><span class="style11">Email</span></td>
            <td bgcolor="#202020"><span class="style13"><? echo $row["email"]; ?></span></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td bgcolor="#202020">&nbsp;</td>
            <td bgcolor="#202020">&nbsp;</td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td bgcolor="#202020">&nbsp;</td>
            <td bgcolor="#202020">&nbsp;</td>
          </tr>
        </table>

       </td>

       <?php 
        if($column == 1)
            echo '</tr>';
        $column = ($column == 0 ? 1 : 0);
       }; ?>

     </table>

3 个答案:

答案 0 :(得分:0)

你要展示哪一个结果?

你可以做的是你可以在你的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'flora'

来完成此操作
<div style="width:500px;">
<?php
    while($rows = mysql_fetch_array($result)){
        echo '<div style="float:left; width:250px;">'.$rows['name'].'</div>';
}
?>
</div>

答案 1 :(得分:0)

您的表中需要有两列,并使用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>

答案 2 :(得分:0)

您需要了解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>
相关问题