Jump to content

Saving the result of a while loop as a variable


Scotte

Recommended Posts

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.

Link to comment
Share on other sites

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!";

Link to comment
Share on other sites

$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..

Link to comment
Share on other sites

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.

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.