放置多个结果中的单个阵列 [英] place multiple result in a single array

查看:139
本文介绍了放置多个结果中的单个阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写在SQL中的code,因为我只想得到这一概念的理解。完成后,我将它改为SQLI / PDO。所以恳请忽略SQL方法和plz帮助我解决这个问题。

我需要一个特定的CATID的基础上同所有的VENDORNAME的列表。为了这个,我有2表子类别和VendorSubCat。我正在tryng通过根据从previous表中选择的subcatid(从VendorSubCat表)的CATID的基础上,先取出subcatid(即从子类表中的id),然后显示供应商名称的两个表联系起来。

表视图子类

  ID subcatname subcatdesc catname CATID

表视图VendorSubCat

  VENDORNAME厂商ID subcatid

code

 < PHP
ob_start();
require_once('config.php文件');
$ CATID = $ _REQUEST ['CATID'];
$ SQL1 =SELECT * FROM子类,其中CATID ='。 $ CATID。 ';
$ RESULT1 =的mysql_query($ SQL1);
而($行= mysql_fetch_array($ RESULT1)){
    $身份识别码= $行['身份证'];
    如果(SQL1!='0'){
        $ productt =的mysql_query(SELECT * FROM VendorSubCat其中id =$身份识别码。');
        $帖=阵列();
        如果(mysql_num_rows($ productt)){
            而($ =后mysql_fetch_assoc($ productt)){
                $信息[] = $职务;
            }
            标题(内容类型:应用程序/ JSON');
            回声的stripslashes(json_en code(阵列('名单'=> $帖)));
        }其他{
            标题(内容类型:应用程序/ JSON');
            回声的stripslashes(json_en code(阵列('名单'=>'无产品列表')));
        }
    }
}
?>

虽然code正常工作,但显示结果不同的数组中为止。它显示了这样的结果。

<$p$p><$c$c>{\"list\":[{\"id\":\"1\",\"vendorname\":\"Marzoogah\",\"vendorid\":\"1\",\"subcatid\":\"4\"}]}{\"list\":[{\"id\":\"2\",\"vendorname\":\"Zee区,厂商ID:2,subcatid:4}]} {清单:[{ID:3,VENDORNAME:Zee的区,厂商ID: 2,subcatid:7}]} {清单:[{ID:4,VENDORNAME:????? ????????,厂商ID:3,subcatid:4}]}

我希望把所有的结果在一个单一的阵列,即所有的上市应该在一个列表。不应该显示的名单每次一个新行被显示。任何帮助将是AP preciated


解决方案

您不必立即呼应的结果:

 回声的stripslashes(json_en code(阵列('名单'=&GT; $帖)));

相反,收集所有一个数组:

  $结果=阵列();
//你的code
$结果[] =阵列('名单'=&GT; $员额);
// ...
$结果[] =阵列('名单'=&GT;'没有产品名单');
// ...
//和回声到底只是一个时间:
回声的stripslashes(json_en code($结果);

或像这样的合并:

  $结果=阵列();
//你的code
$结果= $结果+ $职位;
// ...
$结果='没有产品名单;
// ...
//和回声到底只是一个时间:
回声的stripslashes(json_en code(阵列('名单'=&GT; $结果)));

此外,还可以在没有递归查询执行数据库请求;

是这样的:

  SELECT VSC。* FROM VendorSubCat VSC
INNER JOIN子类别钪在vsc.id = sc.id
WHERE sc.cat_id = 15

I have written the code in sql as i just want to get the understanding of the concept. when done i will change it to sqli/pdo. so kindly ignore the sql method and plz help me in solving the issue

i need a listing of all the vendorname on the basis of a specific catid. for this i have 2 tables subcategory and VendorSubCat. i am tryng to link the two tables by first fetching the subcatid(i.e id from subcategory table) on the basis of catid and then displaying the vendor name according to the subcatid(from VendorSubCat table) selected from previous table.

Table view subcategory

id  subcatname  subcatdesc  catname catid

Table view VendorSubCat

vendorname  vendorid  subcatid  

Code

<?php
ob_start();
require_once('config.php');
$catid = $_REQUEST['catid'];
$sql1 = "SELECT * FROM subcategory where catid='" . $catid . "'";
$result1 = mysql_query($sql1);
while ($row = mysql_fetch_array($result1)) {
    $myid = $row['id'];
    if (sql1 != '0') {
        $productt = mysql_query("select * from VendorSubCat where id = '" . $myid . "' ");
        $posts = array();
        if (mysql_num_rows($productt)) {
            while ($post = mysql_fetch_assoc($productt)) {
                $posts[] = $post;
            }
            header('Content-type: application/json');
            echo stripslashes(json_encode(array('list' => $posts)));
        } else {
            header('Content-type: application/json');
            echo stripslashes(json_encode(array('list' => 'No productlist')));
        }
    }
}
?>

Although the code works fine but displays the result in different array. it shows the result like this

{"list":[{"id":"1","vendorname":"Marzoogah","vendorid":"1","subcatid":"4"}]}{"list":[{"id":"2","vendorname":"Zee Zone","vendorid":"2","subcatid":"4"}]}{"list":[{"id":"3","vendorname":"Zee Zone","vendorid":"2","subcatid":"7"}]}{"list":[{"id":"4","vendorname":"????? ????????","vendorid":"3","subcatid":"4"}]}

I wish to put all the result in a single array, i.e all the listing should come under one list. "list" should not get displayed everytime a new row gets displayed. Any help would be appreciated

解决方案

You need not to echo results immediately:

echo stripslashes(json_encode(array('list' => $posts)));

Instead, collect all to one array:

$results = array();
//Your code
$results[] = array('list' => $posts);
//...
$results[] = array('list' => 'No product list');
//...
//And echo just one time in the end:
echo stripslashes(json_encode($results);

or something like this for merge:

$results = array();
//Your code
$results = $results + $posts;
//...
$results = 'No product list';
//...
//And echo just one time in the end:
echo stripslashes(json_encode(array('list' => $results)));

Also, You can to perform your database request without recursive queries;

Something like:

SELECT vsc.* FROM VendorSubCat vsc
INNER JOIN subcategory sc ON vsc.id=sc.id
WHERE sc.cat_id = 15

这篇关于放置多个结果中的单个阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