Jump to content


Photo

Return data from one query at two places


  • Please log in to reply
5 replies to this topic

#1 Studio4web

Studio4web
  • New Members
  • Pip
  • Newbie
  • 2 posts
  • LocationInternet

Posted 20 September 2006 - 01:53 PM

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:

<?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);
?>


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:

<?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));
?>


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

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 20 September 2006 - 01:59 PM

try something like this:
<?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
}
?>

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
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 20 September 2006 - 02:11 PM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 20 September 2006 - 02:21 PM

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.


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:
<?php
$sql = mysql_query("SELECT * FROM whatever");
$myRes = array();
while ($row = mysql_fetch_row($sql)) $myRes[] = $row;
?>

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.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 Studio4web

Studio4web
  • New Members
  • Pip
  • Newbie
  • 2 posts
  • LocationInternet

Posted 20 September 2006 - 02:43 PM

Thank you very much, your first solution working just like I need.

Miron

#6 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 21 September 2006 - 12:35 AM

You mean you can't just call a "fetchall" method directly?
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users