Jump to content

[SOLVED] Generate a list from an array


arnoldb

Recommended Posts

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

Link to comment
Share on other sites

<?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;
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.