2016-11-22 15 views
0

私は2つのテーブルを作成しました。一つは、別のは、私は私からそれによってabproduct_detailからすべてのデータを選択し、tbproduct_detailに挿入し、この1odeigniterの1つのテーブルIDに基づく値を別のテーブルに取得

<table class="table table-borderd table-hover"> 
       <thead> 
        <tr> 
         <th>Product Name</th> 
         <th>Price</th> 
         <th>Quantity</th> 
         <th>Total</th> 
         <th><input type="button" class="btn btn-primary addmore" value="+"></th> 
        </tr> 
       </thead> 
       <tbody id="itemlist2"> 
        <tr id="tr_1"> 
          <td class="prod_c"> 
          <select class="select-product form-control" id="itemName_1" name="product_name[]"> 
           <option value=""> 
         </option> 
         <?php 
         foreach ($product as $key): 
          ?> 
         <option value="<?php echo $key['id'] ?>"> 
          <?php echo $key['product_name'] ?> 
         </option> 
         <?php endforeach; ?> 
          </select> 
         </td> 

         <td> 
         <input type="text" name="price[]" id="price_1" class="price form-control" value="" data-cell="C1"> 
         </td> 

         <td><input type="text" data-cell="D1" name="quantity[]" id="quantity_1" class="qty form-control"></td> 
         <td><input type="text" data-cell="E1" name="discount[]" id="discount_1" class="form-control"></td> 
         <td><input type="text" data-cell="F1" data-formula="(C1*D1)-(C1*D1*E1/100)" name="total[]" id="total_1" class="amount form-control"></td> 
        </tr> 
       </tbody> 
      </table> 

のようなフォームを作成し

1.abproduct_deatil構造

id product_id  product_name  cost_price  selling_price 

7  4   Alentin DS 400 Tablet  55   60 

tbproduct_detailで、abprodut_detailためのものです。 tbproduct_detailに挿入した後は、このようになります。これは、選択されたオプションiからproduct_nameにproduct id numberを格納したことを意味します。私はID = tbproduct_detai、PRODUCT_NAME

、あなたはそのabproduct_deatilに気づくでしょう、私の編集ビューページ

id product_id  product_name   quantity  price 

19  16   Alentin DS 400 Tablet  5   60 

にこのような構造のように表示したい

2.tbproduct_detail構造

id product_id  product_name quantity  price 

    19  16     7    5   60 

2つの表を結合して、編集ビューのページでデータを表示したい

私の編集ビューページ

<thead> 
        <tr> 
         <th>ProductName</th> 
         <th>Quantity</th> 
         <th>Price</th> 

        </tr> 
       </thead> 
       <tbody class="detail"> 
        <?php 
        if($rows->num_rows() > 0) 
        { 
         foreach($rows->result() as $d) 
         { 
          ?> 
           <tr> 
            <td><input type="text" value="<?= $d->product_name ?>" name="product_name[]" class="form-control"></td> 
            <td><input type="text" value="<?= $d->quantity ?>" name="quantity[]" class="form-control"></td> 
            <td><input type="text" value="<?= $d->price ?>" name="price[]" class="form-control"></td> 

           </tr> 
          <?php 
         } 
        } 
        ?> 

       </tbody> 
      </table> 

私のコントローラ

public function edit($id) 
{ 

    $data['rows']= $this->db->query("SELECT * FROM tbproduct_detail WHERE product_id = '$id'"); 
    $this->load->view('product/edit',$data); 
} 

私はこれを行う方法がわかりませんか?私を助けてください。私はこのフォーラムと初心者のコードネイターでは初めてです。

答えて

0

コントローラで次のクエリを実行してみてください。

$data['rows']= $this->db->query("SELECT a.id, a.product_id, b.product_name, a.quantity,a.price FROM tbproduct_detail a, abprodut_detail b WHERE a.product_name= b.id AND a.product_id = '$id'"); 

トピックをオフにすると、モデルファイルを使用してクエリを書き込む方がよいでしょう。

+0

です。私はちょうど私の最初のHTMLフォームを変更しています。みんなありがとう。 – TPLMedia24

0

コントローラでJOINクエリを使用しました。

あなたの予想クエリは次のとおりです。

select a.id, a.product_id, b.product_name, a.quantity, a.price from tbproduct_detail a join abproduct_deatil b on a.product_name = b.id WHERE a.product_id = '$id'

ので、最終的なコードは、私がIDではなく、テキスト値を渡すことによって、自分自身を解決

public function edit($id) 
{ 
    $data['rows']= $this->db->query("select a.id, a.product_id, b.product_name, a.quantity, a.price 
    from tbproduct_detail a join abproduct_deatil b 
    on a.product_name = b.id WHERE a.product_id = '$id'"); 

    $this->load->view('product/edit',$data); 
} 
+0

テーブル 'invoice.abproduct_deatil'は存在しません tbproduct_detailからa.id、a.product_id、b.product_name、a.quantity、a.priceを選択します。a.product_name = b.idのa.product_name = b.idに結合します。 product_id = '22' – TPLMedia24

+0

また、 'from.'の前と' a.price'の後に 'b.invoice'を追加します。 –

+0

' a.id、a.product_id、b.product_name、a.quantity、a.price、b.invoiceを選択します。 tbproduct_detailからabproduct_deatil b on a.product_name = b.idここでa.product_id = '$ id' ' –

関連する問題