Jump to content

Combine strings and functions into variable.


xiizth

Recommended Posts

I’ve been looking all over and I cannot seem to figure out how to do this…Id like to combine all of this… $message=$message . '<b><font color="black" size=4>' . $title . '</font></b><br>';if ($item=="YES")$message=$message . '<b><font color="blue">' . "THE " . $title . " IS ACCOUNTED FOR AND IN GOOD WORKING ORDER" . '</font></b><br>';else$message=$message . '<b><font color="red">' . "THERE IS A PROBLEM WITH THE " . $title . "!!!" . '</font></b><br>';$message=$message . '<font color="black">' . "COMMENT: " . $item_comment . '</font><br><br>';I want to be able to call this block of code and have the variables inseted into it. I am creating this for my local fire department which i am a member of. Any help would be great.

Link to comment
Share on other sites

This is not ment to be a double post. Just trying to provide more information. I dont want to have to keep copy and pasting the larger block of code over and over again.Thanks $class_a_foam = $_REQUEST["class_a_foam"];$class_a_foam_comment = $_REQUEST["class_a_foam_comment"]; //CLASS A FOAM$title="CLASS A FOAM";$item=$class_a_foam;$item_comment=$class_a_foam_comment; $message=$message . '<b><font color="black" size=4>' . $title . '</font></b><br>';if ($item=="YES")$message=$message . '<b><font color="blue">' . "THE " . $title . " IS ACCOUNTED FOR AND IN GOOD WORKING ORDER" . '</font></b><br>';else$message=$message . '<b><font color="red">' . "THERE IS A PROBLEM WITH THE " . $title . "!!!" . '</font></b><br>';$message=$message . '<font color="black">' . "COMMENT: " . $item_comment . '</font><br><br>';

Link to comment
Share on other sites

I would add the titles to the form data also, so that everything is in there. You can loop over $_REQUEST and check for each thing. If your title was called "class_a_foam_title" then you could check like this:

