Jump to content

Using Arrays


jajtiii

Recommended Posts

In my code, there are many instances (mostly after dB queries) where I am setting a certain object value to an array. For example, I might hit the dB for every flower that blooms in July and create an array called 'flower_names' which has all of the values that were returned.

Later on in my script, I will access this object property and assume it is an array, performing various functions that actually require it to be an array. For example, perhaps my user has submitted a request for a certain flower and i want to check to see if THAT flower is also in my dB, so I do the following :

[code]
if (in_array($user_stuff, $myGarden->flower_names)) {
[/code]

Now, all of this works fine with one exception. If my query pulls only ONE flower, then the property is not an array and php kicks out an error.

I build by arrays like so :

[code]
$res = [array of row results from db];
$ct = [num_rows];

for($x=0; $x<$ct; $x++) {
if ($res[$x]['flower_color'] = 'red') {
$this->flower_name[] = $res[$x]['flower_name'];
}
}
[/code]

There's a lot more to it, but hopefully this lays down the idea. I don't want to do 'if is_array'. What I am hoping to get is another coder's method for building arrays to get around this.

any thoughts are welcome
Link to comment
Share on other sites

[!--quoteo(post=387723:date=Jun 25 2006, 05:39 AM:name=jajtiii)--][div class=\'quotetop\']QUOTE(jajtiii @ Jun 25 2006, 05:39 AM) [snapback]387723[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I build by arrays like so :

[code]
$res = [array of row results from db];
$ct = [num_rows];

for($x=0; $x<$ct; $x++) {
if ($res[$x]['flower_color'] = 'red') {
$this->flower_name[] = $res[$x]['flower_name'];
}
}
[/code]
[/quote]

a better way to build the array I feel is use the while statement instead of a FOR loop.

[code]

$query = "pull all data from the database that you require";

$result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());

$flower_name = array(); //force it to be an array

if (mysql_num_rows($result) >0) {
  while($row = mysql_fetch_assoc($result)) {
    if ($row['flower_color'] == 'Red') {
       $flower_name[] = $row['flower_name']; //puts the flower name into the $flower_name array
       }
   }
}
[/code]

hope that helps
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.