Scotte Posted May 3, 2012 Share Posted May 3, 2012 I want to save the results of a loop as a variable ie $output. I have tried encasing the php within quotes but it does not work. Is there a way to save the complete results as a variable? $result = mysql_query("SELECT * FROM $table2 WHERE $db_item_1 OR $db_item_2", $connection); if (!mysql_num_rows($result)) { echo "Error 13424 - not working"; exit(); } while ($row = mysql_fetch_array($result)) { echo "<tr><td scope=\"col\" style=\"font-size: 16px; color: #333333; padding: 10px 8px; border-bottom: 1px solid #919191;\"><div align=\"left\" style=\"font-size: 20px;\">" . $row['date'] . "</div></td> <td scope=\"col\" style=\"font-size: 16px; color: #333333; padding: 10px 8px; border-bottom: 1px solid #919191;\"><div align=\"left\"></div></td> <td scope=\"col\" style=\"font-size: 16px; color: #333333; padding: 10px 8px; border-bottom: 1px solid #919191;\"><div align=\"left\" style=\"font-size: 20px;\">" . $row['title'] . "</div></td> <td scope=\"col\" style=\"font-size: 16px; color: #333333; padding: 10px 8px; border-bottom: 1px solid #919191;\"><div align=\"left\"></div></td> <td scope=\"col\" style=\"font-size: 16px; color: #333333; padding: 10px 8px; border-bottom: 1px solid #919191;\"><div align=\"left\" style=\"font-size: 20px;\">" . $row['cost'] . "</div></td> </tr>"; } echo $complete; The part I want as one result (ie. $complete) is the result of the while loop. There is always at least one result but sometimes 10 which means that it creates 10 table rows. I need to do it this way as later on in the page I use a pdf converter which does not allow loop checks within it otherwise I would just place it within the converter. Quote Link to comment https://forums.phpfreaks.com/topic/261993-saving-the-result-of-a-while-loop-as-a-variable/ Share on other sites More sharing options...
creata.physics Posted May 3, 2012 Share Posted May 3, 2012 Can you please post the code in [*code*] or [*php*] tags? I'm not sure if you're posting the full code or not. Also, to help you with your problem without the need of your code you can read this page: http://www.php.net/manual/en/language.operators.assignment.php Here's an example from that page, it seems like this is what you want to do: $b = "Hello "; $b .= "There!"; // sets $b to "Hello There!", just like $b = $b . "There!"; Quote Link to comment https://forums.phpfreaks.com/topic/261993-saving-the-result-of-a-while-loop-as-a-variable/#findComment-1342541 Share on other sites More sharing options...
Scotte Posted May 3, 2012 Author Share Posted May 3, 2012 $result = mysql_query("SELECT * FROM $table2 WHERE $db_item_1 OR $db_item_2", $connection); if (!mysql_num_rows($result)) { echo "Error 13424 - not working"; exit(); } while ($row = mysql_fetch_array($result)) { "<tr><td scope=\"col\" style=\"font-size: 16px; color: #333333; padding: 10px 8px; border-bottom: 1px solid #919191;\"><div align=\"left\" style=\"font-size: 20px;\">" . $row['date'] . "</div></td> <td scope=\"col\" style=\"font-size: 16px; color: #333333; padding: 10px 8px; border-bottom: 1px solid #919191;\"><div align=\"left\"></div></td> <td scope=\"col\" style=\"font-size: 16px; color: #333333; padding: 10px 8px; border-bottom: 1px solid #919191;\"><div align=\"left\" style=\"font-size: 20px;\">" . $row['title'] . "</div></td> <td scope=\"col\" style=\"font-size: 16px; color: #333333; padding: 10px 8px; border-bottom: 1px solid #919191;\"><div align=\"left\"></div></td> <td scope=\"col\" style=\"font-size: 16px; color: #333333; padding: 10px 8px; border-bottom: 1px solid #919191;\"><div align=\"left\" style=\"font-size: 20px;\">" . $row['cost'] . "</div></td> </tr>"; } echo $complete; ?> Apologies I thought I had clicked the php tag when pasting the code. Thanks for your link but what I really cant get my head around is how I get the results of my while loop as one variable. I can place an echo within the "{}" and it will echo the result but then if I want to print that result again outside of the loop I cannot, it will just echo the final result.. Quote Link to comment https://forums.phpfreaks.com/topic/261993-saving-the-result-of-a-while-loop-as-a-variable/#findComment-1342551 Share on other sites More sharing options...
noXstyle Posted May 3, 2012 Share Posted May 3, 2012 Do you want to print the whole result again or only access parts of it? If I understood you correctly, creata.physics already answered your question. For instance you would just append to a variable: $output = ''; // don't forget to initialize the variable, otherwise php will throw a notice when appending rows to it while($row = mysql_fetch_array($result)) { $output .= "Your html here\n"; } Other way is to store the table rows you create in an array: while($row = mysql_fetch_array($result)) { $output[] = "Your html here\n"; } If you need to access a single row later on, you might probably want to assign row id's as the array keys or something similar. And if you want to output the whole content you can always implode the array thus giving you the whole row set. Quote Link to comment https://forums.phpfreaks.com/topic/261993-saving-the-result-of-a-while-loop-as-a-variable/#findComment-1342553 Share on other sites More sharing options...
Scotte Posted May 3, 2012 Author Share Posted May 3, 2012 Thanks guys, think I have my head around it! Quote Link to comment https://forums.phpfreaks.com/topic/261993-saving-the-result-of-a-while-loop-as-a-variable/#findComment-1342576 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.