• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
query problem

#1
[eluser]tim1965[/eluser]
This is really confusing me and i would appreciate some help.
I have a function that runs a search against two dates and an amount field.
Function below
function check_charges()
{
$price_per_week =($this->input->post('price_per_week', TRUE) == FALSE) ? '9999' : $this->input->post('price_per_week');
$start_date = ($this->input->post('start_date', TRUE) == FALSE) ? date('Y-m-d') : $this->input->post('start_date');
$end_date = ($this->input->post('end_date', TRUE) == FALSE) ? date('Y-m-d') : $this->input->post('end_date');

$t ="SELECT property_id
FROM v_charges_date_price
WHERE $start_date between `start_date1` and `end_date1`
OR $end_date between `start_date1` and `end_date1`
AND $price_per_week <= 'price_per_week_1'
";
$query_results = $this->db->query($t);

if($query_results->num_rows() >0)
{
$row=$query_results->result_array();
//var_dump($row);
foreach ($row as $val){
$v=$val['property_id'];
$charges=$v;

//var_dump($charges);
return $charges;

}
}
If i var_dump$charges with the return commented out i get this which is correct (7 and 8)
Quote:
string(1) "7" string(1) "8"
Quote:
However if i then add the return the array only contains 7 and not 8 i.e. its dropping everything after the first result
Quote:
string(1) "7"
Quote:
My controller is
Quote:
$charges = $this->M_search_v1->check_charges();
Quote:
Either i am missing something blindingly obvious or there is something funny going on here. All help appreciated.

#2
[eluser]pistolPete[/eluser]
Try this:

Code:
if($query_results->num_rows() >0)
{
            $row=$query_results->result_array();
            foreach ($row as $val)
            {
                          $charges[] = $val['property_id'];
            }        
            return $charges;
}

But why do you need to iterate over the results at all? Why don't you just use this:

Code:
return $query_results->result_array();

#3
[eluser]SitesByJoe[/eluser]
I agree with pistolPete - change the way you're doing your loop etc. You're making things more complex than they need to be.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.