as I order the result to alphabetical order? - mysql

as I order the result to alphabetical order?
need it to read the directory and add the files in alphabetical order in mysql ...
$path = "artes110/$grupo";
$diretorio = opendir($path);
while ($arquivo = readdir($diretorio)){
if ($file != '.' && $file != '..')
{
$arquivo = utf8_encode($arquivo);
$arquivo = html_entity_decode($arquivo);
$query = mysql_query("INSERT INTO artes101 (imagem, data, texto, layout, grupo) VALUES ('$path/$arquivo', '$data2', '$texto', 'peq', '$grupo')") or die(mysql_error());
}
}

In mysql you can add them in any order and then get them ordered by adding an order by clause in your SQL code.
SELECT * FROM artes101 ORDER BY imagem DESC;
You can change imagen to whatever field you want them sorted by, and can change the direction of the order by changing the DESC to ASC

Related

sql query is not working after adding descending order conditions

Well, I have a list of jobs that I want to display so candidates can apply for the job. The problems is, the listing keep showing the old jobs and not the new one. I try to put the DESC query but it's become error.
This is what I have on my database;
and when I input the original query, it's working
original;
<?php
$limit = 4;
$sql = "SELECT COUNT(id_jobpost) AS id FROM job_post";
$result = $conn->query($sql);
if($result->num_rows > 0)
{
$row = $result->fetch_assoc();
$total_records = $row['id'];
$total_pages = ceil($total_records / $limit);
} else {
$total_pages = 1;
}
?>
and this one is after I add the DESC
$sql = "SELECT COUNT(id_jobpost) AS id FROM job_post ORDER BY DESC";
but this error came out
Trying to get property of non-object in C:\xampp\htdocs\jobportal\jobs.php on line 148
and the result doesn't come out in descending order.
what should I do to resolve this?
Thank you in advance.
The Syntax is:
SELECT COUNT(id_jobpost) AS id FROM job_post ORDER BY column1 DESC;
Here column1 refers to the column that is to be sorted.
You need column name in your order by clause.
SELECT COUNT(id_jobpost) AS idCount FROM job_post ORDER BY idCount DESC
you have missed the column you want to order your result by:
"SELECT COUNT(id_jobpost) AS id FROM job_post ORDER BY id DESC"

Is there a variable for mysql that returns all?

I have a MySQL query using a WHERE clause based on a variable. I am trying to figure out if there is a way to set the variable to something like a wildcard that would return any value in that field?
WHERE c.call_state_id = $state
AND (follow_emp_id = $user_id OR follow_emp_id = 117)
ORDER BY $sort
Instead of limiting the results to 1 specific $user_id, I want to virtually eliminate the AND portion of the query.
I would build the query dynamically, and check for the value before putting it into the WHERE clause.
if (empty($userid)) {
$userid_check = ""
} else {
$userid_check = "AND (follow_emp_id = $user_id OR follow_emp_id = 117)"
}
$sql = "SELECT ...
WHERE c.call_state_id = $state
$userid_check
ORDER BY $sort";
See https://stackoverflow.com/a/28909923/1491895 for a more general approach to building queries dynamically.
If you know a value that will not be used, such as 0 (or -1); you can just check for that:
WHERE c.call_state_id = $state
AND ($user_id = 0 OR follow_emp_id = $user_id OR follow_emp_id = 117)
ORDER BY $sort

Eloquent: Nested query to revert order in limited result

