获取php数组来调用以将每一行单独显示到表中

时间:2013-12-27 19:39:11

标签: php mysql mysqli

我正在创建事件CMS而且我在使用mysql中的表中获取数据时遇到了一些困难,以便显示我想要的格式。此外,由于某种原因,数据是在所有字段中复制事件编号。

<?php
    require("includes/con.php");
?>

<?php
    $queryVENUE = "SELECT * FROM venue ORDER BY event_date"; //pulls all data from the venue table.
    $resultVENUE = $mysqli->query($queryVENUE);

    $eventsNameArray = array();
    while ($rowEvents = $resultVENUE->fetch_assoc()) {
        $eventsNameArray[] = $rowEvents['event_id'] . " " . $rowEvents['event_date'] . " " . $rowEvents['event_name'] . " " . $rowEvents['event_location'] . " " . $rowEvents['event_details'] . " " . "&pound" . $rowEvents['event_ticket_cost']   ;
    }

    foreach($eventsNameArray as $eventsOutput){
        echo "<p>{$eventsOutput}</p>";

        echo "<H3>Event Number: {$eventsOutput['event_id']}</H3>";
        echo "<p>Event Date: {$eventsOutput['event_date']}</p>";
        echo "<p>Event Name: {$eventsOutput['event_name']}</p>";
        echo "<p>Event Location: {$eventsOutput['event_location']}</p>";
        echo "<p>Event Details: {$eventsOutput['event_details']}</p>";
        echo "<p>Event Ticket Cost: {$eventsOutput['event_ticket_cost']}</p>";
    }

我目前得到的输出是这样的: 1 2013-11-21 Xscape Castleford在中心下午5点见面。 £5.00

  

活动编号:1

     

活动日期:1

     

活动名称:1

     

活动地点:1

     

活动详情:1

     

活动门票费用:£1

2 2013-11-24乘坐Rossendale骑车前往曼彻斯特的Rossendale,并在假期前练习一下! £5.00

  

活动编号:2

     

活动日期:2

     

活动名称:2

     

活动地点:2

     

活动详情:2

     

活动门票费用:2

目的是显示以下内容: --row 1 -

  

活动编号:

     

活动日期:

     

活动地点:

     

活动详情:

- 第2行 -

  

活动编号:

     

活动日期:

     

活动地点:

     

活动详情:

此外,我想将每个部分的事件编号等放入单独的Div中,以便我可以用css设置样式。

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您将$eventsNameArray[]设置为同心字符串,看起来您需要将其作为数组进行操作。像 -

这样的东西
while ($rowEvents = $resultVENUE->fetch_assoc()) {
       $eventsNameArray[] = array(
              'event_id'=> $rowEvents['event_id'],
              'event_date'=> $rowEvents['event_date'], 
              'event_name'=> $rowEvents['event_name'],
              'event_location'=> $rowEvents['event_location'],
              'event_details'=> $rowEvents['event_details'],
              'event_ticket_cost'=> "&pound".$rowEvents['event_ticket_cost']
       );
}

或@ chas688指出,更简单 -

while ($rowEvents = $resultVENUE->fetch_assoc()) {
       $eventsNameArray[] = $rowEvents;
}

答案 1 :(得分:0)

尝试使用这样的数据群,你是否直接在数据库上检查了你的查询?

<?php
require("includes/con.php");
?>

    <?php
    $queryVENUE = "SELECT * FROM venue ORDER BY event_date"; //pulls all data from the venue table.
    $resultVENUE = $mysqli->query($queryVENUE);

    $eventsNameArray = array();
    while ($rowEvents = $resultVENUE->fetch_assoc()) {
    $eventsNameArray[] = $rowEvents['event_id'] . " " . $rowEvents['event_date'] . " " . $rowEvents['event_name'] . " " . $rowEvents['event_location'] . " " . $rowEvents['event_details'] . " " . "&pound" . $rowEvents['event_ticket_cost']   ;
    }
    ?>
    <table>
    <tr>
    <td>Event Number</td>
    <td>Event Date</td>
    <td>Event Name</td>
    <td>Event Location</td>
    <td>Event Details</td>
    <td>Event Ticket Cost</td>
    </tr>
    <?pho foreach($eventsNameArray as $eventsOutput){
    ?>
    <tr>


    <td><?php echo $eventsOutput['event_id']?></td>
    <td><?php echo $eventsOutput['event_date']?></td>
    <td><?php echo $eventsOutput['event_name']?></td>
    <td><?php echo $eventsOutput['event_location']?></td>
    <td><?php echo $eventsOutput['event_details']?></td>
    <td><?php echo $eventsOutput['event_ticket_cost']?></td>
    </tr>
    <?php }?>
    </table>