Welcome Guest, Not a member yet? Register   Sign In
search in result_array()
#1

hi...
im returning db query as result array:

$sql="select image_name,image_caption from gallery_image_captions where gallery_id=".$gallery_id." order by image_name";
$query = $this->db->query($sql);
return $query->result_array();

now i have
image_name1 "image caption 1"
image_name2 "image caption 2"
image_name3 "image caption 3"
image_name4 "image caption 4"

how can i found in the result array a value by some key? i mean the function will get key (image_name3) and value will be "image caption 3"

thanks
Reply
#2

PHP Code:
$result['image_name3'

Check out docs for result_array
Reply
#3

im getting undefined index...
i believe the data is:
Reply
#4

this gived me "array"
echo $captions_arr[0];

and this gives me error...
$captions_arr[0][0];

result_array is not an array of arrays?...
Reply
#5

Ah right, it's multiarray so you need to iterate through it like:
PHP Code:
foreach ($query->result_array() as $row)
{
 
   if (isset($row['image_name3'])) echo $row['image_name3'];


I really think you should put this into your query condition instead if you want only one row, though.
Reply
#6

now i got it...

return $captions_arr[0]["text_heb"];

so - basically i want to print the value of "text_heb" only when image_name is equal to specific value...

$array = array(
0 => array(
'image_name' => 'img_65.jpg',
'text_heb' => 'my text 1'
),
1 => array(
'image_name' => 'img_66.jpg',
'text_heb' => 'my text 2'
),
Reply
#7

thanks for help...

public function get_caption($captions_arr,$file_name)
{
foreach ($captions_arr as $row)
{
if($row["image_name"] == $file_name)
{
return $row["text_heb"];
}
}
}
Reply




Theme © iAndrew 2016 - Forum software by © MyBB