Results 1 to 10 of 13
-
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.
$detailPhotoView and $DPview are just what is posted into the body of the website.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"> </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"> </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>';
}
}
?>
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?
thanksLast edited by Frinkky; 7 Aug 2011 at @ 14.24. Reason: Cleaned up your code - easier to work with and easier to debug
-
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:
So have a look at your arrays. Check that they are actually arrays, and that they contain the data you expected.Code:<?php var_dump($myVariable); ?>
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.
-
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
-
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:
Is that right?Code:$DetailPA = array [0] => 3 [1] => 1 [2] => 7
Last edited by Mike Hopley; 8 Aug 2011 at @ 09.45.
-
8 Aug 2011 @ 09.47 yeh thats right.
do i need to change the field type in my database to an int value?
-
8 Aug 2011 @ 10.06 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:
That will produce an array like this:Code:$DetailPA = explode(",", $detailPs);
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:$DetailPA = array [0] => '3' [1] => '1' [2] => '7'
Code:foreach ($DetailPA as &$value) { settype($value, "integer"); } unset($value);Last edited by Mike Hopley; 8 Aug 2011 at @ 10.27.
-
8 Aug 2011 @ 10.07 PHP Code:$DetailPA = explode(',',$DetailPA[0]);
-
8 Aug 2011 @ 10.12 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
-
-
8 Aug 2011 @ 10.20 got it
i used
at the moment i havnt made the form for adding the array/string to the database. thats my next task.PHP Code:$DetailPA = explode(',',$DetailPs);
thanks for the help



2Likes
LinkBack URL
About LinkBacks














ContribSurvey library -...
ContribSurvey is a flexible, self-hosted library with ability to...