sp@rky13 Posted August 22, 2009 Share Posted August 22, 2009 How can I reuse a query result on the same page. I need the same query result in 2 different places on the page. How can I duplicate it without rerunning the query. The query involves having this on the page: <?php if ($advanced=='on') include "advanced.html"; else include "vpt.html";?> How can I put the result of that in a second place? Quote Link to comment Share on other sites More sharing options...
awpti Posted August 22, 2009 Share Posted August 22, 2009 Your question doesn't make sense. I'm not seeing a query here. I see two include statements in a conditional. Quote Link to comment Share on other sites More sharing options...
dreamwest Posted August 22, 2009 Share Posted August 22, 2009 Your include (html) wont work in php page. Fixed your original code too <?php if ($advanced=='on'){ include ("advanced.html"); }else{ include ("vpt.html"); } ?> Quote Link to comment Share on other sites More sharing options...
sp@rky13 Posted August 22, 2009 Author Share Posted August 22, 2009 That's weird because it works fine. so that's how you're meant to write it? I'll change it now Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted August 22, 2009 Share Posted August 22, 2009 The two different syntaxes are functionally equivalent for that specific code. Single statements don't need to be enclosed in {} and include is not a function so the () are not required. If your question is how to reuse the result set from one query after your have iterated over it once, see this link - http://us.php.net/mysql_data_seek Quote Link to comment Share on other sites More sharing options...
trq Posted August 22, 2009 Share Posted August 22, 2009 That's weird because it works fine. so that's how you're meant to write it? I'll change it now No, its not how your meant to write it. The code you had was fine. As to your question, once your finished with your result in the first area you can use [mysql_data_seek[/m] to rewind the pointer back to the start and use the resource again in a second location. Quote Link to comment Share on other sites More sharing options...
sp@rky13 Posted August 22, 2009 Author Share Posted August 22, 2009 So my code was right???? :-\ EDIT: @PFMaBiSmAd : How do I use that when I'm using the include thing I have above? Quote Link to comment Share on other sites More sharing options...
trq Posted August 22, 2009 Share Posted August 22, 2009 Yes. You don't need curly braces {} around simply if statements. Also, include is not a function and therefore doesn't require its arguments be enclosed within () braces. Quote Link to comment Share on other sites More sharing options...
sp@rky13 Posted August 22, 2009 Author Share Posted August 22, 2009 thanks for the explanation. In case it was missed: @PFMaBiSmAd : How do I use that when I'm using the include thing I have above? Quote Link to comment Share on other sites More sharing options...
trq Posted August 22, 2009 Share Posted August 22, 2009 You need to show us some code where your actually using a query result. Just showing us how you include files shows us nothing related to your question. Quote Link to comment Share on other sites More sharing options...
sp@rky13 Posted August 22, 2009 Author Share Posted August 22, 2009 Ok, well the thing is, I'm using loads of includes. So that: <?php if ($advanced=='on') include "advanced.html"; else include "vpt.html";?> if advanced is on goes to: <?php if ($vpt =='village') echo $vpt_sub; elseif ($vpt =='player') include "player_28_advanced.html"; else include "tribe_28_advanced.html";?> and then say it's tribe it goes to: <?php if ($x=='on' AND $y=='') include "tribe_28_advanced_x.html"; else if ($y=='on' AND $x=='') include "tribe_28_advanced_y.html"; else if ($y=='on' AND $x=='on') include "tribe_28_advanced_xy.html"; else if ($k=='on') include "tribe_28_advanced_k.html"; ?> and then say only x is on it goes to this: <?php $con = mysql_connect("localhost","",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("wwwspark_tribalwars", $con); $result = mysql_query("SELECT * FROM ally_en28 WHERE tag = '$vpt_sub'"); while($row = mysql_fetch_array($result)) { $id = $row['id']; $result2 = mysql_query("SELECT * FROM players_en28 WHERE ally = '".$id."'"); while($row2 = mysql_fetch_array($result2)) { $id2 = $row2['id']; $result3 = mysql_query("SELECT * FROM villages_en28 WHERE player = '".$id2."' AND x BETWEEN $x1 AND $x2"); while($row3 = mysql_fetch_array($result3)) { echo $row3['x']."|".$row3['y']." "; } } } mysql_close($con); ?> Quote Link to comment Share on other sites More sharing options...
trq Posted August 22, 2009 Share Posted August 22, 2009 So, where exactly do you need to re-use any query result? That last piece of code will only be executed under one certain condition. Quote Link to comment Share on other sites More sharing options...
sp@rky13 Posted August 22, 2009 Author Share Posted August 22, 2009 What I need is the result to be reused later on down the page Quote Link to comment Share on other sites More sharing options...
dreamwest Posted August 22, 2009 Share Posted August 22, 2009 Yes. You don't need curly braces {} around simply if statements. Also, include is not a function and therefore doesn't require its arguments be enclosed within () braces. It just looks better. Having if($_COOKIE['snap']== "on" ) $snap = "on"; looks wrong. But having { is like having a border around the code to easily see the blocks if($_COOKIE['snap']== "on" ){ $snap = "on"; } Quote Link to comment Share on other sites More sharing options...
trq Posted August 22, 2009 Share Posted August 22, 2009 Then pass $result3 to mysql_data_seek, rewind the pointer and use it again. Quote Link to comment Share on other sites More sharing options...
trq Posted August 22, 2009 Share Posted August 22, 2009 Yes. You don't need curly braces {} around simply if statements. Also, include is not a function and therefore doesn't require its arguments be enclosed within () braces. It just looks better. Having if($_COOKIE['snap']== "on" ) $snap = "on"; looks wrong. But having { is like having a border around the code to easily see the blocks if($_COOKIE['snap']== "on" ){ $snap = "on"; } it might look wrong in your opinion, but its not in php's. Quote Link to comment Share on other sites More sharing options...
sp@rky13 Posted August 22, 2009 Author Share Posted August 22, 2009 That might be a problem because it could be result2 instead of result3 for one of the other possible options Quote Link to comment Share on other sites More sharing options...
trq Posted August 22, 2009 Share Posted August 22, 2009 Thats the part I'm not sure you understand. That piece of code wont execute at all unless certain conditions are met. Hence, you won't be re-using any result. Quote Link to comment Share on other sites More sharing options...
sp@rky13 Posted August 22, 2009 Author Share Posted August 22, 2009 No, ok. So what I have is 2 places that need to show the exact same thing. In the first place it will actually find the result. In the second it just needs to as such copy and paste the result that was echoed in the first place EDIT: Look here: http://sparky13.co.cc/test/page_28.php?spear=0&sword=0&axe=0&archer=0&scout=0&lc=0&ma=0&hc=0&ram=0&cat=0&paladin=0&noble=0&village%2Fplayer%2Ftribe=tribe&village%2Fplayer%2Ftribe_submit=farm-u&x1=0&x=on&x2=0&y1=0&y=on&y2=0&k1=0&Submit=Make+Fake+Script See how in 2 places it has those strings of numbers. They are the same string of numbers. That is where my include thing is but I want to copy across the string of numbers from when i get the result the first time to save on load time Quote Link to comment Share on other sites More sharing options...
trq Posted August 22, 2009 Share Posted August 22, 2009 Then, in the second place put your result in mysql_data_seek rewind its pointer and use it again!!!!! Quote Link to comment Share on other sites More sharing options...
sp@rky13 Posted August 22, 2009 Author Share Posted August 22, 2009 Look here: http://sparky13.co.cc/test/page_28.php?spear=0&sword=0&axe=0&archer=0&scout=0&lc=0&ma=0&hc=0&ram=0&cat=0&paladin=0&noble=0&village%2Fplayer%2Ftribe=tribe&village%2Fplayer%2Ftribe_submit=farm-u&x1=0&x=on&x2=0&y1=0&y=on&y2=0&k1=0&Submit=Make+Fake+Script See how in 2 places it has those strings of numbers. They are the same string of numbers. That is where my include thing is but I want to copy across the string of numbers from when i get the result the first time to save on load time I don't get what you mean. Sorry Quote Link to comment Share on other sites More sharing options...
trq Posted August 22, 2009 Share Posted August 22, 2009 A simple example. // get the result once $sql = "SELECT a, b FROM foo"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { // use the result the first time while ($row = mysql_fetch_assoc($result)) { echo $row['a'] . $row['b']; } } } // now, somewhere else on the page we need to use the result again. if (is_resource($result)) { // rewind the resources internal pointer. $result = mysql_data_seek($result); while ($row = mysql_fetch_assoc($result)) { echo $row['a'] . $row['b']; } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.