简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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