Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Sep 2004
    Location
    Minnesota, USA
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Echo'ing to a single string

    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?
    ribbed.us
    ---
    To err is human, to moo bovine.

  • #2
    New Coder
    Join Date
    Feb 2004
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by vern
    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:

    PHP Code:
    while( $foo mysql_fetch_array$bar ) )
    {
      
    $string.= $foo['bar'];

    Kinda thing.

  • #3
    New Coder
    Join Date
    Sep 2004
    Location
    Minnesota, USA
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...
    ribbed.us
    ---
    To err is human, to moo bovine.

  • #4
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    846
    Thanks
    15
    Thanked 10 Times in 10 Posts
    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.
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #5
    New Coder
    Join Date
    Sep 2004
    Location
    Minnesota, USA
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    PHP Code:
    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.

    PHP Code:
            $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$indexdisplayComments($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.
    Last edited by vern; 10-18-2004 at 09:40 AM.
    ribbed.us
    ---
    To err is human, to moo bovine.

  • #6
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    PHP Code:
     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;

    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #7
    New Coder
    Join Date
    Sep 2004
    Location
    Minnesota, USA
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by raf
    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

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

    PHP Code:
    $output '<table>'
    ... instead of all the data and the whole table itself.
    ribbed.us
    ---
    To err is human, to moo bovine.

  • #8
    New Coder
    Join Date
    Feb 2004
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by vern
    Each time you do

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

    PHP Code:
    $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/languag...ors.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.

  • #9
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by vern
    Each time you do

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

    PHP Code:
    $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'
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #10
    New Coder
    Join Date
    Feb 2004
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by raf
    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'



    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").

  • #11
    New Coder
    Join Date
    Sep 2004
    Location
    Minnesota, USA
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the responses everyone. I'll play around with the ideas everyone has given when I get home. Again, much thanks.
    ribbed.us
    ---
    To err is human, to moo bovine.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •