【问题标题】:Use different array variables in same HTML table to show data在同一个 HTML 表格中使用不同的数组变量来显示数据
【发布时间】:2020-03-06 08:09:45
【问题描述】:

我想在 HTML 表格中输出三个这样的数组。我从 7 到 8 天就陷入了这个问题。在这里,我正在编写这个带有 HTML 输出的静态代码,以便你们可以轻松地在这里得到我想要做的事情。当我尝试使用 for each 时,它开始循环重复值。 我的 PHP 代码是 -

<?php 

 $variable1 = array(
        array(
        'name' => 'Jack',
        'roll' => '123',
        'class' => '4th',
        ),  
        array(
        'name' => 'Hena',
        'roll' => '124',
        'class' => '4th',
        ),
        array(
        'name' => 'Jesse',
        'roll' => '125',
        'class' => '4th',
        ),
        array(
        'name' => 'Tyson',
        'roll' => '126',
        'class' => '4th',
        )       
        );
 $variable2 = array(
        array(
        'father_name' => 'John',
        'phone_number' => 'xxx-xxx-xxxx',
        ),  
        array(
        'father_name' => 'Patrick',
        'phone_number' => 'xxx-xxx-xxxx',
        ),
        array(
        'father_name' => 'Michale',
        'phone_number' => 'xxx-xxx-xxxx',
        ),
        array(
        'father_name' => 'Dave',
        'phone_number' => 'xxx-xxx-xxxx',
        )       
        );
   $variable3 = array(
        array(
        'marks' => '89',
        'sub' => 'English',
        ),  
        array(
        'marks' => '56',
        'sub' => 'Math',
        ),  
        array(
        'marks' => '79',
        'sub' => 'Moral Education',
        ),  
        array(
        'marks' => '88',
        'sub' => 'English',
        )   
        );          

?>

而 HTML 表格是 -

table, th, td {
      padding: 10px;
      border: 1px solid black; 
      border-collapse: collapse;
      }
<table>
	<thead>
		<th>Name</th>
		<th>Father Name</th>
		<th>Subject</th>
		<th>Marks</th>
	</thead>
		<tr>
			<td>Jack</td>
			<td>John</td>
			<td>English</td>
			<td>89</td>
		</tr>
		<tr>
			<td>Hena</td>
			<td>Patrick</td>
			<td>Math</td>
			<td>56</td>
		</tr>
		<tr>
			<td>Jesse</td>
			<td>Michale</td>
			<td>Moral Education</td>
			<td>79</td>
		</tr>
		<tr>
			<td>Tyson</td>
			<td>Dave</td>
			<td>English</td>
			<td>88</td>
		</tr>
	<tfoot>
		<th>Name</th>
		<th>Father Name</th>
		<th>Subject</th>
		<th>Marks</th>
	</tfoot>
</table>

【问题讨论】:

    标签: php arrays loops foreach


    【解决方案1】:

    在此代码示例中,我使用for loop 循环遍历第一个数组的每个index。在该 for 循环中,打印出具有实际索引的四个值。 (&lt;?= is equal to &lt;?php echo)。三元运算符 (?:) 就像一个 if else 简而言之语句。在打印数组的值之前,它会检查它们是否设置为防止通知未定义的索引。

    <table>
        <thead>
            <th>Name</th>
            <th>Father Name</th>
            <th>Subject</th>
            <th>Marks</th>
        </thead>
        <?php for( $index = 0; $index < count($variable1); $index++ ) { ?>
          <tr>
            <td><?= isset($variable1[$index]['name']) ? $variable1[$index]['name'] : ''; ?></td>
            <td><?= isset($variable2[$index]['father_name']) ? $variable2[$index]['father_name'] : ''; ?></td>
            <td><?= isset($variable3[$index]['sub']) ? $variable3[$index]['sub'] : ''; ?></td>
            <td><?= isset($variable3[$index]['marks']) ? $variable3[$index]['marks'] : ''; ?></td>
          </tr>
        <?php } ?>
        <tfoot>
            <th>Name</th>
            <th>Father Name</th>
            <th>Subject</th>
            <th>Marks</th>
        </tfoot>
    </table>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-15
      • 2015-06-25
      • 2020-07-12
      • 2020-05-09
      • 2020-11-30
      • 2017-01-06
      相关资源
      最近更新 更多