【问题标题】:Codeigniter - MVC php - print multi value from databaseCodeigniter - MVC php - 从数据库中打印多值
【发布时间】:2020-02-25 13:01:14
【问题描述】:

我将 MVC 与 Codeigniter 一起使用,并尝试从 HTML 文件上的数组(使用数据库中的值)打印。 在每一行中,我需要将 Uniq URL 行打印到标签中。

型号:

<?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>

数据库:

我需要什么: 我需要从我的数组中的特定路由打印每个src="&lt;?php echo base_url('assets/img/products/img-1.jpg'); ?&gt;" 行 - 我从控制器函数中获得:

        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="&lt;?php echo $product[0]['item_img'] ?&gt;" 以及我需要移动到下一个索引的每个标签。 我尝试使用 foreach 但它是多个所有 html 代码

希望我以自己为榜样! 谢谢。

【问题讨论】:

    标签: php arrays codeigniter


    【解决方案1】:
    <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>
    

    类似的东西?

    【讨论】:

    • 我必须使用我的模板 html .. 不能使用表格
    【解决方案2】:

    我修好了! 像这样使用 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!

    【讨论】:

      猜你喜欢
      • 2015-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-19
      • 2012-12-07
      • 1970-01-01
      • 2020-04-11
      相关资源
      最近更新 更多