SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2001
    Location
    Beaverton, OR
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with a function...

    I am trying to run a function that will compare a list of answers with the right answers and add points to a variable for right answers and do nothing for wrong answers. It involves two queries of the same table (which is probably inefficient but I don't know what else to do here...).

    The problem here is that the result I am getting is only the first row and shows that every answer in that row is correct. Can you spot my error?

    PHP Code:
    function scoring()
    {
    global 
    $valid_user;

    if (!(
    $conn db_connect()))
     return 
    false;

    $result mysql_query"Select * from ep1_picks");
     if (!
    $result)
      return 
    false;

    $num_results mysql_num_rows($result);
    for (
    $i=0$i <$num_results$i++)
     {
     
    $row mysql_fetch_array($result);

     
    $player $row["username"];
     
     
    $result2 mysql_query"Select * from ep1_picks where username = '$player'");
     if (!
    $result2)
      return 
    false;

     
    $num_results mysql_num_rows($result2);

     
    //commented this out because there should only be one result per $player
     //for ($i=0; $i <$num_results; $i++)

     
    $ep1_score '0';
     
    $row2 mysql_fetch_array($result2);

     if (
    $row2["arrive"] = 'Boran'){
      
    $ep1_score $ep1_score 40;
     } else {
      
    $ep1_score $ep1_score;
     }
            
     if (
    $row2["jeffname"] = 'Clarence'){
      
    $ep1_score $ep1_score 80;
     } else {
      
    $ep1_score $ep1_score;
     }
            
     if (
    $row2["tribefire"] = 'Samburu'){
      
    $ep1_score $ep1_score 30;
     } else {
      
    $ep1_score $ep1_score;
     }
            
     if (
    $row2["firstIC"] = 'Samburu'){
      
    $ep1_score $ep1_score 50;
     } else {
      
    $ep1_score $ep1_score;
     }
            
     if (
    $row2["firstbootee"] = 'Diane'){
      
    $ep1_score $ep1_score 100;
     } else {
      
    $ep1_score $ep1_score;
     }
        
    echo 
    $player."<br>";
    echo 
    $ep1_score."<br>";
    }

    Thanks for any help...
    Cygnus

  2. #2
    SitePoint Enthusiast
    Join Date
    Oct 2001
    Location
    Beaverton, OR
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK... I fixed one part of it...

    I took the ' out of this line:
    $ep1_score = '0';

    to read
    $ep1_score = 0;

    I also change these:
    if ($row2["arrive"] = 'Boran'){

    to read:
    if ($row2["arrive"] == 'Boran'){

    I am now getting the right score... but I am only getting the first row echo'd back to me.

    Cygnus

  3. #3
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    have you replaced all = by == in each if($option = 'text')?

    Also instead of $ep1_score = $ep1_score + 40; you can write $ep1_score += 40;

    [EDIT]Code fixed
    Last edited by Paul S; Oct 9, 2001 at 16:39.

  4. #4
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Paul S
    have you replaced all = by == in each if($option = 'text')?

    Also instead of $ep1_score = $ep1_score + 40; you can write $ep1_score += $ep1_score;
    Won't that just add $ep1_score to itself? and at the beggining $ep1_score = 0, so $ep1_score += $ep1_score will be eqal to 0, not 40.
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2001
    Location
    Beaverton, OR
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Defender1
    Won't that just add $ep1_score to itself? and at the beggining $ep1_score = 0, so $ep1_score += $ep1_score will be eqal to 0, not 40.
    Right. $ep1_score = 0 at the top. I need to add to that value whenever certain conditions are met, thus $ep1_score = $ep1_score + 40 (or whatever the specific value is...).

    At any rate, I have that part working. Now, it will not cycle through the rows. It only returns the first row and the $ep1_score value for that row.

    What I need help in is getting it to loop through every row in order to score every username.

    Cygnus

  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)
    Okay it seems you are doing way more than you need to, could you explain a little better about how this is supposed to work. I imagine it is easier than you think to combine everything into one query, I just can't figure out exactly what is what and what is supposed to happen here? Give us a table structure and your intended outcome in a little more detail please.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    SitePoint Enthusiast
    Join Date
    Oct 2001
    Location
    Beaverton, OR
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, Freddy... I didn't think that I explained this really well.

    I am running a contest. Contestants will make guesses about what will happen at a future time (stored with their username in a table named ep1_picks). The function that I am writing here and asking for help on is to evaluate their names and stored guesses in table ep1_picks and print them out through echo statements.

    Ultimately, I want to take these results and stick them into a second table, but I am just trying to get them printed out for now to test this.

    Make more sense?

    Cygnus

  8. #8
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Defender1
    Won't that just add $ep1_score to itself? and at the beggining $ep1_score = 0, so $ep1_score += $ep1_score will be eqal to 0, not 40.
    Right, use $ep1_score += 40;

  9. #9
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, this function has not been tested as I don't have dummy data or table stuctures to work with. But I think making an array of correct responses and points to add, then check each user on the first pass would make things a lot easier for you. This should workbut again I have not tested it.

    PHP Code:
    function scoring() {
        global 
    $valid_user;
        
    $answers['arrive'] = array("answer" => 'Boran''score' => 40);
        
    $answers['jeffname'] = array("answer" => 'Clarence''score' => 80);
        
    $answers['tribefire'] = array("answer" => 'Samburu''score' => 30);
        
    $answers['firstIC'] = array("answer" => 'Samburu''score' => 30);
        
    $answers['firstbootee'] = array("answer" => 'Diane''score' => 50);


        
    $result mysql_query"Select * from ep1_picks");
        if (!
    $result) return false;
        
        
    $num_results mysql_num_rows($result);
        while(
    $row mysql_fetch_array($result)) {
            
    $ep1_score '0';
            
    extract($row);
            
    $players[$username] = array("arrive" => $arrive,
                                        
    "jeffname" => $jeffname,
                                        
    "tribefire" => $tribefire,
                                        
    "firstIC" => $firstIC,
                                        
    "firstbootee" => $firstbootee
                                        
    );
        
            }    
        
        foreach(
    $answers as $key => $val) {
            if(
    $players[$username][$key] == $answers[$key]['answer']) {
                
    $ep1_score += $answers[$key]['score']
                }
            else {
                continue;
                }
            }
        

        echo 
    $username."<br>";
        echo 
    $ep1_score."<br>";
        } 
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  10. #10
    SitePoint Enthusiast
    Join Date
    Oct 2001
    Location
    Beaverton, OR
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by freddydoesphp
    PHP Code:
    foreach($answers as $key => $val) {
      if(
    $players[$username][$key] == $answers[$key]['answer']) {
      
    $ep1_score += $answers[$key]['score']
        }
        else { 
    Gives me a parse error in this section.

    ?

    Cygnus

  11. #11
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I missed a semicolon at the end of this line.

    PHP Code:
      $ep1_score += $answers[$key]['score'
    should be

    PHP Code:
      $ep1_score += $answers[$key]['score']; 
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

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

    change

    PHP Code:
    while($row mysql_fetch_array($result)) {
            
    $ep1_score '0'
    to

    PHP Code:
    while($row mysql_fetch_array($result)) {
            
    reset($answers);
            
    $ep1_score '0'
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  13. #13
    SitePoint Enthusiast
    Join Date
    Oct 2001
    Location
    Beaverton, OR
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No errors... but no results...

    Should this:
    PHP Code:
    $players[$username] = array("arrive" => $arrive,
                                
    "jeffname" => $jeffname,
                                
    "tribefire" => $tribefire,
                                
    "firstIC" => $firstIC,
                              
    "firstbootee" => $firstbootee
                                
    ); 
    Be this:

    PHP Code:
    $players[$username] = array("arrive" => $row["arrive"],
                                
    "jeffname" => $row["jeffname"],
                                
    "tribefire" => $row["tribefire"],
                                
    "firstIC" => $row["firstIC"],
                                
    "firstbootee" => $row["firstbootee"]
                                ); 
    ?

    Cygnus
    Last edited by Cygnus; Oct 9, 2001 at 16:59.

  14. #14
    SitePoint Enthusiast
    Join Date
    Oct 2001
    Location
    Beaverton, OR
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope...

    Cygnus

  15. #15
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's the same because Freddy has used extract($row);

  16. #16
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had the foreach() loops a little messed up. This should work I even tested it with some dummy data.

    PHP Code:
    function scoring() {
        global 
    $valid_user;
        
    $answers['arrive'] = array("answer" => 'Boran''score' => 40);
        
    $answers['jeffname'] = array("answer" => 'Clarence''score' => 80);
        
    $answers['tribefire'] = array("answer" => 'Samburu''score' => 30);
        
    $answers['firstIC'] = array("answer" => 'Samburu''score' => 30);
        
    $answers['firstbootee'] = array("answer" => 'Diane''score' => 50);


        
    $result mysql_query("select * from ep1_picks") or die(mysql_error());
        
    //if (!$result) return false;
        
        
    $num_results mysql_num_rows($result);
        while(
    $row mysql_fetch_array($result)) {
            
    extract($row);
            
    $players[$username] = array("arrive" => $arrive,
                                        
    "jeffname" => $jeffname,
                                        
    "tribefire" => $tribefire,
                                        
    "firstIC" => $firstIC,
                                        
    "firstbootee" => $firstbootee
                                        
    );
        
            }    
        foreach(
    $players as $key => $val) {
            
    $ep1_score '0';
            foreach(
    $answers as $key2 => $val2) {
                if(
    $players[$key][$key2] == $answers[$key2]['answer']) {
                    
    $ep1_score += $answers[$key2]['score'];
                    }
                else {
                    continue;
                    }
                }
            echo 
    $key."<br>";
            echo 
    $ep1_score."<br>";
            }    

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

  17. #17
    SitePoint Enthusiast
    Join Date
    Oct 2001
    Location
    Beaverton, OR
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PERFECT!

    Now I am off to take this information and stick it into another table.

    Thanks a ton! I learned a lot!

    Cygnus

  18. #18
    SitePoint Enthusiast
    Join Date
    Oct 2001
    Location
    Beaverton, OR
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    OK... Apparently, I am not as far along as I had hoped...

    There are a couple of values that I am trying to add onto $ep1_score and the only way that I can think to get their values to add to $ep1_score is through two case statements.

    Where would I put those in the above code so that I could add them to the value printed out?

    I tried this (note: $ep1pick1 and $ep1pick2 are values brought in the above query):
    PHP Code:
    function scoring() {
        global 
    $valid_user;
        
    $answers['arrive'] = array("answer" => 'Boran''score' => 40);
        
    $answers['jeffname'] = array("answer" => 'Clarence''score' => 80);
        
    $answers['tribefire'] = array("answer" => 'Samburu''score' => 30);
        
    $answers['firstIC'] = array("answer" => 'Samburu''score' => 30);
        
    $answers['firstbootee'] = array("answer" => 'Diane''score' => 50);


        
    $result mysql_query("select * from ep1_picks") or die(mysql_error());
        
    //if (!$result) return false;
        
        
    $num_results mysql_num_rows($result);
        while(
    $row mysql_fetch_array($result)) {
            
    extract($row);
            
    switch (
    $ep1pick1){
        case 
    "Brandon":
            
    $pick1 "10";
            break;
        case 
    "Carl":
            
    $pick1 "20";
            break;
        case 
    "Clarence":
            
    $pick1 "30";
            break;
        case 
    "Diane":
            
    $pick1 "40";
            break;
      case 
    "Ethan":
            
    $pick1 "50";
            break;
      case 
    "Frank":
            
    $pick1 "60";
            break;
      case 
    "Jessie":
            
    $pick1 "70";
            break;
      case 
    "Kelly":
            
    $pick1 "80";
            break;
      case 
    "Kim J":
            
    $pick1 "90";
            break;
      case 
    "Kim P":
            
    $pick1 "100";
            break;
      case 
    "Lex":
            
    $pick1 "110";
            break;
      case 
    "Linda":
            
    $pick1 "120";
            break;
      case 
    "Lindsey":
            
    $pick1 "130";
            break;
      case 
    "Silas":
            
    $pick1 "140";
            break;
      case 
    "Teresa":
            
    $pick1 "150";
            break;
      case 
    "Tom":
            
    $pick1 "160";
            break;
    }

    switch (
    $ep1pick2){
        case 
    "Brandon":
            
    $pick2 "5";
            break;
        case 
    "Carl":
            
    $pick2 "10";
            break;
        case 
    "Clarence":
            
    $pick2 "15";
            break;
        case 
    "Diane":
            
    $pick2 "20";
            break;
      case 
    "Ethan":
            
    $pick2 "25";
            break;
      case 
    "Frank":
            
    $pick2 "30";
            break;
      case 
    "Jessie":
            
    $pick2 "35";
            break;
      case 
    "Kelly":
            
    $pick2 "40";
            break;
      case 
    "Kim J":
            
    $pick2 "45";
            break;
      case 
    "Kim P":
            
    $pick2 "50";
            break;
      case 
    "Lex":
            
    $pick2 "55";
            break;
      case 
    "Linda":
            
    $pick2 "60";
            break;
      case 
    "Lindsey":
            
    $pick2 "65";
            break;
      case 
    "Silas":
            
    $pick2 "70";
            break;
      case 
    "Teresa":
            
    $pick2 "75";
            break;
      case 
    "Tom":
            
    $pick2 "80";
            break;
    }
    $players[$username] = array("arrive" => $arrive,
                                        
    "jeffname" => $jeffname,
                                        
    "tribefire" => $tribefire,
                                        
    "firstIC" => $firstIC,
                                        
    "firstbootee" => $firstbootee
                                        
    );
        
            }    

    foreach(
    $players as $key => $val) {
    $ep1_score '0';
    foreach(
    $answers as $key2 => $val2) {
    if(
    $players[$key][$key2] == $answers[$key2]['answer']) {
    $ep1_score += $answers[$key2]['score'];
    } else {
    continue;
    }
    }
    echo 
    $key."<br>";
    echo 
    $ep1_score."<br>";
    echo 
    $pick1."<br>";
    echo 
    $pick2."<br>";
    }    

    but I get the same results for all rows.

    Cygnus
    Last edited by Cygnus; Oct 9, 2001 at 19:22.


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
  •