Studio4web Posted September 20, 2006 Share Posted September 20, 2006 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]<?phpdo {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 Link to comment https://forums.phpfreaks.com/topic/21403-return-data-from-one-query-at-two-places/ Share on other sites More sharing options...
obsidian Posted September 20, 2006 Share Posted September 20, 2006 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 rowsfor ($i = 0; $i < 3; $i++) { $row = mysql_fetch_array($ponuda_dana); // display your row}// later in the page, display your remaining rowswhile ($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 Link to comment https://forums.phpfreaks.com/topic/21403-return-data-from-one-query-at-two-places/#findComment-95314 Share on other sites More sharing options...
fenway Posted September 20, 2006 Share Posted September 20, 2006 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. Link to comment https://forums.phpfreaks.com/topic/21403-return-data-from-one-query-at-two-places/#findComment-95334 Share on other sites More sharing options...
obsidian Posted September 20, 2006 Share Posted September 20, 2006 [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. Link to comment https://forums.phpfreaks.com/topic/21403-return-data-from-one-query-at-two-places/#findComment-95345 Share on other sites More sharing options...
Studio4web Posted September 20, 2006 Author Share Posted September 20, 2006 Thank you very much, your first solution working just like I need.Miron Link to comment https://forums.phpfreaks.com/topic/21403-return-data-from-one-query-at-two-places/#findComment-95371 Share on other sites More sharing options...
fenway Posted September 21, 2006 Share Posted September 21, 2006 You mean you can't just call a "fetchall" method directly? Link to comment https://forums.phpfreaks.com/topic/21403-return-data-from-one-query-at-two-places/#findComment-95766 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.