waynew Posted June 24, 2008 Share Posted June 24, 2008 I'm sorry guys. Today has been a very bad day. I'm working on a script that automatically writes a number of template letters to a word document. There are three template letters in the database. I'm using placeholders such as [contact] and [todays_date] so that the user can rearrange each template letter and write what they like. So if the user writes: Dear [contact] it will come out Dear "name of person from that row." Of course, each name is different. Anyway, I've set up two arrays. One is for placeholders, such as [contact] etc and the other is for the replacements that will replace those placeholders. The problem is that every name is coming out the same, meaning the names etc aren't incrementing with the loop below: $i = 0; //Replacements array. $replacements = array($contact_name[$i],$title[$i],$tender_reg_date[$i],$comp_name[$i],$date_for_letter,$address_one[$i],$address_two[$i],$cfg_counties[$county[$i]]); //START of loop while($i < sizeof($comp_name)){ //---------------------------------------------------------------------------------------- // START TEMPLATE LETTER // $template is continuously added to until one big string of tender letters is created.. //---------------------------------------------------------------------------------------- $template = $template.$beginning_of_letter; //remains the same //START SWITCH switch($status[$i]){ //Switch works fine. //JOB WON: STATUS CODE 1 case 1: $bodies['win'] = str_replace($placeholders, $replacements, $bodies['win']); $template = $template.$bodies['win']; break; //JOB LOST: STATUS CODE 2 case 2: $bodies['lost'] = str_replace($placeholders, $replacements, $bodies['lost']); $template = $template.$bodies['lost']; break; //JOB PENDING default: $bodies['pending'] = str_replace($placeholders, $replacements, $bodies['pending']); $template = $template.$bodies['pending']; } END SWITCH $template = $template.$letter_ending; $i++; //INCREMENT COUNTER } //END LOOP Is there any reason why the replacement array isn't changing each time, baring in mind that I've tested it inside the loop aswell. When testing it inside the loop I got one name for every letter When testing it outside the loop I got another different name for every letter When it should be giving me different names for each letter. Link to comment https://forums.phpfreaks.com/topic/111671-loop-problem/ Share on other sites More sharing options...
kenrbnsn Posted June 24, 2008 Share Posted June 24, 2008 Shouldn't the replacement array be inside the loop? <?php for ($i = 0;$i < sizeof($comp_name);$i++) { $replacements = array($contact_name[$i], $title[$i], $tender_reg_date[$i], $comp_name[$i], $date_for_letter, $address_one[$i], $address_two[$i], $cfg_counties[$county[$i]]); // // the rest of your code, except the $i++ // } ?> Ken Link to comment https://forums.phpfreaks.com/topic/111671-loop-problem/#findComment-573214 Share on other sites More sharing options...
waynew Posted June 24, 2008 Author Share Posted June 24, 2008 I know. I tried it inside the loop and still I was getting the same name, although different. Funnily enough, the name that came out was Tom Jones :-\ Link to comment https://forums.phpfreaks.com/topic/111671-loop-problem/#findComment-573220 Share on other sites More sharing options...
waynew Posted June 24, 2008 Author Share Posted June 24, 2008 I put it inside the loop but I'm still getting the same names. Everything increments according to $i, so in theory it should run together. Link to comment https://forums.phpfreaks.com/topic/111671-loop-problem/#findComment-573228 Share on other sites More sharing options...
waynew Posted June 24, 2008 Author Share Posted June 24, 2008 Ok I've troubleshooted the problem a bit more. There are over 370 rows in the db, each one carrying a name. For some reason, all won tenders come out as name A, lost tenders name B, etc. So all the letters end up being addressed to three people, when in reality it should be loads of different people for each one. Am I overwriting the one that came before or something? Link to comment https://forums.phpfreaks.com/topic/111671-loop-problem/#findComment-573245 Share on other sites More sharing options...
waynew Posted June 24, 2008 Author Share Posted June 24, 2008 For Case 1 I'm getting Tom Jones, for Case 2 I'm getting Jean, and for Default I'm getting Colm. Link to comment https://forums.phpfreaks.com/topic/111671-loop-problem/#findComment-573253 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.