+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: ORDER BY challenge..

  1. #1
    freecrm's Avatar
    freecrm is offline x10 Elder freecrm is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    629

    ORDER BY challenge..

    php based...

    I have a fairly simple sql query

    SELECT blah de blah... ORDER BY X

    I am trying to produce a dynamic table with column headers with a link back to the same page, but including

    PHP Code:
    ?ordby=whatevercolumnisclicked 
    so that if the column header is clicked, the page refreshes, ordered by that column...

    So I tried to set a variable with $_GET

    PHP Code:
    $varordby_RSOpps "COMPANY";
    if (isset(
    $_GET['ordby'])) {
      
    $varordby_RSOpps $_GET['ordby'];

    The query is then:

    PHP Code:
    $query_RSOpps sprintf("SELECT CONTACTS.*, OPPS.* FROM CONTACTS INNER JOIN OPPS ON CONTACTS.CONTID=OPPS.OPCONTIDL WHERE CONTACTS.GROUPCODE LIKE %s AND OPPS.OPMANAGER LIKE %s AND OPPS.OPCOMPLETED LIKE %s ORDER BY %s DESC"GetSQLValueString($workgroup_RSOpps"text"),GetSQLValueString("%" $varmanager_RSOpps "%""text"),GetSQLValueString($varcompleted_RSOpps"text"),GetSQLValueString($varordby_RSOpps"text")); 
    So by default, it will order by COMPANY which works great.

    If there is a value in the URL, it should pick it up

    BUT.....

    This is part of a mixed form/results page and when the link is clicked, it just refreshes the whole code, re-setting all the other variables.

    How do I do this correctly?

  2. #2
    mattura's Avatar
    mattura is offline x10 Elder mattura is an unknown quantity at this point
    Join Date
    Oct 2007
    Posts
    563

    Re: ORDER BY challenge..

    I'm not entirely sure what you mean, is it:

    Your page shows results, and then a user can click to order them differently, running the same query with an 'ORDER BY'
    ?


    If you want other variables to be saved, maybe add them to the URL string:

    "page.php?orderby=name&variable2=setting2&variable 3=setting3...."
    ----
    Life is a game. The conception is terrible but the graphics are amazing!
    matt.elementfx.com

  3. #3
    freecrm's Avatar
    freecrm is offline x10 Elder freecrm is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    629

    Re: ORDER BY challenge..

    Quote Originally Posted by mattura View Post
    I'm not entirely sure what you mean, is it:

    Your page shows results, and then a user can click to order them differently, running the same query with an 'ORDER BY'
    ?


    If you want other variables to be saved, maybe add them to the URL string:

    "page.php?orderby=name&variable2=setting2&variable 3=setting3...."
    Good point matt.

    I wonder how long I can make a URL...

    The only trouble i can see is the section:

    PHP Code:
    $varordby_RSOpps "COMPANY";
    if (isset(
    $_GET['ordby'])) {
      
    $varordby_RSOpps $_GET['ordby'];

    Because the variable will be re-set.

    I suppose I could make it...

    PHP Code:
    if (isset($varordby_RSOpps)) 
        {
           
    $varordby_RSOpps "COMPANY";
           if (isset(
    $_GET['ordby']))
          {
              
    $varordby_RSOpps $_GET['ordby'];
           }
        }
        else
        {

        }
    ...

    !!

    {}
    ]





    ..... omg - getting confused now! - need to think...
    Last edited by freecrm; 10-04-2008 at 03:24 PM.

  4. #4
    natsuki's Avatar
    natsuki is offline x10 Sophmore natsuki is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    112

    Re: ORDER BY challenge..

    i don't understand what you want to do with the code, when you reload the url all the vars will be gone unless you appended them to the query url.

    And those are long variable names maybe put them in an assoc array so it's easier to manage them.

  5. #5
    mattura's Avatar
    mattura is offline x10 Elder mattura is an unknown quantity at this point
    Join Date
    Oct 2007
    Posts
    563

    Re: ORDER BY challenge..

    where do you set these other variables?

    Another option is to use a PHP session. You can save the state of variables across page loads/reloads. But is this really necessary?
    ----
    Life is a game. The conception is terrible but the graphics are amazing!
    matt.elementfx.com

  6. #6
    freecrm's Avatar
    freecrm is offline x10 Elder freecrm is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    629

    Re: ORDER BY challenge..

    Quote Originally Posted by natsuki View Post
    ... unless you appended them to the query url.
    ... how do i start to attempt this? I'm not even sure I understand what it means lol.

    I'll be more specific.

    Mattura was correct in that I am trying to refresh the page (and query), adding a new $_GET variable from the URL.
    Edit:
    Quote Originally Posted by mattura View Post
    where do you set these other variables?
    most variables are $_POST

    Quote Originally Posted by mattura View Post
    Another option is to use a PHP session. You can save the state of variables across page loads/reloads. But is this really necessary?
    Not sure.. I need to clarify what Natsuki means first before I start going down the sessions route.
    Last edited by freecrm; 10-04-2008 at 04:41 PM. Reason: Automerged Doublepost

  7. #7
    mattura's Avatar
    mattura is offline x10 Elder mattura is an unknown quantity at this point
    Join Date
    Oct 2007
    Posts
    563

    Re: ORDER BY challenge..

    Why not include the column order in the POST?

    Code:
    <?php
    $var1=$_POST['variable1'];
    ...
    $orderby=$_POST['orderby'];
    $query="SELECT * FROM `table` ORDER BY $orderby";
    
    //echo results;
    
    ?>
    <form action='this.php' method='post'>
    Variables: <input name='variable1' />
    ...
    Order by:
    <input type='radio' name='orderby' value='variable1' />variable1<br/>
    <input type='radio' name='orderby' value='variable2' />variable2<br/>
    <input type='submit' />
    </form>
    ----
    Life is a game. The conception is terrible but the graphics are amazing!
    matt.elementfx.com

  8. #8
    natsuki's Avatar
    natsuki is offline x10 Sophmore natsuki is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    112

    Re: ORDER BY challenge..

    is what mattura said is the query string in the url:

    PHP Code:
    href="page.php?order=name<?= htmlspecialchars(urlencode(&var1=value&var2..)) ?>"></a>
    but that means you have gotten all the data into some variables (meaning the form has been posted at least once, else for larger data, use sessions instead.
    Last edited by natsuki; 10-04-2008 at 06:49 PM.

  9. #9
    orczas is offline x10Hosting Member orczas is an unknown quantity at this point
    Join Date
    Oct 2008
    Posts
    1

    Re: ORDER BY challenge..

    Well, from what I see here, I think that freecrm forgot that http is state-less protocol. Which by definition means, that each time user requests any page (or refresh the same page), server processes it as if the user have done it for the first time.
    Because of that, whatever variables you've set once will be lost when user requests another page.
    At least, that was the design idea. Advantage of this approach is that there is no continuity, so server do not have to check in any way if the user was here before or not. Also, this scales nicely, because you can be served the same page from 10 different servers, depending on their available resources and you won't see any difference.
    However, sometimes, especially with complex web applications, you might find that you need server to 'remember' certain data about user.

    Then there are two ways of achieving that:
    1. By appending all the relevant data to the query string every time you request anything (sorting for example).
    That might be very tedious at times, especially when interaction is more complex.

    The alternative is:
    2. Using sessions. Sessions are the mechanism to make server 'remember' certain variables you want it to associated with the given client.
    After you start session in php, server store enough data to recognise the client when it comes again and will be able to pass some data to it.
    You achieve it with $_SESSION variable. It is an assoc array in which you can store things you want to be remembered across requests.
    However, be aware that after session is finished data will be lost, so for cross-session storage use databases.

    That gets us to your case: sorting.
    To start session use
    PHP Code:
    session_start() 
    Do that in the beginning of every request using sessions.
    Then, you can use following code:
    PHP Code:
    //lets say you want your ordby variable

    //first you check whether it was included in the query
    if(isset($_REQUEST['ordby']))$_SESSION['ordby']=$_REQUEST['ordby'];

    //then, if session stores any value, use it, alternatively use
    //default
    if(isset($_SESSION['ordby']))$ordby $_SESSION['ordby'];
    else 
    $ordby 'default'
    This way, you have $ordby use the new value, or if not available then old one and if even that is not existent, default value.

    You could use a function for that, say:
    PHP Code:
    function getValue($id$default){
       if(isset(
    $_REQUEST[$id]))
          
    $_SESSION[$id]=$_REQUEST[$id];
       return isset(
    $_SESSION[$id])?$_SESSION[$id]:$default;

    And use it like:
    PHP Code:
    $ordby getValue('ordby','COMPANY'); 
    Well, that was quite long, if anyone gets to this point, thanks for your time;)

  10. #10
    ghotel is offline Guest ghotel is an unknown quantity at this point
    Join Date
    Dec 2007
    Posts
    106

    Re: ORDER BY challenge..

    i know the reason why but dont feel like telling you

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 0
    Last Post: 04-14-2008, 03:57 AM
  2. A little help with mySQL and SQL ORDER BY with dates
    By MasterMax1313 in forum Programming Help
    Replies: 2
    Last Post: 04-08-2008, 09:15 AM
  3. Replies: 1
    Last Post: 03-20-2008, 12:15 AM
  4. order by substr(..) ???howdo??
    By Kevv3 in forum Scripts & 3rd Party Apps
    Replies: 2
    Last Post: 05-03-2006, 02:21 AM
  5. Challenge
    By Brandon in forum Off Topic
    Replies: 18
    Last Post: 01-25-2006, 02:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers