如何在php中同时执行两个while循环?

时间:2015-08-22 10:47:25

标签: php

我在表格中显示数据库中的数据 在表格中我有tr个标签,在tr标签中我有两个td标签。

这是代码。

<table class="rag_table" rules="all">
<tr><th><font size="5">Technical Events</font></th><th><font size="5">Non-technical events</font></th></tr>
<?php
$query="SELECT id,name FROM event_content WHERE have_img=1";
$havepage=mysql_query($query);
$query1="SELECT id,name FROM event_content WHERE have_img=0";
$havepage1=mysql_query($query1);
?>
while ($row=mysql_fetch_array($havepage)) {
    ?>
    <tr><td><a href="robo_race.php?eve=eve$evt_id=<?php echo $row['id'];?>"><font color="#FC2B5F"><?php echo $row['name'];?></font></a></td>
    <?php
}
while ($row1=mysql_fetch_array($havepage1)) {
    ?>
    <td><?php echo $row1['name'];?></td></tr>
    <?php
}
?>
</table>

我想要的是左td标记必须显示have_img=1值,而右td标记必须显示have_img=0值。 我希望显示... ...

enter image description here

请帮帮我。

3 个答案:

答案 0 :(得分:3)

试试这个......

<table class="rag_table" rules="all">
    <tr>
        <th><font size="5">Technical Events</font></th>
        <th><font size="5">Non-technical events</font></th>
    </tr>
    <?php
    $query="SELECT id,name FROM event_content WHERE have_img=1";
    $havepage=mysql_query($query);
    $query1="SELECT id,name FROM event_content WHERE have_img=0";
    $havepage1=mysql_query($query1);
    while ($row=mysql_fetch_array($havepage))
    {
    ?>
        <tr>
            <td>
                <a href="robo_race.php?eve=eve$evt_id=<?php echo $row['id'];?>"><font color="#FC2B5F"><?php echo $row['name'];?></font></a>
            </td>
        <?php
        $check_right_td=0;
        while ($row1=mysql_fetch_array($havepage1))
        {
            ?>
            <td>
                <?php echo $row1['name'];?>
            </td>
        </tr>
        <?php
        $check_right_td=1;
        break;
        }
        if($check_right_td==0)
        {
        ?>
            <td></td>
        <?php
        }
    }
    ?>
</table>

答案 1 :(得分:2)

尝试在一个查询而不是两个查询中获取数据,并尝试在循环中进行操作,

$query="SELECT id,name FROM event_content"; // will get you have_img=1 and have_img=0

让查询结果并获得结果,

while($row=mysql_fetch_array($havepage)) {
    $res[] = $row; // collect the mysql data in array
}

使用条件作为您的应用程序结构,

foreach($res as $record)
{
    // echo something common in both cases    

    if($record['have_img'] == 1)
    {
        // echo something for have_img=1
    }
    if($record['have_img'] == 0)
    {
        // echo something for have_img=0
    }
}

答案 2 :(得分:1)

<table class = "rag_table" rules = "all">
<tr><th><font size = "5">Technical Events</font></th><th><font size = "5">Non-technical events</font></th></tr>
<?php
$query = "SELECT id,name,have_img FROM event_content WHERE have_img IN (0,1)";
$havepage = mysql_query($query);

while ($row = mysql_fetch_array($havepage)) {
    $leftTd = $rightTd = '';
    if ($row['have_img'] == 1) {
        $leftTd = $row['name'];
    } else {
        $rightTd = $row['name'];
    }
    ?>
    <tr>
        <td>
            <a href="robo_race.php?eve=eve$evt_id=<?php echo $row['id']; ?>">
                <font color="#FC2B5F"><?php echo $leftTd; ?></font>
            </a>
        </td>
        <td>
            <a href="robo_race.php?eve=eve$evt_id=<?php echo $row['id']; ?>">
                <font color="#FC2B5F"><?php echo $rightTd; ?></font>
            </a>
        </td>
    </tr>
    <?php
}
?>