473,386 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

PHP Loop Problem

133 100+
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("select * 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("SELECT * FROM users WHERE id = '$_COOKIE[id]'");
$rowi = mysql_fetch_assoc($resultsi); // Use the result

$resultsis = mysql_query("SELECT * FROM users WHERE id = '$r[id]'");
$rowis = mysql_fetch_assoc($resultsis); // Use the result

if ($rowi['partner_charming'] == '1' && $rowis['charming'] == '1'){ $i = $i+1; }
if ($rowi['partner_sense_of_humour'] == '1' && $rowis['sense_of_humour'] == '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_attractive'] == '1' && $rowis['attractive'] == '1'){ $i = $i+1; }
if ($rowi['partner_optimistical'] == '1' && $rowis['optimistical'] == '1'){ $i = $i+1; }
if ($rowi['partner_realistic'] == '1' && $rowis['realistic'] == '1'){ $i = $i+1; }
if ($rowi['partner_sexy'] == '1' && $rowis['sexy'] == '1'){ $i = $i+1; }
if ($rowi['partner_ambitious'] == '1' && $rowis['ambitious'] == '1'){ $i = $i+1; }
if ($rowi['partner_visionary'] == '1' && $rowis['visionary'] == '1'){ $i = $i+1; }
if ($rowi['partner_quiet'] == '1' && $rowis['quiet'] == '1'){ $i = $i+1; }
if ($rowi['partner_conservative'] == '1' && $rowis['conservative'] == '1'){ $i = $i+1; }
if ($rowi['partner_tolerant'] == '1' && $rowis['tolerant'] == '1'){ $i = $i+1; }
if ($rowi['partner_curious'] == '1' && $rowis['curious'] == '1'){ $i = $i+1; }
if ($rowi['partner_selfconfident'] == '1' && $rowis['selfconfident'] == '1'){ $i = $i+1; }
if ($rowi['partner_athletic'] == '1' && $rowis['athletic'] == '1'){ $i = $i+1; }
if ($rowi['partner_attentive'] == '1' && $rowis['attentive'] == '1'){ $i = $i+1; }
if ($rowi['partner_shy'] == '1' && $rowis['shy'] == '1'){ $i = $i+1; }
if ($rowi['partner_sociable'] == '1' && $rowis['sociable'] == '1'){ $i = $i+1; }
if ($rowi['partner_dominant'] == '1' && $rowis['dominant'] == '1'){ $i = $i+1; }
if ($rowi['partner_creative'] == '1' && $rowis['creative'] == '1'){ $i = $i+1; }
if ($rowi['partner_active'] == '1' && $rowis['active'] == '1'){ $i = $i+1; }
if ($rowi['partner_passive'] == '1' && $rowis['passive'] == '1'){ $i = $i+1; }
if ($rowi['partner_domesticated'] == '1' && $rowis['omesticated'] == '1'){ $i = $i+1; }
if ($rowi['partner_friendly'] == '1' && $rowis['friendly'] == '1'){ $i = $i+1; }
if ($rowi['partner_sympathetic'] == '1' && $rowis['sympathetic'] == '1'){ $i = $i+1; }
if ($rowi['partner_fond_of_children'] == '1' && $rowis['fond_of_children'] == '1'){ $i = $i+1; }
if ($rowi['partner_fond_of_animals'] == '1' && $rowis['fond_of_animals'] == '1'){ $i = $i+1; }
if ($rowi['partner_intelligent'] == '1' && $rowis['intelligent'] == '1'){ $i = $i+1; }
if ($rowi['partner_romantic'] == '1' && $rowis['romantic'] == '1'){ $i = $i+1; }
if ($rowi['partner_talkative'] == '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_respectful'] == '1' && $rowis['respectful'] == '1'){ $i = $i+1; }
if ($rowi['partner_generous'] == '1' && $rowis['generous'] == '1'){ $i = $i+1; }
if ($rowi['partner_thoughtful'] == '1' && $rowis['thoughtful'] == '1'){ $i = $i+1; }
if ($rowi['partner_down_to_earth'] == '1' && $rowis['down_to_earth'] == '1'){ $i = $i+1; }
if ($rowi['partner_sensitive'] == '1' && $rowis['sensitive'] == '1'){ $i = $i+1; }
if ($rowi['partner_professional'] == '1' && $rowis['professional'] == '1'){ $i = $i+1; }
if ($rowi['partner_business_minded'] == '1' && $rowis['business_minded'] == '1'){ $i = $i+1; }
if ($rowi['partner_solvent'] == '1' && $rowis['solvent'] == '1'){ $i = $i+1; }
if ($rowi['partner_determined'] == '1' && $rowis['determined'] == '1'){ $i = $i+1; }
if ($rowi['partner_successful'] == '1' && $rowis['successful'] == '1'){ $i = $i+1; }


$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'],
'username'=> $r['username'],
'match'=> $i
);
$result[$e++] = $tmp;
}
// pass the results to the template
$smarty->assign('result', $result);
[/PHP]

Cheers,
Adam
Mar 28 '08 #1
16 1491
hsriat
1,654 Expert 1GB
Try array_push($result, $tmp); (line 74)
Mar 28 '08 #2
adamjblakey
133 100+
Thank you for your reply but this did not seem to work.
Mar 28 '08 #3
hsriat
1,654 Expert 1GB
I cleaned your code a little bit. Hope this will work.
[php]$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',

//ADD ALL QUALITIES IN THIS ARRAY

'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'],
'username'=> $r['username'],
'match'=> $i);

array_push($result, $tmp);
}

// pass the results to the template
$smarty->assign('result', $result);[/php]
Mar 28 '08 #4
adamjblakey
133 100+
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.
Mar 28 '08 #5
hsriat
1,654 Expert 1GB
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?
Mar 28 '08 #6
adamjblakey
133 100+
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.
Mar 28 '08 #7
hsriat
1,654 Expert 1GB
Did you look at line 24?
Mar 28 '08 #8
adamjblakey
133 100+
Should this be taking away the last entry somehow?
Mar 28 '08 #9
hsriat
1,654 Expert 1GB
Should this be taking away the last entry somehow?
Yeh, its setting the match thing to 0 in the start of each loop.
Mar 28 '08 #10
adamjblakey
133 100+
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?
Mar 28 '08 #11
hsriat
1,654 Expert 1GB
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?
Mar 28 '08 #12
adamjblakey
133 100+
Yes this is what is in place now:

[PHP] $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]
Mar 28 '08 #13
hsriat
1,654 Expert 1GB
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]
Mar 28 '08 #14
adamjblakey
133 100+
This is weird as when i do this it does print out the correct values


[PHP]Array
(
[id] => 12
[introduction_title] => 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_title] =>
[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_title] =>
[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}
Mar 28 '08 #15
hsriat
1,654 Expert 1GB
See its working...
The problem lies in the code after this.
Mar 28 '08 #16
adamjblakey
133 100+
Sorry to bother you again, just a slight problem.

I managed to get this working fine but i totally forgot that i would need to add pagination into the page so i have had to edit this slightly.

Now i have it gives back a 5 for every match, Any Ideas?

[PHP]


$dataCollection = array();
$getData = mysql_query("SELECT * FROM users WHERE $stored AND status='active' ORDER BY id DESC");

$countrows = mysql_num_rows($getData);
$result = array();

$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($getData))
{
$i = 0;
foreach ($qualities as $quality)
{
if ($rowi['partner_'.$quality] == '1' && $r[$quality] == '1')
$i++;
}


// Get results from the database
if ($rowData = mysql_fetch_assoc($getData)) {
do {
$dataCollection[] = $rowData;
} while($rowData = mysql_fetch_assoc($getData));
}
$smarty->assign('match', $i);
array_push($result, $dataCollection);

}

// Assign the results to an array
$pagination = new pagination();
$dataPages = $pagination->generate($dataCollection, 10);
$smarty->assign('listing', $dataPages);
$smarty->assign('pagination', $pagination->links());
$smarty->assign('countrows', $countrows);

[/PHP]

Cheers,
Adam
Mar 31 '08 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Charles Alexander | last post by:
Hello I am new to php & MySQL - I am trying to retrieve some records from a MySQL table and redisplay them. The data in list form looks like this: Sample_ID Marker_ID Variation ...
5
by: build | last post by:
G'day All, I have a problem with this loop. There are a number of .txt files in 'myPath'. tmpFile = Dir(myPath & "\*.txt") 'PROCESS FOLDER Do Until tmpFile = "" <lottsa code> <too much to...
11
by: ritterhaus | last post by:
Just a simple bit of code to toggle between two state at intervals... import time for i in range(4): print 'On' time.sleep(1) print 'Off' time.sleep(1) .... SHOULD toggle On and Off four...
12
by: reynoldscraigr | last post by:
Hi All, hope someone can see what wrong here I have the following function function RemoveMenuFromHoldArray(menuName) { var i = 0; for (i=0;i<=MenusToHoldOpen.length-1;i++) { if...
43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
63
by: Aaron Ackerman | last post by:
What is the sytax for exiting a for loop in C#?
15
by: Mike Lansdaal | last post by:
I came across a reference on a web site (http://www.personalmicrocosms.com/html/dotnettips.html#richtextbox_lines ) that said to speed up access to a rich text box's lines that you needed to use a...
2
by: d3vkit | last post by:
Okay so I can NOT get my while loop to work. It's the most confusing thing I've ever come across. It was working fine and then suddenly, nothing. No error. The page just dies. I am using PHP5 with...
5
by: sgurukrupagmailcom | last post by:
Hi, I haven't come accross an elegant solution to a design problem that I show below. Have a look at the piece of code here: class Exc { Exc () { System.out.println ("Haribol"); }
1
by: JavaJon | last post by:
Hello, I'm Jon. I've recently picked up Java after using a "gimmick" programming language called GML ( Game Maker Language ). I've read a lot of tutorials and even a Java for Dummies *.pdf book....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.