Welcome Guest, Not a member yet? Register   Sign In
Query works in SQL editor, but not in Code Ignighter
#1

[eluser]The Revel[/eluser]
Ok, here is my code igniter query:

Code:
$date1 = $this->input->post('date1');
          $date2 = $this->input->post('date2');
                  
          
          $query = $this->db->select('store_id, code, SUM(qty) as qty, SUM(total) as total')
                              ->from('records')
                              ->where('store_id', $this->session->userdata('storeid'))
                              ->where('date BETWEEN "' . $date1 . '" AND "' . $date2 .'"')
                              ->group_by('code')
                              ->get();
                              
            if ($query->num_rows() > 0)
            {
                $this->date['reports'] = $query->result();
            }

- I have verified that $date1 and $date2 have the right format - yyyy-mm-dd
- I have verified that the query (shown below) works without error in my SQL editor (SQLyog)
Code:
SELECT store_id, code, SUM(qty) AS qty, SUM(total) AS total FROM records WHERE store_id = '0350C' AND date BETWEEN "2012-05-01" AND "2012-05-31" GROUP BY CODE
above query shows 5 results from database however the Code igniter active Record methods seems to be giving me nothing when I call for the results, so its not passing the variables tot eh next page.

#2

[eluser]The Revel[/eluser]
Here is my view code thats getting the error:
Code:
<table>
    <tr>
    <td>Code</td>
    <td>Qty</td>
    <td>Total</td>
    </tr>
    &lt;?php foreach ($reports as $report):?&gt;  
    <tr>
    <td>&lt;?php echo $report->code; ?&gt;</td>
    <td>&lt;?php echo $report->qty; ?&gt;</td>
    <td>&lt;?php echo $report->total; ?&gt;</td>
    </tr>
    &lt;?php endforeach;?&gt;
</table>

I am getting the error:
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: reports

Filename: auth/reports_view.php

Line Number: 28
and as a result
Code:
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: auth/reports_view.php

Line Number: 28
#3

[eluser]The Revel[/eluser]
HOLY CRAP... I must be frakin tired... Damn typo....
Code:
$this->date['reports'] = $query->result();
should be
Code:
$this->data['reports'] = $query->result();

I gotta stop posting my own stupidity to the forums lol.
#4

[eluser]atno[/eluser]
it's not an sql error, you're not passing your array to your view.
#5

[eluser]The Revel[/eluser]
[quote author="atno" date="1336711747"]it's not an sql error, you're not passing your array to your view.[/quote]

Yeah I was, I didn't post my template code cuz I knew it was ok... ended up being my own mistake (typo, my last post above) It works fine now... fought with it for 2 hours, been up since 8:00am and its after midnight, chalk it up to deadlines and lack of sleep LMAO.
#6

[eluser]Aken[/eluser]
Glad you caught your error. You might reconsider how you're sending data, though, because if your query actually does end up having something wrong with it and no results are passed, no error will go into your view data, and those errors will creep up again.
#7

[eluser]Unknown[/eluser]
thanks
it;s really help full




Theme © iAndrew 2016 - Forum software by © MyBB