mySQL Order by Most Commented and Least Commented - php

I'm trying to order a list of items based on the amount of comments for each topic as shown below:
$page = $_GET['page'];
$query = mysql_query("SELECT * FROM topic WHERE cat_id='$page' LIMIT $start, $per_page");
if (mysql_num_rows($query)>=1)
{
while($rows = mysql_fetch_array($query))
{
$number = $rows['topic_id'];
$title = $rows['topic_title'];
$description = $rows['topic_description'];
//get topic total
$sqlcomment = mysql_query("SELECT * FROM comments WHERE topic_id='$number'");
$commentnumber = mysql_num_rows($sqlcomment);
// TRYING TO ORDER OUTPUT ECHO BY TOPIC TOTAL ASC OR DESC
echo "
<ul>
<li><h4>$number. $title</h4>
<p>$description</p>
<p>$topictime</p>
<p>$commentnumber</p>
</li>
</ul>
";
}
}
else
{
echo "<p>no records available.</p><br>";
}
What would be the best way to order each echo by $num_rows (ASC/DESC values)? NOTE: I've updated with the full code - I am trying to order the output by $commentnumber

The first query should be:
SELECT t.*, COUNT(c.topic_id) AS count
FROM topic AS t
LEFT JOIN comments AS c ON c.topic_id = t.topic_id
WHERE t.cat_id = '$page'
GROUP BY t.topic_id
ORDER BY count
LIMIT $start, $per_page
You can get $commentnumber with:
$commentnumber = $rows['count'];
You don't need the second query at all.

First of all you have error here
echo "divs in order from least to greatest "number = $num_rows"";
It should be
echo "divs in order from least to greatest number = " . $num_rows . "";
And about the most commented try with
$sql = "SELECT * FROM `table` WHERE `id` = '$id' ORDER BY column DESC/ASC";
Or if there is not count column try with
$sql = "SELECT * FROM `table` WHERE `id` = '$id' ORDER BY COUNT(column) DESC/ASC";

Related

How to match two table columns in sql

I have this code
$db = \Config\Database::connect();
$query = $db->query("select * from g WHERE g_status = '0' ORDER BY g_date ASC Limit 10;");
foreach ($query->getResult() as $g) {
$g_amount = $g->g_amount;
}
$query2 = $db->query("select * from g WHERE p_status = '0' ORDER BY p_date ASC Limit 10;");
foreach ($query2->getResult() as $p) {
$p_amount = $p->p_amount;
}
if($p_amount == $g_amount){
echo "do something";
}else{
echo "No match";
}
Here I am trying to match between table g and table p.... if any column in table g is == any column in table p regardless of the number of column, do something but it always echo "NO match"
I put "Limit 10" in case there is much number of rows in the table, it will only match the first 10th row with the "ordering" command.
Please I need some help.
First, get data as an array
$db = \Config\Database::connect();
$query = $db->query("select * from g WHERE g_status = '0' ORDER BY g_date ASC Limit 10;");
$g_results = $query->getResult('array');
$g_amounts = array_column($g_results,'g_amount');
$query2 = $db->query("select * from g WHERE p_status = '0' ORDER BY p_date ASC Limit 10;");
$p_results = $query2->getResult('array');
$p_amounts = array_column($p_results,'p_amount');
foreach(array_intersect($g_amounts,$p_amounts) as $amount){
echo "do something";
}
Why not use a JOIN and see if it returns something? Not sure if my syntax is correct, I don't do JOINs very often:
SELECT * FROM g g_table JOIN p p_table ON g_table.g_amount = p_table.p_amount WHERE g_table.status = '0' AND p_table.status = '0'

remove duplicate camma from mysql query

I am trying to fetch data with mulyiple value with Mysql IN operator but problem I am getting to much (,,,).
Here is my query :
$get_id = "select rel_blog from pages";
$resu = mysql_query($get_id);
$p_id="";
$rel_b="";
$i = 1;
while ($page_id = mysql_fetch_array($resu)){
$p_id.= $page_id['page_id'];
$rel_b.= $page_id['rel_blog'];
}
$p_id."<br>";
echo $rel_b."<br>";
echo $getblog = "SELECT * FROM pages WHERE rel_blog IN ($rel_b) order by add_date desc limit 7";
Ouery output is Like this this is not proper form so please help me .
SELECT * FROM pages WHERE rel_blog IN
(,114,93,126108default,,,,default,,,,default,,default,,,default,,default,,,,,,,default,,default,,,default,,default,,,,,,,,,,,,,,,,,,,,,,,,,,123,145,144,148,81,107default,,,,,default,,,,,,145,138,118default,,,,,,default,,,,,,,default,,,,,,,default,,,,,,,default,,,118default,,,,default,,,,,,,,default,,,,,,,,,default,,default,,,,,,,,default,,,,,,,,,default,,default,,,,,default,,,,,,default,,,,,,,,,default,,,,,,,,default,,,,,default,,,,,,,123,122,125,113default,,,default,,,default,,124,145140default,,,,,,,,,,,,,default,,,,,,,,default,,,default,,,default,,,,default,,,,default,,default,,,,default,,,default,,,default,,default,,,,,,,default,,,,,,default,,,,default,,,,,,default,,,default,,,,137default,,,,,,,default,,,default,,,default,,,,,,default,,,,,,,,,,default,,,,,,,,82,83default,,,,,,default,,,,default,,,,default,,,default,,,default,,,,default,,,,default,,,,default,,,,default,,,default,,,,default,,,,default,,,default,,,default,,,,,,default,,,,,,default,,,,,default,,,default,,,,,default,,,,,default,,,,default,,,default,,default,,default,,default,,,,,,,default,,,,default,,default,,,,default,,,,default,,,,default,,,,,default,,,,default,,,,,default,,,,,,,default,,,,default,,,,,default,,,,,,default,,,,,,,default,,,,,,default,,,,,,,default,,default,,default,,,,,default,,,default,,,default,,,default,,,,default,114,default,,,,,,117,116default,,,,,default,,,,117,116default,,,,default,,,,default,,,,,default,,,,default,,,,,default,,,,default,,,default,,default,,,,,,default,,,,,,default,,,,,108default,,,,118,129,121,143,80,82,81,148,117,146,107,88,118,82,80,81,148,143,123,118,107,142,116,110,128,127,111,109,125,113,123,118,85,118,128,109,126,93,114,82,142,108,111,90,113,123,144,148,108,107,142,116,115,110,128,127,113,118,144,146,80,148,107,143,135,125,143,110,115,118,132,146,80,123,81,148,107,116,108,109,125,135,113,123,144,118,116,108,127,147,125,123,144,129,107,142,112,116,135,113,123,144,132,118,107,85,123,144,118,82,148,107,123,132,146,148,107,108,122,89,136,146,80,81,117,148,142,107,110,147,119,82,80,145,129,146,80,148,107,111,109,125,113,123,118,148,81,107,131,115,109,121,125,135,113,123,144,107,131,126,93,141,114,124,139,113,91,144,118,81,117,148,107,142,115,143,135,91,118,142,116,87,143,140,123,144,132,145,129,146,82,80,117,91,118,148,137,113,141,93,143,136,123,144,146,82,118,148,85,112,108,127,122,87,143,141,91,144,146,80,82,81,117,148,107,115,109,91,129,146,82,80,117,148,81,142,107,110,130,138,89,126,113,default,default,118,146,82,81,148,107,142,115,109,126,default,123,144,145,80,146,82,81,117,142,128,138,135,93,140,123,144,145,132,82,80,117,83,142,130,140,145,129,82,80,81,108,111,138,89,143,125,145,123,118,82,80,81,117,88,112,108,147,139,119,140,123,144,118,80,82,81,142,107,85,108,115,114,144,118,129,146,80,81,83,120,148,107,142,128,127,110,115,91,123,145,82,117,146,81,80,148,82,80,117,81,148,83,120,107,142,88,131,118,129,146,80,117,107,83,88,142,130,89,125,139,134,113,145,144,118,132,123,82,80,146,81,148,83,123,91,137,140,134,139,113,141,124,93140,150,150)
order by add_date desc limit 7
Actually I can't figure out how you are getting even a single comma... This is how you can try it out.
$get_id = "select rel_blog,page_id from pages";
$resu = mysql_query($get_id);
$p_id="";
$rel_b="";
$i = 1;
while ($page_id = mysql_fetch_array($resu)){
$p_id.= $page_id['page_id'];
if($page_id['rel_blog']!=="" && $page_id['rel_blog']==="default") {
$rel_b.= $page_id['rel_blog'] .",";
}
}
$p_id."<br>";
$rel_b = substr($rel_b,0,-1); // get rid of the last comma
echo $rel_b."<br>";
echo $getblog = "SELECT * FROM pages WHERE rel_blog IN ($rel_b) order by add_date desc limit 7";
You can do this before your query.
$rel_b = preg_replace('/,+/', ',', trim($rel_b, ','));
This will remove repeated commas as well as commas from beginning and end of the string.
Try using implode():
$get_id = "select rel_blog from pages";
$resu = mysql_query($get_id);
$p_id = array();
$rel_b = array();
$i = 1;
while ($page_id = mysql_fetch_array($resu)){
$p_id[] = $page_id['page_id'];
$rel_b[] = $page_id['rel_blog'];
}
$p_id = implode(',', $p_id);
$rel_b = implode(',', rel_b);
echo $getblog = "SELECT * FROM pages WHERE rel_blog IN ($rel_b) order by add_date desc limit 7";

