Jump to content

Recommended Posts

For some reason, this chunk of code is not looping through the way it should. When I break each query up and run in by itself it works fine, but I need it to loop through

 

<?php
# If the user is not an admin, only query specific items
if($_SESSION['accessLevel'] < "3") {
# Search CASES table for Client Reference
if($field == "clientReference") {
	$sql = "SELECT caseID FROM cases WHERE $field = '$query'";
	$result = $database->query($sql);
	while($caseID = $database->fetchAssoc($result)) {
		$sql = "SELECT addressID, serveeID FROM serviceJob WHERE caseID = '$caseID[caseID]'";
		$result = $database->query($sql);
		while($row = $database->fetchArray($result)) {
			$sql = "SELECT servees.serveeName, addresses.serviceAddress FROM addresses, servees WHERE serveeID = '$row[serveeID]'  AND addressID = '$row[addressID]'"; # THIS IS WHAT NEEDS TO BE LOOPING THROUGH EACH $row[serveeID] BUT IT ONLY RETURNS THE 1ST RESULT
			$result = $database->query($sql);
			while($row=$database->fetchAssoc($result)) {
				echo $row['serveeName'] . " at " . $row['serviceAddress'] . "<br/>";
			} # End display loop
		} # END $row loop
	} # End caseID loop
} # End the client Reference IF statement
} # End the access level condition
?>

Link to comment
https://forums.phpfreaks.com/topic/137738-nested-query/
Share on other sites

Ok, after some more digging, here is what I have found:

 

<?php
	while($caseID = $database->fetchAssoc($theResult)) {
		$sql = "SELECT addressID, serveeID FROM serviceJob WHERE caseID = '$caseID[caseID]'";
		$result = $database->query($sql);
		$nums = $database->numRows($result); // Returning 7
		$row = $database->fetchAssoc($result);
		$i = 1;
		while($i <= $nums) {
			echo $i . ". " .$row['addressID'] . " is the addressID. " . $row['serveeID'] . " is the serveeID<br/>"; // This is looping 7 times, but it isn't rolling through the array, it's the 1st result 7 times
			$i++;
		} # END $row loop

?>

Link to comment
https://forums.phpfreaks.com/topic/137738-nested-query/#findComment-719976
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.