Divyesh Jesadiya
Divyesh Jesadiya

Reputation: 957

how to execute two while loop at same time in php?

I am displaying data from database in a table.
In the table I have tr tags and in the tr tag I have two td tags.

Here is code.

<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>

What I want exactly is that the left td tag must display the have_img=1 values and the right td tags must display the have_img=0 values. i want to display like...

enter image description here

Please help me.

Upvotes: 0

Views: 4659

Answers (3)

Edwin Thomas
Edwin Thomas

Reputation: 1186

Try this...

<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>

Upvotes: 3

viral
viral

Reputation: 3743

Try fetching data in one query instead of two, and try to manipulate in your loop,

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

Let the query go and get results,

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

Use condition as your application structure,

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
    }
}

Upvotes: 2

Dharmesh Vasani
Dharmesh Vasani

Reputation: 475

<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
}
?>

Upvotes: 1

Related Questions