Codeigniter - MVC php - вывести многозначное значение из базы данных - PullRequest
0 голосов
/ 25 февраля 2020

Я использую MVC с Codeigniter и пытаюсь печатать из массива (со значениями из БД) в файле HTML. в каждой строке мне нужно вывести строку URL Uniq в тег.

модель:

<?php

class product_model extends CI_Model
{

      public function __construct()
     {
                parent::__construct();
                $this->load->database();
     }
      public function get_product()
      {
            $query = $this->db->query('SELECT * FROM `product`');
//            var_dump($query->result_array());
            return $query->result_array();   

      }


}

Контроллер:

<?php

class webs_controller extends CI_Controller{
public function __construct()
{
                parent::__construct();
                $this->load->model('login_model');
                $this->load->model('product_model');
                $this->load->helper('url_helper');
                $this->load->helper('form');
                $this->load->library('session');

}       
        public function view($page = 'home')
        {
             if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
            {
                    // Whoops, we don't have a page for that!
                    show_404();
            }
            $data['title'] = ucfirst($page); // Capitalize the first letter

            $this->load->view('templates/header', $data);
            $this->load->view('pages/'.$page, $data);
            $this->load->view('templates/footer', $data);
        }

        public function home()
        {        
                $data['user'] = $this->session->all_userdata();
                $this->load->view('templates/header', $data);
                $this->load->view('pages/home', $data);
                $this->load->view('templates/footer');          
        }
            public function products()
        {        
                $data['user'] = $this->session->all_userdata();
                $this->load->view('templates/header', $data);
                $this->load->view('pages/our_products', $data);
                $this->load->view('templates/footer');

        }
            public function contact()
        {        
                $data['user'] = $this->session->all_userdata();
                $this->load->view('templates/header', $data);
                $this->load->view('pages/contact', $data);
                $this->load->view('templates/footer');

        }

        public function our_products(){

            $data['title'] = 'Produts';  
            $data['user'] = $this->session->all_userdata();
            $data['product'] = $this->product_model->get_product();
            $this->load->view('templates/header', $data);
            $this->load->view('pages/our_products',$data);
            $this->load->view('templates/footer');   

        }


}

Просмотр файла:

<link rel="stylesheet" href="<?php echo base_url(); ?>assets/css/cart.css" type="text/css">

<!-- Page Add Section Begin -->
<section class="page-add cart-page-add">
    <div class="container">
        <div class="row">
            <div class="col-lg-4">
                <div class="page-breadcrumb">
                    <h2>Cart<span>.</span></h2>
                    <span><a href="#cartMove">To My Cart - click me!</a></span>
                </div>
            </div>
        </div>
    </div>
</section>
<!-- Page Add Section End -->
<!-- Start Store & Item's -->


<section class="content-section">
    <h2 class="section-header">Women</h2>
    <div class="shop-items">
        <div class="shop-item">
            <span class="shop-item-title">Album 1</span>
            <img class="shop-item-image" src="<?php echo base_url('assets/img/products/img-1.jpg'); ?>">
            <div class="shop-item-details">
                <span class="shop-item-price" style="font-weight: bold;" style="font-weight: bold;">
                    $12.99&nbsp&nbsp&nbsp</span>
                <button class="btn btn-primary shop-item-button" type="button">ADD TO CART</button>
            </div>
        </div>
        <div class="shop-item">
            <span class="shop-item-title">Album 2</span>
            <img class="shop-item-image" src="<?php echo base_url('assets/img/products/img-14.jpg'); ?>">
            <div class="shop-item-details">
                <span class="shop-item-price" style="font-weight: bold;">$14.99&nbsp&nbsp&nbsp</span>
                <button class="btn btn-primary shop-item-button" type="button">ADD TO CART</button>
            </div>
        </div>
        <div class="shop-item">
            <span class="shop-item-title">Album 3</span>
            <img class="shop-item-image" src="<?php echo base_url('assets/img/products/img-3.jpg'); ?>">
            <div class="shop-item-details">
                <span class="shop-item-price" style="font-weight: bold;">$9.99&nbsp&nbsp&nbsp</span>
                <button class="btn btn-primary shop-item-button" type="button">ADD TO CART</button>
            </div>
        </div>
        <div class="shop-item">
            <span class="shop-item-title">Album 4</span>
            <img class="shop-item-image" src="<?php echo base_url('assets/img/products/img-15.jpg'); ?>">
            <div class="shop-item-details">
                <span class="shop-item-price" style="font-weight: bold;">$19.99&nbsp&nbsp&nbsp</span>
                <button class="btn btn-primary shop-item-button" type="button">ADD TO CART</button>
            </div>
        </div>
        <div class="shop-item">
            <span class="shop-item-title">Album 4</span>
            <img class="shop-item-image" src="<?php echo base_url('assets/img/products/img-8.jpg'); ?>">
            <div class="shop-item-details">
                <span class="shop-item-price" style="font-weight: bold;">$19.99&nbsp&nbsp&nbsp</span>
                <button class="btn btn-primary shop-item-button" type="button">ADD TO CART</button>
            </div>
        </div>
        <div class="shop-item">
            <span class="shop-item-title">Album 4</span>
            <img class="shop-item-image" src="<?php echo base_url('assets/img/products/img-6.jpg'); ?>">
            <div class="shop-item-details">
                <span class="shop-item-price" style="font-weight: bold;">$19.99&nbsp&nbsp&nbsp</span>
                <button class="btn btn-primary shop-item-button" type="button">ADD TO CART</button>
            </div>
        </div>
        <div class="shop-item">
            <span class="shop-item-title">Album 4</span>
            <img class="shop-item-image" src="<?php echo base_url('assets/img/products/img-12.jpg'); ?>">
            <div class="shop-item-details">
                <span class="shop-item-price" style="font-weight: bold;">$19.99&nbsp&nbsp&nbsp</span>
                <button class="btn btn-primary shop-item-button" type="button">ADD TO CART</button>
            </div>
        </div>
        <div class="shop-item">
            <span class="shop-item-title">Album 4</span>
            <img class="shop-item-image" src="<?php echo base_url('assets/img/products/img-5.jpg'); ?>">
            <div class="shop-item-details">
                <span class="shop-item-price" style="font-weight: bold;">$19.99&nbsp&nbsp&nbsp</span>
                <button class="btn btn-primary shop-item-button" type="button">ADD TO CART</button>

            </div>
        </div>
        <div class="shop-item">
            <span class="shop-item-title">Album 4</span>
            <img class="shop-item-image" src="<?php echo base_url('assets/img/products/img-4.jpg'); ?>">
            <div class="shop-item-details">
                <span class="shop-item-price" style="font-weight: bold;">$19.99&nbsp&nbsp&nbsp</span>
                <button class="btn btn-primary shop-item-button" type="button">ADD TO CART</button>
            </div>
        </div>
        <div class="shop-item">
            <span class="shop-item-title">Album 4</span>
            <img class="shop-item-image" src="<?php echo base_url('assets/img/products/img-2.jpg'); ?>">
            <div class="shop-item-details">
                <span class="shop-item-price" style="font-weight: bold;">$19.99&nbsp&nbsp&nbsp </span>
                <button class="btn btn-primary shop-item-button" type="button">ADD TO CART</button>
            </div>
        </div>
