I want to display just top 5 rows from the database

<?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))
$row=mysqli_fetch_array($result);
[B]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');
?>

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…

You were saying that you want to show the top 5 rows from the database.

Work out what you really want, and then describe it to us.

Restict the MySQL query results by using a LIMIT clause (details are at the SELECT documentation page):


SELECT *
FROM events
LIMIT 5

Then use a while loop, as shown in Example #2 of the documentation page for the mysql_query command.

EDIT: Beat me to it :slight_smile:

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…

$row is an associative array and not an indexed array.

Therefore you should be using a while loop and not a for loop with $i as the counter.

instead of

 
for([COLOR=#0000bb]$i[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#0000bb]0[/COLOR][COLOR=#007700];[/COLOR][COLOR=#0000bb]$i[/COLOR][COLOR=#007700]<[/COLOR][COLOR=#0000bb]5[/COLOR][COLOR=#007700];[/COLOR][COLOR=#0000bb]$i[/COLOR][COLOR=#007700]++)[/COLOR]

try

 
while($row=mysqli_fetch_array($result))

which is what you have commented out above.

and

[LEFT]SELECT *
FROM events
LIMIT 5[/LEFT]

as suggested by pmw57 should be what you are after as the query.

with the for loop all you will get is the first record in $result displayed 5 times

Sir,
First I want to tell you that above code leaves top row and starts from the second row. Ex.–It should print 1,2,3,4 but it prints 2,3,4.
Second problem is when admin update a new row it becomes bottom row in phpMyadmin. So, this updated row never displays.

Plz help…
Thx in advance…

[COLOR=#0000bb]

 
[COLOR=#0000bb]$row[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#0000bb]mysqli_fetch_array[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]$result[/COLOR][COLOR=#007700]);[/COLOR] 

This line gets the first of however many rows have been returned in $result.

With your for loop, where are you getting the next row in $result?
[/COLOR]

mysql_fetch_array() by default returns both an indexed and associative array.

you are using an index $i in your for loop declaration but then referring to the row elements with associative keys. The 2 need to be consistent with each other.

imho the easiest option is to use mysql_fetch_assoc() and then refer to $row using associative and not indexed keys.

It works for me
select * from events order by EventId DESC LIMIT 5

so you asked the wrong question at the start.

what you really wanted was the bottom 5 rows and not the top 5 rows as you originally asked for.

glad it’s sorted out now :drink:

that won’t return the top 5, that will return any 5

LIMIT without ORDER BY really makes very little sense

actually, no :slight_smile:

ORDER BY EventId DESC LIMIT 5 will return the 5 rows with the largest EventID values, and most people would, yes, call them the top 5

the bottm 5 would be given by ORDER BY EventId ASC LIMIT 5

:cool:

that’s ok :slight_smile: you’re entitled to your opinion.

I’m in the group that call that the bottom 5 rows.

Top and bottom are relative terms that depend on the reference point you choose to use.

so you would call this –

ORDER BY salary DESC LIMIT 5

the bottom 5 salaries?

well, you, sir, are entitled to your opinion, too… even if it’s wrong

:smiley: :smiley:

I’m not sure what the issue is here.

you posted your opinion and I replied with mine.

I don’t have an issue if you disagree with my opnion :slight_smile:

unless this is an attempt on your part to hijack this thread we can just agree to disagree or if you need to discuss further, start a new thread on the topic :slight_smile:

Bottom changes along with the context, depending on whether it is the context of a numerically ordered list, or a table with new entries being added to it.

Perhaps the two of you were meaning different types of bottom?

yep agree :agree:

as I said

order based on auto_incrementing values is no different than order based on any other column

the rows are inserted wherever there is space, and you will see them “in order” only if you use an ORDER BY clause in the SELECT

i ~think~ i know where this idea of “bottom” comes from – if you’re using some front-end like phpmyadmin to browse your table data, then the rows usually appear with the smaller auto_increment numbers at the top of the listing and larger numbers at the bottom

however, given my example with salary, there is a more prevalent interpretation for top and bottom, isn’t there

if you were to list out salaries in ascending sequence, like this –

2
4
5
7
8
9
11
23
37

then, yes, the larger numbers are at the “bottom” of the list

however, if you were to ask someone which are the top 5 salaries, what do you get?

i think it is important to make this clarification, because “top” and “bottom” when used in the context of ORDER BY (rather than listings) are very common concepts in the database world, and we shouldn’t be introducing confusion based on some other idea

it’s not just “opinion”

given a list of values like 5, 8, 7, 11, 2, 23, 4, 9, 37…

my “opinion” is that the top 5 values are 37, 23, 11, 9, and 8

your “opinion” appears to be the opposite

and i am not hijacking the thread, this goes to the very heart of what is meant by “top” and “bottom”

please try being a little less defensive, okay?

:slight_smile: