Welcome Guest, Not a member yet? Register   Sign In
Sorting Data
#1

[eluser]snowstar[/eluser]
Hi All,

This is more of a broad/generally question not sure on what to google which is why i thought i would ask the community.

I would like to sort data out of a database based upon months ie results could come out of the db like this:

date amount
2011-12-29 10.00
2011-12-28 10.00
2011-12-27 10.00
2011-11-29 10.00
2011-11-29 10.00
2010-12-29 10.00

I want to group results of the same date together and all the amount together for a result

My Controller
Code:
function date_to_date()
    {
            
            // If Data is submitted do this
            if ($this->input->post('submit')) {
                
                $startYear = $this->input->post('startYear');
                $startMonth = $this->input->post('startMonth');
                $startDay = $this->input->post('startDay');
                $endYear = $this->input->post('endYear');
                $endMonth = $this->input->post('endMonth');
                $endDay = $this->input->post('endDay');
                

                $start = $startYear.'-'.$startMonth.'-'.$startDay;
                $end = $endYear.'-'.$endMonth.'-'.$endDay;
                
                $data['data'] = $this->reports_model->date_to_date($start, $end);
                
                //view
                $this->load->view('includes/header.php');
                $this->load->view('reports/view_datetodate', $data);
                $this->load->view('includes/footer.php');
                
            } else {
            
            $this->load->model('reports/reports_model');
            
            $data['years'] = $this->reports_model->get_years();
            
            //view
            $this->load->view('includes/header.php');
            $this->load->view('reports/view_datetodate', $data);
            $this->load->view('includes/footer.php');
            }
    }

My Model

Code:
function date_to_date($start, $end)
    {
        
        $query = 'SELECT `totalprice`, `date`
        FROM `crm_sales`
        WHERE `date` BETWEEN '.$this->db->escape($start).' AND '.$this->db->escape($end).'
        ORDER BY `date` ASC';
        
        $data = $this->db->query($query);
        
        return $data->result();
        
    }

Any suggests/help is greatly appreciated
#2

[eluser]toopay[/eluser]
Code:
function date_to_date($start, $end)
    {
        $query = 'SELECT DISTINCT `date`
        FROM `crm_sales`
        WHERE `date`=>'.$this->db->escape($start).' AND `date`<='.$this->db->escape($end).'
        ORDER BY `date` ASC';
        $data = $this->db->query($query);
        $date_range = $data->result_array();
        $i=count($date_range);
        $j=0;
        while ($j<$i)
        {
            $query = 'SELECT `totalprice`
                FROM `crm_sales`
                WHERE `date`='.$date_range[$j]['date'].'
                ORDER BY `totalprice` ASC';    
                $data = $this->db->query($query);
                $totalprice_list = $data->result_array();
            $res[$j] = array(
                'date'               => $date_range[$j]['date'],
                'total_itemprice     => count($totalprice_list),
                'grouped_totalprice' => $totalprice_list,
            );
            $j++;
        }

        return $res;
    }
If your 'date' field formated with full time stamp like '2011-01-04 00:00:01', you actually can do it better and cleaner, by set it directly in your query, with 'COUNT()', 'GROUP_CONCAT()' and 'GROUP BY'.
#3

[eluser]toopay[/eluser]
sorry, forgot ' at total_itemprice, it should be 'total_itemprice' not 'total_itemprice. I drink beer too much.. lol
#4

[eluser]danmontgomery[/eluser]
Your question is unclear, you want to group by month or by date?

