mellis95 Posted July 21, 2009 Share Posted July 21, 2009 In a function, I have an html table as follows: function Displaybot2() { echo '<center><table width="1000" name="bot2_table"><td><center><b>BOT2</b></center<</td></tr>'; echo '<tr><th></th><th>Raw Score</th><th>Standard Scale Score</th><th>Age EQ/Percentile</th><th>Description</th></tr>'; echo '<tr><td><b>Fine Motor Precision:</b></td><td><input name="fmp_raw" class="element text" size="8" type="text" value=$HERE IS WHERE I WANT A VARIABLE></td>'; echo '<td><input name="fmp_standard" class="element text" size="8" value="" type="text"></td>'; echo '<td><input name="fmp_age_eq" class="element text" size="12" value="" type="text"></td>'; echo '<td><input name="fmp_description" class="element text" size="50" value="" type="text"></td></tr>'; I cannot for the life of me figure out how to properly code a php variable into the value="" of the input box. (See ALL CAPS code above). I have tried searching google for an answer, but I am not even sure what to search for to get the answer. Thanks in advance for the help. Matt Quote Link to comment https://forums.phpfreaks.com/topic/166841-solved-echo-php-in-html-in-php/ Share on other sites More sharing options...
rhodesa Posted July 21, 2009 Share Posted July 21, 2009 You just need to stop the string, concatenate your variable, then start the string again: echo '<tr><td><b>Fine Motor Precision:</b></td><td><input name="fmp_raw" class="element text" size="8" type="text" value="'.$variable.'"></td>'; the other way to do it is stop/start php <?php echo "Here is some PHP code"; ?> <tr><td><b>Fine Motor Precision:</b></td><td><input name="fmp_raw" class="element text" size="8" type="text" value="<?php echo $variable; ?>"></td> <?php echo "More PHP"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/166841-solved-echo-php-in-html-in-php/#findComment-879747 Share on other sites More sharing options...
jamesxg1 Posted July 21, 2009 Share Posted July 21, 2009 function Displaybot2() { echo <<<EOF <center><table width="1000" name="bot2_table"><td><center><b>BOT2</b></center<</td></tr> <tr><th></th><th>Raw Score</th><th>Standard Scale Score</th><th>Age EQ/Percentile</th><th>Description</th></tr> <tr><td><b>Fine Motor Precision:</b></td><td><input name="fmp_raw" class="element text" size="8" type="text" value=$TheVAR></td> <td><input name="fmp_standard" class="element text" size="8" value="" type="text"></td> <td><input name="fmp_age_eq" class="element text" size="12" value="" type="text"></td> <td><input name="fmp_description" class="element text" size="50" value="" type="text"></td></tr> EOF; Quote Link to comment https://forums.phpfreaks.com/topic/166841-solved-echo-php-in-html-in-php/#findComment-879752 Share on other sites More sharing options...
mellis95 Posted July 23, 2009 Author Share Posted July 23, 2009 function Displaybot2() { echo <<<EOF <center><table width="1000" name="bot2_table"><td><center><b>BOT2</b></center<</td></tr> <tr><th></th><th>Raw Score</th><th>Standard Scale Score</th><th>Age EQ/Percentile</th><th>Description</th></tr> <tr><td><b>Fine Motor Precision:</b></td><td><input name="fmp_raw" class="element text" size="8" type="text" value=$TheVAR></td> <td><input name="fmp_standard" class="element text" size="8" value="" type="text"></td> <td><input name="fmp_age_eq" class="element text" size="12" value="" type="text"></td> <td><input name="fmp_description" class="element text" size="50" value="" type="text"></td></tr> EOF; Okay, I have been pulling my hair out over this one. If I try to go this route, which seems like the best way to do this, I get the following error: Parse error: syntax error, unexpected '"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in..... The line number for the error is this line: if(($p1c["bot2"])==1){ Which I suppose means it could be any line of code in the function that is throwing the error.... I cannot find the problem here. Maybe I just need another set of eyes on it. Here is what I have at this point (relevant code): function Displaybot2() { echo<<<EOF <center><table width='1000' name='bot2_table'><td><center><b>BOT2</b></center<</td></tr> <tr><th></th><th>Raw Score</th><th>Standard Scale Score</th><th>Age EQ/Percentile</th><th>Description</th></tr> <tr><td><b>Fine Motor Precision:</b></td><td><input name="fmp_raw" size="8" type="text" value=$fmp_raw></td> </tr></table></center> EOF;} //Display appropriate tables ---------------- if(($p1c["bot2"])==1){ Displaybot2(); } ?> Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/166841-solved-echo-php-in-html-in-php/#findComment-881426 Share on other sites More sharing options...
rhodesa Posted July 23, 2009 Share Posted July 23, 2009 you are gonna kick yourself when you hear the answer The closing EOF needs to be all the way to the left...no whitespace: function Displaybot2() { echo<<<EOF <center><table width='1000' name='bot2_table'><td><center><b>BOT2</b></center<</td></tr> <tr><th></th><th>Raw Score</th><th>Standard Scale Score</th><th>Age EQ/Percentile</th><th>Description</th></tr> <tr><td><b>Fine Motor Precision:</b></td><td><input name="fmp_raw" size="8" type="text" value=$fmp_raw></td> </tr></table></center> EOF;} //Display appropriate tables ---------------- if(($p1c["bot2"])==1){ Displaybot2(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/166841-solved-echo-php-in-html-in-php/#findComment-881437 Share on other sites More sharing options...
mellis95 Posted July 24, 2009 Author Share Posted July 24, 2009 FINALLY! In addition to having whitespace before the EOF close, I discovered that I cannot display a variable the way I wanted to unless it was declared IN the function I wanted to use it in. Is that right? I am no expert, but I have done a lot of php lately and have not come across this issue yet. If I move the relevant variable declaration inside the function, it works. Quote Link to comment https://forums.phpfreaks.com/topic/166841-solved-echo-php-in-html-in-php/#findComment-881587 Share on other sites More sharing options...
rhodesa Posted July 24, 2009 Share Posted July 24, 2009 Yes, functions do not directly have access to a variable declared outside the function. Here is more info on variable scope: http://devzone.zend.com/node/view/id/637 What you are going to want to do is pass it as an argument Quote Link to comment https://forums.phpfreaks.com/topic/166841-solved-echo-php-in-html-in-php/#findComment-881843 Share on other sites More sharing options...
mellis95 Posted July 24, 2009 Author Share Posted July 24, 2009 That makes sense. I have passed arguments to functions in tutorials while trying to teach myself php, but I just didn't think about that here. So... In this case, the variables for the function isn't used anywhere else in the page. Is there a best practice on when to declare variables within a function or when to pass them as arguments? Also, in this particular function, there are about 20 variables used, all from an array, so would it be appropriate to just pass the array as the argument and then be able to use all of the different elements of the array inside the query? For example: $array=mysql_fetch_array($query); function test($array) { some function here echo $array[field1]; echo $array[field2]; etc....... } Thank you for the help. Quote Link to comment https://forums.phpfreaks.com/topic/166841-solved-echo-php-in-html-in-php/#findComment-881911 Share on other sites More sharing options...
rhodesa Posted July 24, 2009 Share Posted July 24, 2009 it's kind of a judgment call on your first question. functions are supposed to provide an easy way to reuse code. so, if it's something specific, it should probably be passed to the function, while something that is directly related to the code in the function, should be in there with it. as for the array, yes it's perfectly acceptable to just pass the array. Quote Link to comment https://forums.phpfreaks.com/topic/166841-solved-echo-php-in-html-in-php/#findComment-881924 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.