开发者

How do I stop an echo from displaying the same thing twice?

I have a function that displays a few divs running through a loop using echo but when i put the function in it shows the info where i want it in the table cell but also next to the table

Here is my code

function getTestRows($appName)
{
    $implodeArray =array();
    $testsql = "SELECT DISTINCT app_instance_name FROM application_instances WHERE application_name ='$appName' AND environment = 'test' ";
    $testres = mysql_query($testsql);
    if(mysql_num_rows($testres) >=1)
    {
        while($test = mysql_fetch_array($testres))
        {
            echo("<div>".$test['app_instance_name']."</div>");
        }
    }
    else
    {
         echo("<span>No re开发者_运维百科sults found</span>");
    }
} 

and the echo that displays it...

echo("<table id='ver-zebra' style='display: inline-table;'>
<colgroup>
    <col class='vzebra-odd' />
    <col class='vzebra-even'/>
</colgroup>
<thead>
    <th id='appNameHead' OnMouseOver=\"this.style.cursor='pointer';\"  onclick=\"openIndiv('$tmp[0]');\">
        $tmp[0]
    </th>
</thead>
<tbody>
    <tr>
        <th scope='col' id='test'>
        Test
        </th>
    </tr>
    <tr>
<td>
<div style='width: 300px; height: 100px; overflow: auto;'>");
getTestRows($tmp[0]);
echo("</div>
</td>
</tr>

How do I stop an echo from displaying the same thing twice?


When you call echo, it immediately gets put into the response. Have getTestRows() return a String which is your HTML instead:

function getTestRows($appName)
{
    $ret = '';
    $implodeArray =array();
    $testsql = "SELECT DISTINCT app_instance_name FROM application_instances WHERE application_name ='$appName' AND environment = 'test' ";
    $testres = mysql_query($testsql);
    if(mysql_num_rows($testres) >=1)
    {
        while($test = mysql_fetch_array($testres))
        {
            $ret .= "<div>".$test['app_instance_name']."</div>";
        }
    }
    else
    {
         $ret .= "<span>No results found</span>";
    }
    return $ret;
} 


Don't output big chunks of text with echo (or print). It's far easier to just drop out of PHP mode (?>) and output raw HTML instead. If you HAVE to do big chunks of text while in PHP, at least use a HEREDOC. They act exactly like a double-quoted string, but don't use quotes as delimiters, so you don't have to escape anything EXCEPT $ signs when you don't want them seen as variables.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