开发者

How to run a While statement inside of a While statement to fetch values from a database?

Here is my function:

        $result_display = mysql_query("SELECT * FROM Events ORDER BY TimeStamp DESC LIMIT $y, 20");
        $result_comment = mysql_query("SELECT * FROM Comments");


while($row_display = mysql_fetch_array($result_display)){
    echo "<div id='id'" . $row_display['ID'] . " class='eventdiv' data-sort='".$row_display['TimeStamp']."'>".$row_display['Title']." TIME: ".$row_display['TimeStamp']."</div>";

    while($row_comment = mysql_fetch_array($result_comment)){
        echo $row_comment['com_details'];
    }

}

The HTML output is similar to this:

<div id='id12' class='eventdiv' data-sort='238781>Time: 238781</div>
<div id='id13' class='eventdiv' data-sort='238784>Time: 238784</div>
<div id='id14' class='eventdiv' data-sort='238785>Time: 238785</div>
<div id='id15' class='eventdiv' data-sort='238789>Time: 238789</div>
<div id='id16' class='eventdiv' data-sort='238791>Time: 238791</div>
<div id='id17' class='eventdiv' data-sort='238795>Time: 238795</div>

These are the comment details...

But I would like the output to be like this:

<div id='id12' class='eventdiv' data-sort='238781>Time: 238781</div>

These are the comment details...

<div id='id13' class='eventdiv' data-sort='238784>Time: 238784</div>

These are the comment details...

<div id='id14' class='eventdiv' data-sort='238785>Time: 238785</div>

These are the comment details...

<div id='id15' class='eventdiv' data开发者_StackOverflow社区-sort='238789>Time: 238789</div>

These are the comment details...

<div id='id16' class='eventdiv' data-sort='238791>Time: 238791</div>

These are the comment details...

<div id='id17' class='eventdiv' data-sort='238795>Time: 238795</div>

These are the comment details...

I thought that the way I have a while statement inside of a while statement it would display it like the HTML above, but it doesn't.


After you run through your $result_comment results the first time, you have to reset it with mysql_data_seek( $result_comment ). So:

while($row_display = mysql_fetch_array($result_display)){
    echo "<div id='id'" . $row_display['ID'] . " class='eventdiv' data-sort='".$row_display['TimeStamp']."'>".$row_display['Title']." TIME: ".$row_display['TimeStamp']."</div>";

    while($row_comment = mysql_fetch_array($result_comment)){
        echo $row_comment['com_details'];
    }

    mysql_data_seek( $result_comment );

}


You'll need to run a query for each element in the first while loop. Currently, there's nothing that associates the comments with each event:

$result_comment = mysql_query("SELECT * FROM Comments WHERE [id]=$id");


@DemianBrecht, I am going to associate the two tables I just have not done so yet. There doesn't need to be an association to get what I need- @Jed, I'm not sure why but the solution you proposed is producing this error:

'Wrong parameter count for mysql_data_seek()'

I found out why it was not working, the second fetch function needed to be placed inside of the first's while statement. Otherwise, it will just execute once it is completed:

    $result_display = mysql_query("SELECT * FROM Events ORDER BY TimeStamp DESC LIMIT $y, 20");

while($row_display = mysql_fetch_array($result_display)){
    echo "<div id='id'" . $row_display['ID'] . " class='eventdiv' data-sort='".$row_display['TimeStamp']."'>".$row_display['Title']." TIME: ".$row_display['TimeStamp']."</div>";

    while($row_comment = mysql_fetch_array($result_comment)){

       $result_comment = mysql_query("SELECT * FROM Comments");
       echo $row_comment['com_details'];

    }

}

Simple enough.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