图像不明智

时间:2017-10-03 06:22:29

标签: php mysql codeigniter

我的数据库中有2/3个用户,而另一个表中有一些针对每个用户的图片。(例如:userA有img1,img2,img3 || UserB有imga,imgb,imgc)。 当我试图从数据库中获取数据但是每个配置文件图像都显示但是这些图像来自最后的用户。如果userB是数据库中的最后一个用户,那么在显示图像时,图像显示的图像都是userB的图像。

以下是代码:

控制器

public function index()
    {   
        $this->load->model("Interior_listing_model","interior");
        $data['articles']  = $this->interior->interior_list();
       // $type_id   = $this->input->get('score');
        foreach ($data['articles'] as $article) {
        $type_id = $article['type_id'];
        $data['particles'] = $this->interior->image_list($type_id);
        }

        //echo($type_id);
        // Load Interior Listing View
        $this->load->view("interior/interior",$data);
    }

模型

class Interior_listing_model extends CI_model
{
    public function interior_list()
  {
    //$query = $this->db->get('interior_testing');
    $query = $this->db->get_where('interior_testing', array('category_id' => 1),0,0);
    return $query->result_array();

  }
public function image_list($type_id)
  { 

    $query = $this->db->get_where('interior_image_testing', array('type_id' => $type_id),3,0);
    return $query->result_array();

  }

}

视图

    <!DOCTYPE html>
<html lang="en"> 
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
    <link rel="stylesheet" href="assets/css/style.css"/> <!-- needs to be renamed -->
    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">

  </head>

  <body>
    <div class="tophead">
      <!--menu start-->
     <nav> 
      <div class="custom_nav">
        <div class="logoimg pull-left">
           <a class="navbar-brand" href="#">
             <img class="logo" src="assets/img/WhiteLogo.png">   
           </a>
        </div>
        <div id="menu_items" class="pull-right">
         <a class="mobile_toggle_menu"><i class="fa fa-bars fa-2x"></i></a>
         <ul>
          <li><a href="#">Project</a></li>   
            <li><a href="#">How it works</a></li>   
          <li><a href="#">Contact</a></li>   
         </ul>   
        </div>
      </div>
      </nav>
      <!--menu end-->
      <!---writing start-->
        <div class="text-center">
          <h3>Find the right designer</h3>
        </div>
        <div class="text-center">
          <a class="quote" href="<?php echo base_url('BluemasonsForm/registration'); ?>">Get Free Quote</a>
        </div>
      <!---writing end-->
    </div>
    <!-- The subview section starts here -->  
    <div class="container">
      <!--Category start-->
        <div class="text-center category">
           <a class="catname activecat" href= "<?php echo base_url(''); ?>" >Interior</a> 

           <a class="catname" href="<?php echo base_url('welcome/architect'); ?>">Architect</a>
<!--           <a class="catname" href="#">Interior & Architect</a>-->
<!--           <a class="catname" href="#">Others</a>-->
        </div>
      <!--Category end-->  
    </div>
    <!-- type your image fetching query here -->
    <?php foreach($articles as $article): ?>

    <!--###################################################################################-->
    <!---listing section start-->
    <div class="listing">
     <div class="container">
         <!---1st list start-->
       <div class="row listelements">
        <!---listing Profile strat-->
         <div class="col-lg-3 col-12">  
           <div class="media">
            <img class="d-flex mr-1 img-circle" src="assets/img/<?= $article['profile_pic'] ?>" alt="Profile Picture"> <!-- needs to be changed -->
              <div class="media-body">
                <h5 class="mt-0"><?= $article['name'] ?></h5>
                <!--<input type="text" name="id" value="<?= $article->type_id ?>"> --> 
                  <span class="text-center"><?= $article['type'] ?></span><br/>
                     <span class="text-center"><i class="fa fa-map-marker"></i>&nbsp; <?= $article['location'] ?></span><br/>
                  <span class="text-center"><i class="fa fa-phone"></i>&nbsp; Contact</span>
              </div>
            </div>
         </div> 

        <!---listing Profile end--> 
        <!---Profile image start-->

         <div class="col-lg-9 col-12">
           <div class="row mobileimage">



            <?php foreach($particles as $particle): ?>

            <?php foreach($particle as $particle_one): ?>

              <div class="col-lg-4 col-md-4 col-sm-4 col-6 workimg">
                <img src="assets/img/<?= $particle_one['image_path'] ?> " width="100%"> 
              </div> 
            <?php endforeach; ?>  

            <?php endforeach; ?>   
            <!--- View all strat-->
            <div class="col-12">
               <a class="viewall pull-right" href="<?php echo base_url("details/profile/".$article['type_id']); ?>">
                 View all  
               </a>   
            </div>    
           <!--- View all end-->
           </div>    
         </div>

        <!---Profile image end-->
        </div>
         <!---1st list end-->
      </div>
    </div>
    <?php endforeach; ?> 
    <!-- Show more option-->
    <div class="col-12">
      <p class="text-center" id="load_more">
          <a> Show more 
            <i class="fa fa-angle-double-down"></i>
          </a>
      </p>
    </div>

 <!---listing section end-->
    <!-- jQuery first, then Tether, then Bootstrap JS. -->
    <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
    <script src="assets/js/custom.js"></script> <!-- needs to be renamed -->
  </body>
