A few simple PHP questions...

Associate
Joined
29 Dec 2004
Posts
2,253
Hi,

I'm trying to do a few basic things in PHP, problem being i am used to tweaking other's code rather than writing it from scratch so im having a few problems. The first thing i'd like to do is create a statement saying IF foo then echo bar, the problem being that the string containing foo also has a lot of other bits in it that id like to ignore. Here's an example, i have a combobox with lots of addresses, like so:

AAA - Mr Test, Test Town, Testshire, AA11 1AA
AAA - Another Test, No Reason, Just Felt Like It, BB1 1AB
BBB - Test again, But this time with a different beginning, AA12 3DB

What i'd like to do is say IF any part of that string matches AAA then echo "Some statement about AAA", or IF any part of that string matches BBB then echo "A different statement about BBB". I have about 8 different "zones" and this is what these "AAA" refer to.

So basically, how do i code that in PHP? I know how to do a basic IF statement, just not how to search for a certain bit of text in a string? There's more to come but this is my starting block.

TIA
 
Last edited:
Well i'm making progress, but am stuck on one bit. The following works perfectly fine if the string ($theaddress) contains the first part of the IF statement, but if its anything else i get a blank white page. Here's my code:

Code:
$theaddress = $_POST['address'];

if (strpos($theaddress, 'EBD') !== false) {
  $subto = 'Somebody here';
} else if (strpost($theaddress, 'HR') !== false) {
  $subto = 'Somebody else here';
} else if (strpost($theaddress, 'BH') !== false) {
  $subto = 'Somebody else here';
} else if (strpost($theaddress, 'AAW') !== false) {
  $subto = 'Somebody else here';
} else if (strpost($theaddress, 'AAR') !== false) {
  $subto = 'Somebody else here';
} else if (strpost($theaddress, 'HC') !== false) {
  $subto = 'Somebody else here';
} else if (strpost($theaddress, 'MSX') !== false) {
  $subto = 'Somebody else here';
} else if (strpost($theaddress, 'WSX') !== false) {
  $subto = 'Somebody else here';
}

For example, if $theaddress contains EBD it works perfectly, but if it contains anything else (either one of the above conditions such as AAW, MSX, WSX, etc, or none of those conditions) it will fall over and display a white page. I'm sure it's something simple and appreciate any help :p

Oh and i also tried it with the break; as above but no luck.
 
Last edited:
Ok, so here's my next problem. I am doing all of this to basically allow our team to submit orders to our procurement lady a lot quicker, one requirement is that the output can be sent as an attachment (either plain text or HTML file) to an email address.

So next question is, is that possible - can i use PHP to take the output from a form, place it into a templated HTML file, and then send it "attached" to an email address rather than being in the body of the email?
 
Awesome, thanks for that!

One more "main" question:
Are there any utilities that will allow me to "mail merge" the output of a form into a word processor templated document? For example, our current order form is a Word template and our procurement lady would like to keep a similar format...so i was hoping i could "merge" the form output with that template?

Thanks again.
 
Ok another question about getting info out of a string. Same situation as above, an example of what i have in a combobox:

AAA - Dr Test, Test Town, Testshire, AA11 1AA
AAA - Dr Another , No Reason, Just Felt Like It, BB1 1AB
BBB - Dr Again, But this time with a different beginning, AA12 3DB

Let's say i wanted to extract the doctor's name, for example just to output "Dr Another". How do i go about doing that? Obviously i can't use the same thing as before?

Edit: Have been looking into preg_match to do the job but having problems starting, can anyone provide a little bit of example code for how i could use it?
 
Last edited:
Beauty! Works a charm, tweaked it around by removing the -1 from the second line and adding +1 to the first line (as it was losing the last letter of the name and showing the hyphen!) but that is excellent. Thanks very much.

Anyone know about my previous question...merging form output into a Word/Open Office document template?

Oh and the string comes from a combobox, so should all follow the same format.
 
Last edited:
Right then, this is my main problem now. Does anyone know of any PHP script that would allow me to take the output from the form and export it to either a CSV file or Word template so that it can be emailed? I was hoping to do it by putting it into an HTML template but unfortunately people want to be able to edit it after is has been submitted...

Help!!
 
Am making progress, do you know how to format the string im inputting into the file so that Word will open it without wanting to convert it to something? Can i force it to be UTF-8 or something? Also, when i use \n and open it in Notepad it shows a little square symbol rather than adding a line break. Can i sort this somehow?
 
