Jump to content


Photo

2 Queries In A Loop ?


  • Please log in to reply
6 replies to this topic

#1 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 28 June 2006 - 02:18 PM

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.

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

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

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 ?

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 28 June 2006 - 02:33 PM

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

#3 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 28 June 2006 - 02:38 PM

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.

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#4 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 28 June 2006 - 02:47 PM

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;
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#5 radalin

radalin
  • Members
  • PipPipPip
  • Advanced Member
  • 179 posts

Posted 28 June 2006 - 02:48 PM

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
while ($row = mysql_fetch_row($res1) )
{
echo $row[0] . $row[1];
$row = mysql_fetch_row($res2)
echo $row[0] . $row[1];
}

It should work.
Roy Simkes
Yet Another Parkyeri Developer

#6 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 28 June 2006 - 03:14 PM

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

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#7 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 28 June 2006 - 03:36 PM

Sorry for the double post, I just wanna say thanks !
It's working now :D

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users