0

I'm having issues extracting a value from an array of images via Codeigniter/MySQL. So i have a table called "image" in my database and if i echo out it i get the following code:

{"f01efdf6fe3b2bb387d3094aa701203f":{"filename":"f01efdf6fe3b2bb387d3094aa701203f.jpg","alt":"","caption":"","primary":true},"b11581adadd1848acb2898e7a284afc1":{"filename":"b11581adadd1848acb2898e7a284afc1.png","alt":"","caption":""},"2cfee6d3c334696833b1cfe13910fcf1":{"filename":"2cfee6d3c334696833b1cfe13910fcf1.png","alt":"","caption":""}}

As you can see there are 3 images there, what i need is to echo out just the image where "primary" value is:true within a foreach loop...

EDIT:

<?php
$query = $this->db->query("SELECT * FROM offers_products WHERE offer_large LIMIT 5;");
?>

<?php foreach ($query->result() as $row): ?>

<li><a href="/<?=$row->slug?>"><?=$row->id?></a></li>
<li><?=$row->name?></li>
<li><!-- THIS IS THE IMAGES TABLE VALUE --> <?=$row->images?> <!-- --></li>
<li><?=$row->description?></li>

<?php endforeach; ?>
0

2 Answers 2

1

Maybe:

$array = json_decode($stringFromDatabase, true);
$primary = false;
foreach ($array as $longStringDontCare => $imageArray) {
    if (!empty($imageArray['primary'])) {
       $primary = $imageArray;
       break;
    }
} 
if ($primary) {
   echo $primary['filename'];// should give: f01efdf6fe3b2bb387d3094aa701203f.jpg
}

To give you one last hint:

<?php
function getPrimaryImageFromJsonString($stringFromDatabase) {
    $array = json_decode($stringFromDatabase, true);
    $primary = false;
    foreach ($array as $longStringDontCare => $imageArray) {
        if (!empty($imageArray['primary'])) {
           $primary = $imageArray;
           break;
        }
    } 
    if ($primary) {
       return $primary['filename'];
    }
    return null;
}
?>


<?php foreach ($query->result() as $row): ?>

<li><a href="/<?=$row->slug?>"><?=$row->id?></a></li>
<li><?=$row->name?></li>
<li><?php echo getPrimaryImageFromJsonString($row->images);?></li>
<li><?=$row->description?></li>

<?php endforeach; ?>

L.E: a few edits.

Sign up to request clarification or add additional context in comments.

3 Comments

I edited my question above as i have a foreach loop already, any idea ? ;)
you will need a second one to parse the json string into an array using the json_encode function. You can have nested foreach loops.
I understand, thanks for your help, i really appreciate it. But, i'm still struggling as the "$stringFromDatabase" is actually within the foreach loop that i'm using "<?=$row->images?>" I added my full code above if you can give me one more example to work with. that would be very well appreciated, as i've never done this one before ;)... THANK YOU!
1

You have to first decode the json encoded string and extract the primary image from there:

Here is small function which you can use to extract the primary image:(You can place this function in Helper if you are using CodeIgniter)

function get_primary_image($encode_json_data)
{
    $primary_image = '';
    $decoded_json = json_decode($encode_json_data);
    foreach($decoded_json as $obj)
    {
        if(isset($obj->primary) && $obj->primary == 1)
            $primary_image = $obj->filename;
    }
    return $primary_image;
}

You can call above function in your view in following way:

<?php foreach ($query->result() as $row): ?>

<li><a href="/<?=$row->slug?>"><?=$row->id?></a></li>
<li><?=$row->name?></li>
<li> <?=get_primary_image($row->images)?></li><!-- Here, you call above function -->
<li><?=$row->description?></li>

<?php endforeach; ?>

2 Comments

I am wondering how this got an upvote as it doesn't take into consideration the idea that the array key is a hash string and it's value is another array.
@Twisted1919 : I've tested above code and working fine....I don't know, What things do u want me to take into consideration?Please elaborate it more....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.