我正在寻找是否有办法根据我在PHP数组中的元素数量添加td/tr。
更具体地说:我有 4 个数组,由文本文档(PHP-脚本(中的数字数量填充。这很好用。然后我用 HTML 编码我的布局,其中包含一个表格,其中 4 个数组中的一个应该显示一个 TD 中的所有结果,而在表格的另一半是更多行,其余数组数据可以使用它。
我想要一个表单来首先显示数组中的所有数字。 ff u klick 在一个数字上 其余数据应显示在另一半,以便您可以对其进行编辑。
我只是不知道该怎么做。我可能知道如何在 C# 中做到这一点,但由于我在 HTML/PHP 方面不太有经验,所以我只是卡住了。
这是我目前的桌子:
<table style="padding-top:10px" align=center>
<tr>
<?php include ("durchwahl.php"); ?>
<td rowspan = "6"></td> </tr>
<tr>
<td>
<table>
<tr> <td> Durchwahl </td> <td> <input type="text" id="nv_durchwahl" name="nv_durchwahl"> </td> </tr>
<tr> <td> Anzeigename </td> <td> <input type="text" id="nv_anzeigename" name="nv_anzeigename"> </td> </tr>
<tr> <td> Angezeigte Durchwahl </td> <td> <input type="text" id="nv_anzeigenummer" name="nv_anzeigenummer"> </td> </tr>
<tr> <td> Passwort </td> <td> <input type="text" id="nv_passwort" name="nv_passwort" > </td> </tr>
<tr><td><br/></td><td><br/></td></tr>
<tr>
<td><input type="submit" id="nv_speichern" name="nv_speichern" value="Speichern"></td>
<td><input type="submit" id="nv_delete" name="nv_delete" value="Löschen"> </td>
<td> <input type="submit" id="nv_add" name="nv_add" value="Hinzufügen"> </td></tr>
</table>
</td>
</tr>
</table>
这是我从数据库中获取数组的代码(该文件称为 sip_tabelle.php(:
$dw = [];
$result =mysql_query("SELECT durchwahl FROM sip");
while($row = mysql_fetch_array($result))
{
$dw[] = $row;
}
有了这个,我尝试填写我的TD(感谢Guilherme Ferreira(。
<table>
<?php include("./sip_table.php");
foreach($dw as $reihe):?>
<tr>
<?php foreach($reihe as $k=>$zelle):?>
<td><?php echo $zelle ?></td>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</table>
它现在发布数据库的每个条目,但发布两次。所以我有 2 个具有相同条目的 TD。
任何想法可能出错的地方?
这是基本的PHP,我看到有人刚刚投票给你。但我认为,如果你正在学习,那么"假"问题并没有错。所以如果你的数组是这样的
$a = array(
array('somevalue1','anothervalue1','evenother1'),
array('somevalue2','anothervalue2','evenother2'),
array('somevalue3','anothervalue3','evenother3')
);
然后它很容易
<table>
<?php foreach($myarray as $row):?>
<tr>
<td><?php echo $row['some_key_1'] ?></td>
<td><?php echo $row['some_key_2'] ?></td>
<td><?php echo $row['some_key_3'] ?></td>
</tr>
<?php endforeach ?>
</table>
或者,如果您想拥有自动TD
<table>
<?php foreach($myarray as $row):?>
<tr>
<?php foreach($row as $k=>$cell):?>
<td><?php echo $cell ?></td>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</table>
如果您发现可以在 html 中包含 php 循环,就像在 C# 上的 MVC6 模板中一样。你可以直接在 html 中使用任何 php 指令。
在我的示例中,有一个foreach为我提供了数组中的对象,就像C#一样