www.webdeveloper.com
Results 1 to 7 of 7

Thread: Varaiable is keeping a previous value (in PHP)

  1. #1
    Join Date
    Dec 2002
    Location
    Australia
    Posts
    32

    Varaiable is keeping a previous value (in PHP)

    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...
    PHP Code:
    $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?
    Last edited by Brendan Nolan; 02-12-2003 at 11:16 PM.

  2. #2
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    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.

  3. #3
    Join Date
    Dec 2002
    Location
    Australia
    Posts
    32

    You practically asked for this....

    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 Code:
    <?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

  4. #4
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    Just a wild shot here. Try changing this

    PHP Code:
    $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...

    PHP Code:
    $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];


  5. #5
    Join Date
    Dec 2002
    Location
    Australia
    Posts
    32
    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.

  6. #6
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    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.

  7. #7
    Join Date
    Dec 2002
    Location
    Australia
    Posts
    32
    Okay then, thanks anyway -- And I am heading to that forum now!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles