Page 1 of 2 12 LastLast
Results 1 to 10 of 13
Like Tree2Likes

Thread: php wont recognize array

  1. #1
    New Registered User homer93's Avatar
    Join Date
    Aug 2011
    Posts
    17

    Default php wont recognize array 7 Aug 2011 @ 14.09

    i just started designing a website, and most of it requires php to run.
    i have a problem with an array which php isn't recognizing. the array is pulled from a database, so i just put the variable as an array. which doesn't seem to work.
    my code will probably explain better.

    PHP Code:
    <?php
        phpif 
    (isset($_GET['bid'])) {
            
    $DPView "";
            
    $detailPhotoView "";
            
    $targetID $_GET['bid'];
            
    $Bsql mysql_query("SELECT * FROM builds WHERE id = $targetID");
            while (
    $row mysql_fetch_array($Bsql)){
                
    $DP $row["photo"];
                
    $DetailPs $row["detail_photos"];
                
    $DetailPA = array($DetailPs);
                
    $DPsql mysql_query("SELECT * FROM photos WHERE id = $DP");
                
    $DPD mysql_result($DPsql,0,'details');
                foreach (
    $DetailPA as $DetailP){
                    
    $DetailP $DetailP;
                    
    $DetPsql mysql_query("SELECT * FROM photos WHERE id = $DetailP");
                    while (
    $row mysql_fetch_array($DetPsql)){
                        
    $DetailPD $row["details"];
                        
    $detailPhotoView .='
                            <div id="listItem">
                                <div style="float:left; width:210px; height:179px" class="left">
                                    <div style="height:160px"><img src="http://webprocafe.com/db_images/'
    .$DetailP.'.jpg" width="200" /></div>
                                    <div height:19px align="right">Delete | Submit</div>
                                </div>
                                <div style="float:left; width:10px">&nbsp;</div>
                                <div style="float:left; width:695px; height:179px" id="detail">Details:<br />
                                    <textarea name="details" id="details" cols="70" rows="8">'
    .$DetailPD.'</textarea>
                                </div>
                            </div>'
    ;
                    }
                }

                
    $DPView .= '
                    <div id="listItem">
                        <div style="float:left; width:210px; height:179px" class="left">
                            <div style="height:160px">
                                <img src="http://webprocafe.com/db_images/'
    .$DP.'.jpg" width="200" style="align="center; vertical-align:middle" />
                            </div>
                            <div height:19px align="right">Delete | Submit</div>
                        </div>
                        <div style="float:left; width:10px">&nbsp;</div>
                        <div style="float:left; width:695px; height:179px" id="detail">Details:<br />
                            <textarea name="details" id="details" cols="70" rows="8">'
    .$DPD.'</textarea>
                        </div>
                    </div>'
    ;
            }

        }
    ?>
    $detailPhotoView and $DPview are just what is posted into the body of the website.
    the array is $DetailPA, for the array ive put $DeatilPs which is data from a database. if i just put number instead of $DetailPs it semms to work properly, but with the variable it doesnt seem to explode the data to get each individual number.
    any ideas?
    thanks
    Last edited by Frinkky; 7 Aug 2011 at @ 14.24. Reason: Cleaned up your code - easier to work with and easier to debug
      Reply With Quote

  2. #2
    Senior Member Mike Hopley's Avatar
    Join Date
    Jul 2009
    Posts
    712

    Default 8 Aug 2011 @ 08.17

    When debugging your code, you often need to inspect the values of variables. Sometimes they are not what you think they should be. Use the following code to do this:

    Code:
    <?php var_dump($myVariable); ?>
    So have a look at your arrays. Check that they are actually arrays, and that they contain the data you expected.

    Before inserting arrays into databases, you need to use PHP's serialize($array) function. And when you extract the array from the database, you need to use unserialize($array) to convert it back into a PHP array.
      Reply With Quote

  3. #3
    New Registered User homer93's Avatar
    Join Date
    Aug 2011
    Posts
    17

    Default 8 Aug 2011 @ 09.00

    i tried showing the variable and its showing it as an array. it shows variables.PNG
    i think thats what they should be, unless they shouldn't have the ' on each end of the array.
    how would i use the unserialize function, ive never used it before?
    thanks
      Reply With Quote

  4. #4
    Senior Member Mike Hopley's Avatar
    Join Date
    Jul 2009
    Posts
    712

    Default 8 Aug 2011 @ 09.20

    Oh, I think I understand what's happening.

    $detailPs is what came from the database. This is a string, not an array. You then tried to turn this into an array, $DetailPA.

    Technically you succeeded, but I don't think you got the result you wanted. $DetailPA is an array with only one key, which has the value '3,1,7' -- which is just a string of characters (like a word).

    I think you wanted this:

    Code:
    $DetailPA =
    array
    [0] => 3
    [1] => 1
    [2] => 7
    Is that right?
    Last edited by Mike Hopley; 8 Aug 2011 at @ 09.45.
      Reply With Quote

  5. #5
    New Registered User homer93's Avatar
    Join Date
    Aug 2011
    Posts
    17

    Default 8 Aug 2011 @ 09.47

    yeh thats right.
    do i need to change the field type in my database to an int value?
      Reply With Quote

  6. #6
    Senior Member Mike Hopley's Avatar
    Join Date
    Jul 2009
    Posts
    712

    Default 8 Aug 2011 @ 10.06

    quote
    yeh thats right.
    do i need to change the field type in my database to an int value?
    Originally Posted by homer93 View Post
    No, that won't help. At best you'll get an error; at worst, it will mangle your string by forcibly converting it to an integer (maybe the result would be 3, or 317, or even 0 or 1).

    Do you control the code that inserts this into the database? If so, the neatest option would be to serialize() the array before you insert it, and then unserialize() the array when you extract it.

    Otherwise, you'll need to convert the string into an array:

    Code:
    $DetailPA = explode(",", $detailPs);
    That will produce an array like this:

    Code:
    $DetailPA = 
    array
    [0] => '3'
    [1] => '1'
    [2] => '7'
    Note these are strings, not numbers. This is probably okay, because in most cases PHP will work out what you intend (PHP is weakly typed). But if you need them to be numbers, you can typecast them:

    Code:
    foreach ($DetailPA as &$value) {
    settype($value, "integer");
    }
    unset($value);
    Last edited by Mike Hopley; 8 Aug 2011 at @ 10.27.
      Reply With Quote

  7. #7
    Member janvt's Avatar
    Join Date
    Jan 2011
    Posts
    182

    Default 8 Aug 2011 @ 10.07

    PHP Code:
    $DetailPA explode(',',$DetailPA[0]); 
      Reply With Quote

  8. #8
    Member janvt's Avatar
    Join Date
    Jan 2011
    Posts
    182

    Default 8 Aug 2011 @ 10.12

    quote
    Do you control the code that inserts this into the database? If so, the neatest option would be to serialize() the array before you insert it, and then unserialize() the array when you extract it.
    Originally Posted by Mike Hopley View Post
    in most cases, serializing stuff & dumping it in the database is not cool. It decreases "code" readibility ('1,3,7' is easier to read than 'a:3:{i:0;i:1;i:1;i:3;i:2;i:7;}') and also takes up more storage space. Also takes more performance to serialize/unserialize than insert a comma-seperated string and explode it after selecting. Problems like this can usually be solved in a better fashion (in this case, re-think your database schema). There are exceptions, especially when optimizing performance, but in general not good practice
      Reply With Quote

  9. #9
    Senior Member Mike Hopley's Avatar
    Join Date
    Jul 2009
    Posts
    712

    Default 8 Aug 2011 @ 10.17

    quote
    in most cases, serializing stuff & dumping it in the database is not cool. It decreases "code" readibility ('1,3,7' is easier to read than 'a:3:{i:0;i:1;i:1;i:3;i:2;i:7;}') and can usually be solved in a better fashion (in this case, re-think your database schema). There are exceptions, especially when optimizing performance, but in general not good practice
    Originally Posted by janvt View Post
    Readability is only an issue if you actually want to look at this "code" directly in the database.

    I'm very willing to be educated here, but can you point me towards some resources explaining why serialising is "bad practice"?
      Reply With Quote

  10. #10
    New Registered User homer93's Avatar
    Join Date
    Aug 2011
    Posts
    17

    Default 8 Aug 2011 @ 10.20

    got it
    i used
    PHP Code:
    $DetailPA explode(',',$DetailPs); 
    at the moment i havnt made the form for adding the array/string to the database. thats my next task.
    thanks for the help
      Reply With Quote

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

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