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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:27:43+00:00 2026-06-10T07:27:43+00:00

Thanks to folks here on stackoverflow I have mastered most aspects of database (sqlite)

  • 0

Thanks to folks here on stackoverflow I have mastered most aspects of database (sqlite) control using php in a matter of days!!! And managed to build a basic crud cms. I am now making minor improvements and stuck on ‘saving database sort in a session’. When I click off the page and then return, the session is not saved. I have got so far and this seems a frustrating stumbling block. This is what I have:

        session_start(); 
        $_SESSION['sort'] = $_GET['sort'];
        $savedsort = $_SESSION['sort']; // store session data

if(!empty($_GET['sort']) && ctype_alnum(trim($_GET['sort'])))
$sort = trim($_GET['sort']);?> ///if page is empty

    //links to sort data
<h2><?php echo $savedsort;?></h2>
        <table border="0" align="center" cellpadding="8" cellspacing="0" id="show"><tr><th width="5"><a href="?sort=id">ID</a></th><th>IMG</th><th><a href="?sort=name">NAME</a></th>
        <th width="10"><a href="?sort=cat">CAT</a></th></tr>

    //query string
    ///////////////////////////////////////sort columns ////////////////////////////////
    if($sort == $savedsort){
    $result = $db->query("SELECT * FROM '$table1' ORDER BY '$savedsort' ");
    }
    else{$result = $db->query("SELECT * FROM '$table1' ORDER BY id ");}
    //////////////////////////////////////////////////////////////////////////////////////
    foreach($result as $row){

…any pointers greatly appreciated. Thanks in advance.

Saves in session variable now, thanks but will not update query string

session_start();
 if (isset($_GET['srt'])) 
{ $_SESSION['srt'] = $_GET['srt']; } 
$srt = $_SESSION['srt'];
<tr><th width="5"><a href="?srt=id">ID</a></th><th>IMG</th><th><a href="?srt=name">NAME</a></th><th width="10"><a href="?srt=menu">CAT</a></th>........

 $srtd = $db->query("SELECT * FROM '$table1' ORDER BY '$srt' "); ////////////////////////////////////////////////////////////////////////////////‌​////// foreach($srtd as $row){ $id=$row['id']; 

BUT this method works : is the above code ok?

///////////////////////////////////////sort columns ////////////////////////////////
if ($srt=='name'){$srtd = $db->query("SELECT * FROM '$table1' ORDER BY name ");}
elseif($srt=='id'){$srtd = $db->query("SELECT * FROM '$table1' ORDER BY id ");}
elseif($srt=='menu'){$srtd = $db->query("SELECT * FROM '$table1' ORDER BY menu ");}
else{$srtd = $db->query("SELECT * FROM '$table1' ORDER BY id ");}
//////////////////////////////////////////////////////////////////////////////////////

Thanks for that Nick 😉 The strange thing is that I used single quotes in this query and it works $dropdown = $db->query("SELECT * FROM '$table2' WHERE menu = '$menu'"); but I will use back ticks from now on

  • 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-10T07:27:45+00:00Added an answer on June 10, 2026 at 7:27 am

    You are assiging $_SESSION['sort'] = $_GET['sort']; every time you visit the page, and so if $_GET['sort'] isn’t set then it’ll just change $_SESSION['sort'] to a null value.

    Change it to:

    if (isset($_GET['sort'])) {
      $_SESSION['sort'] = $_GET['sort'];
    }
    

    and it should work

    Edit:

    Your query should be updated to:

    SELECT * FROM `$table1` ORDER BY `$srt`
    

    Note the use of back ticks ` instead of single quotes. Back ticks are used for table names, quotes are used for strings.

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

Sidebar

Related Questions

Folks, why jQuery hover does not work? Here is the link - http://layot.prestatrend.com/ Thanks
Hi Folks thanks for your help here. I am trying to create a rails
thanks to the folks here I already learned quite a lot on my way
I have a sticky footer bar thanks to the folks at cssstickyfooter which holds
Sorry folks...first post here. Just getting the hang of this site. Thanks for the
folks. Here's the problem. Let's say I have three items on my menu. I
Thanks to some great folks on SO, I discovered the possibilities offered by collections.defaultdict
Thanks for the help on here with my query on here the other day
Thanks for stopping by. this static analyser warning is annoying me here is my
Thanks for reading this. I have no idea why this is throwing a NullReferenceException

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.