Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2009
    Location
    UK: Oxford
    Posts
    1,256

    Exploding Binary Feilds

    Evening all,

    I need to get values from a table column. Because this site depends heavily on inserting data into a database, I'm trying to make PHP do has much work as possible, and keeping the database fields as small as possible.

    I'm storing a column for users privacy settings as "101101110111". Each 1 or 0 representing a setting. How would you explode this? Or do you have a better pattern to suggest? I thought of either putting a , or .between each value but I think that would look untidy =[

    I dont think explode works like this:

    Code:
    //$work['bit_field'] Is the database field
    $pizza  = $work['bit_field'];
    
    //Will exploding on "" work?
    $pieces = explode("", $pizza);
    I look forward to reading your suggestions!
    Garbott Ltd - Professional web development & consultancy services
    Oxford.Hosting - Prestigious shared, cloud, dedicated and gaming hosting solutions.

  2. #2
    Join Date
    Jan 2004
    Posts
    593
    First off, if you are storing 1's and 0's in the db to try and save resources instead of storing their correct values and converting them in php... you're missing the point of the database setup. Databases are very efficient. Use it's efficiency.

    If you have a string like 1001110101, then you'll need to break it up before storing for best results.

    For example, 1-0-0-1-1-1-0-1-0-1 would be a great way to store it in the db. Cause then you can just explode on the - char. But since you have no separator, you need to use preg_split or equivalent to break the string apart using regex.

  3. #3
    Join Date
    Jun 2009
    Location
    UK: Oxford
    Posts
    1,256
    I know MySQL is very efficient, but as this will be standard, why store more unessential text.

    I suppose I will heed what I initially though; plug the - looks nicer then a . or , otherwise it Could look like a column if IP addresses.
    Garbott Ltd - Professional web development & consultancy services
    Oxford.Hosting - Prestigious shared, cloud, dedicated and gaming hosting solutions.

  4. #4
    Join Date
    Apr 2011
    Location
    Charlotte, NC
    Posts
    102
    Using PHP you can actually reference specific string positions like you would reference a numerically indexed array.

    PHP Code:
    $str "0111001100011010";

    // I want the 4th character
    $char_4 $str[3];

    echo 
    $char_4// Prints "1"

    // Iterate over the values
    for ( $i $i strlen($str) ; $i++ ) {
     
    $char $str[$i];

    There really is no reason to split the values when you can refer to them in this way.
    Adam Hahn
    Software Engineer / System Administrator / Database Engineer

Similar Threads

  1. Exploding a string in c++, need help.
    By Barti1987 in forum Programming Discussion
    Replies: 1
    Last Post: 06-23-2008, 02:14 PM
  2. allreseller.com exploding
    By webuyhosts in forum Shared Hosting Offers
    Replies: 2
    Last Post: 05-30-2006, 07:13 PM
  3. allreseller.com is exploding
    By webuyhosts in forum Shared Hosting Offers
    Replies: 9
    Last Post: 05-20-2006, 09:59 PM
  4. Password Input Feilds:
    By Clicks in forum Web Design and Content
    Replies: 8
    Last Post: 03-08-2005, 06:27 PM
  5. Binary tree to preorder traversal to Binary tree
    By Cyrus in forum Programming Discussion
    Replies: 2
    Last Post: 11-22-2004, 08:17 AM

Posting Permissions

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