私はphp配列からhtmlテーブルを作成するのに苦労していますが、配列に応じて異なる行に異なるCSSクラスを適用しています。私はこの配列からHTMLテーブルを作成したい、とのアレイは「特別な」=>「スタイル」が含まれている場合、それは設定しますPHPの配列から作成されたhtmlテーブルの異なる行に異なるスタイルを設定するにはどうすればいいですか?
<?php
$data = array(
array('Title 1'=>'text11', 'Title 2'=>'text12'),
array('Title 1'=>'text21', 'Title 2'=>'text22'),
array('Title 1'=>'text31', 'Title 2'=>'text32'),
array('Title 1'=>'text41', 'Title 2'=>'text42', 'special'=>'style1'),
array('Title 1'=>'text51', 'Title 2'=>'text52', 'special'=>'style2'),
);
?>
:
は私がdata.phpにこのコードを持っていますその特定の行のスタイル。これは、これまでの私のコードです:
<?php include('data.php'); ?>
<table>
<thead>
<tr>
<th>Title 1</th>
<th>Title 2</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $key=>$row):
if ($row == 'class1') {
$class='class="style1"';
} elseif ($row == 'class1') {
$class='class="style2"';
} else {
$class='';
}?>
<tr <?php echo $class ?>>
<td><?php echo implode('</td><td>', $row); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
そして、これは、所望の出力です:
<table>
<thead>
<tr>
<th>Title 1</th>
<th>Title 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>text11</td><td>text12</td>
</tr>
<tr>
<td>text21</td><td>text22</td>
</tr>
<tr>
<td>text31</td><td>text32</td>
</tr>
<tr class="style1">
<td>text41</td><td>text42</td>
</tr>
<tr class="style2">
<td>text51</td><td>text52</td>
</tr>
</tbody>
</table>
$データとして配列は多次元である、「クラス1」を持つ配列内の何もない.. –
'IF($行[「特別」] ==「クラス1」){ '' special 'の内容はあなたのコード例から逆になっているように見えますが、これはあなたの既存の配列ではうまくいくと思われます: 'if($ row [' special '] ==' style1 '){$ class =' ' class1 ';' –