I need to display table of contents from bottom to top. Since when a new row is inserted, it appears at bottom and when I access it using mysqli_fetch_array it shows the most recent inserted row at the bottom. The code is like this:
<?php
$abc = mysqli_connect("localhost","root","","members") or die(mysqli_error($abc));
$select_query = "SELECT title, url, photographer, genere, timestamp FROM gallery";
$select_query_result = mysqli_query($abc, $select_query) or die(mysqli_error($abc));
?>
And somewhere in html, this code appears.
<ol class="pictures">
<?php while($row = mysqli_fetch_array($select_query_result)) { ?>
<li class="thumbnail" data-div="<?php echo $row['genere'] ?>,<?php echo $row['photographer'] ?>,<?php echo $row['title'] ?>,<?php echo $row['timestamp'] ?>" style="background-image: url(<?php echo $row['url'] ?>)">
</li>
<?php } ?>
</ol>
So, what should I do to display it in reverse order so that I can get the most recent entry on top while displaying.
Simply order your SQL Query by using either
ORDER BY attribute ASC/DESC
So for example if you want the most recent entry on top, simply change your SQL query to:
"SELECT title, url, photographer, genere, timestamp FROM gallery ORDER BY timestamp desc"
And it should work. You can do this with any attribute you want. I recommend to do it with the primary key (ID) if you have one, but since you're not selecting it, you can do it with your timestamp too.
https://www.tutorialspoint.com/sql/sql-sorting-results.htm
Just Modify Your SQL Query
Use ORDER BY
SELECT title, url, photographer, genere, timestamp FROM gallery ORDER BY id DESC ;
If you doesn't want to use ORDER BY:
for($i = count($select_query_result), $i > 0; $i--) {
// Actions
}
Use Order By:
<?php
$abc = mysqli_connect("localhost","root","","members") or die(mysqli_error($abc));
$select_query = "SELECT title, url, photographer, genere, timestamp FROM gallery ORDER BY timestamp DESC";
$select_query_result = mysqli_query($abc, $select_query) or die(mysqli_error($abc));
?>
Related
I am making an extremely basic posting system, and I cant seem to figure out how to get the most recent rows from a certain table. I have tried other solutions offered here, but my posts were randomly placed. How would I accomplish this? My code is below.
function load_posts(){
$posts_sql = "SELECT * FROM posts";
$posts_result = Phoenix\Database\Database::$database->query($posts_sql);
while($row = $posts_result->fetch_assoc()){
$posts_display = '
<div class = "card" style = "width:500px">
<div class = "card-body">
<div class = "card-title">'. $row['username'] .'</div>
<p>'. $row['content'] .'</p>
</div>
</div>
';
echo $posts_display;
}
}
Again, I want the posts to be displayed from most recent, to old.
You need to have information in each row that captures this information. The most common suspects are:
an auto-incrementing id
a creation date
Then you just ask the database to sort the results. For instance, if post_id is an auto-incremented id:
select p.*
from posts p
order by p.post_id;
SELECT * FROM TableName ORDER BY id DESC
// order by should be with primary key
I want to print last post from specific category
Could you please help me with the code?
I want to put on $record manual, for example: I put "design", and just show the last post in design category.
And one thing: table blog it's separate from table record.
thanks
<?php $category = $record ['record']; { ?>
<?php foreach($db->query("select * from blog where category = '$category' order by id desc") as $row){ ?>
<li>
<a href="<?php echo $row['image']; ?>">
<div class="gallery-item"><img src="<?php echo $row['image']; ?>" alt="<?php echo $row['title']; ?>"></div>
</a>
</li>
<?php } } ?>
If you want the most recent post, you could change your SQL to select it.
Try something like this:
select * from blog where category = '$category' order by {DATE_FIELD} desc limit 1
You need to exchange the string {DATE_FIELD} with the actual date field in your table. This select would return the most recent dataset and only that one.
EDIT: You can also sort by youre id if the date isn't changed or the changed date is stored in another field.
select * from blog where category = '$category' order by id desc limit 1
i have made a image slider(or what ever you want to call it) and it displays the 6 latest images. Under the current bigger image you have all 6 of the most recent images , and the process works all fine but when you click the smaller image, it doesn't bring up the big image straight away. Because the very latest image is first its 'active' and has its own <a href="#1> and so the href="#1" is entered as i can manipulate the a tag as it is there. But as i used a foreach to bring up the next 5 images from 2nd to 6th in descending order from date submitted, i cant give each individual result there own href"#number" so they can link up with the bigger images, is there a way to assign each result a number by taking what position it sits within the query then adding 1or2 to the answer which would then give the corresponding number in the href so when clicked, the larger image which is linked to the href number too, it will make the larger image appear straight away.
the code from the tutorial that i have amended looks like this ..
<?php
$latest_headlines = get_latest_headlines();
foreach ($latest_headlines as $latest_headline) {
?>
<img src="img/<?php echo $latest_headline['img_title'].'.'.$latest_headline['img_ext']; ?>" class="nav-thumb" alt="<?php echo $latest_headline['title']; ?>" />
<?php
}
?>
<div id="movers-row">
<?php
$recent_headlines = get_recent_headlines();
foreach ($recent_headlines as $recent_headline) {
?>
<div><img src="img/<?php echo $recent_headline['img_title'].'.'.$recent_headline['img_ext']; ?>" class="nav-thumb" alt="<?php echo $recent_headline['title']; ?>" /></div>
<?php
}
?>
</div>
And here are my two functions to get the results, and before people start picking at all the problems ive done, just want to say this is the way ive learnt it im new, it maybe all wrong but its what stuck in my head to do things this way and its worked(ish) so far....
function get_recent_headlines() {
$sql = "SELECT *
FROM `story`
ORDER BY `submit_date` DESC
LIMIT 1, 5 ";
$result = mysql_query($sql) or die(mysql_error());
$recent_headlines = array();
while (($row = mysql_fetch_array($result)) !== false) {
$recent_headlines[] = array(
'title' => $row['title'],
'img_title' => $row['img_title'],
'img_ext' => $row['image_ext']
);
}
return $recent_headlines;
}
function get_latest_headlines() {
$sql = "SELECT *
FROM `story`
ORDER BY `submit_date` DESC
LIMIT 1 ";
$result = mysql_query($sql) or die(mysql_error());
$lastest_headlines = array();
while (($row = mysql_fetch_array($result)) !== false) {
$latest_headlines[] = array(
'title' => $row['title'],
'img_title' => $row['img_title'],
'img_ext' => $row['image_ext']
);
}
return $latest_headlines;
}
change your query of your function get_recent_headlines()
it uses a variable, and increment it in the SELECT statement:
`$sql = "SELECT *,(#row:=#row+1) AS rowno
FROM `story` inner join (SELECT #row:=0) AS row_count
ORDER BY `submit_date` DESC
LIMIT 1, 5 ";`
You can use a user variable to get a sequence number within MySQL.:-
SELECT Sub1.*,#aCount:=#aCount+1 AS aSequence
FROM (SELECT *
FROM `story`
ORDER BY `submit_date` DESC) Sub1
CROSS JOIN (SELECT #aCount := 0) Sub2
LIMIT 1, 5
ok so i coded in a news section and everytime i insert new news,its shows below the old one.
i want to make it ORDER BY id but make it start like backwards.i dont know how to explain but yeh
i want them to be ordered by the newest added by the id so if the 1st row that was inserted's id is 1 then i want it to show below the next id.
so row with id = 2 will be here
so row with id = 1 will be here
thats how i want it to be, instead of it being like this
so row with id = 1 will be here
so row with id = 2 will be here
.
Sorry for my bad explanation i hope this is understandable
heres my code so far
<?php
require("include/config.php");
$sqlnews = mysql_query("SELECT * FROM news ORDER BY id");
while($row = mysql_fetch_array($sqlnews)) {
$dbdate = $row['date'];
$dbnews = $row['news'];
echo "<h1><strong>$dbdate</strong></h1>";
echo "<div class='content'>$dbnews</div><br><br>";
}
?>
add DESC in your ORDER BY clause
SELECT * FROM news ORDER BY id DESC
by default, it is in ASC mode.
SELECT * FROM news ORDER BY id DESC
DESC is the descending keyword ASC is ascending
If you specify neither then default behaviour is ascending
Just use DESC keyword in your sql query.
$sqlnews = mysql_query("SELECT * FROM news ORDER BY id DESC");
However, it isn't really such a good idea to use id, because semantically, there is nothing preventing somebody from changing the sequence which counts up automatically assigning the ID.
Therefore, you should add a column created_at. Everytime you insert a row, you can use the SQL function NOW().
The advantage is that you can say:
SELECT * FROM news WHERE created_at <= NOW() ORDER BY created_at DESC
This means that you can schedule news items ahead of time, and it will automatically display when the date/time arrives!
$sqlnews = mysql_query("SELECT * FROM news ORDER BY id DESC");
try this:
just have to add
order by id DESC
Just replace your code by this code:
<?php
require("include/config.php");
$sqlnews = mysql_query("SELECT * FROM news ORDER BY id DESC");
while($row = mysql_fetch_array($sqlnews)) {
$dbdate = $row['date'];
$dbnews = $row['news'];
echo "<h1><strong>$dbdate</strong></h1>";
echo "<div class='content'>$dbnews</div><br><br>";
}
?>
I am trying to order data from the table 'tech_inquiry' by the Field 'number' in descending order. This should put the results in order by year. Each 'number' field corresponds to another field with a title/date (what is actually viewed by visitors) which I can't sort by because the date is at the end of the title and not always in the same place.
Also, the table 'tech_inquiry_allowed' determines what is viewable to who when logged in.
With that said, here is the code:
<?
$query2=mysql_query("SELECT * FROM tech_inquiry_allowed where term_code = '$term_code' ");
while($row2=mysql_fetch_assoc($query2))
{
$id2=$row2['id'];
$query3=mysql_query("SELECT * FROM tech_inquiry WHERE id= '$id2' ORDER BY number DESC");
$row3=mysql_fetch_assoc($query3);
$name3=$row3['name'];
?>
<hr />
<li><? echo $name3; ?> </li>
<?
}
?>
Also, I have another 'admin' section that is able to order data correctly. The only difference is there is no conditional 'where' clause because no user authentication is needed (it is used to add data to the table).
Here is that code:
<?
$query2= mysql_query("SELECT * from tech_inquiry ORDER BY number DESC");
while($row2=mysql_fetch_assoc($query2))
{
$id2=$row2['id'];
$name2=$row2['name'];
?>
<hr />
<li><? echo $name2; ?> </li>
<?
I am wondering if it might be the fact that we are running a query inside a loop.
There are a number of problems here. First of all you only need one query to accomplish this. Please read up on SQL query writing when you get a moment. You will find it to be VERY helpful.
Second, you are using way more code than you need to. Below is the much simplified, cleaner, and probably faster code.
<?php
$query = mysql_query("SELECT * FROM tech_inquiry ti WHERE id IN (SELECT id FROM tech_inquiry_allowed where term_code = '$term_code') ORDER BY ti.number");
while ($row = mysql_fetch_object($query)) {
echo "<hr />\n<li><a href='get_file.php?id={$row->id}'>{$row->name}</a></li>";
}
?>
You are not looping the inner query. Anyway, you should be using a single query for this:
SELECT allowed.*, inquiry.*
FROM tech_inquiry_allowed allowed
INNER JOIN tech_inquiry inquiry
ON inquiry.id = allowed.id
WHERE term_code = '$term_code'
ORDER BY inquiry.number DESC