Welcome Guest, Not a member yet? Register   Sign In
Normal PHP to Codeigniter

I'm stuck trying to figure this out . I would like to convert this HTML PHP page to one made with CI.[This is what I want to get (done with basic php )][1] and this is what I can mange to do with Codeigniter [This is the result with Codeigniter][2]

Here is my views page

            table td, table th {
                padding: 5px;
                text-align: center;
            table th {
            table {
                width: 100%;
                border: 2px solid #ccc;
                border-collapse: collapse;
    <main role="main" class="col-md-9 ml-sm-auto col-lg-10 px-4">
        <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
            <h1 class="h2">Lista alergeni si alimente</h1>
            <h4>Total alimente: {total}</h4>
            <div style='max-width:500px; margin-right: 1px;'>
                            <th>{alergen} </th>

And here is my controller file

    defined('BASEPATH') OR exit('No direct script access allowed');
    class Alergeni_alimente extends CI_Controller {
        public function index() {
            $lista_alimente = $this->db->query('select alimente.id, alimente.name as alimente_name  FROM alimente order by name desc')->result();
            $lista_alergeni = $this->db->query('select alergeni.id, alergeni.name FROM alergeni order by name desc')->result();
            $content = $this->parser->parse('alergeni_alimente/select', array("ALERGENI" => $lista_alergeni, "ALIMENTE" => $lista_alimente), true);
            $TITLE = "Selecteaza alergen pentru aliment ";
            $array = array('TITLE' => $TITLE, 'CONTENT' => $content);
            $this->parser->parse('TEST', $array, false);
    //        echo '<pre>';
    //        print_r($lista_alergeni);
    //        print_r($lista_alimente);
        public function selected() {
            $data = array(
                'alergen_id' => $_POST['alergen_id'],
                'aliment_id' => $_POST['aliment_id']
            $this->db->insert('alimente_alergeni', $data);
        public function list() {
            $alergeni_aliment = $this->db->query('select alergen_id, aliment_id, alimente.name as aliment_name, alergeni.name as alergen_name from alimente_alergeni '
                            . 'inner join alimente on alimente_alergeni.aliment_id = alimente.id '
                            . 'inner join alergeni on alimente_alergeni.alergen_id = alergeni.id order by alimente.name asc, alergeni.name asc')->result();
            $alergeni = $this->db->query('select name as alergen from alergeni')->result();
            $alimente = $this->db->query('select name as aliment from alimente')->result();
            $totalQuery = $this->db->query("select count(id) as total from alimente")->result();
            $content = $this->parser->parse('alergeni_alimente/list', array('ALERGENI_ALIMENT' => $alergeni_aliment, "COUNT" => $totalQuery, 'ALERGENI' => $alergeni, 'ALIMENTE' => $alimente), true);
            $TITLE = "Lista alimente si alergenii corespunzatori";
    //        die();
            $array = array('TITLE' => $TITLE, 'CONTENT' => $content);
            $this->parser->parse('TEST', $array, false);


What should ,someone told me to use a foreach statement but I'm not sure where.

Could someone please take a few momments to explain to me step by step how to solve this? Im new to CI

  [1]: https://i.stack.imgur.com/30SQ4.png
  [2]: https://i.stack.imgur.com/IW50i.png

Any help ?

(This post was last modified: 04-08-2020, 09:40 AM by jreklund.)

You need to fetch your results as an array with result_array() instead of result() as the paser want's an array and not an object.


PHP Code:
$alimente $this->db->query('select name as aliment from alimente')->result_array();
$content $this->parser->parse('alergeni_alimente/list', array('ALIMENTE' => $alimente), true);



Theme © iAndrew 2016 - Forum software by © MyBB