A basic while question

Discussion in 'PHP' started by MesCasinos, Jun 17, 2010.

  1. #1
    Hi, i know this is a basic question, I tried to find the answer on google but dah! :confused:

    Ok, this is my own newsletter système with WYMEDITOR. All is working great, i can send the mails, but i tested it with 3 differents emails with the message TEST and its doin' this:

    First email: TEST
    second email: TEST TEST
    thirs email: TEST TEST TEST

    So this is a while problem, i know! But i can't fix it :confused: Here is the code of the sendmail.php

    <?PHP
    include ("../password_protect.php");
    include("../../include/config/langDetect.php");
    
    /* Connection DATABASE */
    include("../../include/config/config.inc.php");
    
    mysql_select_db("roulett3_monster", $con);
    
    $result = mysql_query("SELECT * FROM newsletters WHERE sub_lang='". $_GET['sub_lang'] ."' ORDER BY nom ASC");
    
    
    while($row = mysql_fetch_array($result)){
    
    //titre du mail 
    $sujet = 'Newsletter du jour'; 
    $email = $row['email'];
    $email_md5 = $row['email_md5'];
    
    $contenu .= 'test';
    
    // envoi du mail HTML 
    $from = "From: MonsterBonuses.com <noreply@monsterbonuses.com>\nMime-Version:"; 
    $from .= " 1.0\nContent-Type: text/html; charset=utf-8\n"; 
    
    // envoie du mail 
    if (mail($email,$sujet,$contenu,$from)){ 	
    echo "Envoyé avec succès à ".$row['email']."<br>";
    }else{
    	echo "erreur lors de l'envoie à ".$row['email']."";
    }
    
    }
    ?>
    PHP:
    Ok i'm not that bad in PHP, all my website are php coded but i'm not that good with while :p

    I removed all the while and replaced the $email for one email only to test if the problem wasn't from the mail function and all is goin' great.

    So thanks for all of you will fix this little while error :eek:
     
    MesCasinos, Jun 17, 2010 IP
  2. Cozmic

    Cozmic Member

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    48
    #2
    $contenu .= 'test';
    PHP:
    This will append 'test' to the current value of content. So, this would happen:

    Loop1: content = "test"
    Loop2: content = "testtest"

    and so on. Is this intended?
     
    Cozmic, Jun 17, 2010 IP
  3. MesCasinos

    MesCasinos Peon

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    this is what happen right now. Each emails is adding one more $contenu i mean TEST

    1st -> test
    2 -> test test
    3 -> test test test
    4 -> test test test test
    ...


    i want this result

    1st -> test
    2 -> test
    3 -> test
    4 -> test

    and u undertsand that right now TEST is in fact $contenu
     
    MesCasinos, Jun 17, 2010 IP
  4. Cozmic

    Cozmic Member

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    48
    #4
    Then initiate $contenu as "Test" outside of the while loop, without using the period to append.

    
    <?PHP
    include ("../password_protect.php");
    include("../../include/config/langDetect.php");
    
    /* Connection DATABASE */
    include("../../include/config/config.inc.php");
    
    mysql_select_db("roulett3_monster", $con);
    
    $result = mysql_query("SELECT * FROM newsletters WHERE sub_lang='". $_GET['sub_lang'] ."' ORDER BY nom ASC");
    
    $contenu = 'test';
    while($row = mysql_fetch_array($result)){
    
    //titre du mail 
    $sujet = 'Newsletter du jour'; 
    $email = $row['email'];
    $email_md5 = $row['email_md5'];
    
    // envoi du mail HTML 
    $from = "From: MonsterBonuses.com <noreply@monsterbonuses.com>\nMime-Version:"; 
    $from .= " 1.0\nContent-Type: text/html; charset=utf-8\n"; 
    
    // envoie du mail 
    if (mail($email,$sujet,$contenu,$from)){    
    echo "Envoyé avec succès à ".$row['email']."<br>";
    }else{
        echo "erreur lors de l'envoie à ".$row['email']."";
    }
    
    }
    ?>
    
    PHP:
    As a side note, it is a good practice to initiate all and NEVER append to an empty variable. Knowing the way RAM works, it could add some old data there and will produce a notice error in the latest version of PHP anyway.
     
    Cozmic, Jun 17, 2010 IP
  5. MesCasinos

    MesCasinos Peon

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If foudn the error, i think i just missunderstanded what you said in your first replay.

    I fixed the problem by replacing


    $contenu .= 'test';

    by

    $contenu = 'test';

    I dont know if that you mean in your fiorst reply, but thanks! ;)
     
    MesCasinos, Jun 17, 2010 IP
  6. Cozmic

    Cozmic Member

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    48
    #6
    welcome. For future reference:

    $this .= "that" is a shorter way of saying $this = $this."that". So, these would be the same:

    
    $this .= "that";
    
    PHP:
    
    $this = $this."that";
    
    PHP:
     
    Cozmic, Jun 17, 2010 IP
  7. MesCasinos

    MesCasinos Peon

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Yeah great i had never worker with .= & = :) now i know.

    I was asking myself about the while.. I was looking.. and looking the while code and nothing was wrong! I was totally lost :p
     
    MesCasinos, Jun 17, 2010 IP