SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    The Jellophonic Autobrain CHeeSeBLiND's Avatar
    Join Date
    Jul 2001
    Location
    Bolton, England
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    choosing certain info from a db

    ok im really dumb how do you select a certain value from a certain record in a database?

    what im trying to do is make a poll script and i was wondering how i can add up the total amount of votes so i can figure out the percentages

    in the table there are only 3 fields ( ID, value, votes ) do you think that is enough or will i need to add more?

    thx alot

  2. #2
    SitePoint Guru Richi's Avatar
    Join Date
    Jul 2001
    Location
    Hu,Bp.
    Posts
    842
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $query mysql_query("Select * from Table_Name order by votes");
    list(
    $ID,$value$votes ) = mysql_fetch_row($query);

    echo 
    "$ID ... $VALUE ... $votes"

  3. #3
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    I don't know how you store your votes..but i'll assume 'votes' is the number of vote for each ID

    PHP Code:
    $total_vote 0;
    $query mysql_query("SELECT votes from tablename");
    while (list(
    $votes) = mysql_fetch_row($query)) {
        
    $total_vote += $votes;
    }

    $query mysql_query("SELECT * from tablename ORDER BY votes");
    while (
    $row=mysql_fetch_row($query)) {
        
    extract($row);
        
    $per_vote $votes/$total_vote;
        echo 
    "$ID has a Percentage of $per_vote";

    Last edited by lynlimz; Aug 12, 2001 at 09:28.
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  4. #4
    The Jellophonic Autobrain CHeeSeBLiND's Avatar
    Join Date
    Jul 2001
    Location
    Bolton, England
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thx alot

  5. #5
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    whoops. while looking back at my post, i noticed an error.

    i wrote
    PHP Code:
    $per_vote $vote/$total_vote
    but its actually

    PHP Code:
    $per_vote $votes/$total_vote

    i missed out the 's' in $votes
    I edited my post to reflect the changes. sorry.
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    linlymz,

    I don't think your suggestion is going to work. one your percentage result will be a decimal and needs to be multiplied by 100, also no need to run the first query and loop through all the results, this should be a bit more efficient and it results in real numbers.

    PHP Code:
    <?
    $db 
    mysql_connect("localhost""""");
    mysql_select_db("test");
    $countsql mysql_query("SELECT SUM(votes) as totalvotes from votes") or DIE(mysql_error());
    $totalvotes mysql_result($countsql0);
    ?>
    <br>
    <?
    $result 
    mysql_query("SELECT ID, value, votes, (votes/$totalvotes) as totalnum from votes");
    while(
    $row mysql_fetch_array($result)) {
        
    extract($row);
        print 
    "$ID - $value - $votes - ".floor(($totalnum 100))."<br>";
        }


    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whoops. forgot about the multiple by 100. haha!
    oh..i normally round my numbers off to 2 decimal places.
    bleh.

    thanks freddy for the better code!!!
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein


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
  •