-2

Seems like sorting by group is not working on this one.

The variable $check_all is used to shown all the properties.

$check_all=$wpdb->get_results("select distinct town_village, suburb, state from $table where (concat(suburb,', ',state,', ',postcode)='$value' or concat(suburb,', ',postcode)='$value' or concat(suburb,', ',state,' ',postcode)='$value' or concat(suburb,', ',state)='$value' or CONCAT(town_village,', ',state) like '%$value%') Group by suburb,town_village,state", ARRAY_A);

Then I have this code to filter the output by searched inputted.

$sql_keywords.= "properties.town_village in ('".$check_all[0]['town_village']."') AND properties.state in ('".$check_all[0]['state']."') AND ";

As example the results must be like this all suburb 'Manila' must show first. Please see screenshot http://prntscr.com/inpgjn

But the result I'm getting is in random. The search suburb was shown in random places in results.

EDITED: Here is the current result of the code I shown above

Search by 'Manila' http://prntscr.com/inppj8

The result I want when they search 'Manila' is to show all the Manila at first of the results http://prntscr.com/inpgjn

Please see code for the this function

function lookup_string($value, $sql_keywords, $sur_suburbs='0'){
        global $wpdb;
        $suburb_region=$wpdb->get_results("show columns from suburb_region");
        $table=($suburb_region)?'suburb_region':'properties';
        if($sur_suburbs==1){
            if(strpos($value,',')!==false){
                $check_all=$wpdb->get_results("select distinct town_village, suburb, state from $table where (concat(suburb,', ',state,', ',postcode)='$value' or concat(suburb,', ',postcode)='$value' or concat(suburb,', ',state,' ',postcode)='$value' or concat(suburb,', ',state)='$value' or CONCAT(town_village,', ',state) like '%$value%') Group by suburb,town_village,state", ARRAY_A);
                $check_suburb=$wpdb->get_var("select distinct suburb from $table where CONCAT(suburb,', ',state, ', ',postcode) like '%$value%'");
            }   
            if($check_all){

                $sql_keywords.= "properties.town_village in ('".$check_all[0]['town_village']."') AND properties.state in ('".$check_all[0]['state']."') AND ";
            }
        }
    }

Expected order of query shown below. Right now I'm getting suburb in random.


suburb | state |
----------------------
Manila | Metro |
Manila | Metro |
Manila | Metro |
Manila | Metro |
Pasay  | Metro |
Makati | Metro |
ETC    | Metro |
winresh24
  • 687
  • 1
  • 6
  • 27
  • "Sorting by group" - there's no sort by in your query? – random_user_name Mar 06 '18 at 17:54
  • I added it at $check_all last part 'Group by suburb,town_village,state' don't know if I done it right... – winresh24 Mar 06 '18 at 17:55
  • 1
    you code ins not complete and ..and your question is not clear .. if you have an error show the code really related and the error message – ScaisEdge Mar 06 '18 at 17:56
  • @scaisEdge actually I'm not getting any error it shows the result in random but not showing it as I want to be...Please see screenshot from the question. That's what I want the result will be.. – winresh24 Mar 06 '18 at 17:59
  • then show the code also for the "supposed" order ..and show the expected result and you actual result – ScaisEdge Mar 06 '18 at 18:00
  • @scaisEdge please take a look I updated the question. Thanks – winresh24 Mar 06 '18 at 18:10
  • Still no "sort by"...? – random_user_name Mar 06 '18 at 19:05
  • Also - this code is wide open to [SQL Injection attacks](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php).... You should be using [$wpdb->prepare](https://developer.wordpress.org/reference/classes/wpdb/prepare/) on your queries. – random_user_name Mar 06 '18 at 19:07
  • hi @cale_b can you enlighten me on "sort by" – winresh24 Mar 06 '18 at 19:08
  • 1
    [GROUP BY](http://www.mysqltutorial.org/mysql-group-by.aspx) **does not** sort. If you want sorting, you need to use [ORDER BY](http://www.mysqltutorial.org/mysql-natural-sorting/).... – random_user_name Mar 06 '18 at 19:08
  • @cale_b Correct me if I'm wrong "order by" can only be sort by "asc and desc" right? if that's the case can I use it in my scenario? – winresh24 Mar 06 '18 at 19:14
  • Your question, with screeenshots, does not help us. Only you know what the sort order needs to be. How about you give us a sample output of what you expect from your query, meaning sample "order". – random_user_name Mar 06 '18 at 19:55
  • Hi @cale_b I added the order I want to be..Does it make sense? – winresh24 Mar 06 '18 at 20:07
  • @KirkBeard yes you are right Manila first and the other rows order doesn't matter – winresh24 Mar 06 '18 at 20:21

1 Answers1

2

One method to force a specific result to be first in the sort order is by using a CASE statement in your ORDER BY clause, like so:

ORDER BY CASE `suburb`
    WHEN 'Manilla' THEN 'aaaaaaaa'
    ELSE `suburb` END, `state`

Here's your function, with several revisions in it:

  1. Used $wpdb->prepare to use the "WordPress way" of preventing SQL injection attacks.
  2. Gave the code some formatting (spacing) to help readability. This is recommended by WordPress also.
  3. Used UPPERCASE for reserved words in SQL (SELECT, FROM, OR, CONCAT, etc) also to help readability.
  4. Used UPPERCASE for TRUE / FALSE, etc. in PHP.
  5. Switched your condition to Yoda style for defensive coding.
  6. Use $wpdb->get_row() for $check_all, since you only cared about the first result / row.

your code, modified:

function lookup_string( $value, $sql_keywords, $sur_suburbs = 0 ) {
    global $wpdb;
    $suburb_region = $wpdb->get_results( "SHOW COLUMNS FROM suburb_region" );
    $table=( $suburb_region ) ? 'suburb_region' : 'properties';

    if( (int)$sur_suburbs ) {
        // Your use of $check_all below will throw notices without this
        $check_all = FALSE;
        if( FALSE !== strpos( $value, ',' ) ) {
            // this is how you format "LIKE" queries using $wpdb->prepare
            $like = "%{$value}%";

            // To prevent SQL injection, use $wpdb->prepare
            $prepared = $wpdb->prepare( "SELECT distinct town_village, suburb, state 
                FROM {$table} 
                    WHERE (CONCAT(suburb, ', ', state, ', ', postcode)=%s 
                    OR CONCAT(suburb, ', ', postcode)=%s 
                    OR CONCAT(suburb, ', ', state,' ', postcode)=%s 
                    OR CONCAT(suburb, ', ', state)=%s 
                    OR CONCAT(town_village, ', ' ,state) LIKE %s) 
                    GROUP BY suburb, town_village, state 
                    ORDER BY CASE `suburb`
                        WHEN 'Manilla' THEN 'aaaaaaaa'
                        ELSE `suburb` END, state",
            $value, $value, $value, $value, $like);
            // execute the prepared query
            // Use get_row to get a single row
            $check_all = $wpdb->get_row( $prepared );

            // To prevent SQL injection, use $wpdb->prepare
            $prepared = $wpdb->prepare( "SELECT DISTINCT suburb FROM $table WHERE CONCAT(suburb, ', ', state, ', ', postcode) LIKE %s", $like );
            // execute the prepared query
            $check_suburb = $wpdb->get_var( $prepared );
        }  

        if( $check_all ) {
            // modified $check_all to use object notation, since using get_row above
            $sql_keywords.= "properties.town_village IN ('". $check_all->town_village ."') AND properties.state IN ('" . $check_all->state . "') AND ";
        }
    }
}
random_user_name
  • 25,694
  • 7
  • 76
  • 115