SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Member
    Join Date
    Apr 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need a little help here

    I am working on a page and not sure how to make this work.

    In a form in form.php I have an area of array checkboxes called food(), where the user can check types of food.
    The checkboxes are added from a table called foodtypes and includes food_id and food_type
    Example 1 is vegetarian, 2 is meat, 3 is fast food, and so on...

    Then I have radio buttons with continents of the world and also one for the whole world.
    Theses radio buttons are inside the form and there are world, europe, africa, asia, north america, and so on.

    The user can check what kind of food they are after (one or several).
    They also click one of the radio buttons to decide what part of the world (or the button for the whole world).

    When they click the submit button they will end up on a new page called show.php
    The user have an id in a user_table and the continent selection is stored in the user db.
    The food type selection is stored in another table called food_sel. Here I store the user_id and the selection they made by the food_id.

    On the new page (show.php) I want the user to see all the choices that are available in the database in a list.
    So, if they picked Asia and vegetarian and fast food they will get a list of all the restaurants in the database that serve fast food in asia and also the ones that serve vegetarian food in asia.

    All the restaurants in the database contains only one type of food.
    So I need to get the rows that are selected echoed in a list, but I can not figure out how to make this call to the mysql in the right way.

    Any clues?

    The tables:

    foodtypes: food_id | food_type
    food_sel: user_id | food_id
    user_table: user_id | username | continent_sel

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,131
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Code:
    SELECT * FROM user_table u LEFT JOIN food_sel s ON u.user_id = s.user_id LEFT JOIN foodtypes t ON s.food_id = t.food_id WHERE food_id IN (1, 3) AND u.continent_sel = 'asia'
    You would want to take your $_POST data for the food types (assuming the value you receive is the id) and implode it
    PHP Code:
    $selectedFoodTypes implode(','$_POST['food']);
    $selectedContinent filter_var($_POST['continent'], FILTER_SANITIZE_STRING);
    $query "SELECT * FROM user_table u LEFT JOIN food_sel s ON u.user_id = s.user_id LEFT JOIN foodtypes t ON s.food_id = t.food_id WHERE food_id IN (" $selectedFoodTypes ") AND continent_sel = '" $selectedContinent "'"

  3. #3
    SitePoint Member
    Join Date
    Apr 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But whaif they don't select asia and more than two types? Is there a way to get the info from the selections and the array from the form?

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,131
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by coverman View Post
    But whaif they don't select asia and more than two types? Is there a way to get the info from the selections and the array from the form?
    I updated my response to include a PHP snippet of what you would be looking to do to build your Query (I accidentally posted my response before completing it)

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,131
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Here is a slightly better approach (as it will filter ALL input used in your query):
    PHP Code:
    $input filter_input_array (INPUT_POST, array('continent' => FILTER_SANITIZE_STRING'food' => array('filter' => FILTER_VALIDATE_INT'flags'  => FILTER_REQUIRE_SCALAR)));
    $query "SELECT * FROM user_table u LEFT JOIN food_sel s ON u.user_id = s.user_id LEFT JOIN foodtypes t ON s.food_id = t.food_id WHERE food_id IN (" implode(','$input['food']) . ") AND continent_sel = '" $input['continent'] . "'"

  6. #6
    SitePoint Member
    Join Date
    Apr 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oops, that was a lot of code to make this. And I'm not sure I understand all of it. And not sure if I described it too bad.
    First of all, when the user pick a continent, this is not an array. This is just 6 parts of the world or they want all countries in the world. So, this part will either be asia, north america, south america, europe, africa, oceania or ALL countries.
    Maybe that one is better to make as an IF control first and make two different queries if they select one continent or all?

    And what was that 'flags' part?

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,131
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Okay, yes, you will need an IF statement to run two different queries (one for ALL continents and one for the selected continent).

    As for the flag FILTER_REQUIRE_SCALAR, it requires the values of the food array to be a single value (in other words, it can't be another array or object).
    http://php.net/manual/en/filter.constants.php
    http://php.net/manual/en/function.is-scalar.php (see comments)
    A scalar is a single item or value, compared to things like arrays and objects which have multiple values. This tends to be the standard definition of the word in terms of programming. An integer, character, etc are scalars. Strings are probably considered scalars since they only hold "one" value (the value represented by the characters represented) and nothing else.

  8. #8
    SitePoint Member
    Join Date
    Apr 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So, what would the query look like if they select just one continent that is not in an array? It's just a number between 1 and 6.

  9. #9
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,131
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $input filter_input_array (INPUT_POST, array('continent' => FILTER_SANITIZE_STRING'food' => array('filter' => FILTER_VALIDATE_INT'flags'  => FILTER_REQUIRE_SCALAR)));
    if (
    $input['continent'] == 1// All continents
      
    $query "SELECT * FROM user_table u LEFT JOIN food_sel s ON u.user_id = s.user_id LEFT JOIN foodtypes t ON s.food_id = t.food_id WHERE food_id IN (" implode(','$input['food']) . ")";  
    else
      
    $query "SELECT * FROM user_table u LEFT JOIN food_sel s ON u.user_id = s.user_id LEFT JOIN foodtypes t ON s.food_id = t.food_id WHERE food_id IN (" implode(','$input['food']) . ") AND continent_sel = '" $input['continent'] . "'"

  10. #10
    SitePoint Member
    Join Date
    Apr 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is just one thing I get stuck on when looking:
    PHP Code:
    INPUT_POST, array('continent' 
    Is that one supposed to be there when the continents isn't an array?
    Or am I just lost here?

  11. #11
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,131
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Yes. That array is simply defining the variables you expect to get from your form, then it "validates" them.

    INPUT_POST is a constant telling the function where to read the form values from.

  12. #12
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,131
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    In fact, you can make the filter better by using:

    PHP Code:
    $input filter_input_array (INPUT_POST
                array(
    'continent' => array(
                            
    'filter' => FILTER_VALIDATE_INT
                            
    'options' => array('min_range' => 1'max_range' => 6)
                         ), 
    'food' => array(
                            
    'filter' => FILTER_VALIDATE_INT
                            
    'flags'  => FILTER_REQUIRE_SCALAR
                         
    )
                 )
    );

    if (
    $input['continent'] == 1// All continents
      
    $query "SELECT * FROM user_table u LEFT JOIN food_sel s ON u.user_id = s.user_id LEFT JOIN foodtypes t ON s.food_id = t.food_id WHERE food_id IN (" implode(','$input['food']) . ")";  
    else
      
    $query "SELECT * FROM user_table u LEFT JOIN food_sel s ON u.user_id = s.user_id LEFT JOIN foodtypes t ON s.food_id = t.food_id WHERE food_id IN (" implode(','$input['food']) . ") AND continent_sel = '" $input['continent'] . "'"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •