dougang5993 2015-06-18 11:21
浏览 63
已采纳

如何在表中显示mysql多行/ mysql_fetch_array结果?

I am trying to show mysql_fetch_array() results in a table.

I want to show guests name,their country and their agreed time who are traveling on a same date.

Following code works fine. The code fetches the row values and prints it.

 $select_guests = mysql_query('SELECT name FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); // query for getting guests for the same date
while($row = mysql_fetch_array($select_guests, MYSQL_ASSOC)) { //visitor / guest loop starts here
    echo $row['name'].'<br/>';
}
$select_country = mysql_query('SELECT country FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); // query for getting guests for the same date
while($row = mysql_fetch_array($select_country, MYSQL_ASSOC)) { //country of visitor / guest loop starts here
    echo $row['country'].'<br/>';
}
$select_agreed_time = mysql_query('SELECT agreed_time FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); // query for getting guests for the same date
while($row = mysql_fetch_array($select_agreed_time, MYSQL_ASSOC)) { //visitor / guest agreed time loop starts here
    echo $row['agreed_time'].'<br/>';
}

if there are 5 guests for a same date I am getting all of their names one below another when I execute the above code. Same I am getting there countries and agreed time too.

Now I want to show those results in a HTML table. I tried several line of code but nothing works.

My HTML table should be as following:

<table class="table-fill">
    <thead>
        <tr>
            <th class="text-left">Name</th>
            <th class="text-left">From</th>
            <th class="text-left">Agreed Time</th>
        </tr>
    </thead>
    <tbody class="table-hover">
        <tr>
            <td class="text-left">Name 1</td>
            <td class="text-left">Country 1</td>
            <td class="text-left">Ag Time 1</td>
        </tr>
        <tr>
            <td class="text-left">Name 2</td>
            <td class="text-left">Country 2</td>
            <td class="text-left">Ag Time 2</td>
        </tr>
        <tr>
            <td class="text-left">Name 3</td>
            <td class="text-left">Country 3</td>
            <td class="text-left">Ag Time 3</td>
        </tr>
        <tr>
            <td class="text-left">Name 4</td>
            <td class="text-left">Country 4</td>
            <td class="text-left">Ag Time 4</td>
        </tr>
        <tr>
            <td class="text-left">Name 5</td>
            <td class="text-left">Country 5</td>
            <td class="text-left">Ag Time 5</td>
        </tr>
    </tbody>
</table>

How can create that table td s according to my mysql_fetch_array() ? The above table structure is for 5 guests found or resulted by mysql_fetch_array()

  • 写回答

3条回答 默认 最新

  • dsztc99732 2015-06-18 11:30
    关注

    First of all I think you dont need 3 different queries for your solution..

        <table class="table-fill">
                <thead>
                    <tr>
                        <th class="text-left">Name</th>
                        <th class="text-left">From</th>
                        <th class="text-left">Agreed Time</th>
                    </tr>
                </thead>
            <?php 
            $result = mysql_query('SELECT name,country,agreed_time FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); 
            while($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
            { 
            ?>
              <tr>
                   <td>
                       <?php    echo $row['name']; ?>
                   </td>
                   <td>
                       <?php     echo $row['country'];?>
                   </td>
                    <td>
                       <?php     echo $row['agreed_time']; ?>
                   </td>    
        </tr>
        <?php
        }
    ?>
    </table>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码