Welcome Guest, Not a member yet? Register   Sign In
Please help me with this code
#1

[eluser]Pokhara[/eluser]
I am only getting 1 featured item with this code in codeigniter, I would like to get 5 different featured items please help

Code:
my model
    // GET THE FEATURED PRODUCTS
    function getMainFeature(){
        $data = array();
        $this->db->select("id, a_title, a_description, a_image");
        $this->db->where('a_featured', true);
        $this->db->where('a_status', 'active');
        $this->db->order_by("rand()");
        $this->db->limit(5);

        $Q = $this->db->get('articles');

        if($Q->num_rows() >0){
            foreach($Q->result_array() as $row){
                $data = array(
                    "id" => $row['id'],
                    "a_name" => $row['a_title'],
                    "a_description" => $row['a_description'],
                    "a_image" => $row['a_image']
                );
            }
        }
        $Q->free_result();
        return $data;
    }

Code:
function index(){


    //get featured
    $data['mainfeature'] = $this->MArticles->getMainFeature();
    $data['main'] = 'template/main/home';
    //load data and template
    $this->load->vars($data);
    $this->load->view('template/main/main_template');
}
my views
Code:
<li>
    &lt;?php
    foreach($mainfeature as $feat){
        
    echo "<img src='".$mainfeature[' border='0' align='left' width='320' height='320'/> \n";
    
    }
    ?&gt;
    </li>
#2

[eluser]davidbehler[/eluser]
Each time you go through your foreach-loop, you assign a new array to $data instead of adding the array to the existing content of $data. Easy fix:
Replace this
Code:
$data = array(
  "id" => $row['id'],
  "a_name" => $row['a_title'],
  "a_description" => $row['a_description'],
  "a_image" => $row['a_image']
);
with this
Code:
$data[] = array(
  "id" => $row['id'],
  "a_name" => $row['a_title'],
  "a_description" => $row['a_description'],
  "a_image" => $row['a_image']
);
or even easier instead of the foreach-loop, simply do this:
Code:
$data = $Q->result_array();
#3

[eluser]dhaulagiri[/eluser]
Thanks a lot




Theme © iAndrew 2016 - Forum software by © MyBB