Jump to content

Loop problem.


waynew

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.