Jump to content

I need a little code help


Jessie K. Jepsen

Recommended Posts

I have this snippet of code, I need some help with.

 

/*

* Returns a list of customers for an autofill box

*

* @access public

* @param string $query - A search query to search the database for

*

* @return void

*/

public function ajaxCustomerList($query)

{

if ( ! $query || $query == '' || strlen($query) < 1)

die();

 

$sql = '

SELECT `id_customer` ,

`firstname` ,

`lastname` ,

`email`

FROM `' . _DB_PREFIX_ . 'customer`

WHERE (

`firstname` LIKE "%' . pSQL($query) . '%"

OR `lastname` LIKE "%' . pSQL($query) . '%"

OR CONCAT(`firstname`, " ", `lastname`) LIKE "%' . pSQL($query) . '%"

OR `email` LIKE "%' . pSQL($query) . '%"

)';

 

$customers = Db::getInstance()->ExecuteS($sql);

 

if ($customers && sizeof($customers))

foreach ($customers as $customer)

printf("%s %s (%s) | %d\n", $customer['firstname'], $customer['lastname'], $customer['email'], (int)$customer['id_customer']);

}

 

-----------------------------------------------------------------------------------------------------------------

 

As you can see, it pulls the customer based on a search for the customers first name, lastname, or email from the CUSTOMER table in the database. I am in need of making this code so I can also search for the customer by postcode "zip code" under the ADDRESS table in the database as well.

 

I have been trying for days now to figure out how to make it search both tables instead of just the one, but cant seem to figure it out.

 

If you know how please edit my code and re-post it to me and I will try it, thank you so much in advance!

 

-K

Link to comment
Share on other sites

Ok, This is what I have come up with.. Does this look right to you?

 

-------------------------------------------------------------------------------------------------------------------------------------------

/*

* Returns a list of customers for an autofill box

*

* @access public

* @param string $query - A search query to search the database for

*

* @return void

*/

public function ajaxCustomerList($query)

{

if ( ! $query || $query == '' || strlen($query) < 1)

die();

 

$sql = '

SELECT p.`postcode` ,

pl.`firstname` ,

pl.`lastname` ,

p.`address1` ,

p.`city`

FROM `' . _DB_PREFIX_ . 'address` p

JOIN LEFT `' . _DB_PREFIX_ . 'customer` pl

WHERE (

pl.`firstname` LIKE "%' . pSQL($query) . '%"

OR pl.`lastname` LIKE "%' . pSQL($query) . '%"

OR CONCAT(`pl.firstname`, " ", `pl.lastname`) LIKE "%' . pSQL($query) . '%"

OR pl.`address1` LIKE "%' . pSQL($query) . '%"

OR p.`postcode` LIKE "%' . pSQL($query) . '%"

OR p.`city` LIKE "%' . pSQL($query) . '%"

)';

 

$customers = Db::getInstance()->ExecuteS($sql);

 

if ($customers && sizeof($customers))

foreach ($customers as $customer)

printf("%s %s [%s, %s] | %d\n", $customer['firstname'], $customer['lastname'], $customer['address1'], $customer['city'], (int)$customer['postcode']);

}

Link to comment
Share on other sites

Ok, This is what I have come up with.. Does this look right to you?

 

-------------------------------------------------------------------------------------------------------------------------------------------

/*

* Returns a list of customers for an autofill box

*

* @access public

* @param string $query - A search query to search the database for

*

* @return void

*/

public function ajaxCustomerList($query)

{

if ( ! $query || $query == '' || strlen($query) < 1)

die();

 

$sql = '

SELECT p.`postcode` ,

pl.`firstname` ,

pl.`lastname` ,

p.`address1` ,

p.`city`

FROM `' . _DB_PREFIX_ . 'address` p

JOIN LEFT `' . _DB_PREFIX_ . 'customer` pl

WHERE (

pl.`firstname` LIKE "%' . pSQL($query) . '%"

OR pl.`lastname` LIKE "%' . pSQL($query) . '%"

OR CONCAT(`pl.firstname`, " ", `pl.lastname`) LIKE "%' . pSQL($query) . '%"

OR pl.`address1` LIKE "%' . pSQL($query) . '%"

OR p.`postcode` LIKE "%' . pSQL($query) . '%"

OR p.`city` LIKE "%' . pSQL($query) . '%"

)';

 

$customers = Db::getInstance()->ExecuteS($sql);

 

if ($customers && sizeof($customers))

foreach ($customers as $customer)

printf("%s %s [%s, %s] | %d\n", $customer['firstname'], $customer['lastname'], $customer['address1'], $customer['city'], (int)$customer['postcode']);

}

 

That's wrong.

 

Correct example:

 

SELECT c.id_customer, c.firstname, c.lastname, c.email, a.postcode FROM ps_customer c LEFT JOIN ps_address a ON (c.id_customer = a.id_customer) WHERE (c.firstname LIKE "%75000%" OR c.lastname LIKE "%75000%" OR CONCAT(c.firstname, " ", c.lastname) LIKE "%75000%" OR c.email LIKE  "%75000%" OR a.postcode LIKE "%75000%")

 

Regards.

 

Robin.

 

The CartExpert Team

Link to comment
Share on other sites

Thank you for your help,

 

I tried that code but it still seems to be not working..

I changed it around a little based on some code sites I found on google and still cant get it working..

 

Now whats happening when I paste in the code you gave me is " When I type in the search box for the zip code or even the person lastname or firstname or email, nothing comes up" strange.

 

I must still be doing something wrong..

 

/*

* Returns a list of customers for an autofill box

*

* @access public

* @param string $query - A search query to search the database for

*

* @return void

*/

public function ajaxCustomerList($query)

{

if ( ! $query || $query == '' || strlen($query) < 1)

die();

 

$sql = '

SELECT

x.`firstname` ,

x.`lastname` ,

x.`phone` ,

xl.`postcode`

FROM `' . _DB_PREFIX_ . 'customer` x

LEFT JOIN `' . _DB_PREFIX_ . 'address` xl

ON (

xl.postcode = x.postcode

)

WHERE (xl.postcode LIKE "%' . pSQL($query) . '%" OR x.firstname LIKE "%' . pSQL($query) . '%" x.lastname LIKE "%' . pSQL($query) . '%" x.phone LIKE "%' . pSQL($query) . '%")

AND xl.id_lang = ' . (int)($id_lang);

 

$customers = Db::getInstance()->ExecuteS($sql);

 

if ($customers && sizeof($customers))

foreach ($customers as $customer)

printf("%s %s (%s) | %d\n", $customer['lastname'], $customer['firstname'], $customer['phone'], (int)$customer['postcode']);

}

Edited by EMPeiTQ (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...