dongniaoli1822 2014-08-24 23:50
浏览 58
已采纳

无法在网站上正确显示数组

What I'm trying to do is make a search function for a list of a lot of products. The way the products work is that each product has a product_parent and a product_index the product_parent saves information about the product itself, and product_index saves the "sku" for the products , and when calling for all the procuts I use this code:

<script>
$(function() {
    var availableTags = ['DISCOUNT;;Discount',
        <?php   
                $get_con2 = mysql_query("SELECT * FROM `product_parents`");
                    $conarr2 = array();
                    while($con2 = mysql_fetch_array($get_con2)){
                        array_push($conarr2,$con2['id']);
                    }
                $get_con = mysql_query("SELECT * FROM `product_childs` WHERE `parent_id` IN (".implode(',',$conarr2).")");
                    $conarr = array();
                    while($con = mysql_fetch_array($get_con)){
                        array_push($conarr,$con['index_id']);
                    }
                    $showall = "WHERE `id` IN (".implode(",",$conarr).")";              

                $get_prods = mysql_query("SELECT * FROM `product_index`".$showall);
                $get_brand = mysql_query("SELECT product_parents.supplier, suppliers.id, suppliers.name FROM product_parents WHERE LEFT JOIN suppliers ON suppliers.id=product_parents.supplier");
                $get_prodname = mysql_query("SELECT product_parents.name FROM product_parents");
                $js = "";
                while($prods = mysql_fetch_array($get_prods)){
                    $prodbrand = mysql_fetch_array($get_brand);
                    $prodname = mysql_fetch_array($get_prodname);
                    /* $js .= "'". addslashes($prodbrand['name'])."". addslashes($prodname['name'])." (".indexStock($prods['id'])." on stock);;".addslashes($prods['sku'])."',"; */
                    /* $js .= "'".addslashes($prods['sku']).";; ". addslashes($prods['name'])." (".indexStock($prods['id'])." on stock)',"; */

                    $js .= "'".addslashes($prodbrand['name'])."',";
                    /* $js .= "'".addslashes($prodbrand['name'])." ".addslashes($prodname['name']).";; ". addslashes($prods['name'])." (".indexStock($prods['id'])." on stock)',"; */
                } echo substr($js,0,-1);
        ?>
    ];
    $( "#add_row_sku" ).autocomplete({
        source: availableTags
    });

});
</script>

and it's showing a bunch of results, but not all of them, however when I run the query in phpmyadmin it results every result I am trying to have it show. also, when I use:

/* $js .= "'".addslashes($prods['sku']).";; ". addslashes($prods['name'])." (".indexStock($prods['id'])." on stock)',"; */

it shows things just fine, can anyone see what I'm doing wrong here? I've been stuck for hours. also if you are willing to help but don't quite understand what I'm asking, I'll be happy to add you on skype or whatever so you can further assist me and that would be greatly appreciated!

  • 写回答

1条回答 默认 最新

  • doujianqin5172 2014-08-25 00:47
    关注

    Try this out. You can make your life easier by using json_encode to create the javascript. Build out the array in PHP and it handles any addslashes or encoding. I had to rename some of the variables as it made it hard to read. $rs is result set, $row is well table row. I prefer the $array[] syntax vs .push

    <?php
      $availableTags = array('DISCOUNT;;Discount');
    
      // Get product parents and their supplier
      $sql = "SELECT product_parents.id, product_parents.name as parent_name, suppliers.name as supplier_name FROM `product_parents` LEFT JOIN suppliers ON suppliers.id=product_parents.supplier";
      $rs = mysql_query($sql);
      $parents = array();
      while($row = mysql_fetch_array($rs)){
        $parents[$row['id']] = $row;
      }
    
      // Get all product childs whatever that means
      $sql = "SELECT index_id, parent_id FROM `product_childs` WHERE `parent_id` IN (" . implode(',', array_keys($parents)). ")";
      $rs = mysql_query($sql);
      $childs = array();
      while($row = mysql_fetch_array($rs)) {
        $childs[$row['index_id']] = $row['parent_id'];
      }
    
      // Get all the products
      $sql = "SELECT * FROM `product_index` WHERE `id` IN (" . implode(",", array_keys($childs)) .")";
      $rs = mysql_query($sql);
    
      while($prods = mysql_fetch_array($rs)) {
        $availableTags[] = $prods['name'];
        $availableTags[] = $prods['sku'];
        $parentId = $childs[$prods['id']];
        $brand = $parents[$parentId]['supplier_name'];
        $parentName = $parents[$parentId]['parent_name'];
        $availableTags[] = $brand;
      }
    
    ?>
    <script>
      $(function() {
        var availableTags = <?php echo json_encode($availableTags); ?>
        $( "#add_row_sku" ).autocomplete({
          source: availableTags
        });
    
      });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大