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

时间:2014-08-24 23:50:34

标签: php mysql

我尝试做的是为许多产品列表制作搜索功能。 产品的工作方式是每个产品都有product_parent和product_index product_parent保存有关产品本身的信息,product_index保存" sku"对于产品,当调用所有procuts时,我使用此代码:

<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>

并且它显示了一堆结果,但不是全部,但是当我在phpmyadmin中运行查询时,它会产生我试图让它显示的每个结果。 当我使用时:

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

它表明事情很好,任何人都可以看到我在这里做错了什么?我被困了几个小时。如果你愿意提供帮助但又不太了解我的要求,我很乐意将你加入skype或其他什么,以便你可以进一步帮助我,我将不胜感激!

1 个答案:

答案 0 :(得分:0)

试一试。您可以使用json_encode创建javascript,让您的生活更轻松。用PHP构建数组,它处理任何addslashes或编码。我不得不重命名一些变量,因为它很难阅读。 $ rs是结果集,$ row是表行。我更喜欢$ array []语法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>