Jump to content

2 Queries In A Loop ?


hackerkts

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 ?
Link to comment
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.
Link to comment
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;
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.