The following function (in my User Model) gives me the correct result for my chat system. Almost... I need to revert the order of the results.
public function getChatConv($cp, $page=1){
$limit = $page * 20;
$user = Authek::curUser();
$res = Chatmessage::where('receipient',$cp)->where('sender',$user->id)
->orWhere('receipient',$user->id)->where('sender',$cp)
->orderBy('created_at','desc')->take($limit)->get();
return $res;
}
It returns an object and I need an object as result. I tried already to convert the result to an array, revert the order and then convert the array back to object. This didn't work.
What I need is a nested query like the following raw SQL query:
SELECT *
FROM (
SELECT *
FROM chatmessages
WHERE (
receipient = '422'
AND sender = '22'
)
OR (
receipient = '22'
AND sender = '422'
)
ORDER BY created_at DESC
LIMIT 0 , 20
)faketable
ORDER BY created_at ASC
There are a few articles with nested queries, but I don't find a similar case and it would be good if someone could do this in Eloquent without the use of Raw queries... It must be possible.
Try this..
use take() and skip(),offset()
get 4 items from offset 3/4th:
Chatmessage::take(4)->offset(3)->get();
Or this (get 10 items from 8rd row):
Chatmessage::take(10)->skip(2)->get();
public function getChatConv($cp, $page=1){
$limit = $page * 20;
$user = Authek::curUser();
$res = Chatmessage::where('receipient',$cp)->where('sender',$user->id)
->orWhere('receipient',$user->id)->where('sender',$cp)
->orderBy('created_at','desc')->take(3)->skip(2)->get();
return $res;
}

Field Name 'order' clashes with mysql with Zend_Db_table_Abstract?

unfortunately I created the table with a field name called order.
Is there a way to change the query builder to make sure the field name is encased in the ` (apostrophe's)
My query is as follows:
$select = $this->select();
$select->order('order DESC');
$select->where('order < ?', $row->menu_id);
$select->where('menu_id = ?', $row->menu_id);
The builder creates:
SELECT `menu_items`.*
FROM `menu_items`
WHERE (order < '1')
AND (menu_id = '1')
ORDER BY `order` DESC LIMIT 1
I would like it to create
SELECT `menu_items`.*
FROM `menu_items`
WHERE (`order` < '1')
AND (menu_id = '1')
ORDER BY `order` DESC LIMIT 1
thanks
I'm no expert but according to the docs
No quoting is applied to expressions given to the where() or orWhere() methods. If you have column names that need to be quoted, you must use quoteIdentifier() as you form the string for the condition.
There is an example of using quoteIdentifier() in the section "Adding Expression Columns":
$select = $db->select()
->from(array('p' => 'products'),
array('origin' =>
'(p.' . $db->quoteIdentifier('from') . ' + 10)')
);
So you need to do something like this:
$db = Zend_Db_Table::getDefaultAdapter();
$select = $this->select();
$select->order('order DESC');
$select->where($db->quoteIdentifier('order') . ' < ?', $row->menu_id);
$select->where('menu_id = ?', $row->menu_id);
Note that in the order() method,
column names are quoted as identifiers, unless they contain parentheses or are an object of type Zend_Db_Expr.
so you don't have to do anything special there.

Mysql: Possible errors in queries "WHERE...IN" , "ORDER BY..." and "LIMIT"

As you have observed I have put an ambiguous title for this question, as simply I do not realize (by lack of deep knowledge) if this is the true problem or not.
Let's start then with a short description:
Here is where I select my products:
$select_prod = "SELECT * FROM product WHERE product_id IN ($some_array)";
After that here is where I define the pagination stuff:
$query_page = mysql_query($select_prod);
$product_total = mysql_num_rows($query_page);
$page_rows = 4;
$last = ceil($product_total/$page_rows);
if ($page < 1) {
$page = 1;
} elseif ($page > $last) {
$page = $last;
}
$limit = 'limit ' .($page - 1) * $page_rows .',' .$page_rows;
And where I prepare the render
$page_query = mysql_query($select_prod . $limit);
$results = array();
while ($array_filter = mysql_fetch_array($page_query)) {
$results[] = $array_filter;
}
Until this point everything is flowing easily, and I get my products listed as I wanted, BUT in random ORDER.
I have tried to include "ORDER BY price ASC" at the end of the first query like this:
$select_prod = "SELECT * FROM product WHERE product_id IN ($some_array) ORDER BY price ASC";
but for a strange reason fails to list the products with the error:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given...
I've already had several hours in trying to find where could be the problem, and this forum seems to be the final try for me, after that I would let them order as they want to.
You really need to print out your full query directly before execution. Try this instead:
$select_prod = "SELECT * FROM product WHERE product_id IN ($some_array) ORDER BY price ASC ";
Or, change the limit line to have a space before the limit.
What I believe the problem is the the lack of space before limit. The snippet in ()limit 100 is valid SQL. The snippet in () order by price asclimit 100 is not valid SQL.