Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/166841-solved-echo-php-in-html-in-php/
Share on other sites

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";
?>

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;

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.

 

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();
}

?>

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.

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.