php - Extracting image value from Array in Codeigniter -
i'm having issues extracting value array of images via codeigniter/mysql. have table called "image" in database , if echo out following code:
{"f01efdf6fe3b2bb387d3094aa701203f":{"filename":"f01efdf6fe3b2bb387d3094aa701203f.jpg","alt":"","caption":"","primary":true},"b11581adadd1848acb2898e7a284afc1":{"filename":"b11581adadd1848acb2898e7a284afc1.png","alt":"","caption":""},"2cfee6d3c334696833b1cfe13910fcf1":{"filename":"2cfee6d3c334696833b1cfe13910fcf1.png","alt":"","caption":""}}
as can see there 3 images there, need echo out image "primary" value is:true within foreach loop...
edit:
<?php $query = $this->db->query("select * offers_products offer_large limit 5;"); ?> <?php foreach ($query->result() $row): ?> <li><a href="/<?=$row->slug?>"><?=$row->id?></a></li> <li><?=$row->name?></li> <li><!-- images table value --> <?=$row->images?> <!-- --></li> <li><?=$row->description?></li> <?php endforeach; ?>
maybe:
$array = json_decode($stringfromdatabase, true); $primary = false; foreach ($array $longstringdontcare => $imagearray) { if (!empty($imagearray['primary'])) { $primary = $imagearray; break; } } if ($primary) { echo $primary['filename'];// should give: f01efdf6fe3b2bb387d3094aa701203f.jpg }
to give 1 last hint:
<?php function getprimaryimagefromjsonstring($stringfromdatabase) { $array = json_decode($stringfromdatabase, true); $primary = false; foreach ($array $longstringdontcare => $imagearray) { if (!empty($imagearray['primary'])) { $primary = $imagearray; break; } } if ($primary) { return $primary['filename']; } return null; } ?> <?php foreach ($query->result() $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: few edits.
Comments
Post a Comment