</section>
<section class="content-section">
    <h2 class="section-header">Men</h2>
    <p style="text-align: center;">Coming soon - Men category</p>
</section>

<section class="content-section">
    <h2 class="section-header">Kids</h2>
    <p style="text-align: center;">Coming soon - Kids category</p>
</section>
<!-- End Store & Item's -->


<!-- Start Cart -->
<section id="cartMove" class=" content-section">
    <h2 class="section-header">CART</h2>
    <div class="cart-row">
        <span class="cart-item cart-header cart-column">ITEM</span>
        <span class="cart-price cart-header cart-column">PRICE</span>
        <span class="cart-quantity cart-header cart-column">QUANTITY</span>
    </div>
    <div class="cart-items">
    </div>
    <div class="cart-total">
        <strong class="cart-total-title">Total</strong>
        <span class="cart-total-price">$0</span>
    </div>
    <a href="./check-out.html" class="btn btn-primary btn-purchase" type="button" style="width: 15%;"> PURCHASE</a>
</section>

<!-- End Cart -->

    <script src="<?php echo base_url('assets/js/cart.js'); ?>" async></script>

БД: enter image description here

Что мне нужно: Мне нужно печатать каждую src="<?php echo base_url('assets/img/products/img-1.jpg'); ?>" строку с указанными маршрутами from c из моего массива - что я получаю из функции контроллера:

        public function our_products(){
           $data['title'] = 'Produts';  
           $data['user'] = $this->session->all_userdata();
           $data['product'] = $this->product_model->get_product();
           $this->load->view('templates/header', $data);
           $this->load->view('pages/our_products',$data);
           $this->load->view('templates/footer');   

       }

вроде: src="<?php echo $product[0]['item_img'] ?>" и каждый тег мне нужно перейти к следующему индекс. я пытаюсь делать с foreach, но он кратен всему html коду

надеюсь, я сам себя проиллюстрирую! спасибо.

Ответы [ 2 ]

1 голос
/ 25 февраля 2020
<table>
    <thead>
        <tr>
            <td>Serial number</td>
            <td>item_name</td>
            <td>item_price</td>
            <td>item_count</td>
            <td>item_color</td>
            <td>item_size</td>
            <td>image</td>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($product as $item) : ?>
            <tr>
                <td><?= $item['serial_number'] ?></td>
                <td><?= $item['item_name'] ?></td>
                <td><?= $item['item_price'] ?></td>
                <td><?= $item['item_count'] ?></td>
                <td><?= $item['item_color'] ?></td>
                <td><?= $item['item_size'] ?></td>
                <td><img src="<?= $item['oitem_img'] ?>" alt=""></td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>

Что-то подобное?

0 голосов
/ 25 февраля 2020

Я это исправлю! используйте foreach следующим образом:

<section class="content-section">
        <h2 class="section-header">Women</h2>
        <div class="shop-items">
        <?php foreach ($product as $item) : ?>
            <div class="shop-item">
            <span class="shop-item-title"><?php echo $item['item_name'] ?> 1</span>
                <img class="shop-item-image" src="<?php echo $item['item_img'] ?>">
                <div class="shop-item-details">
                    <span class="shop-item-price" style="font-weight: bold;" style="font-weight: bold;">
                        $12.99&nbsp&nbsp&nbsp</span>
                    <button class="btn btn-primary shop-item-button" type="button">ADD TO CART</button>
                </div>
            </div>
        <?php endforeach; ?>
        </div>
</section>

просто зациклил код. он печатает меня каждый раунд индекс массива + 1!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...