【发布时间】:2019-11-13 07:03:46
【问题描述】:
我从我的服务器上下载了一系列业务服务,并希望将数据显示在一个表单中,以便可以对其进行编辑并将其发送到另一个页面。
我想为每条数据分配一个变量(即输入类型='text'name='service$c')并迭代$c的值,以便它每次都分配一个新值(例如.service1、service2 等),这样当数据发布到下一页时,我可以通过这些变量名检索它。
我发现如果我在 while 循环中放入类似“for ($i=1; $i
我确实找到了一个解决方案,但我想知道它是否有点老套……它有效,但我想知道我是否可以做得更好。
//Create the form
echo "<form action='changeservices2.php' method='post'>";
//Print Out My Array
while ($result_ar = mysqli_fetch_assoc($result)) {
//Shorten the variable
$a=$result_ar['service'];
//Start at 1
$b=1;
//Iterate
$c = $b+$d;
echo "<input type ='text' name='service$c' value='$a' ><br>";
//Since the first variable is null, I needed to help change it
if (is_null($c)) {$c=1;}
$d=$c;
}
echo "<input type ='submit' value='submit' ><br>";
echo "</form>";
它有效,我只是想知道我是否缺少更优雅/简单的解决方案
【问题讨论】:
标签: php loops variables while-loop