CodeIgniter Forums
Normal PHP to Codeigniter - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Normal PHP to Codeigniter (/showthread.php?tid=75970)



Normal PHP to Codeigniter - Matt - 04-02-2020

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


Code:
<head>
        <style>
            table td, table th {
                padding: 5px;
                text-align: center;
            }
            table th {
                background:#3390FF;
            }
            table {
                width: 100%;
                border: 2px solid #ccc;
                border-collapse: collapse;
            }
        </style>
    </head>
   
    <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>
        </div>
        <body>
            {COUNT}
            <h4>Total alimente: {total}</h4>
            {/COUNT}
            <div style='max-width:500px; margin-right: 1px;'>
                <table>
                    <thead>
                        <tr>
                            <th>&nbsp;</th>
                            {ALERGENI}
                            <th>{alergen} </th>
                            {/ALERGENI}
                        </tr>
                    <tbody>
                        {ALIMENTE}
                        <tr>
                            <td>{aliment}</td>
                       
                            <td></td>
                        </tr>
                        {/ALIMENTE}
                    </tbody>
                </table> 
            </div>
        </body>
    </html>`


And here is my controller file

   
Code:
<?php
   
    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);
   
            redirect('alergeni_alimente');
        }
   
        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


RE: Normal PHP to Codeigniter - Matt - 04-08-2020

Any help ?


RE: Normal PHP to Codeigniter - jreklund - 04-08-2020

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.

https://codeigniter.com/userguide3/libraries/parser.html
https://codeigniter.com/userguide3/database/results.html

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);

{
ALIMENTE}
<
tr>
    <
td>{aliment}</td>

    <
td></td>
</
tr>
{/
ALIMENTE