0

i have a php code that that have a function that print html via echo like so:

<?php
function show_html($num_found)
{
$html="

<html>
<head>
</head>
<body>

<table width='100%' border='1'>
<tr align='center'>
<th colspan='2'>ERROR REPORT</th>
</tr>
<tr align='center'>
<td>Number of error found</td>
<td>  ***$num_found*** </td>
</tr>                               
</table>
</body>
</html>";

return $html;

}    
echo show_html($num_found);
?>    

how can i print the $num_found variable inside the table ?

3 Answers 3

3

This is basic PHP. Since you are using double quotes for your string you can just place the variable into the string and it will be interpolated:

<?php
function show_html($num_found)
{
$html="

<html>
<head>
</head>
<body>

<table width='100%' border='1'>
<tr align='center'>
<th colspan='2'>ERROR REPORT</th>
</tr>
<tr align='center'>
<td>Number of error found</td>
<td>$num_found</td>
</tr>                               
</table>
</body>
</html>";

return $html;

}    
echo show_html($num_found);
?>  

or you can use concatentation:

<td>" . $num_found . "</td>
Sign up to request clarification or add additional context in comments.

3 Comments

i have tried that and it doesnt work, the call is via ajax by the way(to the php file)
The ajax shouldn't matter. Are you sure $num_found has a value?
yes it works i had an error in a query before that . :( my bad. thanks any way
3

If what you have isn't working, try concatenating the value:

<?php
function show_html($num_found)
{
   // if you aren't sure if "num_found" is going to be set, add this:
   if (!isset($num_found)) $num_found = 0;

   // now set the html
   $html = "<html><head></head><body>
      <table width='100%' border='1'>
         <tr align='center'><th colspan='2'>ERROR REPORT</th></tr>
         <tr align='center'>
            <td>Number of error found</td>
            <td>".$num_found."</td>
         </tr>                               
      </table>
   </body></html>";

   return $html;
}

// the num found variable needs to be defined somewhere...
$num_found = 10;

// after that the echo will work
echo show_html($num_found);
?>   

6 Comments

i have tried that and it doesnt work, the call is via ajax by the way(to the php file)
I ran this on my test server and it works as long as I define $num_found. Will edit so you can see what I did.
If your using ajax then you will need to get the html table in a javascript variable (the returned value from your ajax call, then add it to the document, which can be done in jquery with something like $(document).append(content)
This code definitely works. If it isn't working for you, your problem is elsewhere
In reference to what @Jake said the html should be correct if based on this example, but you are going to have to use jQuery .html() routine or something similar in order to display the response. Also, unless you are using an IFrame you might not need the html/head/body tags but instead just use a div. Hope that helps!
|
0

What you have now should print the value of the variable $num_found inside the table as-is. If it does not, the variable may not be defined.

Try this: in your function, put this line before your $html="...":

$num_found=5; //test

if the value shows up now, somehow the variable is not being set in your code. I think the issue is elsewhere, i.e. how are you calling your AJAX function. The value passing may be dropped somewhere on the way.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.