1
I’m getting a list of articles from a database and I want to have on page one tool that allows me to list them in order of date they were written or in alphabetical order.
Here I have my bank reading function:
$sel = DBRead('artigos', null ,'*', 'ORDER BY data ASC');
If I let data
the listing is made starting with the oldest post, if I change to nome
the listing is done in alphabetical order.
I built a form in HTML with a "select"
and two "options" inside and through the POST method I can get the value of one of these
"option’s" and put in the variable $mudanca
.
But if I put the variable in the reading function gives error:
$sel = DBRead('artigos', null ,'*', 'ORDER BY $mudanca ASC');
Any different suggestions?
I add something that might be useful to someone who’s building the same thing:
<?php
$mudanca="data";
if ($_SERVER["REQUEST_METHOD"] == "POST"){
$mudanca = $_POST['mudar'];
}
?>
That is, leave a default variable set, because before the post method is triggered $change would be empty causing error in ORDER BY $mudanca.
Another option would be
$sel = DBRead('artigos', null ,'*', 'ORDER BY ' . $mudanca . ' ASC');
Consider slower interpreting a variable within strings.– Papa Charlie
I thought about it, but at the time I put the quotation marks wrong, so it didn’t work... :(
– I Wanna Know
It’s worth reading that answer
– Papa Charlie
From what I read the "heredoc" replaces the double quotes and still dispenses with the use of "escape", but why don’t I see it being used?
– I Wanna Know
@Iwannaknow heredoc is rarely used, only in case many specific (which until today found none) that it should be used. In most cases double/single quotes are enough.
– gmsantos
@Iwannaknow for example, imagine you putting
heredoc
in this code, besides the horrible aesthetic, it would be in my opinion without any precision, in its code it could be concatenation or double quotes!– Maria
The link is an explanation of single and double quotes, not Heredoc or Nowdoc. The last two have their uses, but it does not suit the case of the question.
– Papa Charlie
Don’t show me new thing not that I get curious... thanks!
– I Wanna Know
@Papacharlie I also thought that double quotes had slower processing, and I decided to do a performance test, and I was amazed to see that using
"texto $variavel texto"
is faster than'texto ' . $variavel . ' texto'
in all tests...– Jader A. Wagner
@I Wanna Know, I showed the question by containing your doubt (single quotes and double quotes)... Not everything you read is worth the test, but it’s always good to know what’s available.
– Papa Charlie
@Jader, the quote performance is consensus and the loss is not significant - but it does exist. There are several factors that can change the result of a BENCH. Here’s a debate with some tests. I’d like to know how you came to that conclusion, but I think you’re getting off the point.
– Papa Charlie