Welcome Guest, Not a member yet? Register   Sign In
Question about an array in a query
#1

[eluser]CodeIgniterNoob[/eluser]
I have a function that returns an array with alot of numbers, and I need to use all those numbers in a query, basically search a table for all the records that match those numbers.

Heres my query function, so far it only returns one result, which is wrong.

Code:
foreach ($numbers as $key => $value)
{
$this->db->where('country', $country);
$this->db->where('postal', $key);
$this->db->like('state', $state);
$this->db->orderby('city','asc');
$Q = $this->db->get('us_stores');
if($Q->num_rows()>0)
{
return $Q->result_array();
}

I know theres something wrong here, I was thinking of putting it in a loop for each number.
#2

[eluser]pistolPete[/eluser]
Since you have the return statement inside the foreach loop, it will exit that loop the first time the if condition is met.

Could you please provide:

- your database layout
- some sample data
- what you are trying to achieve in the end?
#3

[eluser]CodeIgniterNoob[/eluser]
Well, there are two tables one containing all the zip codes in america(zipcodes) and the other table has locations(locations). I have a function that reads the zipcodes table and returns all the zipcodes that are within a radius of the users zipcode, they put that info through a form. It gets the data properly.

So now I want to match each returned zipcode from zipcodes table with the zipcode field in locations table and get those locations. Simple right. Im just not sure how to write out the query properly.
#4

[eluser]Italo Domingues[/eluser]
Hello CodeIgniterNoob, if I understood well what you want, you want to bring all the locations that are in a certain range of zip code?
#5

[eluser]CodeIgniterNoob[/eluser]
Yes. I just keep getting only one result. This doesnt work $this->db->where_in('postal', $key); Still get one result.
#6

[eluser]Italo Domingues[/eluser]
Do this, try the code below and see if it still has only one result.

Code:
foreach ($numbers as $key => $value)
{
$this->db->where('postal', $key);
$this->db->orderby('city','asc');
$Q = $this->db->get('us_stores');
if($Q->num_rows()>0)
{
return $Q->result_array();
}
#7

[eluser]CodeIgniterNoob[/eluser]
I keep getting one result. Look, if I write out an array like this: $numbers = array('11356', '11369', '11568');

and pass it into the query: $this->db->where_in('postal', $numbers);

And it works perfectly, I get the right result.

All Im trying to do is pass an array(that comes as a return from a class) into the query. I know its an array, it prints the right results. Any ideas?
#8

[eluser]unsub[/eluser]
ok, i'm tired and have been in the wine tonight, so this might be stupid... but should it be looking for $value instead of $key?
you have the postal code == $key. I assume the $value is the actual postal code(s) you want to search for?

chrz
#9

[eluser]CodeIgniterNoob[/eluser]
Hey guys I fixed it. What I did was, I built a buildString method that took that $key array and broke it down into a string. Like this:
Code:
$string = "";
foreach ($numbers as $key => $value)
{
    $string .= " ".$key." ";
}
return $string;

But I called the method like this:
Code:
$zipstring = explode(" ",buildString($numbers, $radius));

And created an array which ci could understand, because before it wasnt formatted properly. And now everything works like it should.

Thank you all.




Theme © iAndrew 2016 - Forum software by © MyBB