Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 7 of 7
Like Tree4Likes
  • 1 Post By TheGAME1264
  • 1 Post By mlseim
  • 1 Post By TheGAME1264
  • 1 Post By mlseim

Thread: How do you order data from database from within a page?

  1. #1
    Member John Mc Grath's Avatar
    Join Date
    Sep 2011
    Location
    Ireland
    Posts
    80
    Member #
    29172
    Liked
    3 times

    How do you order data from database from within a page?

    Hi everyone, I was wondering if you could help me. I have a site that loads data from a database as the page is loaded using php. The information is loaded in order of 'date added'. I was hoping to put links that will reorder the information such as 'by name' and so on. The question is would i need to create a value and keep reloading the page with this value changed each time or is there another and easier solution. I am still only learning php and have a feeling that this may be a silly question. Thanks for your patients and time.
    Im pretty new to web design but I am learning fast. I will help if I can.
    Check out my sites http://www.mediareviewzone.com http://www.iamrobsmith.com

  2.  

  3. #2
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,485
    Member #
    425
    Liked
    2783 times
    Somewhere on your php page you'd have a query that looks something like this:
    Code:
    select * from 'your_table' order by 'dateAdded'
    The query should be self-explanatory, but if it isn't then I'll do so. * means "every field". 'your_table" is the table you're retrieving the info from. 'dateAdded', and this is the part you'll need to change, is the dateadded field.

    If this query isn't assigned to a variable, you'll want to do so. That way, you can manipulate the sort order and do other things with it if you need to.

    If you want to do this with a querystring, you'd do something like this.
    Code:
    <?php
     $sortOrder = $_GET['sortOrder'];
    if ($sortOrder == 'name') {
      $sql = $sql."nameField";
    }
    ?>
    This should get you started, although my PHP may not be accurate (it's my 3rd server-side language).
    John Mc Grath likes this.
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  4. #3
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,688
    Member #
    5580
    Liked
    717 times
    I'm on a kindle now so i can't provide an example, but you can use PHP SESSIONS ( much like cookies ), or use cookies to remember how the site user wishes to sort. When they pick a sort method or 'filter', their setting is remembered on all other pages and scripts, until they close their browser.

    Also, you have to decide if it's ok to refresh the page after they pick their 'desired sort', or should the page not refresh. There are various ways to build the UI (user interface) for the sort selection. Likewise, you may want a 'filter' system to narrow the search results.

    Maybe you can show us your site or page where the 'action' takes place ... I could create an example script for you.
    John Mc Grath likes this.


  5. #4
    Member John Mc Grath's Avatar
    Join Date
    Sep 2011
    Location
    Ireland
    Posts
    80
    Member #
    29172
    Liked
    3 times
    Thanks for your help, I thought I might have to use another variable like THEGAME1264 said. There are a couple of areas that will need to be sorted. I have figured it out for all my pages except the index page. What I am doing is loading the variable along with the others within the link and then calling it where needed. I am actually having a little problem calling them though. There is probably a simple problem with this line but I can't for the life of me figure it out. The problem is ORDER BY '".$sort."'. I have echoed the variable and it is there and if I replace '".$sort."' with date_added it works.

    PHP Code:
    $subject_set mysql_query("Select * FROM movies WHERE genre like '%".$info."%' and media = '".$med."' ORDER BY '".$sort."' DESC"$connection);} 
    Back to the main problem, can I give the $sort variable a default value on the index page which can be changed within the index page or will it keep being set back to the default value? The site is MRZ | Review & Preview Movies, Games, Television and Music new releases mlseim and I am trying to make the images below the slide show and the reviews below them sortable. Thanks for all your help, it is well appreciated. One more thing, would a review site like this have any legal issues with studios and so on. Thanks again.
    Im pretty new to web design but I am learning fast. I will help if I can.
    Check out my sites http://www.mediareviewzone.com http://www.iamrobsmith.com

  6. #5
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,485
    Member #
    425
    Liked
    2783 times
    Yes, you can. You can give a variable any default value you like. I don't understand the second part of the question.

    And a review site could have issues with anyone whose products you're reviewing since you're treading into copyright infringement areas. There's usually a "fair use" or "transformative work" clause whereby the intellectual property you're using is being used in such a way as to clearly indicate that it isn't the original work and that you're using it for purposes of criticism, parody, things like that. In this case, you'd probably be okay as review sites are criticisms, but I'd say you're in a grey area, especially since this is for profit (i.e. you're selling ads...not that there's anything wrong with selling ads, but it creates an issue in that you're making money indirectly off of others' intellectual property).

    If it were me personally, I'd look at talking to someone like Jonathan over at CopyByte | Practical Copyright and Plagiarism Consulting who could probably clear this up a lot better than I can, although since you're Irish and he's from Nola he may not be able to help you all that much. I'd still chuck a few coins his way, though, if he can. At least that way, you're a lot safer.
    John Mc Grath likes this.
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  7. #6
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,688
    Member #
    5580
    Liked
    717 times
    You're already using JQuery, so create a "settings" button, like a graphic 'gear' that the person clicks on. That opens a form that has your sort options listed as radio buttons. They select the sort choice and click submit. JQuery executes a PHP script that sets a SESSION variable and closes the form. Because it's JQuery, this happens without leaving the page.

    Now, on all other pages, you check for that SESSION variable and use it in the sort. Their setting will be retained as long as their browser remains opened.

    If you can create the 'settings' form that appears ...

    radio buttons will have values like "1", "2", "3"

    The PHP script that is executed will create the SESSION variable like

    session_start();
    if($_POST['sort']==1){
    $_SESSION['sort']="ORDER by name";
    }

    Now, on every other page, you'll check for the SESSION and assign it to $sort.

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

    $subject_set = mysql_query("Select * FROM movies WHERE genre like '%".$info."%' and media = '".$med."' ".$sort." DESC", $connection);}

    The variable called $sort will fill-in as "ORDER by name".

    Sort of like that. I just don't know how much scripting experience you have. I don't have time to write all of the scripting at this time.
    John Mc Grath likes this.


  8. #7
    Member John Mc Grath's Avatar
    Join Date
    Sep 2011
    Location
    Ireland
    Posts
    80
    Member #
    29172
    Liked
    3 times
    Thanks for all that information it has been very helpful. I got that issue fixed with the $sort variable which was down to using wrong commas. Thanks for the legal advice I will have to look into that more. I am still only new to web design and this site is mainly for practice so it won't be the end of the world if I have to scrap it. My php skill is quite low at the moment but the SESSION variable looks interesting. I have already looked it up online and will have to learn it. Thanks again for your help
    Im pretty new to web design but I am learning fast. I will help if I can.
    Check out my sites http://www.mediareviewzone.com http://www.iamrobsmith.com


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 03:55 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com