I want to ask what is the better to write this sequence of if else
statements :
$dir = base_url()."assets/produits/";
$img_src = $dir."none.png";
if (!empty($row->nShape)) {
$nom = $row->nShape;
if (file_exists($dir.$nom.".JPEG")) {
$img_src = $dir.$nom.".JPEG";
}
else
if (file_exists($dir.$nom.".jpg")) {
$img_src = $dir.$nom.".jpg";
}
else
if (file_exists($dir.$nom.".jpeg")) {
$img_src = $dir.$nom.".jpeg";
}
}
The Full MVC Pattern
After implementing : https://stackoverflow.com/a/32034937/5203821 answer
The View File
<div class="container">
<?=$message?>
<?php
foreach ($rows as $row) {
$dir = base_url()."assets/produits/";
$img_src = $dir."none.png";
if (!empty($row->nShape)) {
$nom = $row->nShape;
$type = array(".JPEG", ".jpg", ".jpeg");
foreach ($type as $ext) {
if (file_exists($dir.$nom.$ext)) {
$img_src = $dir.$nom.$ext;
break;
}
}
}
?>
<div class="list-group">
<a class="list-group-item col-md-4" target="_blanc" href="<?=site_url()?>/produits/detail/<?=$row->nProduct?>" title="<?=$row->sSort?>"><b><img src="<?=$img_src?>" alt="<?=$row->nShape?>" class="img-rounded product"><?=$row->sSearch?></b></a>
</div>
<?php
}
?>
<div class="spacer"></div>
</div>
Model File
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Product_model extends CI_Model{
function getProduct($product_id){
$this->db->from('tequivalent')
->where('nReference',$product_id)
->join('tProduct','tProduct.nProduct=tequivalent.nProduct1');
$query = $this->db->get();
$ret['rows'] = $query->result();
$ret['number'] = $query->num_rows();
$ret['id'] = $product_id;
return $ret;
}
}
Controller File
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Produits extends CI_Controller {
function index($product_id='')
{
if($product_id==NULL){
redirect();
}
$this->load->model('Product_model');
$query = $this->Product_model->getProduct($product_id = $this->uri->segment(3, 0));
if($query['number']>0){
$results['message']="<h2>Clique sur une Référence pour Plus de Detail</h2>";
$results['number'] = $query['number'];
$results['rows'] = $query['rows'];
}else{
$results['message'] = "<p>Oops! Y'a Aucun Résultat pour cette recherche</p>";
$results['message'] .= "<a class='btn btn-lg btn-danger' href=".site_url().">Acceuil</a>";
$results['number'] = $query['number'];
$results['rows'] = $query['rows'];
$results['id'] = $query['id'];
}
$this->load->view('constants/header');
$this->load->view('produits',$results);
$this->load->view('constants/footer');
}
}
The above code if the the full MVC Pattern since I'm using Codeigniter And thanks to all of you for your contributions