join函数只回显数据库的1个产品

时间:2017-11-15 10:52:21

标签: php codeigniter foreach codeigniter-3

我创建了一个模型函数,因此它将回显属于我单击的配置文件用户的产品,这只是该用户得到回应的第一个产品。例如,我有一个帐户,上传了总共7个产品,该功能只显示页面上的第一个产品..

这是模型功能:

function getdata_aangeboden($user_id){
  $this->db->select("users.*,products.*"); 
  $this->db->from('users');
  $this->db->join('products','products.user_id = users.user_id','left');
  $this->db->where('users.user_id', $user_id);
  $query = $this->db->get();
  return $query->result();
 } 

    // Gets a list of multiple users.
 function getdata($user_id){
  $this->db->select("*"); 
  $this->db->from('users');
  $this->db->where('user_id', $user_id);
  $query = $this->db->get();
  return $query->result();
 }

控制器功能:

<?php foreach($products as $product) { ?> 
  <div class="main-gifts">
      <a href="<?php echo base_url() ?>/Product/details/<?php echo $product->product_id; ?>">
        <img id="fotos" src="<?php echo base_url(); ?>upload/<?php echo $product->product_foto_thumb; ?>">
        </a>
        <div class="products_discription">
            <h3><?php echo $product->product_naam; ?></h3>
            <div class="ophaal_plaats">
                <p>Plaats: <?php  echo $product->ophaal_plaats; ?></p>
            </div>
            <div class="aangeboden_door">
            <p> <?php  
                    foreach($userdetail_list as $row){ ?>
              <tr>
                  <td><?php echo $row->voornaam;?></td>
               </tr>
                    <?php } ?></p>
          </div>
          <div class="status">
          </div>
      </div>
        <a href="<?php echo base_url() ?>/KdGwController/details_bewerken/<?php echo $product->product_id; ?>"><input type="button" class="btn btn-default cb" value="Cadeau bewerken"></input> </a>
  </div>
 <?php } ?>   

有人可以向我解释为什么它只显示1个产品而不是所有属于同一个user_id的产品? 感谢

