Sql query & foreach

I’m trying to pull data from a range of rows in a table (the table called “pages” & the column called “name”), arrange the results alphabetically, and output the results along with the corresponding id (primary key). the output is supposed to be of the form (simplified for clarity):

<?php foreach ($meals as $name): ?>
  <?php echo '<a href="?page=meal'.$id.'" name="meal['.$id.']" title="'.$name.'">'.$name.'</a>
  <input id="mealSP-'.$id.'" name="mealSP['.$id.']" />
  <label for="mealSP-'.$id.'">Single Portion</label>
  <input id="mealFP-'.$id.'" name="mealFP['.$id.']" />
  <label for="mealFP-'.$id.'">Family Pack</label>'
<?php endforeach ?>

here’s the PHP that fetches the array, minus error-checking:

$takebake = mysqli_query($link, 'SELECT id, name FROM pages WHERE id > 200 AND id < 401');
while ($row = mysqli_fetch_array($takebake)) {
	$meals[] = $row['name'];
	sort ($meals);

That gets the meal name to appear in the output, but I haven’t got a clue how to get the ids in there – especially in the sort order from the names.

Thanks for any advice on this,

you can do the sorting right in the query

     , name
  FROM pages
 WHERE id > 200
   AND id < 401
    BY name[/COLOR]

that keeps each id together with its name, as the entire result rows are sorted

Because Rudy already solved your sorting problem you no longer need to use the sort() function.

In the case that you still need it sort AFTER you are done fetching the data

while ($row = mysqli_fetch_array($takebake)) {

    $meals[] = $row['name'];
sort ($meals);

Your foreach should look like this:

<?php foreach ($meals as $name): ?>

  <?php echo '<a href="?page=meal'.$name['id'].'" name="meal'.$name['id'].'" title="'.$name['name'].'">'.$name['name'].'</a>';

<?php endforeach ?>

First, let me thank you both for the speedy reply. I tried the examples, and I’m getting some pretty strange results. The values should be more like ID 201, NAME Macaroni & Cheese. Any Ideas?

This might be coming from here

while ($row = mysqli_fetch_array($takebake)) {

    $meals[] = $row['name'];

the array meals only has the name element but not the ids.

So by doing this instead:

while ($row = mysqli_fetch_array($takebake)) {
    $meals[] = $row;

It should fix it.

Yay! Now if I can just figure out how to get the same info in the form processor …

What do you mean by ‘form processor’ here? But the correction in your form in the first post should look like this now if you haven’t managed it yet:

foreach ($meals as $name):
    echo '<a href="?page=meal'.$name['id'].'" name="meal['.$name['id'].']" title="'.$name['name'].'">'.$name['name'].'</a> 
    <input id="mealSP-'.$name['id'].'" name="mealSP['.$name['id'].']" /> 
    <label for="mealSP-'.$name['id'].'">Single Portion</label> 
    <input id="mealFP-'.$name['id'].'" name="mealFP['.$name['id'].']" /> 
    <label for="mealFP-'.$name['id'].'">Family Pack</label>';


the form processor is the php that will handle the form submission. i’ll have to ensure that each of these inputs has equivalent data handling in the processor, but i’ll cross that bridge after i get this working.

it’s getting closer, but the output is echoing twice. i sure don’t see why – the code for the output appears only once, and the fetch query doesn’t have anything in it that should cause it to iterate twice, at least, not to my novice eyes.

you can also see a more complete version of the form. i’ve turned off the JavaScript that runs client side checking & operates the accordion, while i’m testing. As you can see, the output appears twice here, too. :frowning:

I just tried replacing




but I’m still getting double results.

I wound up posting to a different forum. The answer can be found here.

Thanks again for helping out.