post view rank php - php

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

Related

Getting the filter query right while displaying db data in wordpress pages

Am trying to display some filtered data from a custom database of wordpress. but it shows me wrong filtered data.
I tried Select option and LIKE option for filtered data but id didnt work.
global $wpdb;
// QUERY HERE TO COUNT TOTAL RECORDS FOR PAGINATION
$total = $wpdb->get_var
("SELECT *
FROM wpfs_teachpress_pub
WHERE wp.fs_teachpress_pub.author LIKE '$halgeri%'");
$post_per_page = 10;
$page = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;
$offset = ( $page * $post_per_page ) - $post_per_page;
$result = $wpdb->get_results( "SELECT * FROM wpfs_teachpress_pub ORDER BY date DESC LIMIT $post_per_page OFFSET $offset");
$mydate = $wpdb->get_results( "SELECT date FROM wpfs_teachpress_pub" );
foreach ( $result as $print ) { ?>
<tr class="tp_publication">
<td class="tp_pub_info"> <p class="tp_pub_author"> <?php echo $print->author; ?><br/>
<span class="tp_pub_title"> <?php echo $print->title; ?></span><br/><?php echo $print->journal; ?> <span class="dd">Year Published: <?php echo date("Y", strtotime($print->date)); ?></span> </p>
</tr>
<?php }
?>
Currently it doesn't filter data that includes "Halgeri" as its author from author column.
Note: My author column structure "Halgeri; some autherx; some authory"
so one publication may contain diffrent authors but want to show "Halgeri's" all publications.
You have not applied filter query in $result. Please check it.$total and $result both have a different query. $total's query seems to be perfect.

an issue with php pdo mysql

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

MySql Query for WordPress Recent Posts Need to Link to Individual Posts

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.

mysql recieve and compare data from multiple tables

the database tables for debat and comments are the same. it seems that $title and $date isnt working. how can i get the information from both, and order by date?
<?php
$showwall = mysql_query("(SELECT * FROM debat WHERE user='$user') UNION (SELECT * FROM comments WHERE user='$user') ORDER BY date LIMIT 0, 25");
$results = array();
while($wall = mysql_fetch_array($showwall, MYSQL_ASSOC)){ // Line 21
$results[] = $wall;
<li class="comment">
<div class="comment">
<div class="comment-author vcard user frame">
<?php echo "<img src=images/avatar/".$select['avatar']." class=avatar avatar-70 photo height=70 width=70 />"; ?>
</div>
<div class="message">
<span class="reply-link"><?php echo ""; ?> Edit </span>
<div class="info">
<h2><?php echo $wall['title']; ?></h2>
<span class="meta">
<?php
$date->setTimestamp($wall['date']);
echo "Dato: ";
echo $date->format('d-m-Y') . "\n";
echo "Kl: ";
echo $date->format('H:i:s') . "\n";
?>
</span>
</div>
<div class="comment-body ">
<p><?php echo $wall['comment']; ?></p>
</div>
</div>
</div>
</li>
<?php } ?>
I want to add another database to load from, it seems to leave a blank result.
$showwall = #mysql_query("(SELECT * FROM debat WHERE user='$user') UNION (SELECT * FROM comments WHERE user='$user') UNION (SELECT * FROM nyheder WHERE user='$user') ORDER BY date LIMIT 0, 25");
the database contains tables with the same names, but some have more fields than others. So this is isnt the right way to do it?
$showwall = #mysql_query("(SELECT * FROM debat WHERE user='$user') UNION (SELECT * FROM comments WHERE user='$user') UNION (SELECT * FROM nyheder WHERE user='$user') ORDER BY date LIMIT 0, 25")
As mentioned in the comments, the # will suppress error messages; those are there for a reason. You're also not checking the result - adding or die (mysql_error()) would give you the error message from the database.
In this case, the issue is because you're using UNIONs. That will only work if all tables have columns in the same order, and I suspect that that isn't the case. I think you just need a simple JOIN:
SELECT * FROM debat d
INNER JOIN comments c ON d.user=c.user
INNER JOIN nyheder n ON d.user=n.user
WHERE d.user='$user'
One final note - you're using mysql_* functions, which are being removed from PHP. You should look at moving to mysqli_* or PDO instead - they both make it easier for you to write safer code.

SQL syntax showing date

I'm trying to get my article to fetch how many comments it has, and display the number. Here's the code:
<?php
$amount_get = mysql_query("SELECT * FROM comment WHERE id='" . mysql_real_escape_string($_GET['articleid']) . "'");
$comments = mysql_num_rows($amount_get);
$grab = mysql_query("SELECT id, news_title, news_content, news_date, news_author FROM articles ORDER BY id DESC LIMIT 5");
while($row = mysql_fetch_array($grab)){
?>
<div class="pane">
<li>
<h2><?php echo $row['news_title'] ?></h2>
<div class="meta">
<span class="color"> • </span>
<?php echo $row['news_date'] ?>
<span class="color"> • </span>
</div>
Comments: <?php echo $comments ?>
but for some reason, it stays on "0". This is what it looks like:
and my columns are id, articleid,name,comment,date & ip.
Your where clause is looking for id = articleid.
What you want is where articleid = articleid.
$amount_get = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['articleid']) . "'");
It looks like the SQL is faulty. Check your articleid GET variable. Also should it not be doing a different query and mysql_num_rows for each article's comments as each article likely contains different number of comments.
Try this:
SELECT
articles.id, news_title, news_content, news_date, news_author
(SELECT COUNT(*) FROM comment WHERE comment.id=articles.id) as comments
FROM articles
ORDER BY id DESC LIMIT 5
This will get you the first 5 articles and their related comment counts.
You could just use a single query.
$grab = mysql_query(
"SELECT a.id, a.news_title, a.news_content, a.news_date, a.news_author, count(*) as comment_count
FROM articles a
LEFT JOIN comment c ON c.articleid = a.id
ORDER BY a.id DESC LIMIT 5 GROUP BY a.id");
Then instead of
Comments: <?php echo $comments ?>
Do:
Comments: <?php echo $row['comment_count']; ?>
Let me know if the query is working, I'm not sure if the Group by is correctly placed there.

Categories