Codigniter - использование класса синтаксического анализа шаблона для итерации по массиву ассоциативных массивов - PullRequest
0 голосов
/ 06 мая 2020

У меня есть массив ассоциативных массивов в таблице в базе данных. Ключи этой таблицы - идентификатор, заголовок, история и тип. Мне нужно преобразовать массив ассоциативных массивов ($ stories) в ассоциативный массив ассоциативных массивов (одна строка кода), а затем вы можете создать шаблон с al oop в нем и отправить все сразу в один шаблон (одна строка кода), и он вернет вам тот же $ theLongStringYouBuilt, что и первый метод.
Затем мне нужно проанализировать его для просмотра. Ниже у меня есть StoriesModel и StoriesController. У меня нет ничего в моем шаблоне историй, и я не уверен, нужно ли мне что-нибудь в нем, потому что мы заполняем его классом анализа шаблона?

<?php

defined('BASEPATH') OR exit('Forbidden');
class StoriesModel extends CI_Model {

    public function __construct() {

    }

    public function getStories(){

        $query = $this->db->get('stories'); 
        return $query->result_array();


    }

}

Это StoriesController

<?php
defined('BASEPATH') OR exit('Forbidden');

class Stories extends CI_Controller {

    public function __construct() {
        parent::__construct();

        // this loads the StoriesModel and creates a StoriesModel object
        $this->load->model('StoriesModel');
    }

    public function index() {

        /* this next line is a hack to solve the contactjs problem explained
         * in the instructions. find a better way to solve this. in theage mean
         * time if you uncomment this line it will at least allow you to load
         * the page without an error.*/



                $data['contactjs']=$this->parser->parse('templates/Javascript/contactjs',[],TRUE);
                $data['bootCSS']=$this->parser->parse('templates/CSS/bootCSS',[],TRUE);
                $data['CSS']=$this->parser->parse('templates/CSS/CSS',[],TRUE);
                $data['jQuery']=$this->parser->parse('templates/Javascript/jQuery',[],TRUE);
                $data['bootstrap']=$this->parser->parse('templates/Javascript/bootstrap',[],TRUE);

        /*
         * this next line will retrieve the stories from the database. it is
         * commented out because you have to write the method in the model first.
         */
          $stories = $this->StoriesModel->getStories();

          //THIS IS WHAT IVE BEEN TRYING AND ITS NOT WORKING AT ALL.
          $stories = array();
          foreach($stories as $key => $row)
          {
              $stories[$key] = $row['stories'];
          }





        THIS IS THE HARD PART
         * Now that you have an array full of data you want to display,
         * you have to figure what to do with it. You can't display it as is.
         * The solution is templates (using the CI parser). You have all the raw
         * Data in the $stories array. Make a template(s) to send the data and get
         * back well-formed HTML which you put into the $data[] associative array.
         * Then you send the $data array to the page below. There are many ways to
         * do this, but the idea is always the same. Do something like this...
         * 
         * $data['somekey'] = $this->parser->parse('templates/some template', [stories data], TRUE);
         * 
         * Where 'somekey' is how you will access the filled template in the page below.
         * 'sometemplate' is the template you want to fill with stories data
         * [stories data] is the data you want to send to the template to fill it in.
         *     that's not syntactically correct, it's just a concept, you have to figure
         * out how to build the [stories data] array and send it.
         * 
         * you will need to read this https://codeigniter.com/userguide3/libraries/parser.html
         * 
         * there are 5 stories in the database, but you cannot count on that! you have to make
         * a general solution that will work if the number of stories change
         */

        $this->load->view('templates/header', $data);
        $this->load->view('pages/stories'); // <-- you need to make this page!
        $this->load->view('templates/footer'); 


    }

}
...