Echo inside and Echo

2019-08-17 18:57发布

I'm using echo to sum up the results form a Table. Inside this echo, I place another echo to show all the results form another table. On this page I have an array, I want every item in that array, that is also in the result from the table, to be checked. I use the code below (remember: this code is inside another echo!), it's not functioning, why not?

<?php

$query = "SELECT * FROM profilestemp"; 

$result = mysql_query($query) or die(mysql_error());


while($row = mysql_fetch_array($result)){


$merkenarray = unserialize($row[merken]);


echo "


A VERY BIG OTHER PART OF THE FORM....

<tr>
<td style=\width:150px;background-color:#a8c11f;padding:2px;\"><p style=\"color:White;font-weight:bold\"><b>Merken</b></p></td>
<td><div id=\"rubrieken\">


<?php

$sql = \"SELECT merknaam FROM merken\";
$result = mysql_query($sql);

while ($row2 = mysql_fetch_array($result)) {

    if (isset($merkenarray) && is_array($merkenarray) && in_array($row2[merknaam], $merkenarray)) {

      $checked = \"checked='checked'\";
    }

    else $checked = \"\";

    echo \"&nbsp;&nbsp;<input \".$checked.\"  type=\"checkbox\" name=\"merken[]\" value='\" . $row2[merknaam] . \"'>&nbsp;\" . $row2[merknaam] . \" <Br />  \";

}

?>



</div></td>
</tr>

}

?>

标签: php echo
2条回答
再贱就再见
2楼-- · 2019-08-17 19:04

Your code should look like this

     <?php

            $query = "SELECT * FROM profilestemp"; 

            $result = mysql_query($query) or die(mysql_error());


            while($row = mysql_fetch_array($result)){


            $merkenarray = unserialize($row[merken]);


         ?>

            A VERY BIG OTHER PART OF THE FORM....

            <tr>
            <td style="width:150px;background-color:#a8c11f;padding:2px;"><p style="color:White;font-weight:bold"><b>Merken</b></p></td>
            <td><div id="rubrieken">


            <?php

            $sql = "SELECT merknaam FROM merken";
            $result = mysql_query($sql);

            while ($row2 = mysql_fetch_array($result)) {

                if (isset($merkenarray) && is_array($merkenarray) && in_array($row2[merknaam], $merkenarray)) {

                  $checked = "checked='checked'";
                }

                else $checked = "";

                echo "&nbsp;&nbsp;<input ".$checked."  type="checkbox" name="merken[]" value='" . $row2[merknaam] . "'>&nbsp;" . $row2[merknaam] . " <Br />  ";

            }

            ?>



            </div></td>
            </tr>

<?php }?>
查看更多
我想做一个坏孩纸
3楼-- · 2019-08-17 19:04

Instead of using echo in your function, have it return the string to be output. An example:

function functionName() {
    return 'Some content to be output';
}

echo functionName();

Additionally, this will give your function more flexibility, as you might not want to echo the result every time, e.g:

function functionName() {
    return 'Some content to be output';
}

// Write the result of functionName to a file
file_put_contents('content.txt', functionName());    
查看更多
登录 后发表回答