Bunch of code into a string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Bunch of code into a string

Post 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.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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...
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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"
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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")
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Thanks mark :) as usual you are much help

btw .= adds to the string?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

In your form make sure you quote the value, eg value="Shopping Cart" instead of value=Shopping Cart
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

lol im so stupid :P ty
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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:
Post Reply