How can i order by id ascending?

I want to order data by Id, how can i do this ?
if($_GET["grupid"]>0){
$DUZEN = array();
$sql = "SELECT * FROM siparis_ana WHERE grupid =".$_GET["grupid"];
$rsDuzen = mysql_query($sql, $conn) or die(mysql_error());
while ($r = mysql_fetch_assoc($rsDuzen)) {
$DUZEN[] = $r;
}
}
i can read all data with this code which have same group id. But data aline random.
You have to use mysql order clause in your query like order by id asc. Which you can use at the end of your query.
$sql = "SELECT * FROM siparis_ana WHERE grupid =".$_GET["grupid"]." order by id asc";
Your sql query should be like given below...
$sql = "SELECT * FROM siparis_ana where grupid = " . $_GET['grupid'] . " ORDER BY id asc ";

Sort query by time and list next entries

I'm trying to sort this query by time.
I have a gaming match system. And I want to get a list of next 5 matches from my local time zone.
<?php
include_once "include/dbcompo.php";
$q=mysqli_query($con, "SELECT * FROM kamper ORDER BY tid LIMIT 5");
while($row = mysqli_fetch_array($q))
{
$clan1 = $row['clan1'];
$clan2 = $row['clan2'];
$server = $row['server'];
$tid = $row['tid'];
echo $clan1." ".$clan2." ".$server." ".$tid;
echo "<br />";
}
?>
Add a WHERE clause in your query: WHERE tid > NOW().
With NOW() you take the time of the server, maybe you should replace it with new DateTime(null)->getTimestamp()
Something like that:
<?php
mysqli_query($con, 'SELECT * FROM kamper WHERE tid > NOW() ORDER BY tid LIMIT 5');
// or
mysqli_query($con, 'SELECT * FROM kamper WHERE tid > '.new DateTime(null)->getTimestamp().' ORDER BY tid LIMIT 5');
?>
Two options:
1st option: Make the time field a numeric field and sort in PHP:
$queryResult = mysqli_query($con, $query);
while($row = mysqli_fetch_array($queryResult) {
$oldArray[$row['time'] = $row;
}
$array = ksort($oldArray);
foreach($array as $time=>$row) {
// do something
}
2nd option: Make a subquery
SELECT * FROM (
SELECT * FROM kamper WHERE timezone = 'UTC' ORDER BY tid
) LIMIT 5

MYSQL like a join but only need the newest row?

I want to do the following but in one query:
$query = mysql_query("SELECT name FROM tbl_users WHERE category = '1'");
while($row = mysql_fetch_assoc($query))
{
$query2 = mysql_query("SELECT datecreated
FROM tbl_comments
ORDER BY datecreated DESC LIMIT 1");
$row2 = mysql_fetch_assoc($query2);
echo $row['name'] . " > " . $row2['datecreated'] ."<br />";
}
There is a user table and a comments table, I want to display a list of users and the date of the last comment next to them?
Is this possible in a single query?
You can do so using the following SQL:
SELECT name,
(
SELECT datecreated
FROM tbl_comments
WHERE tbl_users.user_id = tbl_comments.user_id
ORDER BY datecreated LIMIT 1
)
FROM tbl_users
WHERE category = '1';
OR using:
SELECT tbl_users.name, MAX(datecreated) AS latestcomment
FROM tbl_users LEFT JOIN tbl_comments ON (tbl_users.user_id = tbl_comments.user_id)
GROUP BY tbl_users.name;

Categories