Jump to content

Archived

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

Studio4web

Return data from one query at two places

Recommended Posts

Hello to all. I'm a new user here.

I have one problem with returninh data from query.

I need show data in middle and at end of page but I don't know how to do that.

Here is mysql query:

[code]
<?php
$query_ponuda_dana = "SELECT nekretnine.id, nekretnine.mjesto, nekretnine.opis FROM nekretnine LEFT JOIN slike ON nekretnine.id = slike.id_nekretnine WHERE nekretnine.ponuda = 2 AND nekretnine.aktivan = 1 AND slike.glavna = 1 ORDER BY RAND() LIMIT 13";
$ponuda_dana = mysql_query($query_ponuda_dana) or die(mysql_error());
$row_ponuda_dana = mysql_fetch_assoc($ponuda_dana);
?>
[/code]


Now I got 13 records, and I need show first 3 in the middle and other 10 at the end of the page.

I'm using this code to show query results:

[code]
<?php
do {
echo '<div align="center"><b>'.$row_ponuda_dana['mjesto'].'</b> - '.$row_ponuda_dana['opis'].'</div><br><br>';
} while ($row_ponuda_dana = mysql_fetch_assoc($ponuda_dana));
?>
[/code]


This code return me all 13 records, but I need only 3 and in next run other 10.

I can't use two queries because I'm using "ORDER BY RAND()", and there couldn't be repeating the same rows.


Best regards,
Miron

Share this post


Link to post
Share on other sites
try something like this:
[code]
<?php
$query_ponuda_dana = "SELECT nekretnine.id, nekretnine.mjesto, nekretnine.opis FROM nekretnine LEFT JOIN slike ON nekretnine.id = slike.id_nekretnine WHERE nekretnine.ponuda = 2 AND nekretnine.aktivan = 1 AND slike.glavna = 1 ORDER BY RAND() LIMIT 13";
$ponuda_dana = mysql_query($query_ponuda_dana) or die(mysql_error());

// display first 3 rows
for ($i = 0; $i < 3; $i++) {
  $row = mysql_fetch_array($ponuda_dana);
  // display your row
}

// later in the page, display your remaining rows
while ($row = mysql_fetch_array($ponuda_dana)) {
  // display your row
}
?>
[/code]

handling the remaining rows with the "while" statement will help you if for some reason your query doesn't return a full 13 rows as well.

good luck

Share this post


Link to post
Share on other sites
I assume there's also a way to store the entire result set that is returned from the query, though that would probably be less efficient for an arbitrarily-sized query.

Share this post


Link to post
Share on other sites
[quote author=fenway link=topic=108781.msg437964#msg437964 date=1158761508]
I assume there's also a way to store the entire result set that is returned from the query, though that would probably be less efficient for an arbitrarily-sized query.
[/quote]

yes, i actually often do this when i'm wanting to filter results in different locations on the page. i like to do something like this:
[code]
<?php
$sql = mysql_query("SELECT * FROM whatever");
$myRes = array();
while ($row = mysql_fetch_row($sql)) $myRes[] = $row;
?>
[/code]

at this point, you have a variable $myRes that holds all the data, and you can access it from wherever you like on your page.

Share this post


Link to post
Share on other sites
You mean you can't just call a "fetchall" method directly?

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.