Jump to content

For Loop & While Loop WORKING, but Results are Wrong in Both


OldWest

Recommended Posts

I am working to echo the results in a while or for loop... Both of my sample codes work, but the results are wrong!

 

The while loop ONLY echos a result IF the first record in the postings table matches the id passed (does not display a result unless the first record has a match)

 

The if loop displays ALL listings with the same name (counts them all) so there are no unique listings!

 

<?php

$posts_by_city_sql = "SELECT * FROM postings WHERE id='$_GET[id]'";
$posts_by_city_results = (mysqli_query($cxn, $posts_by_city_sql)) or die("Was not able to grab the Postings!");

/* While Loop  */
while($posts_by_city_row = mysqli_fetch_array($posts_by_city_results)) {
	echo "<li><a href='posting_details.php?id=$posts_by_city_row[id]'>$posts_by_city_row[title]</a></li>";
}

/* For Loop  */
$posts_by_city_row = mysqli_fetch_array($posts_by_city_results);

for ($i=0; $i<sizeof($posts_by_city_row); $i++) {
echo "<li><a href='posting_details.php?id=$posts_by_city_row[id]'>$posts_by_city_row[title]</a></li>";
}

?>

 

Results with for loop (there are 7 total unique book names, but it's just counting the first match on id 7 times like below):

 

AJAX for Beginners

AJAX for Beginners

AJAX for Beginners

AJAX for Beginners

AJAX for Beginners

AJAX for Beginners

AJAX for Beginners

AJAX for Beginners

 

Link to comment
Share on other sites

You are misunderstanding what mysqli_fetch_array returns.

 

Each time it is called it returns an array containing each field from 1 row of your database and moves the pointer to the next row where it waits for the next call.

 

Your 'while' loop is likely only returning 1 row because that is all that matched your query.

 

Your 'for' loop doesn't make much sense because it loops through each column within a single row and simply displays the same data each time through.

Link to comment
Share on other sites

You are misunderstanding what mysqli_fetch_array returns.

 

Each time it is called it returns an array containing each field from 1 row of your database and moves the pointer to the next row where it waits for the next call.

 

Your 'while' loop is likely only returning 1 row because that is all that matched your query.

 

Your 'for' loop doesn't make much sense because it loops through each column within a single row and simply displays the same data each time through.

 

I very much appreciate your forwardness and accuracy.

 

It was of course my error. See I have two tables I am testing with:

 

Cities and Postings

 

Postings belong to cities.

 

And cities have postings of course.

 

In postings, I have field city_id (obviously to be a key for the city id field)

 

When I was running my query like so:

$posts_by_city_sql = "SELECT * FROM postings WHERE id='$_GET[id]'";

.

 

I was supposed to be SQLing ...

WHERE city_id='$_GET[id]

... (note city_id in place of id) ..

 

My script works as expected now.

Link to comment
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.