Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在表格中循环并回显每个循环的编号_Php - Fatal编程技术网

Php 如何在表格中循环并回显每个循环的编号

Php 如何在表格中循环并回显每个循环的编号,php,Php,我可以设法使用while循环将信息显示到表中,但如何为每个主要信息添加一个数字,使其更有条理?我是否需要在这里添加for循环,或者可以使用while循环来完成 我试图添加一个for循环,并用双引号将其括起来,但得到了一个字符串的解析错误 while($row = mysqli_fetch_assoc($result)) { for($x=1; $x<=$resultCheck; $x++) { echo "<tr><th>$x Clinic Nam

我可以设法使用while循环将信息显示到表中,但如何为每个主要信息添加一个数字,使其更有条理?我是否需要在这里添加for循环,或者可以使用while循环来完成

我试图添加一个for循环,并用双引号将其括起来,但得到了一个字符串的解析错误

while($row = mysqli_fetch_assoc($result)) {
   for($x=1; $x<=$resultCheck; $x++) {
      echo "<tr><th>$x Clinic Name:</th><td>$row['clinic_name']</td></tr>
            <tr><th>Clinic Address:</th><td>$row['clinic_address']</td></tr>
            <tr><th>Clinic Number:</th><td>$row['clinic_number']</td></tr>
            <tr><th>Which of the following are you?</th><td>$row['type_of_profession']</td></tr>
            <tr><th>Services:</th><td>$row['services']</td></tr>
            <tr><th>Recommended Doctors:</th><td>$row['doctors']</td></tr>";                  
   }
}
while($row=mysqli\u fetch\u assoc($result)){

对于@Qirel已经提到的($x=1;$x),您只需要添加一个计数器变量,为外部循环的每次迭代递增:

$rowNumber = 0;
while($row = mysqli_fetch_assoc($result)) {
  $rowNumber++;
  echo "<tr><th>$rowNumber  Clinic Name:</th><td>$row['clinic_name']</td></tr>
        <tr><th>Clinic Address:</th><td>$row['clinic_address']</td></tr>
        <tr><th>Clinic Number:</th><td>$row['clinic_number']</td></tr>
        <tr><th>Which of the following are you?</th><td>$row['type_of_profession']</td></tr>
        <tr><th>Services:</th><td>$row['services']</td></tr>
        <tr><th>Recommended Doctors:</th><td>$row['doctors']</td></tr>";                  
}
$rowNumber=0;
while($row=mysqli\u fetch\u assoc($result)){
$rowNumber++;
echo“$rowNumber诊所名称:$row['Clinic_Name']
诊所地址:$row['Clinic_Address']
诊所编号:$row['Clinic_Number']
您是以下哪一位?$row[“职业类型”]
服务:$row[“服务”]
推荐医生:$row[‘医生’”;
}

删除for循环,在while循环中完成所有操作。为每次迭代创建一个递增的变量。谢谢!欣赏它!为什么在while循环中执行此操作比在for循环中执行更好?要点是:您只需要一个循环,即原始的外部
while
循环,而不是两个循环,一个在另一个内部。对于单个循环,a
>for
循环没有多大意义,因为您必须投入精力首先获取行数,以获得循环的终止条件。使用
while
循环更常见于从sql结果集中获取行的任务,在这里工作很好。
$rowNumber = 0;
while($row = mysqli_fetch_assoc($result)) {
  $rowNumber++;
  echo "<tr><th>$rowNumber  Clinic Name:</th><td>$row['clinic_name']</td></tr>
        <tr><th>Clinic Address:</th><td>$row['clinic_address']</td></tr>
        <tr><th>Clinic Number:</th><td>$row['clinic_number']</td></tr>
        <tr><th>Which of the following are you?</th><td>$row['type_of_profession']</td></tr>
        <tr><th>Services:</th><td>$row['services']</td></tr>
        <tr><th>Recommended Doctors:</th><td>$row['doctors']</td></tr>";                  
}