Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    239

    How would I do this (php question)?

    Im coding a little site, just to practice my php skills.

    I have a script, that randomly picks an item from a table, and displays it. I will have a few hundred/thousand items in that table, and I only wish to display each item once (for each user who views the site). Some users will have accounts, but most will not... so I figured this will have to be done with session variables..... but Im not exactly sure how to proceed. The only way I know is to simply keep adding "AND id != xxx" to the query that calls up the random item......

    Any suggestions?

  2. #2
    Join Date
    May 2005
    Location
    Planet Earth
    Posts
    813
    What is the point of showing them randomly over one after the other ?

    User couldn't know the true order in your table..

    G
    PutFile.io Disrupting traditional file hosting.
    █ Signup Early and enjoy Unlimited space/bandwidth for your files hosting, Forever!
    █ No Ads.
    █ No Countdowns.

  3. #3
    If you want to exclude certain ids from select, you use NOT IN(). Example:

    SELECT something FROM table WHERE id NOT IN(1,2,3,4,5,6);
    Dyslexics Have More Fnu

  4. #4
    Join Date
    Dec 2006
    Location
    Missoula,MT
    Posts
    45
    Disclaimer:

    1. This code assumes you have a working db connection already established.
    2. It's not perfect, 100% secure and all that, it's a quick rough example of how you would go about doing this.

    PHP Code:
    <?php
    session_start
    (); //needed at the top of every page that uses $_SESSION and must be called before any output is called

    if( isset($_SESSION["usedItems"]) )
    {
        
    $ids implode(","$_SESSION["usedItems"]); //we make a string from the session vars to use in an exclusion rule for the SQL query
    }
    else
    {
        
    session_register("usedItems");
    }

    $query "SELECT * FROM <table>"//replace <table> with your table name

    if(isset($ids))
        
    $query .= " WHERE id NOT IN(".$ids.") LIMIT 1";  //NOT IN will work to exclude a list of items
    else
        
    $query .= " LIMIT 1"//we use LIMIT 1 because you want only 1 item

    $res mysql_query($query) or die(mysql_error()); //exec query or fail with error

    if(mysql_num_rows($res) > 0//check to see you have results
    {
        
    $row mysql_fetch_object($res);
        
    $_SESSION["usedItems"][] = $row->id//this ads the id's into an array for exclusion
        //also put your display info here, for ex. for displaying 1 image
        
    echo '<img src="'.$row->image.'" />';
    }
    ?>
    SupportLayer - Enterprise Linux Server Management

  5. #5
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    239
    Thanks sockopt, thats quite awesome! Exactly what I needed. My last question is.... how do I add ids to the $_SESSION["usedItems"] variable, so they simply get added onto the variable (id1, id2, id3....), instead of simply replacing it. Im not that great with arrays.

  6. #6
    Join Date
    Dec 2006
    Location
    Missoula,MT
    Posts
    45
    PHP Code:
    $_SESSION["usedItems"][] = $row->id//this ads the id's into an array for exclusion 
    The above line would add the ids to the $_SESSION["usedItems"] array, it's already done! The trick is the [] that follows the var, this will automatically use the next array index.

    You could have done something like:

    $array["blah"][0] = $id1;
    $array["blah"][1] = $id2;

    but, the following is the same, without needing to care about the array index

    $array["blah"][] = $id1;
    $array["blah"][] = $id2;


    Also, remember the golden rule of sessions, always have the following:

    session_start();

    at the top of every page on the site. What will happen is if you have this code on some page, let's call it:

    www.example.com/links.php

    and someone goes to another page, like:

    www.example.com/contact.php

    if contact.php doesn't have "session_start();" at the top, you will lose the $_SESSION state. It needs to be sitewide, not specific to a page. If you only do session stuff on one page, you still must maintain that session on all other pages.

    A good way to handle that would be, if you have some kind of header file that is included in all files, like, say header.php, at the top of the file, call session_start().

    Also, for viewing array contents, you can use this:

    echo "<pre>";
    print_r($array);
    echo "</pre>";

    The above will print out the array in a nice format for you to see it's data. Just replace $array with the actual name of your array and then view the page, it should show you what is going on.

    This comes in handy when doing debug on sessions, as you can do a:

    echo "<pre>";
    print_r($_SESSION);
    echo "</pre>";

    and it will print out the entire session to the screen. This way you can test your script and as the page reloads you will see how $_SESSION["usedItems"] gets incremented.

    Last edited by sockopt; 01-05-2007 at 03:17 PM.
    SupportLayer - Enterprise Linux Server Management

  7. #7
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    239
    Thanks! You are a life saver! WIll try this out tonight!

  8. #8
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    239
    Works perfectly! Your help is much appreciated!

  9. #9
    Join Date
    Dec 2006
    Location
    Missoula,MT
    Posts
    45
    You're welcome!
    SupportLayer - Enterprise Linux Server Management

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •