Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1082839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:21:22+00:00 2026-05-16T22:21:22+00:00

I cannot seem to get foreach to work. Maybe I do not understand it

  • 0

I cannot seem to get foreach to work. Maybe I do not understand it correctly.

Here is my code:

$statement = "SELECT * FROM categories ORDER BY name ASC";
$query = mysql_query($statement)

...
...

$cals = array("sports","general","other","clubs");

foreach ($cals as $value)
{
/*  echo "<h3>".$value."</h3>"; 
*/  echo "<table width='100%'>";
while ($array = mysql_fetch_array($query))
    {
        if ($array['calendar'] == $value)
        {?>
<tr>
  <td><?php echo $array['name']; ?></td>
  <td><a onclick="update_form('<?php echo $array['name']; ?>', '<?php echo $array['calendar']; ?>')" href="#">Edit</a></td>
</tr>
<?php }
    }
    echo "</table><br />Value: $value";
}

The goal of this is to have the foreach change the if statement. I had planned for the if statement to say: if ($array['calendar'] == "sports") the first time, if ($array['calendar'] == "general") the second time, and so on. However, it shows all of the tables (in the source code), but no table rows are created after the first for each array value. For example, I correctly see the sports table, but I do not see any table rows for general, other, or clubs. There are records in that database that should appear in each of those. Could it be a problem with the while and if statements? If I manually set the $value in the if statement to one of the values in the array, it shows the correct records.

What am I missing?

Sample Data:

in the MySQL database –

categories table.
fields:

  • id
  • name
  • num_events
  • calendar
  • calendar_url

All of these fields except the calendar field has dummy data in it.

Currently, I have 5 records in there. Each one has a different calendar value. One is sports, one is clubs, and one is general. Depending on what value I place first in the array, it only shows that one table, of all of the values with whatever the first value in the array is.

Here is the source code from the resulting page:

<table width='100%'><tr>
  <td>test4</td>
  <td><a onclick="update_form('test4', 'sports')" href="#">Edit</a></td>
</tr>
<tr>
  <td>test5</td>

  <td><a onclick="update_form('test5', 'sports')" href="#">Edit</a></td>
</tr>
</table><br />Value: sports<table width='100%'></table><br />Value: general<table width='100%'></table><br />Value: other<table width='100%'></table><br />Value: clubs
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:21:22+00:00Added an answer on May 16, 2026 at 10:21 pm

    First, just a point of style. You might consider renaming your variable $query to something like $results. It’s holding the result of a query, not a query itself.

    The problem is that you’re not resetting $results. After the first table, you’ve iterated all the way through the array. When you get to the end, and there are no more rows to iterate over, mysql_fetch_assoc() returns false.

    So try this:

    foreach ($cals as $value)
    {
        while ($array = mysql_fetch_array($query))
        {
            if ($array['calendar'] == $value)
            {
                ?>
                <tr>
                    <td><?php echo $array['name']; ?></td>
                    <td><a onclick="update_form('<?php echo $array['name']; ?>', '<?php echo $array['calendar']; ?>')" href="#">Edit</a></td>
                </tr>
                <?php
            }
        }
        echo "</table><br />Value: $value";
        mysql_data_seek($query,0); // <=== Set the resultsets internal pointer back to zero (the first record).
    }
    

    The important bit is the mysql_data_seek() on the second to last line.

    You could also stick that mysql_data_seek() right before the while() line, if you prefer. You just need to make sure that for each iteration of the foreach loop, the array pointer is reset before you hit while().

    EDIT: s/reset/mysql_data_seek/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I cannot seem to get the quotes around this statement right. No matter what
I'm pulling my hair out, I cannot seem to get mouse events to work
I cannot seem to get user entered data into a table and then printed.
I cannot seem to get this date picker working at all. I know it
For some reason I cannot seem to get a ko.computed observable to compute when
I have this design which I cannot seem to get right, I would like
I recently started with PHP Object Oriented and I cannot seem to get this
I need a function, but cannot seem to get it quite right, I have
This is a very old problem, but I cannot seem to get my head
So obviously I am doing something wrong, but I just cannot seem to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.