Page 1 of 1

Bunch of code into a string

Posted: Wed Mar 24, 2004 5:59 pm
by John Cartwright
Can i do something like this? Obviously not... but how do I do something like this.

Code: Select all

<?
$message = "echo "Type of site: ".$type;                                             //type
if (isset($type_other)){ echo "Other: ".$type_other."<br>"; }            //type_other
echo "Features for the site: <br>";                                      //features
foreach($feature as $feat){ if(isset($feat)){ echo $feat ."<br>"; } }
if (isset($features_other1)){ echo $features_other1."<br>"; }            //features_other
echo "Estimated Budget: ".$budget."<br>";                                //budget
echo "Time until we can start project: ".$time_start."<br>";             //time to start
echo "Time Frame: ".$time_frame."<br>";                                  //time frame
echo "Overview: ".$description."<br>";                                   //description ; "
"; 
?>
If not is there an alternative? Reason I ask cause it's form a mail script I'm making.

Posted: Wed Mar 24, 2004 6:01 pm
by markl999
You can (not like that, but you can) .. but i'm not sure you want/need to, maybe first explain exactly what it's purpose would be?

Posted: Wed Mar 24, 2004 6:03 pm
by John Cartwright
We'll what's wrong with it if I can do it...

just by looking at it you can see the highlights are all wrong.. obviously the quotes which r giving parse error...

Posted: Wed Mar 24, 2004 6:06 pm
by markl999
Well, you said you need it for a mail script, putting raw PHP commands in an email seems to have no purpose to me, which is why i was just checking what you intended it to do. I still think you don't want to do what you originally asked, "Put a bunch of code in a string"

Posted: Wed Mar 24, 2004 6:08 pm
by John Cartwright
How would get the same results of that php code to fit in $message so I can do this

mail ($to, $subject, "Name: $name\nE-mail: $email\nMessage: $message\n\nIP: $REMOTE_ADDR", "From: $email")

Posted: Wed Mar 24, 2004 6:11 pm
by markl999
Ah, makes sense now ;)

Code: Select all

$message = 'Type of site : '.$type."\n";
if (isset($type_other)){
    $message .= 'Other : '.$type_other."\n";
}
$message .= 'Estimated Budget: '.$budget."\n";
//etc...etc...

Posted: Wed Mar 24, 2004 6:13 pm
by John Cartwright
Thanks mark :) as usual you are much help

btw .= adds to the string?

Posted: Wed Mar 24, 2004 6:14 pm
by markl999
Yeah, the . means 'concatenate' and basically means 'shove on the end of' ;)
Eg
$a = 'hello ';
$b = 'fred';
$c = $a.$b;
echo $c; // shows hello fred

or the same thing with .=
$a = 'hello ';
$a .= 'fred';
echo $a; // shows hello frd

Posted: Wed Mar 24, 2004 6:38 pm
by John Cartwright
This is my problem now

I got a checkbox that has a value of lets say Shopping Cart.
And I got my newly done code for $message

Code: Select all

<?php
$message = 'Type of site: '.$type.'<br>';		   
if ($type_other != ""){ 
$message .= 'Other: '.$type_other.'<br>'; 
} 
$message .= 'Features for the site: <br>';
foreach($feature as $feat){
if(isset($feat)){
$message .= '- '.$feat.'<br>';
}
}
if (isset($features_other1)){
$message .= $features_other1.'<br>';
}
$message .= 'Estimated Budget: '.$budget.'<br>';
$message .= 'Time until we can start project: '.$time_start.'<br>';
$message .= 'Time Frame: '.$time_frame.'<br>';
$message .= 'Overview: '.$description.'<br>'; 
?>
Specifically looking at

Code: Select all

<?php

foreach($feature as $feat){
if(isset($feat)){
$message .= '- '.$feat.'<br>';
}
}

?>
With the value shopping cart I would only get the word Shopping back...it only takes the first word in the output.

Posted: Wed Mar 24, 2004 6:39 pm
by markl999
In your form make sure you quote the value, eg value="Shopping Cart" instead of value=Shopping Cart

Posted: Wed Mar 24, 2004 6:43 pm
by John Cartwright
lol im so stupid :P ty

Posted: Wed Mar 24, 2004 9:32 pm
by m3mn0n
You should read the official PHP Manual (you can download it from the link in my signature), it explains all of this, and more.

Or grab a good book, it's pretty much the same info as the guide books explains things in the way mark did. :wink: