2014-12-26 98 views
2

下面的代碼應該顯示從我的表中隨機發布;它是這樣做的,但是它顯示的帖子與網址中的ID不同。隨機帖子的動態鏈接不符合帖子ID

如何讓它顯示URL中的內容?謝謝。

$db = new PDO('mysql:host=localhost;dbname=db;charset=utf8', 'user', 'pass');   
       $sql = $db->query('SELECT id FROM posts ORDER BY RAND() LIMIT 1'); 
       $row = $sql->fetch(PDO::FETCH_ASSOC); 
echo "<a href='random?page=".$row['id']."'>Randomize!</a><br>"; 

請找到「random.php頁面的相關部分如下:

$db = new PDO('mysql:host=localhost;dbname=db;charset=utf8', 'user', 'pass'); 
$sql = $db->query('SELECT * from posts ORDER BY RAND() LIMIT 1'); 

       $row = $sql->fetch(PDO::FETCH_ASSOC); 
       echo "<a href = 'random.php?page=".$row['id']."'>Re - Randomize!</a><br>"; 
       if ($row['sort'] == "image"){ 
       echo "<b>Update: </b>", $row['tag'], "<br>"; 
       echo "<img src='resize.php?w=240&img=./uploads/".$row['message']."'><br>"; 
       echo "<b>", "By: ", "</b>", ($row['username']), "<br>"; 
       echo "<b>", "At: ", "</b>", ($row['timestamp']), "<br>"; 
       if($row['locked'] !='locked'){ 
       echo "<a href ='edit.php?pid=".$row['id']."&login=true&edit=true' method='post' id ='form-id' data-ajax='false' '><b><span class = 'small_blue'>EDIT</span></b></a>";} 
       echo "&nbsp;", "&nbsp;"; 
       echo "<span class = 'small_green'>", ($row['edited']), "</span>", "&nbsp;", "&nbsp;", ($row['locked']); 

       echo "&nbsp;", "&nbsp;"; 

       } 

       else if ($row['sort'] == "audio"){    
       echo "<b>Update: </b>", $row['tag'], "<br>"; 
       echo "<audio controls>"; 
       echo "<source src='./uploads/".$row['message']."'>"; 
       echo "</audio>", "<br>"; 
       echo "<b>", "By: ", "</b>", ($row['username']), "<br>"; 
       echo "<b>", "At: ", "</b>", ($row['timestamp']), "<br>"; 
       if($row['locked'] !='locked'){ 
       echo "<a href ='edit.php?pid=".$row['id']."&login=true&edit=true' method='post' id ='form-id' data-ajax='false' '><b><span class = 'small_blue'>EDIT</span></b></a>";} 
       echo "&nbsp;", "&nbsp;"; 
       echo "<span class = 'small_green'>", ($row['edited']), "</span>", "&nbsp;", "&nbsp;", ($row['locked']);       
       } 

       else if ($row['sort'] == "video"){    
       echo "<b>Update: </b>", $row['tag'], "<br>"; 
       echo "<video controls>"; 
       echo "<source src='./uploads/".$row['message']."'>"; 
       echo "</video>", "<br>"; 
       echo "<b>", "By: ", "</b>", ($row['username']), "<br>"; 
       echo "<b>", "At: ", "</b>", ($row['timestamp']), "<br>"; 
       if($row['locked'] !='locked'){ 
       echo "<a href ='edit.php?pid=".$row['id']."&login=true&edit=true' method='post' id ='form-id' data-ajax='false' '><b><span class = 'small_blue'>EDIT</span></b></a>";} 
       echo "&nbsp;", "&nbsp;"; 
       echo "<span class = 'small_green'>", ($row['edited']), "</span>", "&nbsp;", "&nbsp;", ($row['locked']); 
       } 

       else { 
       echo "<b>Update:</b><br>"; 
       echo ($row['message']), "<br>"; 
       echo "<b>", "By: ", "</b>", ($row['username']), "<br>"; 
       echo "<b>", "At: ", "</b>", ($row['timestamp']), "<br>"; 
       if($row['locked'] !='locked'){ 
       echo "<a href ='edit.php?pid=".$row['id']."&login=true&edit=true' method='post' id ='form-id' data-ajax='false' '><b><span class = 'small_blue'>EDIT</span></b></a>";} 
       echo "&nbsp;", "&nbsp;"; 
       echo "<span class = 'small_green'>", ($row['edited']), "</span>", "&nbsp;", "&nbsp;", ($row['locked']); 
           } 
+2

你的代碼從頁面'random'使用'$ _GET ['page']'/'$ row ['id']'value? – Sean

+0

@Sean抱歉,我的時間即將到期。將很快發佈。謝謝。 – adeoba

+0

@Sean,我發佈了'random.php'頁面。謝謝。 – adeoba

回答

0

如果我沒有記錯的話,你想,如果頁面被設置爲獲取職位,否則隨機化?

如果這是你願意,你可以試試這個東西(假設頁面是整數,ID是唯一的):

$id = filter_input(INPUT_GET, 'page', FILTER_SANITIZE_NUMBER_INT); 
$q = ($id) ? "SELECT * from posts WHERE id=$id" : "SELECT * from posts ORDER BY RAND() LIMIT 1"; 
$db->query($q); 

然後再隨機只是鏈接到的網頁不帶參數:

echo "<a href='random.php'>Re - Randomize!</a><br>"; 

希望它有幫助。

親切的問候。

0

我random.php網頁缺少一個$ _GET:

$tbl='posts'; 
      $id=$_GET['page']; 

      $db = new PDO('mysql:host=localhost;dbname=db;charset=utf8', 'user', 'pass'); 
      $sql = $db->query("SELECT * from $tbl WHERE id = ".$_GET['page']." "); 
       $row = $sql->fetch(PDO::FETCH_ASSOC); 

謝謝大家!