I’m currently building an automated achievement/badge system and am wondering how i go about doing the validation so for example if i wanted to do it manually i would do this but as its in a foreach im unsure if i can do if statements and also wondering if there is a better way than the one im currently doing.
Thanks
Manual Method:
<?php if( $data['user']['status'] == 1 ) : ?>
<li class="list-inline-item"><img data-toggle="tooltip" data-placement="top" title="Pioneer" src="<?php echo FULL_ROOT;?>/uploads/badges/pioneer.png"></li>
<?php endif; ?>
Here is my database:
Here is my foreach:
<?php if($data['isBadges']) { foreach($data['badges'] as $badge) : ?>
<li class="list-inline-item"><img data-toggle="tooltip" data-placement="top" title="<?php echo $badge['name']; ?>" src="<?php echo FULL_ROOT;?>/uploads/badges/<?php echo $badge['image']; ?>"><?php echo $badge['name']; ?></li>
<?php endforeach; } else { ?>
<p class="mb-0">No Badges Unlocked!</p>
<?php } ?>
Here is my controller code:
public function profile($user_id = '')
{
$this->item = $this->model('Items');
// Get data from model
$sdata = $this->setting->getAll();
// Check if empty
if($user_id == '') { redirect(''); }
// Get data from model
$udata = $this->user->getUserByUsername($user_id);
$getBadge = $this->user->getUserBadges();
$pdata = $this->user->getUserProducts($udata['id']);
if(is_array($pdata[0]) ) {
$isProducts = true;
} else {
$isProducts = false;
}
if(is_array($getBadge[0]) ) {
$isBadge = true;
} else {
$isBadge = false;
}
$data = array(
"user" => $udata,
"products" => $pdata,
"isProducts" => $isProducts,
"badges" => $getBadge,
"isBadges" => $isBadge,
"settings" => $sdata
);
// Checks if no data found (404)
if($udata == false) { redirect('error'); }
// Load view
$this->view('marketplace/user/profile', $data);
}
Here is my model code:
public function getUserBadges()
{
$results = $this->db->select('msi_badges','status = 1');
if(!is_array($results[0])) {
$new_results = array();
array_push($new_results, $results);
return $new_results;
} else {
return $results;
}
}