Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Apr 2012
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts

    PHP foreach loop in SQL query

    Okay, I am not sure the best way to go about this but I have this SQL Query:
    Code:
    $sql="INSERT INTO games VALUES (NULL,'$name','".foreach($_POST['Platform'] as $platform) {$platform.",".}."','$developer','$publisher','$genre','$date','$esrb')";
    I have a form with check boxes for the 'Platform' and I am trying to add each selected check box to the table entry, separated by a ','.

    Would this be best or should I use a second table and join them using a 'JOIN'?

    Thanks
    --EffakT
    Last edited by EffakT; 04-23-2013 at 01:42 AM. Reason: Resolved

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,285
    Thanks
    23
    Thanked 611 Times in 610 Posts
    IMHO using a foreach loop with checkboxes will yield some empty slots and your values count will not match your column count and you'll get an error.

    I would set variables equal to yes [for it's on this platform] and no [not on this platform]. Example 3 platforms PS3, Xbox, and Wii. Columns in that order:
    PHP Code:
    <?php
    $platform 
    '';  // if you force form to have at lest one box checked this can be eliminated
    $ps 'no';
    $xbox 'no';
    $wii 'no';
    if(isset(
    $_POST['platform'])) $platform $_POST['platform'];  // this could be re-written, but OK the way it is

    if(IsChecked($platform'ps3')) $ps "yes";
    if(
    IsChecked($platform'xbox')) $xbox "yes";
    if(
    IsChecked($platform'wii')) $wii "yes";

    function 
    IsChecked($checkbox_name$value){
    if(!empty(
    $checkbox_name)){
        foreach(
    $checkbox_name as $item){
            if(
    $item == $value){
                return 
    true;
            }
        }
    }
    return 
    false;
    }

    $sql="INSERT INTO games VALUES (NULL,'$name', '$ps', '$xbox', '$wii', '$developer','$publisher','$genre','$date','$esrb')";
    ?>
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • Users who have thanked sunfighter for this post:

    EffakT (04-23-2013)

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Doesn't sound like multiple properties, sounds more like all entries into a single property. An implode would do that.

    That said, don't do that. If you have a variable amount of data, normalize the database to use a many to many relationship (or many to one logic with an enum if you don't want a separate lookup table), and it will eliminate all the anomalies that come with un-normalized data. Without doing it, you cannot find all users that for example own a PS3 without checking *every* record in the database. At least in sunfighters you could do that, despite having wasted space for columns not checked (and requiring a structural change if options are changed).
    Also, on a side note, checkboxes are only successful field if they are checked. There is no reference to any checkbox not checked when submitted to the web server.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #4
    New Coder
    Join Date
    Apr 2012
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sunfighter View Post
    IMHO using a foreach loop with checkboxes will yield some empty slots and your values count will not match your column count and you'll get an error.

    I would set variables equal to yes [for it's on this platform] and no [not on this platform]. Example 3 platforms PS3, Xbox, and Wii. Columns in that order:
    PHP Code:
    <?php
    $platform 
    '';  // if you force form to have at lest one box checked this can be eliminated
    $ps 'no';
    $xbox 'no';
    $wii 'no';
    if(isset(
    $_POST['platform'])) $platform $_POST['platform'];  // this could be re-written, but OK the way it is

    if(IsChecked($platform'ps3')) $ps "yes";
    if(
    IsChecked($platform'xbox')) $xbox "yes";
    if(
    IsChecked($platform'wii')) $wii "yes";

    function 
    IsChecked($checkbox_name$value){
    if(!empty(
    $checkbox_name)){
        foreach(
    $checkbox_name as $item){
            if(
    $item == $value){
                return 
    true;
            }
        }
    }
    return 
    false;
    }

    $sql="INSERT INTO games VALUES (NULL,'$name', '$ps', '$xbox', '$wii', '$developer','$publisher','$genre','$date','$esrb')";
    ?>
    Awesome, Thank you so much!


  •  

    Posting Permissions

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