PHP Loop Problem

Discussion in 'PHP' started by adamjblakey, Mar 28, 2008.

  1. #1
    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.

    $sql = mysql_query("select * 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("SELECT * FROM users WHERE id = '$_COOKIE[id]'");
          $rowi = mysql_fetch_array($resultsi); // Use the result
    	  
          $qualities = array(
              'charming',
              'sense_of_humour',
              '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_children',
              'fond_of_animals',		  
              'intelligent',
              'romantic',
              'talkative',
              'joyful',		  
              'humble',
              'modest',
              'caring',
              'respectful',		  
              'generous',
    		  'thoughtful',
              'down_to_earth',
              'sensitive',		  
              'professional',
              'business_minded',
              'solvent',
              'determined',		  
              'successful'
              );
    
          while ($r=mysql_fetch_array($sql))
          {
              $i = 0;
              foreach ($qualities as $quality)
              {
                  if ($rowi['partner_'.$quality] == '1' && $r[$quality] == '1')
                  $i++;
              }
    
              $tmp = array(
                  'id' => $r['id'],
                  'introduction_title'=> $r['introduction_title'],
                  '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($result, $tmp);
          		}
           
          // pass the results to the template
          $smarty->assign('result', $result);
    PHP:
    Cheers,
    Adam
     
    adamjblakey, Mar 28, 2008 IP
  2. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #2
    Hi Adam,


    Try putting array_push after the while loop. On tmp put $tmp[] = ...


     
    Weirfire, Mar 28, 2008 IP
  3. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Thank you for the reply but this does not seem to have worked, any other ideas?
     
    adamjblakey, Mar 28, 2008 IP
  4. yona_t

    yona_t Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    did u finally find a solution to this problem cause I am having the same problem here...
     
    yona_t, Apr 8, 2008 IP