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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。