date:
Code:
$query = $this->db->query('SELECT SUM(`totalprice`), `date`
FROM `crm_sales`
WHERE `date` BETWEEN '.$this->db->escape($start).' AND '.$this->db->escape($end).'
GROUP BY `date`
ORDER BY `date` ASC');

month:
Code:
$query = $this->db->query('SELECT SUM(`totalprice`) AS `totalprice`, DATE_FORMAT("%m %Y", `date`) AS `date`
FROM `crm_sales`
WHERE `date` BETWEEN '.$this->db->escape($start).' AND '.$this->db->escape($end).'
GROUP BY DATE_FORMAT("%m %Y", `date`)
ORDER BY `date` ASC');
#5

[eluser]snowstar[/eluser]
Thanks for the reply,

I got this error back

A Database Error Occurred

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '=>'2010-01-01' AND `date`<='2010-09-29' ORDER BY `date` ASC' at line 3

SELECT DISTINCT `date` FROM `crm_sales` WHERE `date`=>'2010-01-01' AND `date`<='2010-09-29' ORDER BY `date` ASC

I haven't used => in a query before but the data field type is date



[quote author="toopay" date="1301595207"]
Code:
function date_to_date($start, $end)
    {
        $query = 'SELECT DISTINCT `date`
        FROM `crm_sales`
        WHERE `date`=>'.$this->db->escape($start).' AND `date`<='.$this->db->escape($end).'
        ORDER BY `date` ASC';
        $data = $this->db->query($query);
        $date_range = $data->result_array();
        $i=count($date_range);
        $j=0;
        while ($j<$i)
        {
            $query = 'SELECT `totalprice`
                FROM `crm_sales`
                WHERE `date`='.$date_range[$j]['date'].'
                ORDER BY `totalprice` ASC';    
                $data = $this->db->query($query);
                $totalprice_list = $data->result_array();
            $res[$j] = array(
                'date'               => $date_range[$j]['date'],
                'total_itemprice     => count($totalprice_list),
                'grouped_totalprice' => $totalprice_list,
            );
            $j++;
        }

        return $res;
    }
If your 'date' field formated with full time stamp like '2011-01-04 00:00:01', you actually can do it better and cleaner, by set it directly in your query, with 'COUNT()', 'GROUP_CONCAT()' and 'GROUP BY'.[/quote]
#6

[eluser]toopay[/eluser]
My bad. Replace '=>' with '>='
#7

[eluser]snowstar[/eluser]
I think i get the idea of it, going to keep playing with it some more.
Im current getting results like this:
Code:
Array ( [data] => Array ( [0] => Array ( [date] => 2011-01-05 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [1] => Array ( [date] => 2011-01-06 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [2] => Array ( [date] => 2011-01-07 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [3] => Array ( [date] => 2011-01-10 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [4] => Array ( [date] => 2011-01-11 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [5] => Array ( [date] => 2011-01-12 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [6] => Array ( [date] => 2011-01-13 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [7] => Array ( [date] => 2011-01-14 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [8] => Array ( [date] => 2011-01-17 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [9] => Array ( [date] => 2011-01-18 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [10] => Array ( [date] => 2011-01-19 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [11] => Array ( [date] => 2011-01-20 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [12] => Array ( [date] => 2011-01-21 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [13] => Array ( [date] => 2011-01-22 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [14] => Array ( [date] => 2011-01-24 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [15] => Array ( [date] => 2011-01-25 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [16] => Array ( [date] => 2011-01-26 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [17] => Array ( [date] => 2011-01-27 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [18] => Array ( [date] => 2011-01-28 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [19] => Array ( [date] => 2011-02-01 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [20] => Array ( [date] => 2011-02-02 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [21] => Array ( [date] => 2011-02-03 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [22] => Array ( [date] => 2011-02-04 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [23] => Array ( [date] => 2011-02-07 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [24] => Array ( [date] => 2011-02-08 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [25] => Array ( [date] => 2011-02-09 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [26] => Array ( [date] => 2011-02-10 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [27] => Array ( [date] => 2011-02-11 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [28] => Array ( [date] => 2011-02-14 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [29] => Array ( [date] => 2011-02-15 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [30] => Array ( [date] => 2011-02-16 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [31] => Array ( [date] => 2011-02-17 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [32] => Array ( [date] => 2011-02-18 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [33] => Array ( [date] => 2011-02-21 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [34] => Array ( [date] => 2011-02-22 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [35] => Array ( [date] => 2011-02-23 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [36] => Array ( [date] => 2011-02-24 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [37] => Array ( [date] => 2011-02-25 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [38] => Array ( [date] => 2011-02-28 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [39] => Array ( [date] => 2011-03-01 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [40] => Array ( [date] => 2011-03-02 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [41] => Array ( [date] => 2011-03-03 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [42] => Array ( [date] => 2011-03-04 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [43] => Array ( [date] => 2011-03-07 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [44] => Array ( [date] => 2011-03-08 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [45] => Array ( [date] => 2011-03-09 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [46] => Array ( [date] => 2011-03-10 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [47] => Array ( [date] => 2011-03-11 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [48] => Array ( [date] => 2011-03-14 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [49] => Array ( [date] => 2011-03-15 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [50] => Array ( [date] => 2011-03-16 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [51] => Array ( [date] => 2011-03-17 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [52] => Array ( [date] => 2011-03-18 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [53] => Array ( [date] => 2011-03-21 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [54] => Array ( [date] => 2011-03-22 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [55] => Array ( [date] => 2011-03-23 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [56] => Array ( [date] => 2011-03-24 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [57] => Array ( [date] => 2011-03-25 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [58] => Array ( [date] => 2011-03-26 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [59] => Array ( [date] => 2011-03-28 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) [60] => Array ( [date] => 2011-03-29 [total_itemprice] => 0 [grouped_totalprice] => Array ( ) ) ) )
#8

[eluser]snowstar[/eluser]
Not sure where the problem is but i'll keep playing with it, any suggestions is greatly appreciated
#9

[eluser]toopay[/eluser]
I see, there's no data at $totalprice_list variabel. Is there any data in your database, but its returned zero result? Try this and let me know is it working :
Code:
function date_to_date($start, $end)
    {
        $sql = 'SELECT DISTINCT `date`
        FROM `crm_sales`
        WHERE `date`=>'.$this->db->escape($start).' AND `date`<='.$this->db->escape($end).'
        ORDER BY `date` ASC';
        $query = $this->db->query($sql);
        $date_range = $query->result_array();
        $i=0;
        while ($i<count($date_range))
        {
            $this->db->select('totalprice');
            $this->db->where('date', $date_range[$i]['date']);
            $this->db->order_by('totalprice', 'asc');
            $query = $this->db->get('crm_sales');
            $res = $query->result_array();
          
            if(count($res) == 0)
            {
                $totalprice_list = 'No data';
                $total_itemprice = 0;
            }
            else
            {        
                 $totalprice_list = array();
                 $j=0;
                 while($j<count($res))
                 {
                      $totalprice_list[$j] = $res[$j]['totalprice'];
                      $j++;
                 }
                 $total_itemprice = count($res);
            }

            $result[$i] = array(
                'date'               => $date_range[$i]['date'],
                'total_itemprice'     => $total_itemprice,
                'grouped_totalprice' => $totalprice_list,
            );
            $i++;
        }

        return $result;
    }
#10

[eluser]snowstar[/eluser]
Great, data came back, looks solid.

Thanks very much !

Code:
Array (
    [0] => Array (
        [date] => 2010-01-04
        [total_itemprice] => 32
        [grouped_totalprice] => Array ( [0] => 110.00 [1] => 110.00 [2] => 110.00 [3] => 110.00 [4] => 110.00 [5] => 210.00 [6] => 210.00 [7] => 210.00 [8] => 210.00 [9] => 210.00 [10] => 210.00 [11] => 210.00 [12] => 210.00 [13] => 210.00 [14] => 210.00 [15] => 210.00 [16] => 210.00 [17] => 210.00 [18] => 210.00 [19] => 210.00 [20] => 210.00 [21] => 210.00 [22] => 210.00 [23] => 210.00 [24] => 210.00 [25] => 210.00 [26] => 210.00 [27] => 210.00 [28] => 210.00 [29] => 210.00 [30] => 210.00 [31] => 210.00 )
        )

Whats the best way to echo this data out in my view? This is how i'm doing it currently but getting it, i assume i'll need to do count($row->grouped_totalprice) how-ever im getting the "Trying to get property of non-object" error

Code:
<table>
    &lt;?php if(isset($data)) : foreach($data as $row) : ?&gt;
        <tr>
            <td>&lt;?php echo $row->date; ?&gt;</td>
            <td>&lt;?php echo $row->grouped_totalprice; ?&gt;</td>
            <td>&lt;?php echo $row->total_itemprice; ?&gt;</td>
        </tr>    

    &lt;?php endforeach; ?&gt;

    &lt;?php else : ?&gt;    
    <h2>No records were returned.</h2>
    &lt;?php endif; ?&gt;
    </table>




Theme © iAndrew 2016 - Forum software by © MyBB