0

I have three wards A,B,C in my database and 4 Employees (3 on ward A, 4 on B and 1 on C) and the below script prints out all the Wards correctly and prints out the 3 employees that works on ward A but nothing more (I:E it only compares against ward A and not the other 2) See Image

Can you see any obvious error in the order that the php is executing that causes this? I'm new to PHP so thats why I don't quite understand the syntax =)

while ( $row = mysql_fetch_array($wardNames) ) {

        echo("<tr><td><a href=\"javascript:displayWard('" . $row['Name'] ."')\">
        <div class='wardHeader'><div class='plus'></div><div class='wardName'><b>" . $row['Name'] . " </b></div>
        </div>
        </a></td></tr>");

        while ( $employeerow = mysql_fetch_array($Employees)) {//Only prints out employees on ward A and not B or C. Why?
            if($employeerow['Ward']==$row['Name']){
                echo("<tr><td>" . $employeerow['Name'] . "</td></tr>"); 
            }
        }
    }
5
  • 1
    show us your query... Commented May 2, 2013 at 19:06
  • 1
    There is no more support for mysql_* functions, they are officially deprecated, no longer maintained and will be removed in the future. You should update your code with PDO or MySQLi to ensure the functionality of your project in the future. Commented May 2, 2013 at 19:09
  • Just being pedantic, but echo is not a function, but a language construct, just like return. There is no need for parentheses here, they're only of use to overrule operator precedence when used in combination with language constructs Commented May 2, 2013 at 19:21
  • Thanks for the tip, will check it out =) Commented May 2, 2013 at 19:52
  • you don't need to do it like that.. a simple one query can do the job for you.. this way can exhaust your db resources. Commented May 2, 2013 at 22:10

1 Answer 1

1

After iterating over a resultset it's exhausted and thus not iterable anymore. Store it in an array before the outer loop:

$all_employees = array();
while($row = mysql_fetch_array($Employees)) {
    $all_employees[] = $row;
}

while ( $row = mysql_fetch_array($wardNames) ) {

    echo("<tr><td><a href=\"javascript:displayWard('" . $row['Name'] ."')\">
          <div class='wardHeader'><div class='plus'></div><div class='wardName'><b>" . $row['Name'] . " </b></div>
          </div>
          </a></td></tr>");

    foreach($all_employees as $employeerow)
        if($employeerow['Ward']==$row['Name']){
            echo("<tr><td>" . $employeerow['Name'] . "</td></tr>"); 
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.