#1
  1. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Location
    Cairns, Australia
    Posts
    710
    Rep Power
    18

    Question Preg_replace on MYSQL data


    Hi guys,

    I'm a little stuck can anyone possibly help?

    I have a template (row'header') for an email stored in a MYSQL table. An example of the data in the 'header' column is:

    PHP Code:
    Dear %%NAME%%, 
    What I was hoping to achive is to replace "%%NAME%%" with a results from another table:

    PHP Code:

    // Get the contents of the template from the database 
            
    $query " SELECT * FROM Gift_Voucher_temps WHERE id=$template";
        
    $result mysql_query($query); 
                if (!
    $result) { echo("ERROR: " mysql_error() . "\n$result\n");    } 
            while(
    $row=mysql_fetch_array($result))  
        { 


    // Get the gift voucher details 
            
    $query2 " SELECT * FROM Gift_Vouchers WHERE id=$voucher_id";
        
    $result2 mysql_query($query2); 
                if (!
    $result2) { echo("ERROR: " mysql_error() . "\n$result2\n");    } 
            while(
    $row2=mysql_fetch_array($result2))  
        { 



    $newheader preg_replace ('%%name%%'$row2['name'], $row['header']); 
    echo 
    $header
    This didnt echo a thing
    Am I going about this the right way?
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2006
    Location
    California, USA
    Posts
    18
    Rep Power
    0
    PHP Code:
    echo $newheader
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Location
    Cairns, Australia
    Posts
    710
    Rep Power
    18
    Im getting the following error:
    Warning: preg_replace() [function.preg-replace]: Unknown modifier 'N'
  6. #4
  7. Expert Debugger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Apr 2006
    Location
    Dev Shed Forums (-_^)v
    Posts
    1,022
    Rep Power
    1302
    try this:

    PHP Code:
    $newheader preg_replace ('#^%%NAME%%$#'$row2['name'], $row['header']); 
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Location
    Cairns, Australia
    Posts
    710
    Rep Power
    18
    Hmmm... That got rid of the errror but echoed "Dear %%NAME%%," instead of the name ($row2['name'] in the database ?
  10. #6
  11. Expert Debugger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Apr 2006
    Location
    Dev Shed Forums (-_^)v
    Posts
    1,022
    Rep Power
    1302
    OOps try this:

    PHP Code:
    $newheader preg_replace ('#%%NAME%%#'$row2['name'], $row['header']); 
  12. #7
  13. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Location
    Cairns, Australia
    Posts
    710
    Rep Power
    18
    AWESOME!!!
    Thank you so much sarav_dude you have just opened upa whole new world of opportunities to me. This is gonna make my developing work ALOT easier

    Comments on this post

    • sarav_dude agrees : cheers! mate :)

IMN logo majestic logo threadwatch logo seochat tools logo