格式化foreach的结果

时间:2012-08-31 07:12:57

标签: php jquery

字段信息:logi_jo [varchar],logi_cntrlnum [int],logi_remark [varchar],logi_poin [varchar]

var val = document.getElementById("logi_jo").value;
var val2 = document.getElementById("logi_cntrlnum").value;
var val3 = document.getElementById("logi_remark").value;
var val4 = document.getElementById("logi_poperson").value;
document.location.href = "ppc_logistic-mngt_home2.php?logi_jo=" + val + "&logi_cntrlnum=" + val2 + "&logi_remark=" + val3 + "&logi_poperson=" + val4;

点击提交后的网址

sample_link.php logi_jo = 123A&安培; logi_cntrlnum = 000002&安培; logi_remark =递送&安培; logi_poperson = chunkchunks

$logi_jo = $_REQUEST['logi_jo'];
$logi_cntrlnum = $_REQUEST[logi_cntrlnum];
$logi_remark = $_REQUEST['logi_remark'];
$logi_poin = $_REQUEST['logi_poperson'];

if(isset ($_GET)) {
  foreach ($_GET as $key => $value)

  if($key != '') {
    $sql .= "$key = '$value' && "; 
  }
}

$sql= substr_replace ($sql , '', strlen($sql) -3 , 2);
echo $sql;

OUTPUT:logi_jo ='1'&& logi_cntrlnum ='2'&& logi_remark ='3'&& logi_poperson ='4'

问题:如何修改输出...例如 logi_remark无法解答 ..准备输出变为: logi_jo ='123a'&& logi_cntrlnum ='000002'&& logi_remark =''&& logi_poperson ='chunkchunks'我该如何修改它: logi_jo ='123a'&& logi_cntrlnum ='000002'&& logi_poperson ='chunkchunks'(解释:如果变量等于零(0)或''(无应答)它将不会出现..)

并且如果有可能,如果字段是[int]它可能没有像这样的logi_cntrlnum ='000002'它应该像这样logi_cntrlnum = 000002谢谢你xD

1 个答案:

答案 0 :(得分:0)

你可以简单地使用if else语句

foreach ($_GET as $key => $value)
    if($key != '') {
        if($key=='logi_remark' && ($value=='' || $value==0))
            $value='chunkchunks';
        $sql .= "$key = '$value' && ";
    }

要添加'000002'不是int,但你可以将其设为2

if(is_numeric($value))
    $value=(int)$value;