user2823107
user2823107

Reputation: 99

display the data which contains multiple result

This is again an extended quetion of THIS.

I changed my table format, i mean i have created a separate table for vendor_locations where i store multiple locations against that vendor id in multiple rows. and also created a table vendor_products to store multiple products for that vendor.

Now my problem is, As both products and locations will be multiple for each vendor while displaying i am not getting how to display it in a single row.

if the products are 3 and location is 2 then, all the details will be displayed thrice. if i remove either location or product it will work properly. But how can i do this for both

 if(isset($_POST['submit']))
{
$sql="SELECT vendor.id AS venid
, vendor.name AS VNAME
, vendor.category
, vendor.website
, vendor.email
, vendor.phone
, vendor.vat
, vendor.pan
, items.name AS iname
, location.name AS locname 
, items.item_id
FROM vendor 
    INNER JOIN vendor_location ON vendor.id = vendor_location.vendor_id 
    INNER JOIN vendor_products ON vendor.id=vendor_products.vendor_id 
    INNER JOIN location ON vendor_location.location = location.loc_id 
    INNER JOIN items ON vendor_products.item_id=items.item_id 
    ORDER BY vendor.id";

}

$sql1 = mysql_query($sql) or die(mysql_error());
} 

?>
<div class="w-box w-box-blue">
                            <div class="w-box-header">
                                <h4>Vendor</h4>
                            </div>
                            <div class="w-box-content"> 

    <table id="dt_hScroll" class="table table-striped">
                        <thead><tr>
                        <th>Vendor ID</th>
                        <th>Vendor</th>
                        <th>Category</th>
                        <th>Website</th>
                        <th>Email</th>
                        <th>Phone</th>

                        <th>VAT</th>
                        <th>PAN</th>
                        <th>Products</th>
                        <th>Locations</th>




</tr>

</thead>
<tbody> 


<?php
$current = '';            // STORE THE SUPPLIER SID
    while($row = mysql_fetch_array($sql1)) {
        if ($row['venid'] != $current) {
            echo "<tr>
                <td><a href='edit_vendor_details.php?id=$row[venid]'>{$row['venid']}</a></td>
                <td>{$row['VNAME']}</td>
                <td>{$row['category']}</td>
                <td>{$row['website']}</td>
                <td>{$row['email']}</td>
                <td>{$row['phone']}</td>
                <td>{$row['vat']}</td>
            <td>{$row['pan']}</td>
                ";
            $current = $row['venid'];   // RESET  STORED SID
        }
        else {
            echo "<tr><td colspan='8'>&nbsp;</td>";
        }

        echo "<td>{$row['iname']}</td>";
        echo "<td>{$row['locname']}</td>";


         echo "</tr>";
    }
    echo('</tbody></table>');


?>
</tbody></table>

Upvotes: 1

Views: 43

Answers (1)

Suchit kumar
Suchit kumar

Reputation: 11859

use group_concat on the fields if you only wish to show. like this:

SELECT vendor.id AS venid --rest of selection
    , GROUP_CONCAT(location.name) AS locname 
    , GROUP_CONCAT(items.item_id)
FROM vendor --rest of your joins
GROUP BY vendor.id

see more about it here: MySQL Aggregate Functions

Upvotes: 3

Related Questions