</html>

2 个答案:

答案 0 :(得分:0)

您的数组变量每次都会被覆盖。如下所示更改控制器中的循环:

foreach ($data['articles'] as $article) {
        $type_id = $article['type_id'];
        $data['particles'][] = $this->interior->image_list($type_id); // append each value to array
        }

此外,如果有多个与每个用户相关的图像,您还必须在视图中循环两次

 <?php foreach($particles as $particle): ?>

      <?php foreach($particle as $particle_one): ?>

          <div class="col-lg-4 col-md-4 col-sm-4 col-6 workimg">
            <img src="assets/img/<?= $particle_one['image_path'] ?> " width="100%"> 
          </div> 
      <?php endforeach; ?>  

 <?php endforeach; ?>  

答案 1 :(得分:0)

更改您的控制器代码

public function index()
    {   
        $articles = array();
        $this->load->model("Interior_listing_model","interior");
        $articlesArray  = $this->interior->interior_list();
        // $type_id   = $this->input->get('score');
        foreach ($articlesArray as $article) {

            $article['images'] = $this->interior->image_list($article['type_id']);
            $articles[] = $article; 
        }

        $data['articles'] = $articles;
        // Load Interior Listing View
        $this->load->view("interior/interior",$data);
    }

查看您的查看代码,如

<!DOCTYPE html>
<html lang="en"> 
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
    <link rel="stylesheet" href="assets/css/style.css"/> <!-- needs to be renamed -->
    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">

  </head>

  <body>
    <div class="tophead">
      <!--menu start-->
     <nav> 
      <div class="custom_nav">
        <div class="logoimg pull-left">
           <a class="navbar-brand" href="#">
             <img class="logo" src="assets/img/WhiteLogo.png">   
           </a>
        </div>
        <div id="menu_items" class="pull-right">
         <a class="mobile_toggle_menu"><i class="fa fa-bars fa-2x"></i></a>
         <ul>
          <li><a href="#">Project</a></li>   
            <li><a href="#">How it works</a></li>   
          <li><a href="#">Contact</a></li>   
         </ul>   
        </div>
      </div>
      </nav>
      <!--menu end-->
      <!---writing start-->
        <div class="text-center">
          <h3>Find the right designer</h3>
        </div>
        <div class="text-center">
          <a class="quote" href="<?php echo base_url('BluemasonsForm/registration'); ?>">Get Free Quote</a>
        </div>
      <!---writing end-->
    </div>
    <!-- The subview section starts here -->  
    <div class="container">
      <!--Category start-->
        <div class="text-center category">
           <a class="catname activecat" href= "<?php echo base_url(''); ?>" >Interior</a> 

           <a class="catname" href="<?php echo base_url('welcome/architect'); ?>">Architect</a>
<!--           <a class="catname" href="#">Interior & Architect</a>-->
<!--           <a class="catname" href="#">Others</a>-->
        </div>
      <!--Category end-->  
    </div>
    <!-- type your image fetching query here -->
    <?php foreach($articles as $article): ?>

    <!--###################################################################################-->
    <!---listing section start-->
    <div class="listing">
     <div class="container">
         <!---1st list start-->
       <div class="row listelements">
        <!---listing Profile strat-->
         <div class="col-lg-3 col-12">  
           <div class="media">
            <img class="d-flex mr-1 img-circle" src="assets/img/<?= $article['profile_pic'] ?>" alt="Profile Picture"> <!-- needs to be changed -->
              <div class="media-body">
                <h5 class="mt-0"><?= $article['name'] ?></h5>
                <!--<input type="text" name="id" value="<?= $article->type_id ?>"> --> 
                  <span class="text-center"><?= $article['type'] ?></span><br/>
                     <span class="text-center"><i class="fa fa-map-marker"></i>&nbsp; <?= $article['location'] ?></span><br/>
                  <span class="text-center"><i class="fa fa-phone"></i>&nbsp; Contact</span>
              </div>
            </div>
         </div> 

        <!---listing Profile end--> 
        <!---Profile image start-->

         <div class="col-lg-9 col-12">
           <div class="row mobileimage">



            <?php foreach($article['images'] as $particle): ?>



              <div class="col-lg-4 col-md-4 col-sm-4 col-6 workimg">
                <img src="assets/img/<?= $particle['image_path'] ?> " width="100%"> 
              </div> 

            <?php endforeach; ?>   
            <!--- View all strat-->
            <div class="col-12">
               <a class="viewall pull-right" href="<?php echo base_url("details/profile/".$article['type_id']); ?>">
                 View all  
               </a>   
            </div>    
           <!--- View all end-->
           </div>    
         </div>

        <!---Profile image end-->
        </div>
         <!---1st list end-->
      </div>
    </div>
    <?php endforeach; ?> 
    <!-- Show more option-->
    <div class="col-12">
      <p class="text-center" id="load_more">
          <a> Show more 
            <i class="fa fa-angle-double-down"></i>
          </a>
      </p>
    </div>

 <!---listing section end-->
    <!-- jQuery first, then Tether, then Bootstrap JS. -->
    <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
    <script src="assets/js/custom.js"></script> <!-- needs to be renamed -->
  </body>
</html>