2014-01-08 43 views
0

我的頁面左側有一個菜單,列出帖子的所有標題。如何根據帖子ID以博客系統的形式顯示帖子

menu.inc.php

<?php $rArticles = selectArticles($conn); ?> 

<ul class="menu"> 
<?php while($row = mysqli_fetch_array($rArticles)){ ?> 
<li><a href="index.php?page=articleform"><?php echo $row['artTitre'];?></a></li> 

<?php } ?> 
</ul> 

我想發生這樣的是,當我點擊標題,帖子將在頁面上顯示:

<?php $rArticles = selectArticles($conn); ?> 

<h2>Articles <span><a class="btn" href="index.php?page=articleform">Ajouter un article</a></span></h2> 


<div class="article"> 
    <h3> 

      <?php 
       $rows = mysqli_fetch_array($rArticles); 
       $rArticlesId = selectArticleById($rows['artID'], $conn); 
       while($row = mysqli_fetch_array($rArticlesId)){ 
      ?> 
<a href="index.php?page=articleform&article=<?php echo $rows['artID']?>"> <?php echo $rows['artTitre']; ?></a> 

    <span><a class="btn" href="index.php?page=articles&action=delete&item=<?php echo $rows['artID'];?>">supprimer</a></span> 
    </h3> 
    <p><em><?php echo $rows['artDate']; ?> - <?php echo $rows['artAuteur']; ?></em></hp> 
    <p><?php echo $rows['artContenu']; ?></p> 

    <fieldset> 
     <legend> Commentaires </legend> 
      <?php 
      $rComm = selectCommentairesByIdArticle($rows['artID'], $conn); 
      while($row = mysqli_fetch_array($rComm)){ 
      ?> 
      <p> 
      <a href="index.php?page=commentaires&action=delete&item=<?php echo $row['commentID'];?>"><img src="images/cross.png" /></a> 

      <strong><?php echo $row['commentPseudo']; ?> </strong><?php echo $row['commentText']; ?> 
      </p> 
      <?php } ?> 
     <form action="index.php?page=commentaires&action=insert" method="post"> 

     <table class="admin_form" cellpadding="0" cellspacing="0" width="100%"> 

      <tr> 
       <td width="30%"><label for="PseudoCommentaire">Pseudo</label></td> 
       <td><input id="PseudoCommentaire" type="text" name="PseudoCommentaire" value="<?php if(isset($_POST['PseudoCommentaire'])) ?> " /></td> 
      </tr> 
      <tr> 
       <td valign="top"><label for="TexteCommentaire">Commentaire</label></td> 
       <td><textarea id="TexteCommentaire" name="TexteCommentaire"><?php if(isset($_POST['TextCommentaire'])) ?></textarea></td> 
      </tr> 
      <tr>  
       <td> 

       <input type="hidden" name="IdArticle" value="<?php echo $rows['artID'] ?>" /> 
       </td> 
       <td> 
        <input type="submit" value="Envoyer le commentaire" /> 
       </td> 
      </tr> 
     </table> 
     </form> 
    </fieldset> 
<?php } ?> 

</div> 

我設置爲「刪除」和「添加」帖子,但根據帖子ID顯示和編輯失敗。我的數據庫連接和查詢工作正常。我認爲問題是指向頁面。你能告訴我這個嗎?

+0

我認爲你需要傳遞的參數的帖子ID,似乎你不將href鏈接傳遞。 – Goikiu

回答

0

對於menu.inc.php 被修改:<a href="index.php?page=home&article=<?php echo $rows['artID'];?>">

<?php 
$rArticles = selectArticles($conn); ?> 

<ul class="menu"> 
<?php while($rows = mysqli_fetch_array($rArticles)){ ?> 
<li><a href="index.php?page=home&article=<?php echo $rows['artID'];?>"><?php echo $rows['artTitre'];?></a></li> 

<?php } ?> 
</ul> 

然後我做了另一種形式進行編輯文章:articleformupdate.php

<?php $id = (int)$_GET['article']; 
$sql = 'SELECT * FROM articles WHERE artID = \''.$id.'\''; 
$rArticle = mysqli_query($conn, $sql); 
$row = mysqli_fetch_array($rArticle); 

?> 
<h2>Articles</h2> 
<form action="index.php?page=articles&action=update" method="post"> 

<table class="admin_form" cellpadding="0" cellspacing="0" width="100%"> 

<tr> 
    <td width="30%"><label for="TitreArticle">Titre</label></td> 
    <td><input id="TitreArticle" type="text" name="TitreArticle" value="<?php 
     if(isset($_POST['TitreArticle'])){ 
      echo ($_POST['TitreArticle']); 
     }else{ 
      echo $row['artTitre']; 
     } 
     ?>" /></td> 
</tr> 
<tr> 
    <td width="30%"><label for="AuteurArticle">Auteur</label></td> 
    <td><input id="AuteurArticle" type="text" name="AuteurArticle" value="<?php 
     if(isset($_POST['AuteurArticle'])){ 
      echo ($_POST['AuteurArticle']); 
     }else{ 
      echo $row['artAuteur']; 
     } 
     ?>" /></td> 
</tr> 
<tr> 
    <td valign="top"><label for="ContenuArticle">Contenu de l'article</label></td> 
    <td><textarea id="ContenuArticle" name="ContenuArticle"><?php 
    if(isset($_POST['ContenuArticle'])){ 
     echo ($_POST['ContenuArticle']); 
    }else{ 
     echo $row['artContenu']; 
    } ?></textarea></td> 

    </tr> 
<tr>  
    <td> 
    <input type="hidden" name="dateArticle" value="" /> 
    <input type="hidden" name="IdArticle" value="<?php 
     if(isset($_POST['IdArticle'])){ 
      echo ($_POST['IdArticle']); 
     }else{ 
      echo $row['artID']; 
     } ?>" /> 
     </td> 

     <td> 
     <a class="btn" href="index.php">Annuler</a> 
     <input type="submit" value="Envoyer" /> 
     </td> 
    </tr> 
</table> 
0

menu.inc.php

<?php $rArticles = selectArticles($conn); ?> 

<ul class="menu"> 
<?php while($row = mysqli_fetch_array($rArticles)){ ?> 

<li> 
    <a href="index.php?page=articleform&post_id=<?php echo $row['YOUR-POST-ID'];?>"> 
    <?php echo $row['artTitre'];?> 
    </a> 
</li> 

<?php } ?> 
</ul> 

而且在顯示/編輯頁面

$post_id = $_GET['post_id'] 
0

這樣做是從

$row = mysqli_fetch_array($rArticles) 

加入你的唯一參數的簡單方法到你的菜單一代。假設你的唯一的參數被命名爲「身份證」,是自動增量主鍵,並通過查詢返回,這將是:

<?php $rArticles = selectArticles($conn); ?> 

<ul class="menu"> 
<?php while($row = mysqli_fetch_array($rArticles)){ ?> 
<li><a href="index.php?page=articleform&article=<?php echo $row['id'];?>"><?php echo $row['artTitre'];?></a></li> 

<?php } ?> 
</ul> 

那麼接下來的頁面上,你可以從數據庫中使用要求您的資料(簡化的形式):

$id = (int)$_GET['article']; 
$sql = "SELECT data FROM table WHERE id=$id LIMIT 1"; // place your SQL here 
$rArticle = mysqli_query($conn, $sql); 
$row = mysqli_fetch_array($rArticle); 
相關問題