PHP Loop Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adamjblakey
    New Member
    • Jan 2008
    • 133

    #1

    PHP Loop Problem

    Hi,

    I am having a problem with my code, what is happening is i am displaying members from a users table and for every user in the foreach loop i am checking to see if any of there qualities match with the user that is running the search.

    The way it matches is it checks about 25 fields and if there is a match then it adds 1 for every match.

    Then it adds all the 1's up and assigns that to a variable to use.

    The problem i am having each time it loops it adds the previous results and the current results and keeps doing that through the loop.

    What i need to do is for it not to add the previous entrys so that the result is correct for each users.

    The code is here:

    [PHP]
    $sql = mysql_query("se lect * from users WHERE $stored AND status='active' ");
    $countrows = mysql_num_rows( $sql);
    // get all the products from the table
    $result = array();
    $e=0;
    while ($r=mysql_fetch _array($sql)) {


    $resultsi = mysql_query("SE LECT * FROM users WHERE id = '$_COOKIE[id]'");
    $rowi = mysql_fetch_ass oc($resultsi); // Use the result

    $resultsis = mysql_query("SE LECT * FROM users WHERE id = '$r[id]'");
    $rowis = mysql_fetch_ass oc($resultsis); // Use the result

    if ($rowi['partner_charmi ng'] == '1' && $rowis['charming'] == '1'){ $i = $i+1; }
    if ($rowi['partner_sense_ of_humour'] == '1' && $rowis['sense_of_humou r'] == '1'){ $i = $i+1; }
    if ($rowi['partner_loyal'] == '1' && $rowis['loyal'] == '1'){ $i = $i+1; }
    if ($rowi['partner_honest '] == '1' && $rowis['honest'] == '1'){ $i = $i+1; }
    if ($rowi['partner_attrac tive'] == '1' && $rowis['attractive'] == '1'){ $i = $i+1; }
    if ($rowi['partner_optimi stical'] == '1' && $rowis['optimistical'] == '1'){ $i = $i+1; }
    if ($rowi['partner_realis tic'] == '1' && $rowis['realistic'] == '1'){ $i = $i+1; }
    if ($rowi['partner_sexy'] == '1' && $rowis['sexy'] == '1'){ $i = $i+1; }
    if ($rowi['partner_ambiti ous'] == '1' && $rowis['ambitious'] == '1'){ $i = $i+1; }
    if ($rowi['partner_vision ary'] == '1' && $rowis['visionary'] == '1'){ $i = $i+1; }
    if ($rowi['partner_quiet'] == '1' && $rowis['quiet'] == '1'){ $i = $i+1; }
    if ($rowi['partner_conser vative'] == '1' && $rowis['conservative'] == '1'){ $i = $i+1; }
    if ($rowi['partner_tolera nt'] == '1' && $rowis['tolerant'] == '1'){ $i = $i+1; }
    if ($rowi['partner_curiou s'] == '1' && $rowis['curious'] == '1'){ $i = $i+1; }
    if ($rowi['partner_selfco nfident'] == '1' && $rowis['selfconfident'] == '1'){ $i = $i+1; }
    if ($rowi['partner_athlet ic'] == '1' && $rowis['athletic'] == '1'){ $i = $i+1; }
    if ($rowi['partner_attent ive'] == '1' && $rowis['attentive'] == '1'){ $i = $i+1; }
    if ($rowi['partner_shy'] == '1' && $rowis['shy'] == '1'){ $i = $i+1; }
    if ($rowi['partner_sociab le'] == '1' && $rowis['sociable'] == '1'){ $i = $i+1; }
    if ($rowi['partner_domina nt'] == '1' && $rowis['dominant'] == '1'){ $i = $i+1; }
    if ($rowi['partner_creati ve'] == '1' && $rowis['creative'] == '1'){ $i = $i+1; }
    if ($rowi['partner_active '] == '1' && $rowis['active'] == '1'){ $i = $i+1; }
    if ($rowi['partner_passiv e'] == '1' && $rowis['passive'] == '1'){ $i = $i+1; }
    if ($rowi['partner_domest icated'] == '1' && $rowis['omesticated'] == '1'){ $i = $i+1; }
    if ($rowi['partner_friend ly'] == '1' && $rowis['friendly'] == '1'){ $i = $i+1; }
    if ($rowi['partner_sympat hetic'] == '1' && $rowis['sympathetic'] == '1'){ $i = $i+1; }
    if ($rowi['partner_fond_o f_children'] == '1' && $rowis['fond_of_childr en'] == '1'){ $i = $i+1; }
    if ($rowi['partner_fond_o f_animals'] == '1' && $rowis['fond_of_animal s'] == '1'){ $i = $i+1; }
    if ($rowi['partner_intell igent'] == '1' && $rowis['intelligent'] == '1'){ $i = $i+1; }
    if ($rowi['partner_romant ic'] == '1' && $rowis['romantic'] == '1'){ $i = $i+1; }
    if ($rowi['partner_talkat ive'] == '1' && $rowis['talkative'] == '1'){ $i = $i+1; }
    if ($rowi['partner_joyful '] == '1' && $rowis['joyful'] == '1'){ $i = $i+1; }
    if ($rowi['partner_humble '] == '1' && $rowis['humble'] == '1'){ $i = $i+1; }
    if ($rowi['partner_modest '] == '1' && $rowis['modest'] == '1'){ $i = $i+1; }
    if ($rowi['partner_caring '] == '1' && $rowis['caring'] == '1'){ $i = $i+1; }
    if ($rowi['partner_respec tful'] == '1' && $rowis['respectful'] == '1'){ $i = $i+1; }
    if ($rowi['partner_genero us'] == '1' && $rowis['generous'] == '1'){ $i = $i+1; }
    if ($rowi['partner_though tful'] == '1' && $rowis['thoughtful'] == '1'){ $i = $i+1; }
    if ($rowi['partner_down_t o_earth'] == '1' && $rowis['down_to_earth'] == '1'){ $i = $i+1; }
    if ($rowi['partner_sensit ive'] == '1' && $rowis['sensitive'] == '1'){ $i = $i+1; }
    if ($rowi['partner_profes sional'] == '1' && $rowis['professional'] == '1'){ $i = $i+1; }
    if ($rowi['partner_busine ss_minded'] == '1' && $rowis['business_minde d'] == '1'){ $i = $i+1; }
    if ($rowi['partner_solven t'] == '1' && $rowis['solvent'] == '1'){ $i = $i+1; }
    if ($rowi['partner_determ ined'] == '1' && $rowis['determined'] == '1'){ $i = $i+1; }
    if ($rowi['partner_succes sful'] == '1' && $rowis['successful'] == '1'){ $i = $i+1; }


    $tmp = array(
    'id' => $r['id'],
    'introduction_t itle'=> $r['introduction_t itle'],
    'image_1'=> $r['image_1'],
    'body_type'=> $r['body_type'],
    'occupation'=> $r['occupation'],
    'sdate'=> $r['sdate'],
    'age'=> $r['age'],
    'personality'=> $r['personality'],
    'username'=> $r['username'],
    'match'=> $i
    );
    $result[$e++] = $tmp;
    }
    // pass the results to the template
    $smarty->assign('result ', $result);
    [/PHP]

    Cheers,
    Adam
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    Try array_push($res ult, $tmp); (line 74)

    Comment

    • adamjblakey
      New Member
      • Jan 2008
      • 133

      #3
      Thank you for your reply but this did not seem to work.

      Comment

      • hsriat
        Recognized Expert Top Contributor
        • Jan 2008
        • 1653

        #4
        I cleaned your code a little bit. Hope this will work.
        [php]$sql = mysql_query("se lect * from users WHERE $stored AND status='active' ");
        $countrows = mysql_num_rows( $sql);
        // get all the products from the table
        $result = array();
        //$e=0;

        $resultsi = mysql_query("SE LECT * FROM users WHERE id = '$_COOKIE[id]'");
        $rowi = mysql_fetch_arr ay($resultsi); // Use the result

        $qualities = array(
        'charming',
        'sense_of_humou r',
        'loyal',
        'honest',

        //ADD ALL QUALITIES IN THIS ARRAY

        'successful'
        );

        while ($r=mysql_fetch _array($sql))
        {

        $i = 0;
        foreach ($qualities as $quality)
        {
        if ($rowi['partner_'.$qua lity] == '1' && $r[$quality] == '1')
        $i++;
        }


        $tmp = array(
        'id' => $r['id'],
        'introduction_t itle'=> $r['introduction_t itle'],
        'image_1'=> $r['image_1'],
        'body_type'=> $r['body_type'],
        'occupation'=> $r['occupation'],
        'sdate'=> $r['sdate'],
        'age'=> $r['age'],
        'personality'=> $r['personality'],
        'username'=> $r['username'],
        'match'=> $i);

        array_push($res ult, $tmp);
        }

        // pass the results to the template
        $smarty->assign('result ', $result);[/php]

        Comment

        • adamjblakey
          New Member
          • Jan 2008
          • 133

          #5
          Thank you very much for taking the time to re-write the code it is a lot cleaner this way.

          I have just tested it though and it still does not work and is adding them all up.

          Comment

          • hsriat
            Recognized Expert Top Contributor
            • Jan 2008
            • 1653

            #6
            Originally posted by adamjblakey
            I have just tested it though and it still does not work and is adding them all up.
            What's still getting added up which you don't want to?

            Comment

            • adamjblakey
              New Member
              • Jan 2008
              • 133

              #7
              Well say the first result = 3 then the next result = 2 and so on..

              It should be like this

              Result 1 - 3
              Result 2 - 2
              Result 3 - 6
              Result 4 - 3
              Result 5 - 1
              etc

              But what it is doing is this

              Result 1 - 3
              Result 2 - 5
              Result 3 - 11
              Result 4 - 14
              Result 5 - 15
              etc

              So basically with each result it is adding the last result on top of its own value.

              Comment

              • hsriat
                Recognized Expert Top Contributor
                • Jan 2008
                • 1653

                #8
                Did you look at line 24?

                Comment

                • adamjblakey
                  New Member
                  • Jan 2008
                  • 133

                  #9
                  Should this be taking away the last entry somehow?

                  Comment

                  • hsriat
                    Recognized Expert Top Contributor
                    • Jan 2008
                    • 1653

                    #10
                    Originally posted by adamjblakey
                    Should this be taking away the last entry somehow?
                    Yeh, its setting the match thing to 0 in the start of each loop.

                    Comment

                    • adamjblakey
                      New Member
                      • Jan 2008
                      • 133

                      #11
                      Sorry to be so thick but i don't know how i would correct this, i think i would need to get the value from the last results and take it away from the current result but do not know how to do this?

                      Comment

                      • hsriat
                        Recognized Expert Top Contributor
                        • Jan 2008
                        • 1653

                        #12
                        Originally posted by adamjblakey
                        Sorry to be so thick but i don't know how i would correct this, i think i would need to get the value from the last results and take it away from the current result but do not know how to do this?
                        Didn't you try the code I gave you?

                        Comment

                        • adamjblakey
                          New Member
                          • Jan 2008
                          • 133

                          #13
                          Yes this is what is in place now:

                          [PHP] $sql = mysql_query("se lect * from users WHERE $stored AND status='active' ");
                          $countrows = mysql_num_rows( $sql);
                          // get all the products from the table
                          $result = array();
                          //$e=0;

                          $resultsi = mysql_query("SE LECT * FROM users WHERE id = '$_COOKIE[id]'");
                          $rowi = mysql_fetch_arr ay($resultsi); // Use the result

                          $qualities = array(
                          'charming',
                          'sense_of_humou r',
                          'loyal',
                          'honest',
                          'attractive',
                          'optimistical',
                          'realistic',
                          'sexy',
                          'ambitious',
                          'visionary',
                          'quiet',
                          'conservative',
                          'tolerant',
                          'curious',
                          'selfconfident' ,
                          'athletic',
                          'attentive',
                          'shy',
                          'sociable',
                          'dominant',
                          'creative',
                          'active',
                          'passive',
                          'omesticated',
                          'friendly',
                          'sympathetic',
                          'fond_of_childr en',
                          'fond_of_animal s',
                          'intelligent',
                          'romantic',
                          'talkative',
                          'joyful',
                          'humble',
                          'modest',
                          'caring',
                          'respectful',
                          'generous',
                          'thoughtful',
                          'down_to_earth' ,
                          'sensitive',
                          'professional',
                          'business_minde d',
                          'solvent',
                          'determined',
                          'successful'
                          );

                          while ($r=mysql_fetch _array($sql))
                          {
                          $i = 0;
                          foreach ($qualities as $quality)
                          {

                          if ($rowi['partner_'.$qua lity] == '1' && $r[$quality] == '1')
                          $i++;
                          }

                          $tmp = array(
                          'id' => $r['id'],
                          'introduction_t itle'=> $r['introduction_t itle'],
                          'image_1'=> $r['image_1'],
                          'body_type'=> $r['body_type'],
                          'occupation'=> $r['occupation'],
                          'sdate'=> $r['sdate'],
                          'age'=> $r['age'],
                          'personality'=> $r['personality'],
                          'birthday'=> $r['birthday'],
                          'username'=> $r['username'],
                          'match'=> $i);

                          array_push($res ult, $tmp);
                          }

                          // pass the results to the template
                          $smarty->assign('result ', $result);[/PHP]

                          Comment

                          • hsriat
                            Recognized Expert Top Contributor
                            • Jan 2008
                            • 1653

                            #14
                            This should be working!

                            Is it doing the same thing?... like adding up the last match with the present one.

                            Add this code just after line 81, and see what is the error...
                            [php]echo "<pre>";
                            print_r($tmp);
                            echo "</pre>";
                            [/php]

                            Comment

                            • adamjblakey
                              New Member
                              • Jan 2008
                              • 133

                              #15
                              This is weird as when i do this it does print out the correct values


                              [PHP]Array
                              (
                              [id] => 12
                              [introduction_ti tle] => Sweet Girl Looking For Soulmate
                              [image_1] => 75825365.jpg
                              [body_type] => Slim/Petite
                              [occupation] => Teacher
                              [sdate] => 2008-02-08
                              [age] => 32
                              [personality] => I am outgoing, friendly with good personality
                              [birthday] => 1975-04-05
                              [username] => Cutie
                              [match] => 3
                              )

                              Array
                              (
                              [id] => 14
                              [introduction_ti tle] =>
                              [image_1] => 283841086.jpg
                              [body_type] => Slim/Petite
                              [occupation] =>
                              [sdate] => 2008-02-26
                              [age] => 0
                              [personality] => im a down to earth person....i can mingle with everybody as long as u want my
                              company...i love life and i want to make the best of it...im always open to any
                              opportunities. adventurous kind of girl...tough and will never surrender..i love
                              to talk and u will never feel bored with me. i like to do things which i think
                              im capable of doing..i love to see places.i will tell u more later...(:
                              [birthday] => 1981-02-06
                              [username] => xamantha27
                              [match] => 3
                              )

                              Array
                              (
                              [id] => 16
                              [introduction_ti tle] =>
                              [image_1] =>
                              [body_type] =>
                              [occupation] =>
                              [sdate] => 2008-02-27
                              [age] => 22
                              [personality] =>
                              [birthday] => 1986-02-02
                              [username] => apriljoy_m
                              [match] => 0
                              )

                              [/PHP]

                              But when i print the value next to each result as i am printing them out it adds them up.

                              Could it be because i am looping the results through on the template side e.g.

                              {section name=nr loop=$result}

                              Displaying results here
                              {/section}

                              Comment

                              Working...