Welcome Guest, Not a member yet? Register   Sign In
controller and view not working correctly
#1

I have a question about CodeIgniter and how to get some information from some tables using a controller and then have it displayed in a view

I have it working using 6 tables, but I've changed the layout of the database and the names of the tables and I can't quite work out how to change the code (i'm no longer using some of the tables that i was using so theoretically it should be easier!) to have it working again and I'm hoping it will be an easy 'find' for someone to look at and give me some pointers.

It has a couple of errors of

   Message: Undefined variable: cluster_task

And then one for the date of

   Message: Undefined index:item_date

Complaining about the view, but there’s probably the way I’ve defined in the controller, and I have tried changing it about 100 times and I cant see where the error(s) are?

This is what it comes out with in browser

From 3 June 2015 to 5 June 2015
Date        Support Item        Ref Number      Hours       Price       Cost    
4 June 2015                                        
3 June 2015                                        
5 June 2015                                        
5 June 2015                                        
1 January 1970  

The controller code is – there’s more but this should be the relevant part =

Code:
       $items = new Cluster_task();
       $items->where_in('id', $cluster_task_ids)->order_by('id', 'asc')->get();

        $cluster_task_id_count = array_count_values($cluster_task_ids);

       $dates = new List_item();
       $dates->where_in('user_list_id', $id)->where('item_date >=', $start)->where('item_date <=', $end)->order_by('cluster_task_id', 'asc')->get();
       if($items->exists()) {
          echo $data['item_count_item'] = $items->result_count();  
           $data['items'] = $items;  
           $data['dates'] = array();
           foreach($dates AS $date)
           {
             $data['dates'][$date->id] = array('item_date' => $date->item_date, 'cluster_tasks' => array());
           }  
           foreach($items AS $item)
           {
               for($count = 0; $count < $cluster_task_id_count[$item->id]; $count++)
               {
                       $data['dates'][$item->cluster_task_id]['cluster_tasks'][$item->cluster_task_id] = array(
                           'name' => $item->cluster_task_support_item
                       );
               }
           }
       }        
       $data['start'] = $start;
       $data['end'] = $end;
       $this->cfd->build('diet/preview_claim', $data);

And the view code is – again, there’s more but this should be the relevant part =

Code:
<table class="atable">
<tr>
 <th colspan="12">From <?php echo date('j F Y', strtotime($start)) ?> to <?php echo date('j F Y', strtotime($end)) ?></th>
</tr>
<tr>
<td>Date</td>
<td>Support Item</td>
<td>Ref Number</td>
<td>Hours</td>
<td>Price</td>
<td>Cost</td>
</tr>

<?php
foreach($dates AS $date) {            
   $date = $date['item_date'];        //item_categories - name ie fruit
    echo '<td class="special_column1">'.date('j F Y',    strtotime($date)).'</td>';
foreach($date['cluster_tasks'] AS $cluster_task)    
{
       echo '<tr>';
       echo '<td class="special_column1">'.date('j F Y', strtotime($date)).'</td>';
       echo '<td class="special_column3">'.$cluster_task['name'].'</td>';
       echo '</tr>';
       $name = '';
  }
}
?>
</table>

If i take out

Code:
               foreach($items AS $item)
               {
                   for($count = 0; $count < $cluster_task_id_count[$item->id]; $count++)
                   {
                           $data['dates'][$item->cluster_task_id]['cluster_tasks'][$item->cluster_task_id] = array(
                               'name' => $item->cluster_task_support_item
                           );
                   }
               }

from the controller and the relevant part in view the dates return with no error messages (and not the 1 January 1970  either) but i want it to display the support_item field next to the date. I should be able to work out how to get the other columns once i can see where i'm going wrong with just the one that i'm trying to output.

Thanks  for your time.
Reply
#2

Since your code is not complete, i can't help you to discover where the error come from.
"$cluster_taks" doesn't seems to be used... only "$cluster_taks_ids" instead.
as i see the first code, that last hasn't be defined..
NexoPOS 2.6.2 available on CodeCanyon.
Reply
#3

I have redefined some of the above code so it now reads
$data['dates'][$date->id] = array('ctask_id' => $date->cluster_task_id,'item_date' => $date->item_date, 'cluster_tasks' => array());

and print_r($data['dates']); (in the view) gives

Array ( [26] => Array ( [ctask_id] => 217 [item_date] => 2015-06-04 [cluster_tasks] => Array ( ) )
[27] => Array ( [ctask_id] => 218 [item_date] => 2015-06-03 [cluster_tasks] => Array ( ) )
[34] => Array ( [ctask_id] => 218 [item_date] => 2015-06-05 [cluster_tasks] => Array ( ) )
[28] => Array ( [ctask_id] => 219 [item_date] => 2015-06-05 [cluster_tasks] => Array ( ) ) )
so it has passed the $data
so i guess i'm now trying to work out how to recode the following part to put the name into the empty cluster_tasks array to forward to the view - i think that will work?
foreach($items AS $item){
for($count = 0; $count < $cluster_task_id_count[$item->id]; $count++){
so how would i redefine these lines?
$data['dates'][$item->cluster_task_id]['cluster_tasks'][$item->cluster_task_id] = array(
'name' => $item->cluster_task_name
);
so the code will read the first entry in the dates array of 26, see the next portion where the ctask_id is 217 and compare this to the cluster_task table and get the corresponding name and put that into the array so i the end the array will then look like

Array ( [26] => Array ( [ctask_id] => 217 [item_date] => 2015-06-04 [cluster_tasks] => Array (nameA ) )
[27] => Array ( [ctask_id] => 218 [item_date] => 2015-06-03 [cluster_tasks] => Array (nameB ) )
[34] => Array ( [ctask_id] => 218 [item_date] => 2015-06-05 [cluster_tasks] => Array (nameC ) )
[28] => Array ( [ctask_id] => 219 [item_date] => 2015-06-05 [cluster_tasks] => Array (nameD ) ) )

or am I completey off track with my thinking? I think I've been thinkng too much as to how I thought it would work :-)
Reply
#4

This is how i got it working for anyone who may be interested...

$cluster_task_id_count = array_count_values($cluster_task_ids);

$dates = new List_item();

$dates->where_in('user_list_id', $id)->where('item_date >=', $start)->where('item_date <=', $end)->order_by('item_date', 'asc')->get();

if($items->exists()) {
$data['item_count_item'] = $items->result_count();
$data['items'] = $items;
$data['dates'] = array();
foreach($dates AS $date)
{
$data['dates'][$date->id] = array('ctask_id' => $date->cluster_task_id,'item_date' => $date->item_date, 'cluster_tasks' => array());
$new_date=$date->cluster_task_id;
foreach($items AS $item)
{
for($count = 0; $count < $cluster_task_id_count[$item->id]; $count++)
{
$item->id; //217, then 218, then 218, then 219
if($item->id==$new_date)
{
$data['dates'][$date->id]['cluster_tasks'] = array(
'name' => $item->support_item
);
}
}
}
}
//print_r($data['dates']);
}
$data['start'] = $start;
$data['end'] = $end;
$this->cfd->build('diet/preview_claim', $data);
Reply




Theme © iAndrew 2016 - Forum software by © MyBB