I want to display just top 5 rows from the databse

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ankit.pandeyc012
Forum Newbie
Posts: 8
Joined: Sun Nov 14, 2010 11:25 pm

I want to display just top 5 rows from the databse

Post 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]
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

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

Post 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.
ankit.pandeyc012
Forum Newbie
Posts: 8
Joined: Sun Nov 14, 2010 11:25 pm

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

Post 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..................
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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
Post Reply