Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7658285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:10:55+00:00 2026-05-31T13:10:55+00:00

My application requires to create a stock management table which has a lot of

  • 0

My application requires to create a stock management table which has a lot of items categorised into subgroups. For and idea, the stock management table looks like:

                           Location1             Location2                Total
Desktops    // Main Category
Microsoft   //Sub-Category

Windows 7                    23000                 150000                173000
Office 2011                 203300                   3002                206302 
....

Apple       //Sub-Category
OS Snow Leopard              4000                    3000                  7003
OS Lion                     39494                   40034                 79528
...

Tablets    //Main Category
Lenovo     //Sub-Cateogry

LX-243                       3434                   4399                   7833
...

This is a visualisation of what the table should look like, now i have a huge mysql query which does that for me, it is scary. In a gist, what i am doing is the following:

  1. Select 1st category and start a while loop
    // echo as the main category
  2. Select sub-category corresponding to the main category and start a while loop again.
    // echo as the sub-category
  3. select all items in the sub-category and start a while loop.
  4. select 1st location.
  5. select the current item from 3 and the current location from 4 and echo item.
    // echo item names and the quantity in the locations.

Now my question here is is there a better way to display this than using several while loops, i tried using functions but they are becoming a mess too. Also i couldn’t figure out where should i perform the calculations to get the total in the last column.

Database structure:

Category: CategoryID, Description
Sub-Cateogry: Sub-Category_ID, Category_ID, Description
Item: Item_ID, Sub-category_id, Description
Location: Location_ID, Description
Stock_Management: Item_ID, Location_ID, Quantity

Code as requested:

$sql = mysql_query("select Board_ID, Title from Board where Company_ID = '$company_id' order by Title");

while($row = mysql_fetch_array($sql)) {
        $curr_ID = $row[0];
        $curr_category = $row[1];
        echo "<tr class='sub-heading' style='background: rgba(76, 178, 255, 0.1)'><td colspan='".$count."'>".$curr_category."</td></tr>";
        $sql1 = mysql_query("select Sub_Category_ID, Title from Sub_Category where Category_ID = '$curr_ID' order by Title");
        while($row1 = mysql_fetch_array($sql1)) {
            $curr_sub_cat_id = $row1[0];
            $curr_sub_cate = $row1[1];
            echo "<tr style='background: rgba(149, 255, 145, 0.10'><td colspan='".$count."'><b>".$curr_sub_cate."</b></td></tr>";
            $sql2 = mysql_query("select Book_ID, title from Book where sub_category_id = '$curr_sub_cat_id' Order by title");
            while($row2 = mysql_fetch_array($sql2)) {
                $curr_book = $row2[0];
                echo "<tr><td>".$row2[1]."</td>";
                $sql4 = mysql_query("select OfficeID, OfficeTitle from Office where OfficeTitle IN ('$locations')");
                while($row3 = mysql_fetch_array($sql4)) {
                    $curr_location = $row3[0];
                    $sql3 = mysql_query("select Quantity from Stock_Management where Book_ID = '$curr_book' and Location_ID = '$curr_location'");
                    while($row3 = mysql_fetch_array($sql3)) {
                        echo "<td>".$row3[0]."</td>";
                    }   
                }
                echo "</tr>";
            } 
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:10:56+00:00Added an answer on May 31, 2026 at 1:10 pm

    How about something like this –

    <?php
    
    $locations = array('Location 1', 'Location 2', 'Location 3');
    $locationCols = array();
    $locationList = array();
    
    foreach ($locations as $location) {
        $locationColName = preg_replace('[^a-z0-9]', '_', strtolower($location));
        $location = mysql_real_escape_string($location);
        $locationCols[] = "SUM(IF(Office.OfficeTitle = '$location', Stock_Management.Quantity, 0)) AS `$locationColName`";
        $locationList[] = "'$location'";
    }
    $locationCols = implode(', ', $locationCols);
    $locationList = implode(',', $locationList);
    
    $sql = "SELECT Board.Title AS BoardTitle, Sub_Category.Title AS SubCatTitle, Book.title AS BookTitle, $locationCols, SUM(Stock_Management.Quantity) AS Total
            FROM Board
            INNER JOIN Sub_Category
                ON Board.Board_ID = Sub_Category.Category_ID
            INNER JOIN Book
                ON Sub_Category.Sub_Category_ID = Book.sub_category_id
            INNER JOIN Office
            LEFT JOIN Stock_Management
                ON Book.Book_ID = Stock_Management.Book_ID
                AND Office.OfficeID = Stock_Management.Location_ID
            WHERE Board.Company_ID = 2
            AND Office.OfficeTitle IN ($locationList)
            GROUP BY Board.Title, Sub_Category.Title, Book.title";
    
    $result = mysql_query($sql);
    
    $prevBoard = '';
    $prevSubCat = '';
    
    echo '<table>';
    
    while ($row = mysql_fetch_object($result)) {
    
        // if new board print board
        if ($prevBoard != $row->BoardTitle) {
            echo '<tr class="sub-heading" style="background: rgba(76, 178, 255, 0.1)"><td colspan="">' . $row->BoardTitle . '</td></tr>';
        }
        $prevBoard = $row->BoardTitle;
    
        if ($prevSubCat != $row->SubCatTitle) {
            echo '<tr style="background: rgba(149, 255, 145, 0.10)"><td colspan=""><b>' . $row->SubCatTitle . '</b></td></tr>';
        }
        $prevSubCat = $row->SubCatTitle;
    
        // print product row
        echo '<tr>';
        echo "<td>{$row->BookTitle}</td>";
    
        foreach ($locations as $location) {
            $locationColName = preg_replace('[^a-z0-9]', '_', strtolower($location));
            echo "<td>{$row->$locationColName}</td>";
        }
    
        echo "<td>{$row->Total}</td>";
        echo '</tr>';
    }
    
    echo '<table>';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm about to create a web application that requires a lot of different web
My application requires the .NET Framework version 3.5. I recently ran into a customer
My application requires the user to enter their business name, which the application will
I'm using .NET 3.5 to create an application that requires a Stack<T> with unique
I have this application that requires me to have a QList which will contain
My application requires that I have access to the file system. Regardless if the
My application requires events to be fired based on some specific activities that happen.
If Java application requires certain JRE version then how can I check its availability
One of the functionalities in my current flex application requires me to maintain a
I am creating a script to configure and launch software. My current application requires

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.