Basically, I have a class that is used to add a note to the database and it links the note to a particular business.
The for loop goes through all the notes (since you can have multiple for one business) and adds them to an array that is then returned.
The trouble I am having is that the variable $i in the for loop is only recognized as 0 inside of the while, but it shows as the number it is supposed to outside the while.
I need the while to loop through the information in the DB, but I also need the For to get the number it is at. If there is a better way to do this, I would be glad to hear it, but this way worked before so I don't know what I could have done to break it. The code is below:

if($numRows > 0){
			$note = array();
			for($i = 0; $i < $numRows; $i++){
                               echo $i; // This shows the correct number
				while($n = $db->fetchArray($query)){
					echo $i; // This always shows as 0
					$note[$i]['noteID'] = $n['noteID'];
					$note[$i]["title"] = $n['title'];
					$name = $db->getUserName($n['author']);
					$note[$i]["author"] = $name['first'] . " " . $name['last'];
					$note[$i]["date"] = $n['added'];
					$note[$i]["text"] = $n['text'];
					echo "<pre>";print_r($note);echo "</pre>";
				}
			}
			return $note;

My brain is slightly fried, so the answer/problem might be obvious.
Thanks ahead of time!

Recommended Answers

All 6 Replies

The trouble I am having is that the variable $i in the for loop is only recognized as 0 inside of the while, but it shows as the number it is supposed to outside the while.

On the first iteration (when $i equals zero) BEFORE the while() you have NOT yet fetched any db records. As soon as the while() FINISHES executing the first time (again, when $i equals zero), then $query (assuming that it is a resource that "points" to the the result set of a previously executed sql command - a SELECT to be specific) is NO longer pointing to a valid result set. So on the next iteration of your for construct (when $i equals 1), then $db->fetchArray($query) NO longer returns an array.

So, instead of:

for(...){
 while(...){
 ...
 }
}

do:

while(...){
 for(...){
 }
}

Okay, I did that. It does get the required number of notes, but it gets one of them three times (the business I am using to test has three notes in the database). Here is my code:

$note = array();
			while($n = $db->fetchArray($query)){
				for($i = 0; $i < $numRows; $i++){
					echo "~" . $i . "~";
					$note[$i]['noteID'] = $n['noteID'];
					$note[$i]["title"] = $n['title'];
					$name = $db->getUserName($n['author']);
					$note[$i]["author"] = $name['first'] . " " . $name['last'];
					$note[$i]["date"] = $n['added'];
					$note[$i]["text"] = $n['text'];
					
				}
			}
			echo "<pre style='text-align:left;'>";print_r($note);echo "</pre>";
			return $note;

I can tell from the noteID that they are all the same. It also does the loop six times in total, so there is still something that we are missing.

...but it gets one of them three times

Well, you originally were (incorrectly) nesting a while() within a for() so you would clearly end up with "duplicate" records. Based on your last feedback, then perhaps you shouldn't even be using the for construct at all:

$note = array();
$i=-1;
			while($n = $db->fetchArray($query)){
					++$i;
					echo "~" . $i . "~";
					$note[$i]['noteID'] = $n['noteID'];
					$note[$i]["title"] = $n['title'];
					$name = $db->getUserName($n['author']);
					$note[$i]["author"] = $name['first'] . " " . $name['last'];
					$note[$i]["date"] = $n['added'];
					$note[$i]["text"] = $n['text'];

			}
			echo "<pre style='text-align:left;'>";print_r($note);echo "</pre>";
			return $note;

That worked great! I never realized you could do it that way but it makes sense.
Just out of curiosity and a want to learn, why is it incorrect to nest a while in a for? Is it because they are both types of loops?

Thank you!

Is it because they are both types of loops?

No. There is nothing inherently wrong with nesting looping constructs. In your case it is wrong because with what you originally posted it was going to make duplicates of each of the db records in your array, which is what you did NOT want (based on your follow-up comment).

Oh, okay. Thank you for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.