PHPGeek80 Posted February 13, 2007 Share Posted February 13, 2007 Hi, I have the following php code and when i try to count the items of the expolded array it gives me a value of "1" even though the data im getting from the database is empty. Does anyone know why this is? $row = mysql_fetch_array($sql_result); $field_description = $row["field_description"]; $field_cost = $row["field_cost"]; $field_setup = $row["field_setup"]; $A_field_description = array(); $A_field_cost = array(); $A_field_setup = array(); $A_field_description = explode(',', $field_description); $A_field_cost = explode(',', $field_cost); $A_field_setup = explode(',', $field_setup); $A_field_count = count($A_field_description); print $A_field_count; Quote Link to comment Share on other sites More sharing options...
redarrow Posted February 13, 2007 Share Posted February 13, 2007 <?php $row = mysql_fetch_assoc($sql_result); $field_description = $row["field_description"]; $field_cost = $row["field_cost"]; $field_setup = $row["field_setup"]; $A_field_description = array(); $A_field_cost = array(); $A_field_setup = array(); $A_field_description = explode(' ', $field_description); $A_field_cost = explode(' ', $field_cost); $A_field_setup = explode(' ', $field_setup); $A_field_count = count($A_field_description); print $A_field_count; ?> Quote Link to comment Share on other sites More sharing options...
zq29 Posted February 13, 2007 Share Posted February 13, 2007 Ummm, have you ever thought that he might be exploding comma delimited strings redarrow? It is returning 1 because if explode() can't find the delimited in the string, it returns the original string (in an array). So explode() is returning an array with one key and an empty value. Hence the count of 1. Quote Link to comment Share on other sites More sharing options...
heckenschutze Posted February 13, 2007 Share Posted February 13, 2007 A note: Since PHP is loosely typed, you don't need to define the objects/vars before you use them... If your data is empty, $a = explode(',', $b); will return NULL (0) or the original string, then count($a) would infact count: $a = array([0] => NULL [or original string]); Which of course is 1 Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted February 13, 2007 Share Posted February 13, 2007 Just because you don't have to define or initialize objects / vars before you use them doesn't mean that not doing it is good programming practice. Quote Link to comment Share on other sites More sharing options...
heckenschutze Posted February 13, 2007 Share Posted February 13, 2007 Reducing the work load and # of lines is good programming practice... effective, efficient coding... Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted February 13, 2007 Share Posted February 13, 2007 le sigh Quote Link to comment Share on other sites More sharing options...
play_ Posted February 13, 2007 Share Posted February 13, 2007 $row = mysql_fetch_assoc($sql_result); $field_description = $row["field_description"]; $field_cost = $row["field_cost"]; $field_setup = $row["field_setup"]; $A_field_description = explode(' ', $field_description); $A_field_cost = explode(' ', $field_cost); $A_field_setup = explode(' ', $field_setup); $A_field_count = count($A_field_description); print $A_field_count; ?> Use that instead. Should work. When you do this: $A_field_description = array(); $A_field_cost = array(); $A_field_setup = array(); You are converting those variables to empty arrays. and then by exploding the empty array, you're giving it a key, but no value. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted February 13, 2007 Share Posted February 13, 2007 func1 <?php function func1($param){ $var = Array(); // Init var $var = $param; // set var return $var; } ?> Above, there is no reason to init $var since the very first use of it is an assignment. func2 <?php function func2(){ $var = explode(' ', $some_var); return $var; } ?> Above, func2 is taking for granted that $some_var has been set elsewhere in the code. Who knows where it's been set and what it's been set to. func2 is a good way to introduce an obscure bug into your code. func2 - fixed <?php function func2(){ $some_var = some_func(); $var = explode(' ', $some_var); return $var; ?> Now we know what $some_var is before using it which makes contamination less likely. Better programming practice. General guideline: You don't need to initialize variables if the first use of them is assignment. If the first use is anything other than assignment, you'll save yourself a headache somewhere down the line by taking the extra 1.5 seconds to type whichever of these fits your need: $var = NULL; $var = Array(); $var = ''; $var = ""; $var = false; $var = true; I have nothing more to say on this matter. ;^] Quote Link to comment Share on other sites More sharing options...
PHPGeek80 Posted February 13, 2007 Author Share Posted February 13, 2007 Hi, Whats the difference between: mysql_fetch_assoc and mysql_fetch_array Thanks. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 13, 2007 Share Posted February 13, 2007 http://us2.php.net/manual/en/function.mysql-fetch-assoc.php http://us2.php.net/manual/en/function.mysql-fetch-array.php Quote Link to comment Share on other sites More sharing options...
emehrkay Posted February 13, 2007 Share Posted February 13, 2007 isnt this about $field_cost = $row["field_cost"]; being a comma delimited string? Quote Link to comment Share on other sites More sharing options...
PHPGeek80 Posted February 13, 2007 Author Share Posted February 13, 2007 Hi, Yes that is correct emehrkay. I have tried it with the mysql_fetch_assoc feature but get the same result. I used print_r on the exploded array and got: Array ( [0] => ) If there is no data in the array why is it still giving a key? I have also tried this without defining the arrays with the same result. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 13, 2007 Share Posted February 13, 2007 It is returning 1 because if explode() can't find the delimited in the string, it returns the original string (in an array). So explode() is returning an array with one key and an empty value. Hence the count of 1. Before you use explode, consider checking the length of the string, or if it even contains any ',' Quote Link to comment Share on other sites More sharing options...
PHPGeek80 Posted February 13, 2007 Author Share Posted February 13, 2007 Hi all, Issue resolved. <?php $row = mysql_fetch_assoc($sql_result); $field_description = $row["field_description"]; $field_cost = $row["field_cost"]; $field_setup = $row["field_setup"]; $A_field_count = 0; if (!empty($field_description)) { $A_field_description = array(); $A_field_cost = array(); $A_field_setup = array(); $A_field_description = explode(' ', $field_description); $A_field_cost = explode(' ', $field_cost); $A_field_setup = explode(' ', $field_setup); $A_field_count = count($A_field_description); } print $A_field_count; ?> Thanks for all you help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.