Page 1 of 1

I want to display just top 5 rows from the databse

Posted: Sun Nov 21, 2010 12:35 am
by ankit.pandeyc012

Code: Select all

<?php
require_once('upper.php');

require_once('database.php');
if(isset($_COOKIE['LoginIdCookie']))
{
echo '<h4>Welcome '.$_COOKIE['LoginIdCookie'].'</h4>';
}
else{ echo '<h4>Events</h4>';}
$result=mysqli_query($dbc,"select * from events") or die('Not Connected');
echo "<html>
		<body>";
  echo "<form method='post' action='EventParticipator.php'>"; 
  echo	"<u><h4>Please tick events in which you want to participate</h4></u>";
  /*echo "<table cellspacing='0' cellpadding='15'>
  		<th><b>Event Title:</b></th>
  		<th ><b>Event City:</b></th>
		<th><b>Content:</b></th>
		<th><b>Images:</b></th>
		<th><b>Event Date:</b></th>";*/
		
//while($row=mysqli_fetch_array($result))
[b]$row=mysqli_fetch_array($result);
for($i=0;$i<5;$i++)
{
$Title=$row['Title'];
$City=$row['City'];
$Content=$row['Content'];
//$Photo=$row['Photo'];
$Date=$row['Date'];
$EventId=$row['EventId'];
$Photo=$row['Photo'];
//echo $Photo;

echo "<tr><td><img src='$Photo' width='120' height='90'/></td><td>$City</td><td>,</td><td>$Date</td><td>:</td><td>$Title</td></tr>
		<tr><td>$Content</td></tr>
		<tr><td><a href='#'>Know more</a></td><td>/</td><td><a href='#'>Click here to participate</a></td></tr>"; 
}[/b]
//echo "<tr><td><input type='submit' name='submit' value='Participate'</td>";
if(isset($_COOKIE['LoginIdCookie']))
{
echo "<td><a href='log_out.php'>Log out</a></td>";
echo "<td><a href='EditActivity.php'>Edit Your Activities</a></td></tr>";
}
echo "</table></form></body></html>";
require_once('lower.php');
?>




[text]Hi friends ..........
I write above code to display top 5 rows from the database but it not works properly. It displays first row 5 times which is undesireable.
Anyone plz help?????????????????
Thx in advance..................[/text]

Re: I want to display just top 5 rows from the databse

Posted: Sun Nov 21, 2010 12:52 am
by s992
Change your query to:

Code: Select all

SELECT * FROM events LIMIT 5
and put

Code: Select all

$row=mysqli_fetch_array($result);
inside your for loop instead of outside.

Re: I want to display just top 5 rows from the databse

Posted: Sun Nov 21, 2010 1:02 am
by ankit.pandeyc012
But Now if I update a new row in database. It not update on output.
I mean to say as admin update this table, these updation must be shown to user......
Help plz..................

Re: I want to display just top 5 rows from the databse

Posted: Sun Nov 21, 2010 5:42 am
by Celauran
Then you'll need to change your query. Right now it's returning the first five results. If you want the five most recent, you'll need an ORDER BY clause. Date would be ideal, if you have such a column in that table.

Code: Select all

SELECT *
FROM events
ORDER BY date DESC
LIMIT 5