Forum Moderators: coopster

Message Too Old, No Replies

How to display random titles from latest 10 records?

         

phprockz

7:45 am on Jun 17, 2006 (gmt 0)

10+ Year Member



hi,

Please suggest How to display random titles from last latest 10 records?.

Here is my code to generate 10 latest records from mysql.

<?php require_once('Connections/Rss.php');?>
<?php
$maxRows_rsnews = 10;
$pageNum_rsnews = 0;
if (isset($_GET['pageNum_rsnews'])) {
$pageNum_rsnews = $_GET['pageNum_rsnews'];
}
$startRow_rsnews = $pageNum_rsnews * $maxRows_rsnews;

mysql_select_db($database_Rss, $Rss);
$query_rsnews = "SELECT Title FROM content ORDER BY `timestamp` DESC";
$query_limit_rsnews = sprintf("%s LIMIT %d, %d", $query_rsnews, $startRow_rsnews, $maxRows_rsnews);
$rsnews = mysql_query($query_limit_rsnews, $Rss) or die(mysql_error());
$row_rsnews = mysql_fetch_assoc($rsnews);

if (isset($_GET['totalRows_rsnews'])) {
$totalRows_rsnews = $_GET['totalRows_rsnews'];
} else {
$all_rsnews = mysql_query($query_rsnews);
$totalRows_rsnews = mysql_num_rows($all_rsnews);
}
$totalPages_rsnews = ceil($totalRows_rsnews/$maxRows_rsnews)-1;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Random</title>
</head>

<body>
<table border="0">

<?php do {?>
<tr>
<td><?php echo $row_rsnews['Title'];?></td>
</tr>
<?php } while ($row_rsnews = mysql_fetch_assoc($rsnews));?>
</table>
</body>
</html>
<?php
mysql_free_result($rsnews);
?>

ArthurDent

10:40 am on Jun 17, 2006 (gmt 0)

10+ Year Member



try in mysql: order by rand(), timestamp limit 10

phprockz

12:17 pm on Jun 17, 2006 (gmt 0)

10+ Year Member



Not working...its showing random titles from db instead of from latest 10 articles

Habtom

1:19 pm on Jun 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You might try to use it outside the sql query.

$random_result = rand($rsnews);

Hab

phprockz

1:25 pm on Jun 17, 2006 (gmt 0)

10+ Year Member



i think rand funtion need two parameters...this ( $random_result = rand($rsnews);) not working when i implemented

jatar_k

5:41 pm on Jun 17, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



use your query to get the latest 10 results
read them into an array
use rand() to get a number between 0 and 9
output from the array at the position specified by the number you got from rand()

joaquin112

4:52 am on Jun 18, 2006 (gmt 0)

10+ Year Member



Nice advice ;)