2017-04-11 60 views
0
同一頁

從這樣多交部分在

[     POSTS     ] 
    | id |  title | part1 | part2 | part3 | 
    |------|--------------|-------------|-------------|-------------| 
    | 1 |  ST1  | storyp1 | storyp1 | storyp1 | 
    | 2 |  ST2  | storyp2 | storyp2 | storyp2 | 
    | 3 |  ST3  | storyp3 | storyp3 | storyp3 | 
    | 4 |  ST4  | storyp4 | storyp4 | storyp4 | 
    __________________________________________ 

數據庫我怎麼能分離頁面到3件顯示上的一個按鈕,當點擊?

<?php 
$query = "SELECT * FROM posts WHERE id = :id"; 
$stmt = $conn->prepare($sqlFetch); 
    $stmt->execute([':id' => $id]); 
    $row = $stmt->fetch() 
     $title = $row['title']; 
     $part1 = $row['part1']; 
     $part1 = $row['part2']; 
     $part1 = $row['part3']; 
?> 


    <body> 
     <h1 class="title"> 
      <?php echo $title; ?> 
     </h1> 
     <button>part1</button> 
     <button>part2</button> 
     <button>part3</button> 

     <div class="part1"> 
      <?php echo $part1; ?> 
     </div> 
     <div class="part2"> 
      <?php echo $part2; ?> 
     </div> 
     <div class="part3"> 
      <?php echo $part3; ?> 
     </div> 
    </body> 

我想過使用CSS display: none;但無法弄清楚如何觸發按鈕,是否有使用PHP來做到這一點可能的方式是什麼?我也想過Radio buttons但我想這不是最佳方式。

+0

使用JS和onclick來查找印刷機,並據此採取行動(查找內容並隱藏/顯示它) – Zoe

+0

@LunarWatcher這不可能使用PHP? –

+0

編號PHP是服務器端語言,JavaScript是客戶端。按鍵是客戶端。請參閱[有關按鈕按下的更多詳細信息](http://stackoverflow.com/a/20738409/6296561) – Zoe

回答

0

我認爲這將有助於你: 創建第一訪問getdata.php

<?php 
$q = ($_GET['q']); 
include_once 'DbConnection.php'; //This is your connection 
$Query = "SELECT * FROM posts WHERE id='$q'"; 
$Result = mysqli_query($Link, $Query); 
while ($Rows = mysqli_fetch_assoc($Result)) 
{ 
$title = $Rows['title']; 
$part1 = $Rows['part1']; 
$part2 = $Rows['part2']; 
$part3 = $Rows['part3']; 
echo "<div class='part1'>$part1</div>"; 
echo "<div class='part2'>$part2</div>"; 
echo "<div class='part3'>$part3</div>"; 
} 
?> 

現在創建的index.php

<html> 
<head><title>Show the result</title> 
<script> 
function ShowResult(str) 
{ 
    if (str == "") { 
     return; 
    } 
    if (window.XMLHttpRequest) { 
     xmlhttp=new XMLHttpRequest(); 
    } else { 
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.onreadystatechange=function() { 
     if (xmlhttp.readyState==4 && xmlhttp.status==200) { 
      document.getElementById("DisplayData").innerHTML=xmlhttp.responseText; 
     } 
    } 
    xmlhttp.open("GET","GetData.php?q="+str,true); 
    xmlhttp.send(); 
} 
</script> 
</head> 
<body> 
<div> 
<input type="text" id="txtSearch" onkeyup="ShowResult(this.value); onblur="ShowResult(this.value);"/> 
<div> 
<div id="DisplayData"></div> 
</body> 
</html> 

如果你想通過按鈕事件做如此,您需要傳遞id並從服務器返回數據的表單。如果你需要,也評論或收件箱我。