How do ORDER BY with information that may vary?

Asked

Viewed 233 times

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.

  • 1

    I thought about it, but at the time I put the quotation marks wrong, so it didn’t work... :(

  • 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?

  • 1

    @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.

  • 1

    @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!

  • 1

    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.

  • Don’t show me new thing not that I get curious... thanks!

  • @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...

  • @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.

  • @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.

Show 6 more comments

1 answer

4


Put it like this:

$sel = DBRead('artigos', null ,'*', "ORDER BY $mudanca ASC");

I mean, double quotes!

Inside single quotes PHP does not interpret anything it puts what is in it as a text only, while in double quotes if you have PHP variables for example it will interpret and show you the result.

$texto = 'imprime texto';
echo '$texto'; // saída -> $texto
echo "$texto"; // saída -> imprime texto

Online Example: Ideone

Or

Concatenation

$sel = DBRead('artigos', null ,'*', 'ORDER BY '.$mudanca.' ASC');

Good reading.

  • 2

    **$mudanca** I think it was meant to be bold, right? It can also be "{$mudanca}" as default you use the keys to mark the output of a variable

  • 1

    Thank you @Maria, every day I realize that programming is not just about logic, you have to pay attention to detail at all times. I drew up the whole stop and because of a quote was not working. Thanks!

  • 1

    Era Papa: I tried to put bold here in the stack, but I already took, it doesn’t work in the codes. Yeah, my database functions are between keys. Thanks!

  • 1

    @Papacharlie keys are also going to have effect in double quotes, just remembering ..., single quotes will have the text effect!

  • 1

    Not at all @Iwannaknow ...!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.