Foreach有两个查询PHP

时间:2016-09-05 13:37:56

标签: php foreach

我必须使用2个查询,第一个返回所有字段,第二个查询返回字段图像。

使用我的方法,它在我的数据表中不显示任何内容。如何在同一个数据库中返回两个查询的结果?

这是我的疑问:

  $sql = "

    SELECT 
    catalog.id as id,

    product.codes as codes,
    color,

    catalog.name as product_name,
    catalog.description,
    product.price as price,
    product.original_price as original_price,
    product.code as code



FROM 

    product_camp


    LEFT JOIN product_camp_has ON product_camp_has.fk_product_camp = product_camp.id_product_camp

    LEFT JOIN catalog ON product_camp_has.fk_catalog = catalog.id

    LEFT JOIN product ON product.fk_catalog =  catalog.fk_catalog


WHERE product.code LIKE '".$_GET["codegr"]."%'

GROUP BY 
    product.codes
 ";

    $data->exec ( "set names utf8" );
   $reqsql = $data-> prepare($sql);
   $reqsql -> execute();
   $fdebquery = $reqsql -> fetchAll(PDO::FETCH_ASSOC);

    $sql_image = "  
         SELECT 

        catalog_image.id_catalog,
        catalog_image.fk_id as id,
        catalog_image.image,
        catalog_image.mainn,
        catalog_image.position,

        product.code as code


    FROM catalog_image

    LEFT JOIN catalog on catalog_image.fk_catalog = catalog.id
    LEFT JOIN product on catalog.id_catalog_config = product.fk_catalog

    WHERE product.code LIKE '".$_GET["codegr"]."%'

    ORDER BY catalog_image.mainn DESC, catalog_image.position ASC

   ";

     $data->exec ( "set names utf8" );
     $reqsql = $data-> prepare($sql_image);
     $reqsql -> execute();
     $sdebquery = $reqsql -> fetchAll(PDO::FETCH_ASSOC);

代码PHP:

    <table class="table table-striped table-bordered table-hover dataTables-example dataTable dtr-inline">
        <?php

                      $Array = array(); 

                      $output = implode(', ', array_map(
                        function ($v, $k) {
                            if(is_array($v['id'])){
                                return implode('&'.$k.'[]=', $v);
                            }else{
                                return $v['id'];
                            }
                        }, 
                        $sdebquery, 
                        array_keys($sdebquery)
                    ));

                      $newids = explode(', ', $output);

                      foreach($fquery as $k => $fvalue)
                      {
                             if(in_array($fvalue['id'], $newids))
                             {
                              $fquery[$k]['image'] = $sdebquery[array_search($fvalue['id'], $newids)]['image'];
                                    $Array[] = $fquery[$k];      
                              }
                       }

        ?>
        <tbody>
            <?php   

              foreach($Array as $a) :

                ?>

感谢您的帮助。

0 个答案:

没有答案