简体   繁体   中英

Dynamic Link for Random Post not Tallying with Post ID

The code below is supposed to display a random post from my table; it does this, but the post it displays is NOT the same in ID as in the URL.

How can I make it display what it says in the URL? Thanks.

$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>";

Please find below the relevant part of the 'random.php' page:

$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']);
                               }

if I'm not mistaken you want to retrieve the post if page is set, and randomize otherwise?

If that's what you want you could try this (supposing page is integer and id is unique):

$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);

Then to re-randomized just link to the page without parameters:

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

Hope it helps.

Kind regards.

My random.php page was missing a $_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);

Thanks all!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM