Php 如何获取动态无线阵列值
我就是搞不懂这个。 在PHP中,我创建了动态单选按钮,它基于从关联数组收集的表单数据进行填充。例如,关联的数组和值为: 数组列表代码:Php 如何获取动态无线阵列值,php,arrays,Php,Arrays,我就是搞不懂这个。 在PHP中,我创建了动态单选按钮,它基于从关联数组收集的表单数据进行填充。例如,关联的数组和值为: 数组列表代码: foreach($_SESSION['items'] as $key=>$value) { echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />'; } foreach($\u会话['items']作为$key=>$value) { echo“
foreach($_SESSION['items'] as $key=>$value)
{
echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';
}
foreach($\u会话['items']作为$key=>$value)
{
echo“会话[..”“$key.”的值为“.”“$value.”“
”;
}
阵列回波结果:
$\u会话['0']的值为'Ralph'
$\u会话['1']的值为'Cindy'
$_会话['2']的值为'Katie'
接下来,我使用数组值创建一个单选按钮数组,以请求用户进行比较,如下所示(使用双循环效果很好):
这将生成以下允许选择的表单代码(html):
<form name="ranking_submit" action="" method="post">
<br><table><tbody><tr><td class="rright ratecell"><div class="big ccenter"><input id="41" type="radio" required="required" name="1" value="Ralph"><label for="41" title="Ralph">Ralph</label></div></td>
<td class="ccenter"> vs </td>
<td class="ratecell"><div class="big ccenter"><input id="81" type="radio" name="1" value="Cindy"><label for="81" title="Cindy">Cindy</label></div></td>
</tr><tr><td class="rright ratecell"><div class="big ccenter"><input id="42" type="radio" required="required" name="2" value="Ralph"><label for="42" title="Ralph">Ralph</label></div></td>
<td class="ccenter"> vs </td>
<td class="ratecell"><div class="big ccenter"><input id="82" type="radio" name="2" value="Katie"><label for="82" title="Katie">Katie</label></div></td>
</tr><tr><td class="rright ratecell"><div class="big ccenter"><input id="43" type="radio" required="required" name="3" value="Cindy"><label for="43" title="Cindy">Cindy</label></div></td>
<td class="ccenter"> vs </td>
<td class="ratecell"><div class="big ccenter"><input id="83" type="radio" name="3" value="Katie"><label for="83" title="Katie">Katie</label></div></td>
</tr></tbody></table><input name="ranking_submit" type="submit" value="Rank Them">
</form>
拉尔夫
vs
辛蒂
拉尔夫
vs
凯蒂
辛蒂
vs
凯蒂
我需要从无线电选择中获取值,最好使用键和值。稍后,我想计算每个$_会话['items']的选择次数-换句话说,找出每个项目被选择的次数
我已尝试使用此代码获取初始无线电选择输出,但它似乎不起作用:
<?php
if (isset($_POST["ranking_submit"]) && !empty($_POST["ranking_submit"])) {
foreach($_POST['radiocountb'] as $value){
echo "My values here"."</b>";
echo "<span>selected :<b> ".$value."</b></span>";
}
}
?>
如果我只是回显最后一个$radiocountb,那么我会得到正确的最新计数:
echo $radiocountb ."<br>";
echo$radiocountb.“
”;
生成三个收音机选项的最新计数:3
请帮助-我如何获得$_会话['items']的列表,并根据每个会话通过无线电选项选择的次数进行适当计数
非常感谢您的帮助和考虑。为了大家的利益,我使用WOUNDEDStevenJones的建议发布了问题的答案: 我改变了:
name='". $radiocountb ."'
致:
详情如下:
<form name="ranking_submit" action="" method="post">
<?php
echo "<br>";
// compare each array item with each other - one time
$radiocounta = 40;
$radiocountb = 0;
$radiocountc = 80;
$sizeZ=count($_SESSION['items']);
echo "<table>";
for ($i = 0; $i < $sizeZ-1; $i++)
{
$radiocountb += 1;
for ($j = $i+1; $j < $sizeZ; $j++)
{
$radiocounta += 1;
$radiocountc += 1;
$radiocountb += 1;
echo "<tr><td class='rright ratecell'><div class='big ccenter'>"."<input id='".$radiocounta . "' type='radio' required='required'
name='radiocountb[". $radiocountb ."]' value= '".$_SESSION['items'][$i]."' />"."<label for='".$radiocounta. "' title='".$_SESSION['items'][$i]."'>".$_SESSION['items'][$i]."</label></div></td>
<td class='ccenter'>"." vs "."</td>
<td class='ratecell'><div class='big ccenter'>"."<input id='".$radiocountc . "' type='radio' name='radiocountb[". $radiocountb ."]' value= '".$_SESSION['items'][$j]."' />"."<label for='".$radiocountc. "' title='".$_SESSION['items'][$j]."'>".$_SESSION['items'][$j]."</label></div></td>
</tr>";
}
}
echo "</table>";
?>
<input name="ranking_submit" type="submit" value="Rank Them" />
</form>
这改变了无线阵列的命名方法
然后,为了获得无线电值,我使用了以下方法:
<?php
$selections= array();
if (isset($_POST['radiocountb'])) {
foreach($_POST['radiocountb'] as $key=>$value){
echo "<span>selected :<b> ".$key." ".$value."</b></span>"."<br />";
$selections[] = $value;
}
}
?>
这就成功了。
感谢StevenJones为您提供的帮助性意见,这导致了解决方案。我在您的表格上没有看到任何名为
radiocountb
的内容。你看到了吗?为了回应@u_mulder的评论,你在表单上做了name='“$radiocountb.”
,但是$radiocountb
是一个数字,而不是文本。所以本质上你是在做name='4'
而不是name='radiocountb'
。不确定这是出于什么目的,但似乎有点可疑。因此,$\u POST['radiocountb']
将为空,而不是您期望的值,因为没有名为“radiocountb”的输入。您还可以执行类似于name=“radiocountb[41]”value=“Ralph
或name=“radiocountb[81]”value=“Cindy”
的操作,然后在PHP中您可以获得$\u POST['radiocountb']
它将返回一个类似于数组的数组(41=>'Ralph',81=>'Cindy')
$radiocountb变量是一个计数器,因为单选按钮是由上一个数组动态创建的。我需要每个单选对有一个唯一的名称,所以我认为$radiocountb可以工作。因此,在上面,我将尝试使用name='”单选,而不是使用$radiocountb$radiocountb。“”-那会变成字符串吗?谢谢你的帮助。我会尝试WOUNDEDStevenJones的评论-我会尝试回到小组-这对我来说很有意义,但需要时间来尝试。非常感谢你的帮助,我会让你知道进展的。。
<form name="ranking_submit" action="" method="post">
<?php
echo "<br>";
// compare each array item with each other - one time
$radiocounta = 40;
$radiocountb = 0;
$radiocountc = 80;
$sizeZ=count($_SESSION['items']);
echo "<table>";
for ($i = 0; $i < $sizeZ-1; $i++)
{
$radiocountb += 1;
for ($j = $i+1; $j < $sizeZ; $j++)
{
$radiocounta += 1;
$radiocountc += 1;
$radiocountb += 1;
echo "<tr><td class='rright ratecell'><div class='big ccenter'>"."<input id='".$radiocounta . "' type='radio' required='required'
name='radiocountb[". $radiocountb ."]' value= '".$_SESSION['items'][$i]."' />"."<label for='".$radiocounta. "' title='".$_SESSION['items'][$i]."'>".$_SESSION['items'][$i]."</label></div></td>
<td class='ccenter'>"." vs "."</td>
<td class='ratecell'><div class='big ccenter'>"."<input id='".$radiocountc . "' type='radio' name='radiocountb[". $radiocountb ."]' value= '".$_SESSION['items'][$j]."' />"."<label for='".$radiocountc. "' title='".$_SESSION['items'][$j]."'>".$_SESSION['items'][$j]."</label></div></td>
</tr>";
}
}
echo "</table>";
?>
<input name="ranking_submit" type="submit" value="Rank Them" />
</form>
<?php
$selections= array();
if (isset($_POST['radiocountb'])) {
foreach($_POST['radiocountb'] as $key=>$value){
echo "<span>selected :<b> ".$key." ".$value."</b></span>"."<br />";
$selections[] = $value;
}
}
?>