Connecting Tech Pros Worldwide Forums | Help | Site Map

how to pass a php array to a sql query

Newbie
 
Join Date: Aug 2005
Posts: 2
#1: Nov 12 '07
hi all,
i have a string for example:
$str="The annual general meeting is scheduled on ::date:: at ::time::.
you all are requested to be present on ::date:: at ::time::."

i store it in the Templates_T table in my DB. Template_ID =1 [for eg.] now this format can be used further for drafting letters or creating newsletters etc..

Now in newsletters.php page for every ::date:: and ::time:: instance, for Template_ID 1 , i load textboxes in array for date[<input type =text name="dispdate[]">] and <select NAME="hours[]" > </select> <select NAME="minutes[]" > </select>in array[] for time. user enters some date and time in the textboxes and <select> respectively. now while updating the content in DB it should go as:
Step 1 : select the entire content from Template_T where Template_ID=1
Step 2: modify the message and replace ::date:: and ::time:: with relevant textbox values , <select > values. some thing like below
Expand|Select|Wrap|Line Numbers
  1. $EntireContent="The annual general meeting is scheduled on 'ValueOfBoxInArray' at 'ValueOfSelectInArray'.
  2. you all are requested to be present on 'ValueOfBoxInArray' at 'ValueOfSelectInArray'."
  3. $sql=mysql_query("insert into mytable_t values('$EntireContent')");
  4.  
how will i accomplish this?

Thanks in advance

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,752
#2: Nov 12 '07

re: how to pass a php array to a sql query


Hi.

All you need is the str_replace function.

You simply loop through each of the values you want to switch out, replacing them one at a time. Something like this:
Expand|Select|Wrap|Line Numbers
  1. foreach($dateArray as $date) {
  2.   $str = str_replace("::date::", $date, $str, 1);
  3. }
  4.  
Reply