1

If I want to display the text as follows in example, how can it be done?

$tmp=mysql_query("SELECT ... FROM ....");
$total=mysql_num_rows($tmp);
$tekst1='Your total number of friends are: '.$total.'. Your friends are: ';
while($p=mysql_fetch_array($tmp)) {
$tekst2=$p['friend'].", ";
}
$text_complete=$tekst1.$tekst2;
echo $text_complete;

$text_complete should look like: "Your total number of friends are: 3. Your friends are: John, Mike, Michael". But, using this code above I get "Your total number of friends are: 3. Your friends are: John,"

How can I combine both texts, the one obtained from the loop and the one which is fixed?

6 Answers 6

1

I think you shoul do like this:

       $tmp=mysql_query("SELECT ... FROM ....");
       $total=mysql_num_rows($tmp);
       $tekst1='Your total number of friends are: '.$total.'. Your friends are: ';
       $friends = array();
       while($p = mysql_fetch_array($tmp)) {
             $friends[] = $p['friend'];
       }
       $tekst2= implode(', ', $friends);
       $text_complete=$tekst1.$tekst2;
       echo $text_complete;

otherwise you overwrite the variable each time!

EDIT - i corrected the code to use implode as in Xaerxess answer. Previousli i just concatenated and use a substr to remove trailing comma and space

Sign up to request clarification or add additional context in comments.

4 Comments

That's right but you should use it on $tekst1, so you don't need $tekst2 and $text_complete..
@Nicola Peluchetti - I get error message: "Undefined variable: tekst2 on line 25". That line is: "$tekst2.=$p['friend'].", ";"
@Nicola Peluchetti -I found why this happens. Now it's working fine. Thanks.
implode version in other comments is better
1

You can use:

$tekst2 .= $p['friend'].", ";

To add to the variable (rather than overwriting it. Then remove the trailing comma:

$text_complete=$tekst1. rtrim( $tekst2, "," );

Comments

1
$tekst2 = new Array();
while($p=mysql_fetch_array($tmp)) {
    $tekst2[] = $p['friend'];
}
$text_complete = $tekst1 . implode(', ',$tekst2);

But better would be to use GROUP_CONCAT in sql

Comments

1
$tmp=mysql_query("SELECT ... FROM ....");
$total=mysql_num_rows($tmp);
$tekst = '';
$tekst='Your total number of friends are: '.$total.'. Your friends are: ';
while($p=mysql_fetch_array($tmp)) {
$tekst.=$p['friend'].", ";
}
echo $tekst;

Comments

1
        $tmp=mysql_query("SELECT ... FROM ....");
        $total=mysql_num_rows($tmp);
        $tekst1='Your total number of friends are: '.$total.'. Your friends are: ';
        while($p=mysql_fetch_array($tmp)) 
           {
           $tekst1 .= $p['friend'].", "; // add friend names from MySQL result.
           }
        $tekst1 = substr($tekst2, 0, -2);//Remove comma.
        echo $tekst1; // Outputs the wanted string

Comments

1

Use PHP implode / join funciton, so there isn't any trailing comma at the end. In this case:

$fiends = array();
while($p = mysql_fetch_array($tmp)) {
     $friends[] = $p['friend'];
}
implode(', ', $friends);

EDIT: I basically doubled Billy Moon answer. What's more don't use mysql_* functions - they're deprecated not recommended - see comments below.

4 Comments

mysql_* functions are deprecated by whom? Reference?
@George Cummins: According to PHP docs mysql_* extention isn't developed, isn't recommended by MySQL for new projects, doesn't support all MySQL 4.1+ functionality. As it says: If you are using MySQL versions 4.1.3 or later it is strongly recommended that you use the mysqli extension instead.
There is a difference between the details you indicated and deprecation. Deprecation is "a status applied to software features to indicate that they should be avoided." (en.wikipedia.org/wiki/Deprecation) The mysql_* functions need not be avoided in suitable circumstances. PDO or mysqli* may be more suitable in some cases, but not all.
@George Cummins: True, I've used wrong word. I've already edited my answer, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.