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
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 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));
?>
This code displays the most recent 3 posts in WordPress. However, when following the links, every link connects to the most recent post, not the post associated with the excerpt. I need the excerpt links to connect to their associated full posts.
I did not code this and I realize it's deprecated. I've also tried WP recent posts function and shortcodes which don't work. The PHP code is so messed up that it causes lots of issues. I have very limited knowledge of MySql.
$sql = mysql_query("SELECT * from wp_term_relationships where term_taxonomy_id = '3' ORDER BY object_id DESC LIMIT 3 ");
while ($row = mysql_fetch_assoc($sql))
{
$object_id = $row['object_id'];
$sql_posts = mysql_query("SELECT * From wp_posts where ID = '$object_id' AND post_status = 'publish' AND post_type = 'post' ORDER BY ID DESC LIMIT 3");
while($row_posts = mysql_fetch_assoc($sql_posts))
{?>
<div class=" gaming_news_col col-lg-4 col-md-4 col-sm-4">
<h4><a href="<?php the_permalink() ?>"><?php echo $row_posts['post_title'];?></h4>
<p><?php
$content = $row_posts['post_content'];
$post_content = myTruncate($content, 150, " ");
echo $post_content;
?></p>
The method myTruncate cuts the text to some specific range, for instance, at your case, it will show only first 150 characters. If you want to show the full text, do not use myTruncate at all. Just echo the content.
$content = $row_posts['post_content'];
echo $post_content;
This should do the trick and not cut the text to specified limit.
Hi everyone i have one question for my dashboard rank panel.
the number of page views I want to sort from smallest to largest.
For example: a total of 100 times a page, but the other page ... 75-74-73-68-45-30 80 times.
I want to get older and smaller than the sequencing of numbers.
My php code is this. post_view is for how many people visit my post .
<?php include("connect.php");
$select_posts = "SELECT * FROM posts LIMIT 0,9";
$run_posts = mysql_query($select_posts);
while($row=mysql_fetch_array($run_posts)){
$post_id = $row['post_id'];
$post_title = $row['post_title'];
$post_date = date('d-m-y');
$post_author = $row['post_author'];
$post_view = $row['post_view'];
?>
<div class="div_name">
<div class="page-id"><?php echo $post_id; ?></div>
<div class="post_title"><?php echo $post_title; ?></div>
<div class="post-view"><?php echo $post_view; ?> </div>
</div>
<?php } ?>
Use SQL's ORDER BY in the query:
SELECT * FROM posts ORDER BY post_view ASC LIMIT 0,9
If this field is not set to a numerical, you can use the following syntax
SELECT * FROM posts ORDER BY cast(post_view as int) ASC LIMIT 0,9
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