Click to See Complete Forum and Search --> : Varaiable is keeping a previous value (in PHP)


Brendan Nolan
02-12-2003, 10:04 PM
Hi... me again!!

Pulling stuff from a databese. I am inside a while loop set up like so...

while ($myrow = mysql_fetch_array($result)){

then inside it I am doing a few SQL things to get some contact details from a table...

$contactsql = "SELECT phone, mobile_phone, after_hours_phone, fax FROM people WHERE person_id = '$persid' AND person_name = 'Main'";

$contactresult = mysql_query($contactsql);

$pphone = mysql_result($contactresult,"phone");
$pmob = mysql_result($contactresult,"mobile_phone");
$pah = mysql_result($contactresult,"after_hours_phone");
$pfax = mysql_result($contactresult,"fax");

if ($pphone != ""){
$ph = "Ph: " . $pphone;
}
else if ($pmob != ""){
$ph = "Mob: " . $pmob;
}
else if ($pah != ""){
$ph = "A/H: " . $pah;
}
else if ($pfax != ""){
$ph = "Fax: " . $pfax;
}


As you can see, I am pulling all the contact numbers from the table, then chosing the first number the script comes accross as the number to use. The problem is that all of the contact numbers are optional, and for some reason when the script gets to a listing without a Phone number, it uses the phone number of the previous listing. That is bad.

I have even tried resetting all of those variables mentioned above by going...

$pphone = "";
$pmob = "";
$pah = "";
$pfax = "";

at the start, so each time it goes through the loop it has all the variables equalling ""





Ummm... if anyone understands my confusing explanation, any ideas?

pyro
02-12-2003, 10:25 PM
If possible can you remove the while loop? It looks like with what you are quering, you shouldn't need it...That being said, I can't see the rest of your code, so you probably have a reason for it. :D

Brendan Nolan
02-12-2003, 10:36 PM
Okay... this is the code as it is at the moment... For the previous post I changed some variables, as they had names that did not describe them.. so don't get confused - the "namesql" and "nameresult" should of course be something like "getcontactssql" and "contactsresult".

I was afraid to show the whole code in public because I have see people call other people's code "Dog S***" :)

<?php
$sql = "SELECT * FROM entities ORDER BY entity_name ASC";
$result = mysql_query($sql);

while ($myrow = mysql_fetch_array($result)){

$entidtoget = $myrow["entity_id"];
//echo $entidtoget . "<br>";

$getstuffsql = "SELECT entity_department_id FROM entity_departments WHERE entity_id = '$entidtoget' and entity_department_name = 'Main'";
$phnumbs = mysql_query($getstuffsql);
$entdepid = mysql_result($phnumbs,"entity_department_id");
$getstuffsql = "SELECT person_id FROM entity_department_members WHERE entity_department_id = '$entdepid'";
$phnumbs = mysql_query($getstuffsql);
$persid = mysql_result($phnumbs,"person_id");
//echo $entdepid . " : " .$persid . "<br>";


$namesql = "SELECT phone, mobile_phone, after_hours_phone, fax FROM people WHERE person_id = '$persid' AND person_name = 'Main'";


$pphone = "";
$pmob = "";
$pah = "";
$pfax = "";

$nameresult = mysql_query($namesql);
$pphone = mysql_result($nameresult,"phone");
$pmob = mysql_result($nameresult,"mobile_phone");
$pah = mysql_result($nameresult,"after_hours_phone");
$pfax = mysql_result($nameresult,"fax");


if ($pphone != ""){
$ph = "Ph: " . $pphone;
}
else if ($pmob != ""){
$ph = "Mob: " . $pmob;
}
else if ($pah != ""){
$ph = "A/H: " . $pah;
}
else if ($pfax != ""){
$ph = "Fax: " . $pfax;
}

printf("<tr>\n <td><a href=\"#\">%s</a></td>\n <td>%s</td>\n </tr>", $myrow["entity_name"], $ph);

}

?>


Umm... this give you any insight into where I am going wrong?
The first bit contained in the while loop is just working out who's contact details we are getting... If you need I can send you a schema of the database.

Thanks for helping me out :)

pyro
02-12-2003, 10:57 PM
Just a wild shot here. Try changing this

$pphone = "";
$pmob = "";
$pah = "";
$pfax = "";

$nameresult = mysql_query($namesql);
$pphone = mysql_result($nameresult,"phone");
$pmob = mysql_result($nameresult,"mobile_phone");
$pah = mysql_result($nameresult,"after_hours_phone");
$pfax = mysql_result($nameresult,"fax");

to this...

$pphone = "";
$pmob = "";
$pah = "";
$pfax = "";

$nameresult = mysql_query($namesql);
while($row = mysql_fetch_array($nameresult))
{
$pphone = $row[phone];
$pmob = $row[mobile_phone];
$pah = $row[after_hours_phone];
$pfax = $row[fax];
}

Brendan Nolan
02-12-2003, 11:09 PM
That gives me a parse error... :(

I am trying to figure out why it would possibly give the code you gave me a parse error! There doesn't seem to be anything wrong with it... I tried a few variations on that - - without luck - all parse errors!

Sigh.

pyro
02-12-2003, 11:25 PM
Unfortunately, I think I've exhausted myself on this one. Sorry. I will give advice as to where you can find you answer. Head over to http://phpbuilder.com/board and ask them. Someone is sure to know.

Brendan Nolan
02-12-2003, 11:30 PM
Okay then, thanks anyway -- And I am heading to that forum now!