SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: item views

  1. #1
    SitePoint Member
    Join Date
    Feb 2001
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    to any php programmers out there,

    i didn't know what to call this... but i've seen websites where people list the most "viewed" item in a database... for example.. a song archive.. the "top" songs are listed... by the number of views for each one... how do you go about doing this with php & MySQL? an example of what i mean can be found at: http://www.christianguitar.ws/topsongs.php

    thanks for any help!

    regards,

    leo chan

  2. #2
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Steps involved as I imagine:
    + record the number of view (when the song is downloaded/viewed)
    + so a query with ORDER BY #view
    + transfer into the page
    That's pretty simple. Don't know what level of details you need though
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  3. #3
    SitePoint Member
    Join Date
    Feb 2001
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for your response!

    i think my problem.. is how to you record the number of views or times it's downloaded? i'm a newbie at PHP.. is there some sort of syntax that allows you to do it? thanks for any help!

    regards,

    leo

  4. #4
    SitePoint Zealot JordanTLClive's Avatar
    Join Date
    Feb 2001
    Location
    Las Vegas, NV
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I'm new too but here is how I would go about it.
    Each time a select statement is done (basically each time the file is accessed) you would do a select to retrieve a column we'll call numberviews. Then store it to a variable, add one to it, and then use the update query to get it back in. Oh im sure there is a quicker or more efficient way but then there is my way too

    Code:
    $result = mysql_query("SELECT numberviews FROM tablename WHERE conditions_go_here");
    if (!$result) {
     echo "There was an error!";
     mysql_error();
     exit();
    } else {
     $resulta = mysql_fetch_array($result);
     $numviews = $resulta["numberviews"];
     $numviews++;
    }
    
    $upd = mysql_query("UPDATE tablename SET numberviews='$numviews' WHERE conditions_go_here");
    if (!$upd) {
     echo "There was an error!";
     mysql_error();
     exit();
    } else {
    // You could output some sort of confirmation that the view was incremented if you wanted to right here
    }
    -Jordan

  5. #5
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When a page is requested and you want to update the counter in the database for that page - you can do it all in one SQL statement like this:

    Code:
    $sqlString = "
       UPDATE tableName
       SET numViews = numViews + 1
       WHERE itemID = $itemID";


Bookmarks

Posting Permissions

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