Jump to content

Displaying multiple row stored data in a single <td>


surveen

Recommended Posts

I have stored supplier information in a table and same form user will select multiple products for that supplier and that will be stored in a seperate table against that supplier id. Now when i would like to display all supplier information with muliple products in a single line. Now all the information will be displayed as many time as the products are. How to display all the products in a same line. 

 

Now my display is like this

 

if(isset($_POST['submit']) && ($_POST['vendor']!='') && ($_POST['item']!=''))
{
$sql="SELECT supplier.id AS sid, supplier.name AS SNAME, supplier.category, supplier.website, supplier.email, supplier.phone, supplier.vat, supplier.pan, supplier_location.id, supplier_location.supplier_id, supplier_location.location, supplier_products.id, supplier_products.supplier_id, supplier_products.product_id, location.loc_id, location.name AS locname, products.product_id, products.name AS pname FROM supplier INNER JOIN supplier_location ON supplier.id = supplier_location.supplier_id INNER JOIN supplier_products ON supplier.id=supplier_products.supplier_id INNER JOIN location ON supplier_location.location = location.loc_id INNER JOIN products ON supplier_products.product_id=products.product_id WHERE supplier.id=".$sup." AND supplier_products.product_id=".$product;
$sql1 = mysql_query($sql) or die(mysql_error());


<table>
<thead><tr>
<th>Vendor ID</th>
<th>Vendor</th>
<th>Category</th>
<th>Website</th>

<th>Email</th> 

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


</tr>


</thead>
<tbody> 


<tr>
<?php
while($row = mysql_fetch_array($sql1))
{ 


?>
   
   
    <td><?php echo $row['sid'] ?></td>
<td><?php echo $row['SNAME'] ?></td>
    <td><?php echo $row['category'] ?></td>
    <td><?php echo $row['website'] ?></td>
     <td><?php echo $row['email'] ?></td>
    <td><?php echo $row['phone'] ?></td>
    <td><?php echo $row['iname']; ?></td>


    <td><?php echo $row['locname']; ?></td>


    <td><?php echo $row['vat'] ?></td>
    <td><?php echo $row['pan'] ?></td>




</tr>


<?php 




}
?>
</tbody></table>


}

 

Link to comment
Share on other sites

Not sure if I understand your intent.  Do you wish to display a table similar to what you show?  You obviously need to close your PHP tag before displaying the table header, or echo the table header.  I also think you should clean up how you echo each row (I typically echo both the array elements as well as the HTML tags, and use double quotes so that I don't need to concatenate each).

Link to comment
Share on other sites

I am attaching the screenshot of my output, As there is multiple locations and multiple products for that supplier, it displays all the information as many a times. instead of that i want to display only one time with all the products, as comma seperated

 

post-173769-0-13516400-1418993992_thumb.jpg

Edited by surveen
Link to comment
Share on other sites

Untested, but should be close.

<?php

if(isset($_POST['submit']) && ($_POST['vendor']!='') && ($_POST['item']!='')) {
    $sql="SELECT supplier.id AS sid, supplier.name AS SNAME, supplier.category, supplier.website, supplier.email, supplier.phone, supplier.vat, supplier.pan, supplier_location.id, supplier_location.supplier_id, supplier_location.location, supplier_products.id, supplier_products.supplier_id, supplier_products.product_id, location.loc_id, location.name AS locname, products.product_id, products.name AS pname FROM supplier INNER JOIN supplier_location ON supplier.id = supplier_location.supplier_id INNER JOIN supplier_products ON supplier.id=supplier_products.supplier_id INNER JOIN location ON supplier_location.location = location.loc_id INNER JOIN products ON supplier_products.product_id=products.product_id WHERE supplier.id=".$sup." AND supplier_products.product_id=".$product;
    $sql1 = mysql_query($sql) or die(mysql_error());
    echo('<table>
        <thead>
        <tr>
        <th>Vendor ID</th>
        <th>Vendor</th>
        <th>Category</th>
        <th>Website</th>
        <th>Email</th>
        <th>Phone</th>
        <th>Products</th>
        <th>Locations</th>
        <th>VAT</th>
        <th>PAN</th>
        </tr>
        </thead>
    <tbody> ');
    while($row = mysql_fetch_array($sql1)) {
        echo("<tr>
            <td>{$row['sid']}</td>
            <td>{$row['SNAME']}</td>
            <td>{$row['category']}</td>
            <td>{$row['website']}</td>
            <td>{$row['email']}</td>
            <td>{$row['phone']}</td>
            <td>{$row['iname']}</td>
            <td>{$row['locname']}</td>
            <td>{$row['vat']}</td>
            <td>{$row['pan']}</td>
            </tr>");
    }
    echo('</tbody></table>');
}

else{echo('Nothing');}
Link to comment
Share on other sites

If you don't want to repeat the supplier details then keep track of when the supplier changes and only output the details on that change. I have modified NotionCommotion's code to demonstrate. I also removed the superfluous columns that you were selecting but not using.

if(isset($_POST['submit']) && ($_POST['vendor']!='') && ($_POST['item']!='')) {
    $sql="SELECT supplier.id AS sid
    , supplier.name AS SNAME
    , supplier.category
    , supplier.website
    , supplier.email
    , supplier.phone
    , supplier.vat
    , supplier.pan
    , location.name AS locname
    , products.name AS pname 
    FROM supplier 
        INNER JOIN supplier_location ON supplier.id = supplier_location.supplier_id 
        INNER JOIN supplier_products ON supplier.id=supplier_products.supplier_id 
        INNER JOIN location ON supplier_location.location = location.loc_id 
        INNER JOIN products ON supplier_products.product_id=products.product_id 
    WHERE supplier.id='$sup' 
        AND supplier_products.product_id= '$product'
    ORDER BY sid";
    $sql1 = mysql_query($sql) or die(mysql_error());
    echo('<table>
        <thead>
        <tr>
        <th>Vendor ID</th>
        <th>Vendor</th>
        <th>Category</th>
        <th>Website</th>
        <th>Email</th>
        <th>Phone</th>
        <th>Products</th>
        <th>Locations</th>
        <th>VAT</th>
        <th>PAN</th>
        </tr>
        </thead>
    <tbody> ');
    $current = '';            // STORE THE SUPPLIER SID
    while($row = mysql_fetch_array($sql1)) {
        if ($row['sid'] != $current) {
            echo "<tr>
                <td>{$row['sid']}</td>
                <td>{$row['SNAME']}</td>
                <td>{$row['category']}</td>
                <td>{$row['website']}</td>
                <td>{$row['email']}</td>
                <td>{$row['phone']}</td>";
            $current = $row['sid'];   // RESET  STORED SID
        }
        else {
            echo "<tr><td colspan='6'> </td>";
        }
        
        echo "<td>{$row['pname']}</td>
            <td>{$row['locname']}</td>
            <td>{$row['vat']}</td>
            <td>{$row['pan']}</td>
            </tr>";
    }
    echo('</tbody></table>');
}

else{echo 'Nothing';}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.