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 9006487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:25:30+00:00 2026-06-16T01:25:30+00:00

I am doing a inventory system for someone. I want to be able to

  • 0

I am doing a inventory system for someone. I want to be able to click on a link in the table header and cause it to sort by PN and ASC. Then if I click PN again, by DESC. But I also want to order it by description and do the same thing. Here is my code so far. I cannot figure out how to let it swap directions (ASC, DESC) easily.

            if (!isset($cd))
        {
            $cd = 0;
        }
        if (isset($_SESSION['direction']) && $cd == 1)
        {
            if ($_SESSION['direction'] == 'DESC')
            {
                $_SESSION['direction'] = 'ASC';
                $cd = 0;
            } elseif ($_SESSION['direction'] == 'ASC') 
            {
                $_SESSION['direction'] = 'DESC';
                $cd = 0;
            }
        } else
        {
            $_SESSION['direction'] = 'ASC';
        }
        if (isset($_REQUEST['sort']))
        {
            if ($_REQUEST['sort'] == 'pn')
            {
                $sql=mysql_query("select * from inventory ORDER BY pn {$_SESSION['direction']}");
            } elseif ($_REQUEST['sort'] == 'description') {
                $sql=mysql_query("select * from inventory ORDER BY description {$_SESSION['direction']}");
            } elseif ($_REQUEST['sort'] == 'wholesale') {
                $sql=mysql_query("select * from inventory ORDER BY wholesale {$_SESSION['direction']}");
            } elseif ($_REQUEST['sort'] == 'list') {
                $sql=mysql_query("select * from inventory ORDER BY list {$_SESSION['direction']}");
            } elseif ($_REQUEST['sort'] == 'stock') {
                $sql=mysql_query("select * from inventory ORDER BY stock {$_SESSION['direction']}");
            } elseif ($_REQUEST['sort'] == 'location') {
                $sql=mysql_query("select * from inventory ORDER BY location {$_SESSION['direction']}");
            }
        } else {
            $sql=mysql_query("select * from inventory ORDER BY pn {$_SESSION['direction']}");
        }      
        echo "<center><table class=\"myTable\">
        <th><a href=\"inventory.php?mode=list&sort=pn\">PN</a></th><th><a href=\"inventory.php?mode=list&sort=description\">Description</a></th><th><a href=\"inventory.php?mode=list&sort=wholesale\">Wholesale</th><th><a href=\"inventory.php?mode=list&sort=list\">List</th><th><a href=\"inventory.php?mode=list&sort=stock\">Stock</th><th><a href=\"inventory.php?mode=list&sort=location\">Location</th><th>Links</th>";
        while ($result=mysql_fetch_array($sql))
        {
            echo "<tr><td>{$result['pn']}</td><td>{$result['description']}</td><td>{$result['wholesale']}</td><td>{$result['list']}</td><td>{$result['stock']}</td><td>{$result['location']}</td><td>[<a href='inventory.php?mode=edit&id={$result['id']}'>Edit</a>]  [<a href='inventory.php?mode=delete&id={$result['id']}'>Delete</a>]  [<a href='orders.php?mode=list_c&id={$result['id']}'>View Orders</a>]</tr>";
        }
        echo "</table></center>";
  • 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-06-16T01:25:31+00:00Added an answer on June 16, 2026 at 1:25 am

    OMG.

    Look at this much shortened code of what You have:

    $sort_column = 'description'; // the default
    $sort_dir = 'DESC'; // the default
    
    $columns = array('pn', 'description', 'wholesale', 'name', 'list', 'stock', 'location');
    
    if (isset($_GET['dir']) && in_array($_GET['dir'], array('ASC', 'DESC'))) {
        $sort_dir = $_GET['dir'];
    }
    if (isset($_GET['sort']) && in_array($_GET['sort'], $columns))
        $sort_column = $_GET['sort'];
    }
    
    $sql = "select * from inventory ORDER BY {$sort_column} {$sort_dir}";
    
    $result = mysql_query($sql);
    
    ?>
    
    <center><table class="myTable">
        <th><a href="inventory.php?mode=list&sort=pn<?php if($sort_column == 'pn' && $sort_dir == 'DESC') echo '&dir=ASC'; ?>">PN</a></th>
        <th><a href="inventory.php?mode=list&sort=description<?php if($sort_column == 'description' && $sort_dir == 'DESC') echo '&dir=ASC'; ?>">Description</a></th>
        <th><a href="inventory.php?mode=list&sort=wholesale<?php if($sort_column == 'wholesale' && $sort_dir == 'DESC') echo '&dir=ASC'; ?>">Wholesale</th>
        <th><a href="inventory.php?mode=list&sort=list<?php if($sort_column == 'list' && $sort_dir == 'DESC') echo '&dir=ASC'; ?>">List</th>
        <th><a href="inventory.php?mode=list&sort=stock<?php if($sort_column == 'stock' && $sort_dir == 'DESC') echo '&dir=ASC'; ?>">Stock</th>
        <th><a href="inventory.php?mode=list&sort=location<?php if($sort_column == 'location' && $sort_dir == 'DESC') echo '&dir=ASC'; ?>">Location</th><th>Links</th>
    
    <?php while ($result=mysql_fetch_array($sql)) { ?>
        <tr>
            <td><?=$result['pn']?></td>
            <td><?=$result['description']?></td>
            <td><?=$result['wholesale']?></td>
            <td><?=$result['list']?></td>
            <td><?=$result['stock']?></td>
            <td><?=$result['location']?></td>
            <td>[<a href="inventory.php?mode=edit&id=<?=$result['id']?>">Edit</a>]  [<a href="inventory.php?mode=delete&id=<?=$result['id']?>">Delete</a>]  [<a href="orders.php?mode=list_c&id=<?=$result['id']?>">View Orders</a>]</td>
        </tr>
    <?php } ?>
    
    </table></center>
    

    But You should not use mysql_* functions, learn PDO or at least mysqli_*

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

OK so I'm doing a small part of my inventory. I got MOST of
In our inventory database (SQL Server 2008 std edition) we have a table (called
var RowClick = function() { (#Grid).click( var s = $(#Grid).jqGrid('getGridParam', 'selarrrow').toString(); alert(s); $(#showgrid).load('/Inventory/Products/List/' +
Maybe someone else looking at this code will be able to tell me why
This'll be my second time doing a CMS and Inventory Management app for my
I'm making an inventory system and I'm stuck at the part where the items
I'm trying to build an inventory system for the various servers and apps we
I'm using meta_search to sort columns in a table. One of my table columns
I'm trying to make an inventory system with lots of slots, and I think
I am having an issue getting a vector-based inventory system to work. I am

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.