SitePoint Sponsor

User Tag List

Results 1 to 11 of 11

Thread: checked?

  1. #1
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,582
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    checked?

    I have a variable in a mysql database (amenities) which holds selected values from a checkbox
    below is when I echo the variable to the screen,
    PHP Code:
        [amenities] => 2,
    The numbers (separated by a ,) are values of a bunch of checkboxes
    HTML Code:
    <input type="checkbox" name="amenities[]" value="0" >Washer/Dryer<br>
    <input type="checkbox" name="amenities[]" value="1" >Kitchen Appliances<br>
    <input type="checkbox" name="amenities[]" value="2" >Patio<br>
    <input type="checkbox" name="amenities[]" value="3" >BBQ Grill<br>
    <input type="checkbox" name="amenities[]" value="4" >Balcony<br>
    how do I make it so that if the value of the checkbox is in the list, the box will be checked?

    Thanks...
    "Oh, and Jenkins--apparently your mother died this morning."

  2. #2
    SitePoint Enthusiast burNMind's Avatar
    Join Date
    Jan 2009
    Location
    UK
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Instead of saving the selected values as plain text in the db, I would suggest to store them as a serialised array, since it's going to be more flexible. When you retrieve them, you can unserialise them, and search in them for the value of a specific checkbox. If it's present, you can mark it as selected.
    blogging @ burnmind.com

  3. #3
    SitePoint Addict
    Join Date
    Sep 2005
    Posts
    267
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Assuming your variable is an array, you could use in_array

    Code:
    if(in_array(0,$amenities)) echo "checked";

  4. #4
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,582
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    thanks
    "Oh, and Jenkins--apparently your mother died this morning."

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,807
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Taken from the PHP comments for in_array:
    Be aware of oddities when dealing with 0 (zero) values in an array...

    This script:
    PHP Code:
    <?php
    $array 
    = array('testing',0,'name');
    var_dump($array);
    //this will return true
    var_dump(in_array('foo'$array));
    //this will return false
    var_dump(in_array('foo'$arrayTRUE));
    ?>
    It seems in non strict mode, the 0 value in the array is evaluating to boolean FALSE and in_array returns TRUE. Use strict mode to work around this peculiarity.
    This only seems to occur when there is an integer 0 in the array. A string '0' will return FALSE for the first test above (at least in 5.2.6).
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  6. #6
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,582
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    ok, going to make it a serialized array, I have a few questions though
    when I go like this
    PHP Code:
            $amenities $_POST['amenities'];
            
    $amenities serialize($amenities);
            echo 
    $amenities
    I get

    a:2:{i:0;s:1:"0";i:1;s:1:"1";}

    But when I echo the original array
    PHP Code:
    echo "<pre>"print_r ($_POST); echo "</pre>"
    I get
    [amenities] => Array
    (
    [0] => 0
    [1] => 1
    )

    why is the serialized array show a string type
    http://php.net/manual/en/function.serialize.php
    shouldnt it be an integer? (the values can only be integers; 0, 1, 2, 3, and 4)

    Also, how do I store the serialized array into the database (datatype?)

    Thanks...
    "Oh, and Jenkins--apparently your mother died this morning."

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,807
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lukeurtnowski View Post
    ok, going to make it a serialized array, I have a few questions though
    when I go like this
    PHP Code:
            $amenities $_POST['amenities'];
            
    $amenities serialize($amenities);
            echo 
    $amenities
    I get

    a:2:{i:0;s:1:"0";i:1;s:1:"1";}

    But when I echo the original array
    PHP Code:
    echo "<pre>"print_r ($_POST); echo "</pre>"
    I get
    [amenities] => Array
    (
    [0] => 0
    [1] => 1
    )

    why is the serialized array show a string type
    http://php.net/manual/en/function.serialize.php
    shouldnt it be an integer? (the values can only be integers; 0, 1, 2, 3, and 4)

    Also, how do I store the serialized array into the database (datatype?)

    Thanks...
    It isn't really a string type more like a string representation of your array. When you call unserialize($amenities) you will see you get back the exact array you dumped using print_r($_POST['amentities'])

    To put that into perspective:

    PHP Code:
    <?php
    $amentities 
    = array(012);
    echo 
    '<pre>'print_r($amentities); echo '</pre>';
    $amentities serialize($amentities);
    echo 
    $amentities;
    $amentities unserialize($amentities);
    echo 
    '<pre>'print_r($amentities); echo '</pre>';
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  8. #8
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,042
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Surprising that everyone has failed to mention that serializing data and stuffing a representation of multiple values into a single column breaks first normal form. Generally speaking you should not be serializing data and tossing it into a single column. What happens when you want to filter entities by specific amenities? I will tell you what if your not the person who has to deal with that problem the person who does will curse your incompetence. There are only very rare cases that multiple values should be stuffed into a single column and even those instances would be debatable. Read up on proper normalization so you don't end up like one of the many posters on this very forum dealing with a problem later down the road that is the result of not properly normalizing the database. If you need some help I'm sure people over in the database section of the forum will set you straight. Especially if you believe first normal form is insignificant.

    Instead of saving the selected values as plain text in the db, I would suggest to store them as a serialised array, since it's going to be more flexible. When you retrieve them, you can unserialise them, and search in them for the value of a specific checkbox. If it's present, you can mark it as selected.
    Never mind… I see where you got that idea from. Completely ignore this. On the contrary serialized data in a relational database schema is a nightmare to manage. Especially when it comes to filtering, aggregating, and sorting by segments data stored within the serialized array that would be better off represented with 1:m or m:n relationship.

    In this specific case it would seem appropriate to use a m:n relationship. One table for the amenities and another to relate an amenity to another table (entity). I've dealed with my fair share of real estate system in all cases this is how the data is stored in the simplest forms – not serialized arrays.

    listing
    - id

    listing_amenity
    - listing_id
    - amenity_id

    amenity
    - id
    - name

    You could even use surrogate key of the amenity table if that is what floats your boat but I do prefer using surrogate keys for many reasons. This architecture is now flexible enough to filter, sort, and group by specific amenities. Where as serializing crap and throwing it into a single column will be a gigantic clusterf**k down the road in regards to performance, accuracy, flexibility, and maintainability.

    I know there are always exceptions but in general never serialize data and toss it into a single column. Especially, when you are just starting out. Once you know some of rules and what not than perhaps in some cases where a lot of magic is needed it is "ok"… ish.

    Also, in this day and age if you find yourself working a lot with key/value pairs than perhaps a relational database is not the best option. Perhaps a NoSQL storage facility is better like MongoDB. Something to consider.
    The only code I hate more than my own is everyone else's.

  9. #9
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,807
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Not all that surprising really... there are times where normalizing your data, doesn't exactly make sense. What if I don't need to sort/search my information? From his array layout, sorting/searching it would be meaningless from a database standpoint anyway.

    Think of it this way, let's say I have a site that allows you to customize a few things: Layout, Thread Display, Searches per Page, etc. Why would I want to store that in a separate table? I don't need to search it, and when I likely need to use it, I'll need more than just one of the values, so I can't just grab the one I want. Storing that either serialized or in a delimited string makes perfect sense.
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  10. #10
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,582
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I didn't think about that before, better have a separate table for the amenities, cause ill eventually want to sort by them
    "Oh, and Jenkins--apparently your mother died this morning."

  11. #11
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,042
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by cpradio View Post
    Not all that surprising really... there are times where normalizing your data, doesn't exactly make sense. What if I don't need to sort/search my information? From his array layout, sorting/searching it would be meaningless from a database standpoint anyway..
    You never know what will be needed in the future. Best to plan ahead.

    Quote Originally Posted by cpradio View Post
    Think of it this way, let's say I have a site that allows you to customize a few things: Layout, Thread Display, Searches per Page, etc. Why would I want to store that in a separate table?
    I work with Drupal quite of bit and yes the system does promote mass serialization of these things. I don't particularly like it but see why it is being done given the complexity of the data and flexibility the system offers. Though that isn't to say that mass serialization doesn't lead to major problems. Which is probably why Drupal 8 uses YAML based file storage for those cases instead of tossing that type of data in the db like it us to in versions prior.

    There isn't any reason though why this simple data can't be stored in a separate table using proper normalization. It is no where as near as complex as many of Drupal data structures that get stuffed in a single database column using serialization.
    The only code I hate more than my own is everyone else's.


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
  •