...

View Full Version : Echo'ing to a single string



vern
10-18-2004, 12:01 AM
I have a function that takes entries from a database and loops through all the entries and echos each entry out, formatted in a table.

My question is ... how do I echo all of the generated HTML/data into a single string that I can use somewhere else?

kliqster
10-18-2004, 02:24 AM
I have a function that takes entries from a database and loops through all the entries and echos each entry out, formatted in a table.

My question is ... how do I echo all of the generated HTML/data into a single string that I can use somewhere else?

Inside the loop, use $string.=

Basic example:


while( $foo = mysql_fetch_array( $bar ) )
{
$string.= $foo['bar'];
}

Kinda thing.

vern
10-18-2004, 05:20 AM
I know I can put the values in the loop into a variable ... but what I want is a little bit different.

The loop creates a table for me by echoing the tags and the data everytime it loops. What I want to do is ... be able to echo that same table that is created by the loop just by echoing a single variable...

dniwebdesign
10-18-2004, 07:51 AM
Can we see some code, I know what you are talking about but I would need to see some code to see how I'd do it so it would work for you.

vern
10-18-2004, 09:31 AM
OK here is the code that I currently have that extracts what I want from the DB and just spits it out in a table:



function displayComments($item_id)
{
require("config.php");

/* Comments */
$comment_query = "SELECT * FROM item_comment WHERE item_id = $item_id ORDER BY date";
$comment_result = mysql_query($comment_query, $connection);

$counter = mysql_num_rows($comment_result);
echo '<table width="100%">';
while ($counter > 0)
{
$counter--;
$comment = stripslashes(mysql_result($comment_result, $counter, "comment"));
$date = convertTime(mysql_result($comment_result, $counter, "date"));
if ($counter % 2)
$comment_color = "#99CCFF";
else
$comment_color = "#FFFFFF";
echo '<tr>';
echo '<td width="100%" BGCOLOR="'.$comment_color.'"><b>Date:</b> '.$date.'<br /><b>Comment: </b>'.$comment.'<br /></td>';
echo '</tr>';

}
echo '</table>';
}


This is in another function which basicaly just replaces keywords in a template with a variable.



$html = fread($fr, $size);

$comments = displayComments($item_id);

/* Array containing variables to replace keywords */
$new = array($name, $poweredby, $category, $price, $date_added, $date_modified, $comment, $image, $index, displayComments($item_id));

/* Array containing keywords */
$replace_strings = array('{ITEM_NAME}', '{POWEREDBY}', '{CAT_NAME}', '{PRICE}', '{DATE_ADDED}', '{DATE_MOD}', '{DESCRIPTION}', '{IMAGE}', '{INDEX}', '{COMMENTS}');

$newhtml = str_replace($replace_strings, $new, $html);

echo $newhtml; // Output HTML


Note the {COMMENTS} keyword and the $comments variable. Basically I want all the output of my function that creates the table to go into one variable so I can spit it out just like the rest of the variables. Obviously the second section of code needs work to make it work the way it's supposed to.

Thanks for any help. If you know how to do this another/better way, feel free to tell me. :)

raf
10-18-2004, 10:06 AM
i probably don't understand it, but what's against kliqster's sollution (which is what almost anyone would do, i suppose)

just something like


function displayComments($item_id)
{
require("config.php");

/* Comments */
$comment_query = "SELECT * FROM item_comment WHERE item_id = $item_id ORDER BY date";
$comment_result = mysql_query($comment_query, $connection);

$counter = mysql_num_rows($comment_result);
$output = '<table width="100%">';
while ($counter > 0)
{
$counter--;
$comment = stripslashes(mysql_result($comment_result, $counter, "comment"));
$date = convertTime(mysql_result($comment_result, $counter, "date"));
if ($counter % 2)
$comment_color = "#99CCFF";
else
$comment_color = "#FFFFFF";
$output .= '<tr>
<td width="100%" BGCOLOR="'.$comment_color.'"><b>Date:</b> '.$date.'<br /><b>Comment: </b>'.$comment.'<br /></td>
</tr>';
}
$output .= '</table>';
return $output;
}

vern
10-18-2004, 10:19 AM
i probably don't understand it, but what's against kliqster's sollution (which is what almost anyone would do, i suppose)


Each time you do



$output =


... you are assigning a new value to $output. With the code you have, you only are returning ...



$output = '<table>';


... instead of all the data and the whole table itself.

kliqster
10-18-2004, 11:28 AM
Each time you do



$output =


... you are assigning a new value to $output. With the code you have, you only are returning ...



$output = '<table>';


... instead of all the data and the whole table itself.

I'm not sure I follow.

By using .= everything will be appended to the end of $output.

http://www.php.net/manual/en/language.operators.string.php

We're only using $output = once to initialize the variable. As the function isn't in a loop itself, $output will only be set once and the rest of the time the HTML will be appended tot he end of it.

raf
10-18-2004, 12:11 PM
Each time you do



$output =


... you are assigning a new value to $output. With the code you have, you only are returning ...



$output = '<table>';


... instead of all the data and the whole table itself.
You misread my code. there is a big difference between
$a = 'blabla';
and
$a .= 'blabla';

$a .= 'blabla';
is the same as
$a = $a . 'blabla';

and the "." is the concatination-operator which basically glues the value of $a to 'blabla' and then assigns the result of this operation to $a.

From a matimatical point of view, this may look strange, but the "=" operator doesn't need to be considered as an equation here, but as 'becomes'.
$a becomes $a + 'blabla'

kliqster
10-18-2004, 05:15 PM
You misread my code. there is a big difference between
$a = 'blabla';
and
$a .= 'blabla';

$a .= 'blabla';
is the same as
$a = $a . 'blabla';

and the "." is the concatination-operator which basically glues the value of $a to 'blabla' and then assigns the result of this operation to $a.

From a matimatical point of view, this may look strange, but the "=" operator doesn't need to be considered as an equation here, but as 'becomes'.
$a becomes $a + 'blabla'


:thumbsup:

From the PHP manual:


The basic assignment operator is "=". Your first inclination might be to think of this as "equal to". Don't. It really means that the the left operand gets set to the value of the expression on the rights (that is, "gets set to").

vern
10-18-2004, 07:36 PM
Thanks for the responses everyone. I'll play around with the ideas everyone has given when I get home. Again, much thanks.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum