Re: Problem with if statement and while loops
| From: | Steve Buehler | Date: | Fri, 24 May 2002 19:25:46 +0000 |
| Subject: | Re: Problem with if statement and while loops | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-99136@lists.php.net to get a copy of this message | ||
At 12:29 PM 5/24/2002 -0500, Steve Buehler wrote:
I am having trouble with the following function. What it should do is to check one table for team_id's. Than it goes to another table and gets all rows with that team_id. If the team_id is in the new table, it should do one thing, else it should do something else. Can somebody look at this code and see if they can find out where my problem is besides in my inexperience? It always goes to the else statement inside the if statement.
function searchbyteamname($team_name){
GLOBAL $PHP_SELF;
$result=mysql_query("SELECT team_id,name FROM team WHERE name like '%$team_name%' AND deleted NOT LIKE '1' ORDER BY 'name'");
$i=1;
while (($row=mysql_fetch_object($result))){
if(!($result1=mysql_query("SELECT * FROM team_season WHERE team_id = '$row->team_id' AND deleted NOT LIKE '1'"))){
echo "$i. $row->name";
$i++;
}else{
while(($row1=mysql_fetch_object($result1))){
echo "$i. <a href=\"$PHP_SELF?action=edit&team_id=$row->team_id\">$row->name</a>";
echo getdivisionnameshort($row1->div_id);
echo getseasonnameshort($row1->sea_id)\n";
$i++;
}
}
}
}
Ok. Nobody (that has seen this) was able to and/or wanted to solve this or they just haven't had the time to yet. :) Anyway, I have found a solution to the problem and am listing it here so that other people can learn. That is what this list is about, right? I just used mysql_num_rows() to find out if it returned something or not. Here is the code that works:
function searchbyteamname($team_name){
GLOBAL $PHP_SELF;
$result=mysql_query("SELECT team_id,name FROM team WHERE name like '%$team_name%' AND deleted NOT LIKE '1' ORDER BY 'name'");
$i=1;
while (($row=mysql_fetch_object($result))){
$result1=mysql_query("SELECT * FROM team_season WHERE team_id = '$row->team_id' AND deleted NOT LIKE '1'");
$num_results = mysql_num_rows($result1);
if(!$num_results){
echo "$i. $row->name";
$i++;
}else{
while(($row1=mysql_fetch_object($result1))){
echo "$i. <a href=\"$PHP_SELF?action=edit&team_id=$row->team_id\">$row->name</a>";
echo getdivisionnameshort($row1->div_id);
echo getseasonnameshort($row1->sea_id);
$i++;
}
}
}
}