weixin_33698823 2016-10-03 18:14 采纳率: 0%
浏览 68

在PHP中读取JSON数组

I have a this variable:

var order = $('#listadoImages').sortable('serialize');

When send the order var from JSON to PHP, shows "id[]=18&id[]=19", my problem is I can not read that in php. I tried with foreach but returns error.

My ajax code:

 $("#mainAction").click(function(){
         //alert("New position: " + ui.item.index());
            var order = $('#listadoImages').sortable('serialize');
            var rutas = [];
            $('#listadoImages li').each(function(index, li){
                var bg = $(this).find('span').css('background-image');
                bg = bg.replace('url(','').replace(')','').replace(/\"/gi, "");
                var res = bg.substring(42);

                if(index == 0){
                    rutas += res;
                }else{
                    rutas += ";" + res;
                }

            });


            //alert(rutas);
            $.ajax({
              type: "POST",     
              url: "functions/guardar.php",
              data: {
              orden : order,
              path : rutas
              },
              "success":function(data){

              }
            });
        });

My PHP code:

$paths =  $_POST['path'];
$orden = $_POST['orden'];
$rutas = explode(";", $paths);

if(isset($paths)){

    $sql = "SELECT * FROM imagenes";
    $respuesta = mysql_query($sql);

    if($respuesta){
    $i=0;
        while( $rs = mysql_fetch_assoc($respuesta) ){
            $rutaBD = $rs['ruta_imagen'];
          foreach ($orden as $k => $value) {
            foreach ($rutas as $key => $rut) {
                        for($i=0;$i<count($rut);$i++){
                            if($rutaBD == $rut){
                               //Here Update Statment
                            }
                        }
                }       
            }


         }      

    }

Thank you for readme. I hope someone can help me. Thank you! Regards.

  • 写回答

1条回答 默认 最新

  • weixin_33716154 2016-10-03 18:20
    关注

    You need to use parse_str to turn the string into an array. It will then create an array $id that you can iterate through.

    parse_str($_POST['orden']);
    var_dump($id);
    

    To iterate through the array, you can use foreach, or better yet, use implode to put it into an IN clause.

    $sqlUpdate = "UPDATE imagenes SET ruta_imagen='".$rut."', orden=".$i." WHERE id_imagen IN (".implode(',',$id).")";
    

    Please be aware that this sql query is open to SQL injection. I'd highly recommend moving to PDO or mysqli, and using prepared statements. You also need a comma between your ruta_imagen and order assignments.

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题