#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Colombo, Western, Sri Lanka
    Posts
    41
    Rep Power
    14
    This script works fine. It searches for master categories then looks for any sub-categories in a different table and then for products in the sub-category in a different table and outputs the data.

    So what I get is something like a tree with Master categories, sub-categories and products. Everything's fine upto this point.

    I want to output this data in a table with 3 columns. Master Category, Sub-Category and Product.

    A new row should be created for every Master Category ONLY. Any subcategories in each master category should be displayed one after the other seperated by a <br> in the 2nd column. If a sub-category contains products these should be displayed in the 3rd column. If there's more than 1 product, they should be seperated with a <br>.

    So, if there are 5 Master Categories in the catalogue, the table should contain only 5 rows.

    I hope I've managed to explain this clearly.

    The code which simply diplays what I need without the table is given below:

    (connection to db)

    $master=mysql_query("select * from master_categories");

    $masternum=mysql_numrows($master);

    $i=0;

    if ($masternum>0){

    while ($i<$masternum){

    echo "<b>";

    echo mysql_result($master,$i,"name");

    echo "</b><p>";

    $j=0;

    $masterid=mysql_result($master,$i,"id");

    $subcat=mysql_query("select * from categories where parent_id='$masterid'");

    $subcatnum=mysql_numrows($subcat);

    if ($subcatnum>0){

    while ($j<$subcatnum){

    $subcatid=mysql_result($subcat,$j,"id");

    echo "***";

    echo "<b><i>";

    echo mysql_result($subcat,$j,"name");

    echo "</b></i><p>";

    $products=mysql_query("select * from products where subparent_id='$subcatid'");

    $productsrows=mysql_numrows($products);

    $k=0;

    if ($productsrows>0) {

    while ($k<$productsrows) {

    echo "<p>****";

    echo mysql_result($products,$k,"common_name");

    echo "<p>";

    $k++;}

    }else { ;}

    $j++;}

    }else {

    ;}

    $i++;}

    } else {

    ;}

    ?>

    <a href="../catalogue_administration.htm">Catalogue Administration</a>

    </body>

    </html>


    Any help with this will be appreciated. Thnx.


    The output should look like this. (The HTML of the desired output. Please view it in a browser)


    <html>

    <head>
    <title>New Page 1</title>
    </head>

    <body>

    <div align="center">
    <center>
    <table border="1" width="100%">
    <tr>
    <td width="33%">Master Category</td>
    <td width="33%">Sub-Category</td>
    <td width="34%">Product</td>
    </tr>
    <tr>
    <td width="33%" valign="top">Marine Fish</td>
    <td width="33%" valign="top">Sword Fish<br>
    Star Fish</td>
    <td width="34%" valign="top">&nbsp;</td>
    </tr>
    <tr>
    <td width="33%" valign="top">Fresh Water Fish</td>
    <td width="33%" valign="top">Mollies<br>
    Guppies
    <p><br>
    Gold Fish</td>
    <td width="34%" valign="top"><br>
    Bluetail Guppy<br>
    Blacktail Guppy<br>
    Black Cobra</td>
    </tr>
    <tr>
    <td width="33%" valign="top">Brackish Fish</td>
    <td width="33%" valign="top">&nbsp;</td>
    <td width="34%" valign="top">&nbsp;</td>
    </tr>
    <tr>
    <td width="33%" valign="top">Plants</td>
    <td width="33%" valign="top">&nbsp;</td>
    <td width="34%" valign="top">&nbsp;</td>
    </tr>
    </table>
    </center>
    </div>

    </body>

    </html>



    [This message has been edited by ranesh_w (edited September 25, 2000).]
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Location
    Alkmaar, The Netherlands
    Posts
    123
    Rep Power
    14
    I think you're nearly there: Although I would suggest one row per subcategory and use rowspan. It looks the same, but works easier.

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    <head><title>New Page 1</title></head>
    <body>
    <div align="center">
    <center>
    <table border="1" width="100%">
    <?
    (connection to db)
    $master=mysql_query("select * from master_categories");
    $masternum=mysql_numrows($master);
    $i=0;
    if ($masternum>0){
    while ($i<$masternum){
    $masterid=mysql_result($master,$i,"id");
    $subcat=mysql_query("select * from categories where parent_id='$masterid'");
    $subcatnum=mysql_numrows($subcat);

    echo "<tr>";
    echo "<td rowspan="".$subcatnum."" width="33%">";
    echo mysql_result($master,$i,"name");
    echo "</td>";

    $j=0;
    if ($subcatnum>0){
    while ($j<$subcatnum){
    $subcatid=mysql_result($subcat,$j,"id");

    echo "<td width="33%">";
    echo mysql_result($subcat,$j,"name");
    echo "</td>";
    $products=mysql_query("select * from products where subparent_id='$subcatid'");
    $productsrows=mysql_numrows($products);

    $k=0;
    if ($productsrows>0) {
    echo "<td width="34%">";
    while ($k<$productsrows) {
    echo mysql_result($products,$k,"common_name");
    echp "<br>";
    k++;
    }
    echo "</td>";
    }
    else {
    echo "<td width="34%">&nbsp;</td>";
    }
    $j++;
    }
    }
    else {
    echo "<td width="33%">&nbsp;</td>";
    }
    echo "</tr>";
    $i++;
    }
    }
    ?>
    </table>
    </center>
    </div>

    <a href="../catalogue_administration.htm">Catalogue Administration</a>
    </body>
    </html>

    [/code]
    This is the way I would do it. I haven't tested it yet, but try it.

    ------------------
    Good luck,
    Bas

    ------------------
    E-mail me at: b.vandermeijden@pecoma.nl

Similar Threads

  1. League table problem
    By silverduck in forum MySQL Help
    Replies: 8
    Last Post: February 10th, 2004, 12:38 PM
  2. MySQL output to table using phpBB
    By CCCMTech in forum PHP Development
    Replies: 0
    Last Post: February 2nd, 2004, 02:43 PM
  3. output data to table
    By phpfan in forum PHP Development
    Replies: 2
    Last Post: January 31st, 2004, 06:04 AM
  4. Replies: 0
    Last Post: January 30th, 2004, 02:18 PM
  5. how to keep table on top, not centered??
    By vinyl in forum HTML Programming
    Replies: 2
    Last Post: January 20th, 2004, 06:19 PM

IMN logo majestic logo threadwatch logo seochat tools logo