当我打印$ products:

} ?> Array ( [0] => stdClass Object ( [user_id] => 4 [email] => jeremy@gmail.com [voornaam] => Jeremy [achternaam] => Mathieu [straat] => [huisnummer] => 22 [postcode] => 1223lL [woonplaats] => Rotterdam [wachtwoord] => $2y$10$rO.e4j0dofAUZSWV8sP6yOHYqLzhPkRk9FjBylwbaMMmWRoCBp2oy [beschrijving] => Hallo ik ben Jeremy [geboortedatum] => 2017-06-10 [startdatum] => 2017-06-29 [geslacht] => Man [reset_password_key] => [profiel_foto] => 1498736939jeremy.jpg [product_id] => 85 [product_naam] => HIHIHIHI [product_beschrijving] => [ophaal_plaats] => ALLOVER [product_foto] => new_f5cea9c914751e0ad0048800ffec3d05.jpg [product_foto_thumb] => thumb_f5cea9c914751e0ad0048800ffec3d05.jpg [date_created] => 2017-10-25 [date_updated] => 2017-10-25 [category_id] => 2 ) [1] => stdClass Object ( [user_id] => 4 [email] => jeremy@gmail.com [voornaam] => Jeremy [achternaam] => Mathieu [straat] => [huisnummer] => 22 [postcode] => 1223lL [woonplaats] => Rotterdam [wachtwoord] => $2y$10$rO.e4j0dofAUZSWV8sP6yOHYqLzhPkRk9FjBylwbaMMmWRoCBp2oy [beschrijving] => Hallo ik ben Jeremy [geboortedatum] => 2017-06-10 [startdatum] => 2017-06-29 [geslacht] => Man [reset_password_key] => [profiel_foto] => 1498736939jeremy.jpg [product_id] => 86 [product_naam] => HUHUUHU [product_beschrijving] => [ophaal_plaats] => NOWHERE [product_foto] => new_ba2430d2b531bc421f27b5f14667cffb.jpg [product_foto_thumb] => thumb_ba2430d2b531bc421f27b5f14667cffb.jpg [date_created] => 2017-10-25 [date_updated] => 2017-10-25 [category_id] => 4 ) [2] => stdClass Object ( [user_id] => 4 [email] => jeremy@gmail.com [voornaam] => Jeremy [achternaam] => Mathieu [straat] => [huisnummer] => 22 [postcode] => 1223lL [woonplaats] => Rotterdam [wachtwoord] => $2y$10$rO.e4j0dofAUZSWV8sP6yOHYqLzhPkRk9FjBylwbaMMmWRoCBp2oy [beschrijving] => Hallo ik ben Jeremy [geboortedatum] => 2017-06-10 [startdatum] => 2017-06-29 [geslacht] => Man [reset_password_key] => [profiel_foto] => 1498736939jeremy.jpg [product_id] => 87 [product_naam] => HOHOHO [product_beschrijving] => [ophaal_plaats] => NOIDEA [product_foto] => new_e1dfb9a860859ff882d13435e796578c.jpg [product_foto_thumb] => thumb_e1dfb9a860859ff882d13435e796578c.jpg [date_created] => 2017-10-25 [date_updated] => 2017-10-25 [category_id] => 7 ) [3] => stdClass Object ( [user_id] => 4 [email] => jeremy@gmail.com [voornaam] => Jeremy [achternaam] => Mathieu [straat] => [huisnummer] => 22 [postcode] => 1223lL [woonplaats] => Rotterdam [wachtwoord] => $2y$10$rO.e4j0dofAUZSWV8sP6yOHYqLzhPkRk9FjBylwbaMMmWRoCBp2oy [beschrijving] => Hallo ik ben Jeremy [geboortedatum] => 2017-06-10 [startdatum] => 2017-06-29 [geslacht] => Man [reset_password_key] => [profiel_foto] => 1498736939jeremy.jpg [product_id] => 88 [product_naam] => HEHEHE [product_beschrijving] => [ophaal_plaats] => Korea [product_foto] => new_c080495685ed05ab5d4f8a7dcdb8e668.jpg [product_foto_thumb] => thumb_c080495685ed05ab5d4f8a7dcdb8e668.jpg [date_created] => 2017-10-25 [date_updated] => 2017-10-25 [category_id] => 8 ) [4] => stdClass Object ( [user_id] => 4 [email] => jeremy@gmail.com [voornaam] => Jeremy [achternaam] => Mathieu [straat] => [huisnummer] => 22 [postcode] => 1223lL [woonplaats] => Rotterdam [wachtwoord] => $2y$10$rO.e4j0dofAUZSWV8sP6yOHYqLzhPkRk9FjBylwbaMMmWRoCBp2oy [beschrijving] => Hallo ik ben Jeremy [geboortedatum] => 2017-06-10 [startdatum] => 2017-06-29 [geslacht] => Man [reset_password_key] => [profiel_foto] => 1498736939jeremy.jpg [product_id] => 89 [product_naam] => ugilh [product_beschrijving] => hjgluhsfkh;sidh fihsdiuhfluisdhauighsudiahguivhadsuihudisahluihvsaduihvluidsahuihv [ophaal_plaats] => Amsterdam [product_foto] => new_c6dd9fa0dc0be0830ed43940b5907115.jpg [product_foto_thumb] => thumb_c6dd9fa0dc0be0830ed43940b5907115.jpg [date_created] => 2017-10-26 [date_updated] => 2017-10-26 [category_id] => 1 ) [5] => stdClass Object ( [user_id] => 4 [email] => jeremy@gmail.com [voornaam] => Jeremy [achternaam] => Mathieu [straat] => [huisnummer] => 22 [postcode] => 1223lL [woonplaats] => Rotterdam [wachtwoord] => $2y$10$rO.e4j0dofAUZSWV8sP6yOHYqLzhPkRk9FjBylwbaMMmWRoCBp2oy [beschrijving] => Hallo ik ben Jeremy [geboortedatum] => 2017-06-10 [startdatum] => 2017-06-29 [geslacht] => Man [reset_password_key] => [profiel_foto] => 1498736939jeremy.jpg [product_id] => 93 [product_naam] => 11111 [product_beschrijving] => frfewefwe [ophaal_plaats] => 1111 [product_foto] => new_14de53777d648dcbe31dea26fa031ced.jpg [product_foto_thumb] => thumb_14de53777d648dcbe31dea26fa031ced.jpg [date_created] => 2017-10-26 [date_updated] => 2017-10-26 [category_id] => 2 ) [6] => stdClass Object ( [user_id] => 4 [email] => jeremy@gmail.com [voornaam] => Jeremy [achternaam] => Mathieu [straat] => [huisnummer] => 22 [postcode] => 1223lL [woonplaats] => Rotterdam [wachtwoord] => $2y$10$rO.e4j0dofAUZSWV8sP6yOHYqLzhPkRk9FjBylwbaMMmWRoCBp2oy [beschrijving] => Hallo ik ben Jeremy [geboortedatum] => 2017-06-10 [startdatum] => 2017-06-29 [geslacht] => Man [reset_password_key] => [profiel_foto] => 1498736939jeremy.jpg [product_id] => 94 [product_naam] => hiu;ij [product_beschrijving] => hk;h uihucuhsducuisdhuvsdphvjuisjhuvsudih [ophaal_plaats] => Amsterdam [product_foto] => new_f68becfc0741d3a2300f49ed5bcf58a5.jpg [product_foto_thumb] => thumb_f68becfc0741d3a2300f49ed5bcf58a5.jpg [date_created] => 2017-10-26 [date_updated] => 2017-10-26 [category_id] => 5 ) )

3 个答案:

答案 0 :(得分:2)

我希望你能正确地声明foreach循环。

<?php foreach($products as $product) {
     // print here the product info
}
?>

答案 1 :(得分:1)

您需要按如下方式更改查询

function getdata($user_id){
  $this->db->select("products.*,users.*"); 
  $this->db->from('products');
  $this->db->join('users','users.user_id = products.user_id','left');
  $this->db->where('users.user_id', $user_id);
  $query = $this->db->get();
  return $query->result();
 }

答案 2 :(得分:0)

我发现你只放了

  

TR

标签,但不是

  

标签,这可能会导致问题,所以请查看下面的答案

<?php 
  foreach($products as $product) { ?>
    <div class="main-gifts">
        <a href="<?php echo base_url() ?>/Product/details/<?php echo $product->product_id; ?>">
            <img id="fotos" src="<?php echo base_url(); ?>upload/<?php echo $product->product_foto_thumb; ?>">
        </a>
        <div class="products_discription">
            <h3><?php echo $product->product_naam; ?></h3>
            <div class="ophaal_plaats">
                <p>Plaats: <?php  echo $product->ophaal_plaats; ?></p>
            </div>
            <div class="aangeboden_door">
                <p> 
                    <table>
                    <?php
                    foreach($userdetail_list as $row){ ?>
                        <tr>
                            <td><?php echo $row->voornaam;?></td>
                        </tr>
                    <?php } ?>
                    </table>
                </p>
            </div>
            <div class="status">
            </div>
        </div>
        <a href="<?php echo base_url() ?>/KdGwController/details_bewerken/<?php echo $product->product_id; ?>">
            <input type="button" class="btn btn-default cb" value="Cadeau bewerken">
        </a>
    </div>
<?php } ?>