0

I have a mysql database called menus. I am able to query my database to obtain a recordset accordingly. I do NOT want to write a query for each season. My problem: I want to write a php if statement within body to echo a message if the season field CONTAINS 2.

table menu

id | item (varcar)  | season (set)
1  | fresh lemonade | 2,3
2  | smoothie       | 2
3  | cafe latte     | 4

My query works fine

mysql_select_db($database_rest, $rest); $query_menus = "SELECT * FROM menus";

$menus = mysql_query($query_menus, $rest) or die(mysql_error());

$row_menus = mysql_fetch_assoc($menus);

$totalRows_menus = mysql_num_rows($menus);

I can write a php if to work where recordset field = 2 that works.

<?php echo $row_menus['item']; ?>: <?php
      if (@$row_menus['season'] == 1)
        {
        echo "Winter";
        }
      else if (@$row_menus['season'] == 2)
        {
        echo "Spring";
        }
      else if (@$row_menus['season'] == 3)
        {
        echo "Summer";
        }
      else if (@$row_menus['season'] == 4)
        {
        echo "Fall";
        }
      ?> 

Result Shows:

fresh lemonade: Spring
smoothie: Spring
cafe latte: Fall

I want to write php so that if season CONTAINS 2 (example: if (@$row_menus['season'] CONTAINS 1) echo Spring, etc for each. so that result would look like this:

fresh lemonade: Spring
fresh lemonade: Summer
smoothie: Spring
cafe latte: Fall
3
  • 2
    Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. Commented Apr 26, 2013 at 14:57
  • I don't understand your question. Are you looking for a "switch" construct? Commented Apr 26, 2013 at 14:58
  • I am not looking to switch anything. I am wanting to show Winter if the season field contains 1, Spring if season field contains 2, Summer if season field contains 3, and Fall if season field contains 4. Commented Apr 26, 2013 at 15:53

2 Answers 2

1

First of all, it's time to upgrade those mysql_* functions! Second, there's better ways to set up the relationship between the item and its seasons. However, I'll let you look into that stuff.

$query_menus = "SELECT * FROM menus";
$menus = mysql_query($query_menus, $rest) or die(mysql_error());

$seasons_array = array('All Seasons', 'Winter', 'Spring', 'Summer', 'Fall');

while($row_menus = mysql_fetch_array($menus))
{
    $item = $row_menus['item'];
    $season = $row_menus['season'];

    if(strpos($season, ',') !== false)
    {
        $seasons = explode(',', $season);

        // To show the item and season on separate rows
        foreach($seasons as $s)
            echo $item . ': ' . trim($seasons_array[(int)$s]);

        // To show the item, and then a list of seasons
        echo $item . ': ';
        foreach($season as $k => $s)
            echo trim($seasons_array[(int)$s]) . (($k + 1) == count($season) ? '' : ', ');
    }
    else
    {
        echo $item . ': ' . $seasons_array[(int)$season];
    }
}

I didn't test this, but it should work for you. Try it out, and let us know.

EDIT: I updated it so that you can list the items on separate rows, or list the item followed by each season.

Sign up to request clarification or add additional context in comments.

8 Comments

Please, do advice everybody to move on to mysqli_*
@iroegbu I did in my opening line. I suppose I could have been more specific, though. :)
I tried this. what it shows is a complete list of items but nothing for the season. I'm wondering how seasonal number (1) from the database gets translated into Winter, etc. on the page
That's where the $seasons_array comes into play. It uses the number from the database, and pulls the name from the array based on the number.
OK Kacey I think i get this. However, I still only get a list of items with : and nothing listing for actual season. Would i throw a wrench in this option if i have a season equal to 0 for "All Seasons"?
|
0

You have two choices:

  1. use PHP to split the field into an array and work through that
  2. use multiple rows where season only holds one value

Using PHP, you could use $season=explode(',',$row_menus['season']); and then use a foreach statement to iterate through $season

The problem you have is that PHP interprets 2,3 as a string. When it's converted into a number, it appears as 2

Try using multiple rows for the season field:

id | item (varcar)  | season
1  | fresh lemonade | 3
2  | smoothie       | 2
3  | cafe latte     | 4
4  | fresh lemonade | 2

This will also help where you want to select all rows where season=2

Finally, as Tobias said, don't use the mysql_ functions. Use newer functions which are supported

1 Comment

Thanks DaveyBoy. Using multiple rows is not an option for me, as there are hundreds of menu items, and adding them multiple times for each season is just not an option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.