cdoyle Posted July 13, 2009 Share Posted July 13, 2009 Hi, I'm not sure if this is possible, but can you use the values from a while loop multiple times within a page? I'm not sure if I'm explaining it right, so I'll try and show what I mean below. Say you have a while loop on a page that pulls data. $query 1 = $db->execute ("Select `column1`, `column2` FROM table); $results1 = $query1 ->fetchrow(); while { looped results here and do something with them. } but let's say I need to use the exact same data again later on in the page. Do I need to recreate the exact same query again, or is there a way to re-loop it? $query 1 = $db->execute ("Select `column1`, `column2` FROM table); $results1 = $query1 ->fetchrow(); while { looped results here and do something with them. } if (something happens) { need to use the data again from the loop above. } else { don't need it } So in the if Statement, I need that data again. I thought maybe I could do something like $results2 = $query1 ->fetchrow(); and that would reloop it based off of $query but it doesn't do anything. I could just create another query, that's exactly the same as the first one, but thought it would be nice if I could just use the first one again if possible. Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/165743-use-the-data-from-a-while-loop-twice-in-a-page/ Share on other sites More sharing options...
.josh Posted July 13, 2009 Share Posted July 13, 2009 Assign the results to a (multi-dim) array in the first loop. Then you can use the array later on in your script. Quote Link to comment https://forums.phpfreaks.com/topic/165743-use-the-data-from-a-while-loop-twice-in-a-page/#findComment-874295 Share on other sites More sharing options...
cdoyle Posted July 13, 2009 Author Share Posted July 13, 2009 OK, I'm reading up on multi dimensional arrays, and trying to figure them out. All the tutorials I see are for manually creating an array. I'm getting confused on how to assign the values from the loop to an array. Quote Link to comment https://forums.phpfreaks.com/topic/165743-use-the-data-from-a-while-loop-twice-in-a-page/#findComment-874316 Share on other sites More sharing options...
PFMaBiSmAd Posted July 13, 2009 Share Posted July 13, 2009 If you are using mysql - http://us3.php.net/mysql_data_seek The db class you implied probably has a method that performs the same function or could be extended to add and/or it may in fact already internally hold the results in an intermediate array that you can just reuse. Quote Link to comment https://forums.phpfreaks.com/topic/165743-use-the-data-from-a-while-loop-twice-in-a-page/#findComment-874317 Share on other sites More sharing options...
cdoyle Posted July 13, 2009 Author Share Posted July 13, 2009 If you are using mysql - http://us3.php.net/mysql_data_seek The db class you implied probably has a method that performs the same function or could be extended to add and/or it may in fact already internally hold the results in an intermediate array that you can just reuse. I'm using ADOdb, reading that page I'm still unsure how to do what I need to do. Quote Link to comment https://forums.phpfreaks.com/topic/165743-use-the-data-from-a-while-loop-twice-in-a-page/#findComment-874324 Share on other sites More sharing options...
PFMaBiSmAd Posted July 13, 2009 Share Posted July 13, 2009 Try either - $query1->MoveFirst(); or $query1->Move(0); Quote Link to comment https://forums.phpfreaks.com/topic/165743-use-the-data-from-a-while-loop-twice-in-a-page/#findComment-874327 Share on other sites More sharing options...
cdoyle Posted July 13, 2009 Author Share Posted July 13, 2009 I'm still not sure what I'm doing, I'm looking over that page and it's all confusing. I found this example <? // Start snipit 1 $sql = "SELECT * from <table>"; $result = mysql_query($sql); while ($row = mysql_fetch_assoc($result)) { // do stuff with $row } mysql_data_seek($result, 0); while ($row = mysql_fetch_assoc($result)) { // do other stuff with $row } I thought this is what I would want, but I get these errors when I try them Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/caraudi/public_html/CAC_Mafia_Test_Site/test.php on line 31 Warning: mysql_data_seek(): supplied argument is not a valid MySQL result resource in /home/caraudi/public_html/CAC_Mafia_Test_Site/test.php on line 35 Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/caraudi/public_html/CAC_Mafia_Test_Site/test.php on line 37 Would it be possible to post a sample code, that I could take a look at? Quote Link to comment https://forums.phpfreaks.com/topic/165743-use-the-data-from-a-while-loop-twice-in-a-page/#findComment-874606 Share on other sites More sharing options...
PFMaBiSmAd Posted July 13, 2009 Share Posted July 13, 2009 Those errors (please read them) mean that the result resource you are using in the function calls in those lines of code is not valid. This is because the query failed and returned a FALSE value instead of a result resource. If this is your actual query - $sql = "SELECT * from <table>"; It failed because the characters '<' and '>' are not correct sql syntax. Quote Link to comment https://forums.phpfreaks.com/topic/165743-use-the-data-from-a-while-loop-twice-in-a-page/#findComment-874622 Share on other sites More sharing options...
aggrav8d Posted July 13, 2009 Share Posted July 13, 2009 also 2x check that in your production code you don't do something like <? // Start snipit 1 $sql = "SELECT * from <table>"; $result = mysql_query($sql); while ($row = mysql_fetch_assoc($result)) { // do stuff with $row } mysql_free_result($result); // too early... mysql_data_seek($result, 0); // BOOM! while ($row = mysql_fetch_assoc($result)) { // do other stuff with $row } Quote Link to comment https://forums.phpfreaks.com/topic/165743-use-the-data-from-a-while-loop-twice-in-a-page/#findComment-874648 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.