Nice one, \r\n works a treat. The UTF thing i was talking was because when i opened the .txt file in Word it wanted to convert it to some MS format...but since doing \r\n it's sorted that as well so i'm all good!

Do you know of any way to format things so it looks slightly "fancier" or is it only possible to do plain text outputs?
 
Couldn't figure out where to put the header(msword) bit as doesn't seem to work without it (tried <b>test</b> with no luck) so kept going through the thread and found a link to http://phprtf.com which looks pretty good, might take me a while but may be worth it.
 
I'm not sure what to say so i'll post my code and hope you point out something ridiculously easy like earlier :p

Code:
// write the file

$thecontent = "<b>Reference:</b> " . $ref . 
	"\r\n<b>Date submitted:</b> " . $date . "\r\n" . 
	"_____________________________________" .
	"\r\n\r\n<b>Submitted by:</b> " . $subby .
	"\r\n<b>Submitted to:</b> " . $subto .
	"\r\n<b>Delivery address:</b> \r\n\r\n" . $deladdress .
	"\r\n\r\n<b>Log number:</b> " . $logno . "\r\n" .
	"_____________________________________" .
	"\r\n\r\nItem 1: " . $item1 .
	"\r\nQuantity: " . $quantity1 .
	"\r\nReason: " . $reason1 . 
	"\r\nType: " . $type1 .
	"\r\nOld assets: " . $asset1 .
	"\r\nNotes: " . $notes1;

$filename = "c:\\inetpub\\wwwroot\\data\\submitted\\" . $ref . ".doc";
$somecontent = $thecontent;

$fh = fopen($filename, 'w+');

if (is_writable($filename)) {

    if (!$handle = fopen($filename, 'w+')) {
         echo "Cannot open file ($filename)";
         exit;
    }
    if (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }
    echo "<br \><br \>Success, wrote to file ($filename)";

    fclose($handle);

} else {
    echo "The file $filename is not writable";
}
fclose($fh);

If my code is weird please tell me and i'll correct it! I notice your using ' and im using ", does that make a difference?!
 
you're doing something weird - you're running fopen and assigning it to $fh, but then you're doing it again and assigning it to $handle, but not fclose-ing $handle.

For some reason if i didn't do this it would never open the file, but as soon as i added that in it worked fine. If i didn't have it it would give the "The filename is not writable" message for some reason?

have you checked your path is right, too? for now, try writing to one in the same directory, you won't need to refer to something like c:\ - whilst you're testing to

The file is created and written alright, it writes all the data i want into the file but Word simply doesn't "process" the <b></b> tags, it displays them as text!
 
If no idea's, do you know how i can use this to save a file rather than "output" it? At the moment when you use those sample scripts they all let you save the file to your HDD but i need to save it to the server like i'm doing above?

Huge thanks for your help by the way :)
 
Ha that's strange, literally finally found that out 10 minutes ago by hurting my eyes looking through the API - it doesn't help that i don't particularly know what im looking for! Am all sorted with that bit now. :)

Next question, i think i'm going to stick with this phprtf as it looks pretty cool and should do all i need (and more). First problem i'm having is using the "setLandscape" function in the rtf class.

I've been trying variations of

Code:
$rtf->setLandscape();

But the document is always portrait :p. So question being, what is "void" (as that's what the API says it is, and how do i "set" it?!

Thanks very much for the help by the way, i missed you :D
 
Turns out i had to set it using the pages height/width, apparently the landscape function sets the printing default to landscape and thats all!

Now all i have to do is try and figure out how to give people the option of ordering more than one item at one time...my only idea at the moment is to create loads of forms and PHP processing pages to co-incide with the amount of items the user wants to order...

Unless there's a better option?
 
Right then, new question!

As my form is "embedded" into the Wiki application i am using i can't do any client side validation on it so have some validation at the start of the PHP file that processes the form and creates my RTF file etc. The only problem i have is if the validation isn't passed it will send the user back to the previous page (using history.go(-1)) and the form contents has disappeared.

Obviously this could be quite annoying if you have filled out the entire form and missed one required field, and on being told you missed the filed you then have to fill out the entire form all over again!

Is there a better way of returning the user to the form without them loosing the data they entered?
 
Back
Top Bottom