foreach ($_REQUEST as $k => $v){  echo 'checking ' . $k . '<br>';  if (strpos($k, '_title') == strlen($k) - strlen('_title') - 1) // if $k ends with '_title'  {    $base = substr($k, 0, strlen($k) - strlen('_title')); // remove '_title' from the end of $k    echo 'base is ' . $base . '<br>';    $title = $v;    $item = $_REQUEST[$base];    $item_comment = $_REQUEST[$base . '_comment'];    echo 'title: ' . $title . '<br>';    echo 'item: ' . $item . '<br>';    echo 'comment: ' . $item_comment . '<br>';  }}

http://www.php.net/m...res.foreach.phphttp://www.php.net/m...tion.substr.phphttp://www.php.net/m...tion.strlen.php

Link to comment
Share on other sites

Thank you for the reply. I should have started off with saying that I am very new to php. So I really appreciate your help. I’ve attached one of the previous html forms with the corresponding php script. It works; I just know there is a better way of achieving the same goal without retyping all the script over again. The vehicle check list I attached has the fewest amounts of items that we check for.Thank you so much!

m13_tcheck.html

m13_tcheck_submit.php

Link to comment
Share on other sites

Right, the code I showed above is the code you need to loop through $_REQUEST. I added the echo statements so that you can run that and see what the code does, you can replace those echo statements with the HTML that you want to print for each item. I made variables called $title, $item, and $item_comment, those are the same variable names as your code. That code expects you to have something like "class_a_foam", "class_a_foam_comment", and "class_a_foam_title". It finds the "_title" item and gets the other 2 with the same name, and then you can use those variables with your other code to print the HTML. It requires you to add the _title items to the form also, which you can add as hidden inputs so that they don't show on the page. I included the links to the PHP manual in case you didn't know what those things did.

Link to comment
Share on other sites

This is the output i am getting: checking x2_spanner_wrenches_titlechecking x2_spanner_wrencheschecking x2_spanner_wrenches_commentchecking class_a_foam_titlechecking class_a_foamchecking class_a_foam_commentchecking emailchecking SUBMITYour mail was sent successfully Shouldn't it echo the title, item, and comment? Thanks

Edited by xiizth
Link to comment
Share on other sites

It should, I didn't test that code so the if statement must be wrong. Print out what it shows for strpos: echo 'checking ' . $k . '<br>';echo 'position of _title: ' . strpos($k, '_title') . '<br>';echo 'length: ' . strlen($k) . '<br>';Maybe subtracting the 1 is not necessary, I did that because strpos is 0-based (the first character is position 0).

Link to comment
Share on other sites

checking x2_spanner_wrenches_titleposition of _title: 19length: 25checking x2_spanner_wrenchesposition of _title:length: 19checking x2_spanner_wrenches_commentposition of _title:length: 27checking class_a_foam_titleposition of _title: 12length: 18checking class_a_foamposition of _title:length: 12checking class_a_foam_commentposition of _title:length: 20checking emailposition of _title:length: 5checking SUBMITposition of _title:length: 6 Here is the output with the echos in there. Thanks

Link to comment
Share on other sites

It's fine if it scans everything that was submitted since it only acts on things that end in "_title", it will skip everything else. But that will let you add any arbitrary number of items to your form and still have the code work, as long as the names follow the same pattern. You can even have names that contain "_title" in the middle, it will only process things that end with it. You can also change strlen('_title') to the number 6 if you want, since the length of "_title" is always 6, I just wrote it like that to make it more obvious what it's doing.

Link to comment
Share on other sites

  • 10 months later...

I haven't been able to work on this is quite a while. Life gets in the way.

 

I have been trying to make this whole process a lot more easy for me to maintain. I have decided to have a have php read a csv file and output to an html table. It seems to be working-ish. Still some bugs to work out. I guess the issue I have is the submit function that you helped me with before is now not working because the vars that I had no longer exist.

 

I hope im not doing the wrong thing by opening this topic back up.

 

this is my code...

<html><head><title>PPFD</title><link rel="stylesheet" href="css/lightbox.css" type="text/css" media="screen" /><script src="js/prototype.js" type="text/javascript"></script><script src="js/scriptaculous.js?load=effects,builder" type="text/javascript"></script><script src="js/lightbox.js" type="text/javascript"></script><style type="text/css">#checklist{	font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;	width: 100%;	border-collapse: collapse;}#checklist td, #checklist th{	font-size-:1em;	border:1px solid #98bf21;	passing:3px 7px 2px 7px}    #checklist th{	font-size:1.1em;	text-align:center;	padding-top:5px;	padding-bottom:4px;	background-color:#A7C942;	color:#ffffff;}#checklist tr.alt td{	color:#000000;	background-color:#EAF2D3;}</style></head><body><h1 align="center">PAINTED POST FIRE DEPARTMENT</h1><h1 align="center">ENGINE AND STATION CHECK SHEET</h1><!-- Truck name --><p align="center"><strong>B67</strong></p><!-- NAME YOU WANT TO GIVE TO THE TRUCK FORM --><form id="truck_check_m67_form" name="truck_check_m67_form" method="post" action="submit.php"><?phpecho '<table id="checklist"><tr><th width="35%">ENGINE CHECKS</th><th width="24%">ACCOUNTED</th><th width="30%">COMMENT</th><th width="11%">PICTURE</th></tr>';$file_handle = fopen("b67.csv", "r");$odd = true;$colors = array('#E0E0E0', '#FFFFFF');while (!feof($file_handle) ) {    $line_of_text = fgetcsv($file_handle, 1024);    $odd = true;    while ($line_of_text)    {           if ($odd)             {                $clr = $colors[0];                $odd = false;                            }        else            {                $clr = $colors[1];                $odd = true;                                            }                echo '    <tr>    <td align="center" valign="middle" bgcolor="'.$clr.'">'.$line_of_text[0].'</td><td align="center" valign="middle" bgcolor="'.$clr.'"><label><input type="radio" name="'.$line_of_text[1].'" value="YES" id="'.$line_of_text[1].'_accounted_0" />Yes</label><label><input type="radio" name="'.$line_of_text[1].'" value="NO" id="'.$line_of_text[1].'_accounted_1" />No</label></td><td align="center" valign="middle" bgcolor="'.$clr.'"><input type="text" name="'.$line_of_text[2].'_comment" id="'.$line_of_text[2].'_comment" /></td><td align="center" valign="middle" bgcolor="'.$clr.'"><a href="images/image_comming_soon.jpg" rel="lightbox"><img src="images/question_mark_icon.jpg" width="30" height="30" /></a></td>	</tr>';    $line_of_text = fgetcsv($file_handle, 1024);    }  }fclose($file_handle);                        echo '</table>';  ?><p align="center"><input name="email" type="hidden" value="B67_TRUCK_CHECKLIST@PPFD.COM" size="30" /></p><p align="center"><input type="submit" name="SUBMIT" id="SUBMIT" value="Submit" /></p></form></body></html>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...