SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP string concatenation

    I have set $message variable. Now I wana concatenate foreach loop's results to existing $message

    PHP Code:
    <img style="vertical-align: text-bottom" src="<?=$user->img($folder_img$folder_altfalse'''src');?>" title="<?=$user->lang[$folder_alt];?>" alt="<?=$user->lang[$folder_alt];?>" />
                <a href="<?=$phpbb_root_path 'viewtopic.php?f=' $t['forum_id'] . '&amp;t=' $t['topic_id'] ;?>"><?=html_entity_decode($t['topic_title']);?></a><br />
    My Complete code is below

    PHP Code:
    <?
        $message 
    .= $msg\n";


        // Get active topics.
        
    $sql="SELECT *
        
    FROM " . TOPICS_TABLE . "
        
    WHERE topic_approved '1' AND " . $db->sql_in_set('forum_id', $forums) . "
        
    ORDER BY topic_last_post_time DESC";
        
    $result = $db->sql_query_limit($sql,TOPICS_LIMIT);
        while (
    $r = $db->sql_fetchrow($result))
        {
            
    $topics[] = $r;
        }
       
    $db->sql_freeresult($result);

       // How can I add "
    foreach" loop's output to $message 
       
    $message .= 
            
        foreach(
    $topics as $t)
        {
            // Get folder img, topic status/type related information
            
    $topic_tracking_info = get_complete_topic_tracking($t['forum_id'], $t['topic_id']);
            
    $unread_topic = (isset($topic_tracking_info[$t['topic_id']]) && $t['topic_last_post_time'] > $topic_tracking_info[$t['topic_id']]) ? true : false;
            
    $folder_img = $folder_alt = $topic_type = '';
            topic_status(
    $t$t['topic_replies'], $unread_topic$folder_img$folder_alt$topic_type);
            
            // output the link
            ?>
                <img style="
    vertical-aligntext-bottom" src="<?=$user->img($folder_img$folder_altfalse'''src');?>" title="<?=$user->lang[$folder_alt];?>" alt="<?=$user->lang[$folder_alt];?>" />
                <a href="<?=$phpbb_root_path 'viewtopic.php?f=' $t['forum_id'] . '&amp;t=' $t['topic_id'] ;?>"><?=html_entity_decode($t['topic_title']);?></a><br />
        <?
        
    }
        
    ?>

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    // How can I add "foreach" loop's output to $message
    $message .=
    In principle:
    PHP Code:
    $array = array('this''that''the''other');

    $msg "" // message is an empty string to start with, say.

    $msg .= "Today my talk is about: ";  // no space between .= !!

    foreach( $array as $element ){
    $msg .= $element " "// add a space between each word
    }

    echo 
    $msg ". Thank you.";

    // Today my talk is about: this that the other . Thank you. 
    There are other ways of adding array elements to a string, but to answer your question this ought to illustrate.

    ps this line
    PHP Code:
        $message .= $msg\n"; 
    should be
    PHP Code:
        $message .= $msg "\n"
    And if a line end is what you want you could do this:

    PHP Code:
        $message .= $msg PHP_EOL// php end of line

    // An OS specific line end 

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you so mush Cups for your valuable suggestions. Sorry for being late, I was out of town. Now I have changed my script according to your guidelines. Everything is working perfectly except two things. I wana send automatic email notifications to Admin on my forums. Whenever a new thread is started Admin get email. Now I am trying to add recent x number topic to email message.


    test.php

    PHP Code:
    foreach($topics as $t)
        {
            
    // Get folder img, topic status/type related information
            
    $topic_tracking_info get_complete_topic_tracking($t['forum_id'], $t['topic_id']);
            
    $unread_topic = (isset($topic_tracking_info[$t['topic_id']]) && $t['topic_last_post_time'] > $topic_tracking_info[$t['topic_id']]) ? true false;
            
    $folder_img $folder_alt $topic_type '';
            
    topic_status($t$t['topic_replies'], $unread_topic$folder_img$folder_alt$topic_type);
            

            
    // output the link
                
            
    $newposts .= '<img style="vertical-align: text-bottom" src="' $user->img($folder_img$folder_altfalse'''src') . '" title="' $user->lang[$folder_alt] . '" alt="' $user->lang[$folder_alt'" />


                   <a href="' 
    .$phpbb_root_path 'viewtopic.php?f=' $t['forum_id'] . '&amp;t=' $t['topic_id'] . '">' html_entity_decode($t['topic_title']) . '</a><br />';
        }

            
    $_SESSION['new_posts'] = $newposts


    posttomail.php


    PHP Code:

    include('test.php');

    $message .= "<div>" $_SESSION['new_posts'] . "</div>\n"
    1. Initially variable $newposts was declared. I changed it to $_SESSION['new_posts'] because $newposts was not being passed to posttomail.php file. Any Idea ?

    2. If I remove
    PHP Code:
    $newposts .= '<img style="vertical-align: text-bottom" src="' $user->img($folder_img$folder_altfalse'''src') . '" title="' $user->lang[$folder_alt] . '" alt="' $user->lang[$folder_alt'" /> 
    these lines from test.php file, it works perfectly without the image. To display the image these lines are required.


Bookmarks

Posting Permissions

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