繁体   English   中英

如何并排显示MySQL结果

[英]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">&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>

您要显示哪个结果?

您可以做的是,可以在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM