猿问

函数在尝试重复代码时中断页面

我是新手开发人员。有这段代码:


<?php


$sql_client = "SELECT * FROM clienti WHERE nume = ? LIMIT 1";

$stmt_client = $conn->prepare($sql_client);

$stmt_client->bind_param("s", $nume);

$stmt_client->execute();

$result_client = $stmt_client->get_result();

while($row = $result_client->fetch_assoc())

    {

?>

        <td style="width:35%;">

            <b>Cumpărător</b>:<br>

            <?php echo $row["nume"]; ?><br>

            <b>Nr Orc</b>: <?php echo $row["reg_com"]; ?><br>

            <b>CIF</b>: <?php echo $row["cif"]; ?><br>

            <b>Sediu</b>:<br>

            <?php echo $row["adresa"]; ?><br>

            <b>Banca</b>:<br>

            <?php echo $row["banca"]; ?><br>

            <b>Cont bancar</b>:<br>

            <?php echo $row["cont_bancar"]; ?><br>

        </td>

    </tr>

</table>


<?php

    }

?>

来自第二个文件的代码


<?php 

$sql_client = "SELECT * FROM clienti WHERE nume = ? LIMIT 1";

$stmt_client = $conn->prepare($sql_client);

$stmt_client->bind_param("s", $nume);

$stmt_client->execute();

$result_client = $stmt_client->get_result();

while($row = $result_client->fetch_assoc())

    {

?>

            Am încasat de la <?php echo $row["nume"]; ?> <br>

            Nr ORC/an: <?php echo $row["reg_com"]; ?> <br>

            CIF: <?php echo $row["cif"]; ?><br>

            Adresa: <?php echo $row["adresa"]; ?> <br>

<?php

    }

?> 

如您所见,php 代码被 html“中断”,然后通过关闭 while 循环的大括号继续。


问题是我需要重复 php 代码而不是 html。下次 php 运行时,内部的 html 代码将有所不同(在 html 内部,有 echo 函数可以从循环结果中检索不同的数据)。


我尝试将第一段代码放入一个函数中并运行该函数,但它只会破坏页面的布局并且不会显示代码应呈现的部分。


我的问题是:如何重用第一段不完整的代码?谢谢你!


收到一只叮咚
浏览 119回答 1
1回答

潇湘沐

不是启动和停止 PHP 代码,而是简单地回显要包含的 HTML 代码吗?它可能会帮助您组织您想要在循环中重复的内容以及您不想重复的内容。这是一个例子:$myArray = [];&nbsp; &nbsp;//array that will hold the values you get from database for later use$sql_client = "SELECT * FROM clienti WHERE nume = ? LIMIT 1";$stmt_client = $conn->prepare($sql_client);&nbsp;$stmt_client->bind_param("s", $nume);$stmt_client->execute();$result_client = $stmt_client->get_result();echo '<table>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //does not repeatwhile($row = $result_client->fetch_row())&nbsp; &nbsp; {&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; array_push($myArray, $row);&nbsp; &nbsp; &nbsp; &nbsp; //add each row to an array outside the scope of your loop&nbsp; &nbsp; &nbsp; &nbsp; echo '<tr>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//repeats once for each table row&nbsp; &nbsp; &nbsp; &nbsp; foreach($row as $columnValue){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<td><p>'.$columnValue.'</p></td>';&nbsp; //repeats for every value in table&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; echo '</tr>';&nbsp; &nbsp; }echo '</table>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //does not repeatecho $myArray[0][0];&nbsp; &nbsp;//echo first value of first row&nbsp;
随时随地看视频慕课网APP
我要回答