arnoldb Posted August 12, 2009 Share Posted August 12, 2009 Greetings everyone! I am working on a backend script for our Helpdesk at work (K12 education) and seem to have hit a roadblock. Here are the steps and code I am using: 1) Get info from database: $bpe = mysql_query("SELECT * FROM `hesk_tickets` WHERE `custom1`='015- BPE' and `category`='1' and `status`='3' and `custom10`='1'"); 2) Do a little stripping on the date to make thing easier and check to make sure this ticket is from today only: while($row = mysql_fetch_array($bpe)) { // Take lastchange from database and strip down // to just the date to match our $today date $ticket_date = date('Y-m-d',strtotime($row['lastchange'])); // Check to make sure tickets are for today only if ($ticket_date == $today) { $bpe_arr[] = $row['subject']; } } 3) Now the problem area. I am counting the elements in the array and using that to know how many times to loop through the array. The $message is the body of an email that will be sent, and I'm trying to replace %%REPAIRED-COMPUTERS%% with the information from the code below: for ($i = 0,$c = count($bpe_arr); $i < $c; $i++) { $message = file_get_contents('inc/principal-email.txt'); $message = str_replace('%%REPAIRED-COMPUTERS%%',$bpe_arr[$i],$message); echo "<pre>" . $message . "</pre>"; // print $bpe_arr[$i] . "<br />"; unset($bpe_arr[$i]); } The problem is the whole thing is looping when I only want the str_replace to loop. The way it is right now this is the output I get with 3 items in the $bpe_arr array: The following computers have been repaired and are ready to be picked up at the IT Center between the hours of 8:00am - 11:00am and 12:00pm - 4:00pm. Click on the computer name to be taken to the corresponding Helpdesk ticket. 100-10001-01 The following computers have been repaired and are ready to be picked up at the IT Center between the hours of 8:00am - 11:00am and 12:00pm - 4:00pm. Click on the computer name to be taken to the corresponding Helpdesk ticket. 036-60044-09 The following computers have been repaired and are ready to be picked up at the IT Center between the hours of 8:00am - 11:00am and 12:00pm - 4:00pm. Click on the computer name to be taken to the corresponding Helpdesk ticket. 080-01234-56 My goal is to have the email body displayed once with the list of computers from $bpe_arr under it instead of 3 separate outputs. I hope this makes sense enough for someone to understand. The main problems is that I don't really know what to search for to research this more and fix it! Thanks for any and all thoughts and suggestions! -Brad Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted August 12, 2009 Share Posted August 12, 2009 <?php echo <<<EMAIL The following computers have been repaired and are ready to be picked up at the IT Center between the hours of 8:00am - 11:00am and 12:00pm - 4:00pm. Click on the computer name to be taken to the corresponding Helpdesk ticket. EMAIL; foreach( $bpe_arr as $v ) { echo $v . PHP_EOL; } echo PHP_EOL; ?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted August 12, 2009 Share Posted August 12, 2009 You could also use implode instead of the foreach loop: <?php echo <<<EMAIL The following computers have been repaired and are ready to be picked up at the IT Center between the hours of 8:00am - 11:00am and 12:00pm - 4:00pm. Click on the computer name to be taken to the corresponding Helpdesk ticket. EMAIL; echo implode(PHP_EOL,$bpe_arr) . PHP_EOL; ?> Ken Quote Link to comment Share on other sites More sharing options...
arnoldb Posted August 13, 2009 Author Share Posted August 13, 2009 Both of these methods are already working better than my method but they are still echoing the email body each time there is a $bpe_arr in the foreach or implode() strings. I'm sure this is because I have everything wound up in a for loop but for the life of me cannot figure out how to change this! Any ideas? I'm looking to print the one email body with the data from $bpe_arr under it: The following computers have been repaired and are ready to be picked up at the IT Center between the hours of 8:00am - 11:00am and 12:00pm - 4:00pm. Click on the computer name to be taken to the corresponding Helpdesk ticket. $bpe_arr data $bpe_arr data $bpe_arr data Thanks again! Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted August 13, 2009 Share Posted August 13, 2009 Replace your #3 with either of our solutions and outside of any loops. Quote Link to comment Share on other sites More sharing options...
arnoldb Posted August 13, 2009 Author Share Posted August 13, 2009 Oops sorry about that, I didn't understand exactly what was going on with your solutions and thought I was still needing to process the array. Working like a champ now, thank you very much! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.