Basic PHP help please

Discussion in 'PHP' started by fgs, Mar 16, 2007.

  1. #1
    Hello, is there any comand like RANK or COUNT in php? more particularly when selecting from database:

    SELECT * FROM table1 WHERE category = 4 AND id = 12345 LIMIT 1
    PHP:
    1. Lets say that item has id, category and VIEWS attributes.. is it possible to tell how that item with that particular ID ranks by views for category 4?
    2. Is there a way to count the number of entries in category 4, without having to do something rediculous like selecting all items from table where category = 4 and doing a loop with $count++;


    any help greatly appreciated
     
    fgs, Mar 16, 2007 IP
  2. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #2
    1. SELECT * FROM table1 WHERE category = 4 ORDER BY views DESC

    2. SELECT COUNT(*) FROM table1 WHERE category = 4

    This is more to do with databases than PHP.
     
    T0PS3O, Mar 16, 2007 IP
  3. steb

    steb Peon

    Messages:
    213
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thats more mysql than php..

    im not too sure about number 1, but for 2, you can do

    SELECT count(id) FROM table1 WHERE category = 4
     
    steb, Mar 16, 2007 IP
  4. ThreeGuineaWatch

    ThreeGuineaWatch Well-Known Member

    Messages:
    1,489
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    140
    #4
    It is also good practice to add a LIMIT to your results.

    If you have 250 000 lines in your table and are only interested in returning the first, say, 20 for your web page, then it makes no sense in selecting the entire set every time you run the query.

    SELECT * FROM table1 WHERE category = 4 ORDER BY views DESC LIMIT 0,20
     
    ThreeGuineaWatch, Mar 16, 2007 IP
  5. fgs

    fgs Peon

    Messages:
    967
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thanks a lot for the help guys, but i guess what i am trying to do for the first one is... lets say there are 5 values in category 4 with VIEWS = {10,20,30(id=1),40,50}

    so it would be somethign like
    RANK by id where cat = 4 and id = 1 order DESC
    and it would return 3, since it is the third highest value

    Thanks for help
     
    fgs, Mar 16, 2007 IP