简体   繁体   English

将多个结果放在单个数组中

[英]place multiple result in a single array

I have written the code in sql as i just want to get the understanding of the concept. 我已经用sql编写了代码,因为我只是想了解这个概念。 when done i will change it to sqli/pdo. 完成后,我将其更改为sqli / pdo。 so kindly ignore the sql method and plz help me in solving the issue 因此请忽略sql方法,请plz帮助我解决问题

i need a listing of all the vendorname on the basis of a specific catid. 我需要根据特定catid列出所有供应商名称。 for this i have 2 tables subcategory and VendorSubCat. 为此,我有2个表子类别和VendorSubCat。 i am tryng to link the two tables by first fetching the subcatid(ie 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. 我试图链接两个表,方法是首先基于catid提取subcatid(即,来自子类别表的id),然后根据从上一个表中选择的subcatid(来自VendorSubCat表)显示供应商名称。

Table view subcategory 表格视图子类别

id  subcatname  subcatdesc  catname catid

Table view VendorSubCat 表格视图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, ie 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM