Jump to content
Updating IPB tonight Read more... ×

Archived

This topic is now archived and is closed to further replies.

hackerkts

2 Queries In A Loop ?

Recommended Posts

I have stored informations in database, and I wanna loop them out.
Let's say I have 20 rows of informations in database, and I wanna echo them out by putting them in this format.

[code]<table width="234" border="1">
    <tr>
        <td><div align="center">Index1</div></td>
        <td><div align="center">File1</div></td>
        <td><div align="center">Index10</div></td>
        <td><div align="center">File10</div></td>
    </tr>
</table>[/code]

[code]    <tr>
        <td><div align="center">Index1</div></td>
        <td><div align="center">File1</div></td>
        <td><div align="center">Index10</div></td>
        <td><div align="center">File10</div></td>
    </tr>[/code]

Basically, it's just look like:
|Index1|File1|Index11|File11|
|Index2|File2|Index12|File12|
|Index3|File3|Index13|File13|
|Index4|File4|Index14|File14|
|Index5|File5|Index15|File15|
|Index6|File6|Index16|File16|
|Index7|File7|Index17|File17|
|Index8|File8|Index18|File18|
|Index9|File9|Index19|File19|
|Index10|File10|Index20|File20|

I only could think of making 2 queries but I can't figure out how to do 2 queries in 1 loop.
Anyone has an idea ?

Share this post


Link to post
Share on other sites
Prehaps having a read of [a href=\"http://www.phpfreaks.com/forums/index.php?showtopic=95443\" target=\"_blank\"]this thread[/a] may help.

Share this post


Link to post
Share on other sites
I have read that, but I still can't get 2 query working on the same loop.
I would either get a page which keeps loading (some problem with the scripts, it just keep looping and does the query) or not working properly.

Do you have another suggestion ?
Btw... Thanks for replying.

Share this post


Link to post
Share on other sites
May I ask why you think you need to make two queries? What you described sounded like a single query would work. Something like...
$_q = "SELETC * FROM $_table";
then take the result and
$_string = '<table>';
while ($_data = mysql_fetch_array($result, MYSQL_NUM)) // something like this, I'll let you double check
{
$_string .= '<tr><td>' . $_data[0] . '</td>' . '<td>' .$_data[1] . '</td>' ... etc etc ;
}
$_string .= '</table>';
echo $_string;

Share this post


Link to post
Share on other sites
You could try a thing like

$res1 = mysql_query("select * from foo order by id asc limit 0,10");
$res2 = mysql_query("select * from foo order by id asc limit 10,10")

which means you have two queries which one begins from 0th index and other from 10th
and your loop should be something like
[code]
while ($row = mysql_fetch_row($res1) )
{
echo $row[0] . $row[1];
$row = mysql_fetch_row($res2)
echo $row[0] . $row[1];
}
[/code]

It should work.

Share this post


Link to post
Share on other sites
Sorry for the late reply, thanks for replying and helping me.
As to why I think I need 2 queries because I still doesn't get array into hand.
Array is a weird thing :(

I will try the examples now, thanks again. :D

Share this post


Link to post
Share on other sites
Sorry for the double post, I just wanna say thanks !
It's working now :D

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.