Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there a better way to do this? 2

Status
Not open for further replies.

JSProgramIA

Programmer
Oct 28, 2005
41
US
by better I guess more efficient, less code maybe?

Code:
.
.
.

$emails_row = mysql_fetch_array($emails_result);
			
$message = $emails_row['emails_text'];
		
// Replace values
$message = str_replace("::company name::", stripslashes($emails_row['company_name']), $message);
			
$message = str_replace("::project name::", stripslashes($emails_row['project_name']), $message);
			
$message = str_replace("::current total hours::", $emails_row['total_hours'], $message);			

	 
$message = str_replace("::current hours owed::", $emails_row['total_hours']-$emails_row['hours_paid'], $message);			

	 
$message = str_replace("::current hours paid::", $emails_row['hours_paid'], $message);			

$message = str_replace("::tasks::", $_POST['email_text'], $message);	
			
$message = str_replace("::developer::", $_POST['developer'], $message);
			
$message = str_replace("::date::", date("F j, Y"), $message);

Are there too many 'variables' for this to be done in a function?
 
You could do something similar to this:
Code:
function sendEmail($company,$project_name,$total_hours e.t.c.....)
{
  $message="$company \n $project_name \n $total_hours e.t.c....";

  $subject= "subject line here";
  $address= "whoever@wherever.com";

  mail($address,$subject,$message,'additional headers here');

}


sendEmail($emails_row['company_name'],$emails_row['project_name'],e.t.c.....)

Mail function from manual:
 
actually i meant all the str_replace's.

do you see a pattern similar enough to make a function there?
 
Something like this should do it then:
Code:
function sendEmail($company,$project_name,$total_hours e.t.c.....)
{

$message = str_replace("::company name::", stripslashes($company), $message);
            
$message = str_replace("::project name::", stripslashes($project_name), $message);
            
$message = str_replace("::current total hours::", $total_hours,$message);            

     
e.t.c.....


}



sendEmail($emails_row['company_name'],$emails_row['project_name'],e.t.c.....);
 
Doesn't really help you with making the code more compact though.
 
Ok here it is in context:

In a database there is a string like this:

On ::date:: we worked on project ::project name:: and spent ::hours worked:: on it. ( etc ).

Is it possible to have an array of the things we are looking to replace in the string ( the ::variable:: ) and yet ANOTHER array of what to replace with? Then just loop through and voila... everything is replaced by using one str_replace, rather than a ton of them?

This would be BEST if this was possible:

$message = myfunction(searchfor, replace);

but thats only psuedocode. not sure if php-able.
 
The thing is, the user can change the email, so thats why i used those weird ::variables:: so i can easily replace them with data from the data source.
 
You should be able to do it like this:
Code:
$to_replace = array("::company name::","::project_name::","::total_hours::",e.t.c....);

for($i = 0;$i < 8;$i++)
{
$message = str_replace($to_replace[$i],stripslashes($emails_row[$i]),$message);
}
The $emails_row variable is already an array. There may be a better way of doing this using a native function, but if there is I don't know it.
 
You could put the loop above in a function and pass the arrays in.
 
if both $to_replace and $emails_row are arrays, you can use preg_replace, you can just call both arrays:



$message = preg_replace($to_replace,$emails_row,$message);

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
and if you use the same variable names as field names you could just extract the relevant row variables and use them directly:

Code:
while ($row = mysql_fetch_array($result)):
 foreach ($row as $var):
  $var = stripslashes ($var);
 endforeach;
 extract($row);
 echo "$field1 $field2 $field3"; //etc
endwhile:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top