我想从不同的循环中获取一些id并在另一页上破坏theidès,但我每次只得到循环中的最后一个id。
这里是第一页的代码 - 我的表格:
while($rowSCA = mysql_fetch_array($ResCSCA)) {
$libSCA = $rowSCA['lib_service'];
$idSCA = $rowSCA['id_service'];
echo '<tr>';
echo "<td class='HSCA'>".$libSCA."</td><td class='HSCA'><input type='checkbox' class='HSCA' name=\"centre[".$idSCA."]\"/></td>";
echo '</tr>';
echo '<input type="hidden" name="SCA['.$idSCA.']" value="'.$idSCA.'"></input>';
}
...
while($rowSRiA = mysql_fetch_array($ResCSRiA)) {
$libSRiA = $rowSRiA['lib_service'];
$idSRiA = $rowSRiA['id_service'];
echo "<tr class='HSRiA'>";
echo "<td class='HSRiA'>".$libSRiA."</td><td class='HSRiA'><input type='checkbox' class='HSRiA' name=\"centre[".$idSRiA."]\"/></td>";
echo '</tr>';
echo '<input type="hidden" name="RiA['.$idSRiA.']" value="'.$idSRiA.'"></input>';
}
这里是我内幕第二页的代码:
// First implode operation
foreach($_POST['SCA'] as $Cservices)
{
@$CALLservices = array($Cservices);
}
$IMPCServices = implode(",", $CALLservices);
$CNewServices = $IMPCServices;
// Second implode operation
foreach($_POST['RiA'] as $Cservices2)
{
@$CALLservices2 = array($Cservices2);
}
$IMPCServices2 = implode(",", $CALLservices2);
$CNewServices2 = $IMPCServices2;
echo $CNewServices;
echo '<br>';
echo $CNewServices2;
我必须正常得到这个结果集:
94,100,101,104
93
但我只得到
104
93
但是我的var_dump仍然告诉我,我的所有id都正确无误:
array(4) {
[94]=>
string(2) "94"
[100]=>
string(3) "100"
[101]=>
string(3) "101"
[104]=>
string(3) "104"
}
array(1) {
[93]=>
string(2) "93"
}
那么为什么内爆不能正常工作?
有人有想法吗?
提前THX答案 0 :(得分:2)
当你这样做时:
foreach($_POST['SCA'] as $Cservices)
{
@$CALLservices = array($Cservices);
}
每次循环时,你都会用一个只包含循环当前元素的数组覆盖变量$CALLservices
。如果你想制作一个包含所有元素的数组,你应该这样做:
$CALLservices = array()
foreach($_POST['SCA'] as $Cservices)
{
$CALLservices[] = array($Cservices);
}
但是,根本不需要这个循环。你可以写:
$CALLservices = $_POST['SCA']
复制数组。为什么你甚至需要复制数组?你可以写:
$IMPCServices = implode(",", $_POST['SCA']);
答案 1 :(得分:1)
您正在以错误的方式添加到数组中尝试此
$CALLservices2 = array();
$i = 0;
foreach($_POST['SCA'] as $Cservices)
{
$CALLservices2[$i] = $Cservices;
$i++;
}
现在进行内爆
$IMPCServices2 = implode(",", $CALLservices2);
答案 2 :(得分:1)
在foreach
循环中,您没有获取数组中的所有值。
有不同的方法,但这里有一个基本方法:
foreach($_POST['SCA'] as $Cservices) {
@$CALLservices[] = $Cservices;
}
$IMPCServices = implode(",", $CALLservices);
享受....