2015-06-01 42 views
3

我有一個產品名稱和ID列表的HTML表格,點擊產品名稱鏈接後,我想打開一個模態並顯示與ID相關的項目。mysql數據引導模式

我想將$ code傳遞給Model並檢索數據。我應該怎麼做?

我下面的代碼..

<a href="#myModal" data-toggle="modal" data-target="#myModal" data-code="@<? echo $code; ?>">Product 1</a> 
<a href="#myModal" data-toggle="modal" data-target="#myModal" data-code="@<? echo $code; ?>">Product 2</a> 

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 
<div class="modal-dialog"> 
    <div class="modal-content"> 
     <div class="modal-header"> 
      <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
      <h4 class="modal-title" id="myModalLabel">Modal title</h4> 
     </div> 
     <div class="modal-body"> 
      <? 
       $code_id = isset($_GET['code']) ? $_GET['code'] : false; 
       $result = $db->prepare("SELECT * FROM $tbl_name WHERE id=:code LIMIT 1"); 
       $result->bindValue(':code', $code_id, PDO::PARAM_STR); 
       $result->execute(); 
       $row = $result->fetch(PDO::FETCH_ASSOC); 
       $unit = $row['unit']; $name = $row['name']; $price = $row['price']; 
       ?> 
      <table class="table"> 
       <tr> 
        <th style="text-align: center;">#</th> 
        <th style="text-align: center;">Unit</th> 
        <th style="text-align: center;">Product Name</th> 
        <th style="text-align: center;">Price</th> 
       </tr> 
       <tr> 
        <td><? echo $i; ?></td> 
        <td><? echo $unit; ?></td> 
        <td><? echo $name; ?></td> 
        <td><? echo $price; ?></td> 
       </tr> 
      </table> 
     </div> 
     <div class="modal-footer"> 
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
     </div> 
    </div> 
</div> 

+0

我不確定我明白問題是什麼,您當前的代碼不工作? – Epodax

+2

你應該爲此使用ajax。 – Daan

+0

你能指導我嗎?因爲我不知道如何在Modal中使用$ _GET數據。如果Ajax如何實現? – tmariaz

回答

0

你的鏈接將決定開什麼模式的data-target。因此,您必須通過讓模型的class屬性與鏈接的data-target屬性相同來鏈接data-target的屬性。

試試這個:

while(/* YOUR CONDITION FOR FETCHING DATA FROM YOUR DB */){ 
    ?> 
    <a href="#myModal" data-toggle="modal" data-target=".myModal<?php echo $uniqueid; ?>" data-code="@<? echo $code; ?>">Product 1</a> 
    <?php 

    <!-- START OF MODAL --> 
    <div class="modal fade myModal<?php echo $uniqueid; ?>" .....> 
    <!-- REST OF MODAL CODE --> 
    </div> 

} /* END OF LOOP */ 

只需用主鍵代替$uniqueid

+0

這意味着如果我有100個產品,我在While Loop中創建了100個Modals。對? – tmariaz

+0

試過這個代碼但沒有運氣。 :( – tmariaz

+0

@tmariaz - 對不起,我回到你太晚了,我更新了我的答案,你的模態的'class'應該是'modal fade myModal <?php $ uniqueid;?>''而不是'modal fade bs- example-modal-lg <?php $ uniqueid;?>'。看看我的更新後的答案。 –

1

我意識到Logan的回答是行不通的,因爲他是針對類而不是Id。 data-target每個鏈接到模態應該是一個唯一的ID。我創建了一個變量$ uid(唯一標識符),並將其初始化爲一個(或者您可以使用您的主鍵)。每個模式將有一個ID爲myModal+$uid,每個鏈接將指向一個特定的模式的ID。

<?php 
    $code_id = isset($_GET['code']) ? $_GET['code'] : false; 
    $result = $db->prepare("SELECT * FROM $tbl_name WHERE id=:code LIMIT 1"); 
    $result->bindValue(':code', $code_id, PDO::PARAM_STR); 
    $result->execute(); 

    //checks if there are results before sending it to the while loop 
    if ($result->num_rows > 0) { 
     $uid = 1;//alternatively you can use your primary key from the table 
     while($row = $result->fetch_assoc()){ 
?> 
<a href="#myModal" data-toggle="modal" data-target="#myModal<?php echo $uid; ?>" data-code="@<? echo $code; ?>">Product <?echo $uid?></a> 


    <!-- start modal, realise the id of each modal --> 
    <div class="modal fade" id="myModal<?php echo $uid; ?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 
    <div class="modal fade"> 
    <!--the rest of your code--> 
    </div> 

<?php 
     $uid = $uid +1;//increase uid   
     }// end while 

}//end if statement ?>