Welcome Guest, Not a member yet? Register   Sign In
To those array experts
#1

I can see the values in the array, but i cant get access to them - Help!!

I am writing the following code in codeigniter

Code:
if ($this->form_validation->run() == FALSE) {
// Bill Not Created
}else {
for($i = 0; $i < $totalElements; $i++) {
$job_date = $job_dates[$i];
$cust = $customer_ids[$i];

$items = new Customer();
$items->where('id', $cust)->where_in('user_id',$uid)->get();
$customer_id_count = array_count_values($customer_ids);
$dates = new Bill_item();
$dates->where('customer_id =',$cust)->where('user_id =',$uid)->where('item_date =', $job_date)->get();
//SELECT * FROM (`bill_items`) WHERE `customer_id` = '6' AND `bill_items`.`user_id` = 2 AND `bill_items`.`item_date` = '2015-08-05'

$data['item_count_item'] = $items->result_count();  
$data['items'] = $items;  
$data['dates2'] = array();
$data['dates'] = array();
$data['total'] = array();
$data['total_for_cost'] = array();
$total1 = 0;
foreach($dates AS $date)
{
$data['dates'][$date->id] = array('cust' => $date->customer_id, 'item_date' => $date->item_date, 'item_hours' => $date->hours, 'item_amount' => $date->amount, 'cost' => ($date->amount*$date->hours), 'total_cost' => ($date->amount*$date->hours), 'cluster_tasks' => array());
$data['total'][$date->id] = array('total_cost' => ($date->amount*$date->hours));  
$new_date=$date->customer_id;
foreach($items AS $item)
{
for($count = 0; $count < $customer_id_count[$item->id]; $count++)
{
$item->id;  
if($item->id==$new_date)
{
$data['dates'][$date->id]['customers'] = array(
'name' => $item->support_item
);
}
}
}
}
$data['dates2'][$i]=$data['dates'];
}  
echo $i; //2015-08-05 6 0 2015-08-07 8 1
$data['dates2'][$i]=$data['dates'];
print_r($data['dates2'][$i]);
$data['total_for_cost'] = array_reduce($data['total'], create_function('$total, $next', '$total += $next["total_cost"]; return $total;'));
}

The above print_r outputs =
0Array ( [23] => Array ( [cust] => 6 [item_date] => 2015-08-05 [item_hours] => 2.00 [item_amount] => 37.00 [cost] => 74 [total_cost] => 74 [cluster_tasks] => Array ( ) [jobs] => Array ( [name] => 3 Aireys Street ) ) )
1Array ( [26] => Array ( [cust] => 8 [item_date] => 2015-08-07 [item_hours] => 1.00 [item_amount] => 40.00 [cost] => 40 [total_cost] => 40 [cluster_tasks] => Array ( ) [jobs] => Array ( [name] => 48 Taroona Road ) ) )



but my form page is only displaying the final record of '7 August 2015 48 Taroona Road 1 hour/s $40/hr $40'

This is doing my head in, I would really appreciate it if anyone ccould point out how to get both records to display. I assume it's because I'm overwriting it somewhere?

Thanks heaps
Reply
#2

What is the output you're expecting?
Reply
#3

I am needing to get both entris ftom the array so instead of just

5 August 2015 3 Aireys Street 2 hour/s $37/hr $74

I need

4 August 2015   48 Taroona Road          3 hour/s     $45/hr      $135  (I added this manually obviously)
5 August 2015 3 Aireys Street       2 hour/s $37/hr $74

As per print_r output (which i reformatted to see easier)

0Array
( [22] => Array ( [cust] => 8 [item_date] => 2015-08-04 [item_hours] => 3.00 [item_amount] => 45.00 [cost] => 135 [total_cost] => 135 [cluster_tasks] => Array ( ) [jobs] => Array ( [name] => 48 Taroona Road ) ) )

1Array
( [23] => Array ( [cust] => 6 [item_date] => 2015-08-05 [item_hours] => 2.00 [item_amount] => 37.00 [cost] => 74 [total_cost] => 74 [cluster_tasks] => Array ( ) [jobs] => Array ( [name] => 3 Aireys Street Aireys Inlet ) ) )

If you need me to include the view code I can do ?

I thought that the line $data['dates2'][$i]=$data['dates']; would copy the array into a new array 'after' the
for($i = 0; $i < $totalElements; $i++)  but it seems to be going to first line ie [0] and putting in the Taroona Road line, then overwriting the [0] line ? with 3 Aireys Street ? I'm not sure - i've looked at it too much to know what's going on !

Thanks for taking the time to look at - arrays are not my strong point - every time i think i am conquering them something happens to change my mind :-(
Reply
#4

(08-23-2015, 06:01 AM)scoobie Wrote: I am needing to get both entris ftom the array so instead of just

5 August 2015 3 Aireys Street 2 hour/s $37/hr $74

I need

4 August 2015   48 Taroona Road          3 hour/s     $45/hr      $135  (I added this manually obviously)
5 August 2015 3 Aireys Street       2 hour/s $37/hr $74

As per print_r output (which i reformatted to see easier)

0Array
( [22] => Array ( [cust] => 8 [item_date] => 2015-08-04 [item_hours] => 3.00 [item_amount] => 45.00 [cost] => 135 [total_cost] => 135 [cluster_tasks] => Array ( ) [jobs] => Array ( [name] => 48 Taroona Road ) ) )

1Array
( [23] => Array ( [cust] => 6 [item_date] => 2015-08-05 [item_hours] => 2.00 [item_amount] => 37.00 [cost] => 74 [total_cost] => 74 [cluster_tasks] => Array ( ) [jobs] => Array ( [name] => 3 Aireys Street Aireys Inlet ) ) )

If you need me to include the view code I can do ?

I thought that the line $data['dates2'][$i]=$data['dates']; would copy the array into a new array 'after' the
for($i = 0; $i < $totalElements; $i++)  but it seems to be going to first line ie [0] and putting in the Taroona Road line, then overwriting the [0] line ? with 3 Aireys Street ? I'm not sure - i've looked at it too much to know what's going on !

Thanks for taking the time to look at - arrays are not my strong point - every time i think i am conquering them something happens to change my mind :-(
I suspect the following lines because they not in the scope of the first for block so the var $i is undetermined.

PHP Code:
echo $i//2015-08-05 6 0 2015-08-07 8 1

$data['dates2'][$i]=$data['dates'];
print_r($data['dates2'][$i]);  
Reply
#5

I put another for loop around the above
for($i = 0; $i < $totalElements; $i++) {
  echo $i;
  print_r($data['dates2'][$i]
);
and i get

A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 0
Filename: controllers/diet_plan.php

Line Number: 723
1Array ( [23] => Array ( [cust] => 6 [item_date] => 2015-08-05 [item_hours] => 2.00 [item_amount] => 37.00 [cost] => 74 [total_cost] => 74 [cluster_tasks] => Array ( ) [jobs] => Array ( [name] => 3 Aireys Street Aireys Inlet ) ) )

so it isn't putting the first output into [0] of array - interesting - but i am not sure how to fix it because it looks like it should work to me...! please could u look for the line that i must have wrong somewhere - thank you
}
Reply
#6

(This post was last modified: 08-23-2015, 07:27 PM by RobertSF.)

I just took a look at the original code you posted. I hope I'm not missing something, but you know how your print_r statement executed twice, once for each time through the loop? In your view file, you also have to have a loop that goes through $data['dates2'], displaying one element/row at a time, until the screen is full or you run out of element/rows.
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply
#7

So it's almost working. I used
Code:
for($i = 0; $i < $totalElements; $i++) {
$job_date = $job_dates[$i]; //2015-08-05 6 2015-08-07 8
$cust = $customer_ids[$i];

$items = new Customer();
$items->where('id', $cust)->where_in('user_id',$uid)->get();
//SELECT * FROM (`customers`) WHERE `idrr` = '6' AND `customers`.`user_id` IN (2)
$customer_id_count = array_count_values($customer_ids);
$dates = new Bill_item();
$dates->where('customer_id =',$cust)->where('user_id =',$uid)->where('item_date =', $job_date)->get();
//SELECT * FROM (`bill_items`) WHERE `customer_idmm` = '6' AND `bill_items`.`user_id` = 2 AND `bill_items`.`item_date` = '2015-08-05'
$data['item_count_item'] = $items->result_count();  //1
$data['items'] = $items;  
//$data['dates3'] = array();
$data['dates2'] = array();
$data['dates'] = array();
$data['total'] = array();
$data['total_cost'] = array();
$data['total_for_cost'] = array();
foreach($dates AS $date)
{
 $data['dates'][$i][$date->id] = array('cust' => $date->customer_id, 'item_date' => $date->item_date, 'item_hours' => $date->hours, 'item_amount' => $date->amount, 'cost' => ($date->amount*$date->hours), 'total_cost' => ($date->amount*$date->hours), 'cluster_tasks' => array());
 $data['total'][$date->id] = array('total_cost' => ($date->amount*$date->hours));  
$new_date=$date->customer_id;
foreach($items AS $item)
{
for($count = 0; $count < $customer_id_count[$item->id]; $count++)
{
 $item->id;  
 if($item->id==$new_date)
{
$data['dates'][$date->id]['jobs'] = array(
'name' => $item->support_item
);
}
}
}
}  
$data['dates2'][$i]=$data['dates'];  
$data['dates3'][$i]=$data['dates2'][$i];  
$data['total_for_cost'] = array_reduce($data['total'], create_function('$total, $next', '$total += $next["total_cost"]; return $total;'));
}
for($i = 0; $i < $totalElements; $i++) {
echo $i;
print_r($data['dates3'][$i]);
echo "<br>";
}
}

this output

0Array ( [0] => Array ( [23] => Array ( [cust] => 6 [item_date] => 2015-08-05 [item_hours] => 2.00 [item_amount] => 37.00 [cost] => 74 [total_cost] => 74 [cluster_tasks] => Array ( ) ) ) [23] => Array ( [jobs] => Array ( [name] => 3 Aireys Street Aireys Inlet ) ) )
1Array ( [1] => Array ( [26] => Array ( [cust] => 8 [item_date] => 2015-08-07 [item_hours] => 1.00 [item_amount] => 40.00 [cost] => 40 [total_cost] => 40 [cluster_tasks] => Array ( ) ) ) [26] => Array ( [jobs] => Array ( [name] => 48 Taroona Road ) ) )

looking good, but then i did a var_dump on the view and i got

array(2) { [0]=> array(2) { [0]=> array(1) {
[23]=> array(7) { ["cust"]=> string(1) "6" ["item_date"]=> string(10) "2015-08-05" ["item_hours"]=> string(4) "2.00" ["item_amount"]=> string(5) "37.00" ["cost"]=> float(74) ["total_cost"]=> float(74) ["cluster_tasks"]=> array(0) { } } } [23]=> array(1) { ["jobs"]=> array(1) { ["name"]=> string(28) "3 Aireys Street Aireys Inlet" } } } [1]=> array(2) { [1]=> array(1) {
[26]=> array(7) { ["cust"]=> string(1) "8" ["item_date"]=> string(10) "2015-08-07" ["item_hours"]=> string(4) "1.00" ["item_amount"]=> string(5) "40.00" ["cost"]=> float(40) ["total_cost"]=> float(40) ["cluster_tasks"]=> array(0) { } } } [26]=> array(1) { ["jobs"]=> array(1) { ["name"]=> string(16) "48 Taroona Road " } } } }

not good as the view couldnt see the elements in the array

so all i need now is to know how to remove the top empty levels in the array??

so to go from

array(2) {
[0]=> array(2) { [0]=> array(1) { [23]=> array(7) { ["cust"]=> string(1) "6" ["item_date"]=> string(10) "2015-08-05" ["item_hours"]=> string(4) "2.00" ["item_amount"]=> string(5) "37.00" ["cost"]=> float(74) ["total_cost"]=> float(74) ["cluster_tasks"]=> array(0) { } } } [23]=> array(1) { ["jobs"]=> array(1) { ["name"]=> string(28) "3 Aireys Street Aireys Inlet" } } }
[1]=> array(2) { [1]=> array(1) { [26]=> array(7) { ["cust"]=> string(1) "8" ["item_date"]=> string(10) "2015-08-07" ["item_hours"]=> string(4) "1.00" ["item_amount"]=> string(5) "40.00" ["cost"]=> float(40) ["total_cost"]=> float(40) ["cluster_tasks"]=> array(0) { } } } [26]=> array(1) { ["jobs"]=> array(1) { ["name"]=> string(16) "48 Taroona Road " } } } }


to

array(2) {
[23]=> array(7) { ["cust"]=> string(1) "6" ["item_date"]=> string(10) "2015-08-05" ["item_hours"]=> string(4) "2.00" ["item_amount"]=> string(5) "37.00" ["cost"]=> float(74) ["total_cost"]=> float(74) ["cluster_tasks"]=> array(0) { } } } [23]=> array(1) { ["jobs"]=> array(1) { ["name"]=> string(28) "3 Aireys Street Aireys Inlet" } } }
[26]=> array(7) { ["cust"]=> string(1) "8" ["item_date"]=> string(10) "2015-08-07" ["item_hours"]=> string(4) "1.00" ["item_amount"]=> string(5) "40.00" ["cost"]=> float(40) ["total_cost"]=> float(40) ["cluster_tasks"]=> array(0) { } } } [26]=> array(1) { ["jobs"]=> array(1) { ["name"]=> string(16) "48 Taroona Road " } } } }

note the '[0]=> array(2) { [0]=> array(1)' at start

and then I think it'll work fine

I've ttried lots already like 'array_shift' 'array_merge' and a couple of others i found on google and didn't do anything. Please, if there any suggestions I'd be eternally grateful.

PS I will investigate the debugging options soon and i appreciate your help so far

Thanks
Reply
#8

(This post was last modified: 08-24-2015, 10:58 AM by RobertSF.)

You're welcome. I formatted those arrays to understand them better.

Code:
so to go from

array(2) {
 [0]=> array(2) {
   [0]=> array(1) {
     [23]=> array(7) {
       ["cust"]=> string(1) "6"
       ["item_date"]=> string(10) "2015-08-05"
       ["item_hours"]=> string(4) "2.00"
       ["item_amount"]=> string(5) "37.00"
       ["cost"]=> float(74)
       ["total_cost"]=> float(74)
       ["cluster_tasks"]=> array(0) {
       }
     }
   }
   [23]=> array(1) {
     ["jobs"]=> array(1) {
       ["name"]=> string(28) "3 Aireys Street Aireys Inlet"
     }
   }
 }
 [1]=> array(2) {
   [1]=> array(1) {
     [26]=> array(7) {
       ["cust"]=> string(1) "8"
       ["item_date"]=> string(10) "2015-08-07"
       ["item_hours"]=> string(4) "1.00"
       ["item_amount"]=> string(5) "40.00"
       ["cost"]=> float(40)
       ["total_cost"]=> float(40)
       ["cluster_tasks"]=> array(0) {
       }
     }
   }
   [26]=> array(1) {
     ["jobs"]=> array(1) {
       ["name"]=> string(16) "48 Taroona Road "
     }
   }
 }
}

to

array(2) {
 [23]=> array(7) {
   ["cust"]=> string(1) "6"
   ["item_date"]=> string(10) "2015-08-05"
   ["item_hours"]=> string(4) "2.00"
   ["item_amount"]=> string(5) "37.00"
   ["cost"]=> float(74)
   ["total_cost"]=> float(74)
   ["cluster_tasks"]=> array(0) {
   }
 }
}
 [23]=> array(1) {
   ["jobs"]=> array(1) {
     ["name"]=> string(28) "3 Aireys Street Aireys Inlet"
   }
 }
}
 [26]=> array(7) {
   ["cust"]=> string(1) "8"
   ["item_date"]=> string(10) "2015-08-07"
   ["item_hours"]=> string(4) "1.00"
   ["item_amount"]=> string(5) "40.00"
   ["cost"]=> float(40)
   ["total_cost"]=> float(40)
   ["cluster_tasks"]=> array(0) {
   }
 }
}
 [26]=> array(1) {
   ["jobs"]=> array(1) {
     ["name"]=> string(16) "48 Taroona Road "
   }
 }
}
}
There's something not right about the "to" array. It's probably just typos, but I'm not sure what it's supposed to look like. In any case, let's look at the "from" array first.

When I prepare data for a view, I put it into a plain two-dimension array, representing the rows and columns. I do this in the controller, and then pass the plain 2D array to the view. That requires a new array just for that, but memory in this context is not a concern.

Before working on converting the first array into the second, consider if the first array has to be that complex in the first place. I'll be back later. Maybe others can chime in too.
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply
#9

In theory, you could continue down this path and, eventually, you might get the result you're looking for, but it's likely that this code will be impossible to maintain.

What I would recommend is that you clarify what data you're looking for and define a set of variables with meaningful names before the loop. Also before the loop, determine what data you need from the database and get it (emphasis on before the loop). The data from the database should also be stored in variables with meaningful names. From the looks of what you're doing here, it might even be easier to get the data you're looking for with a join instead of making two separate queries and looping through all of the results.

Make sure you're consistent with the way you treat your data. For example, you use $uid twice in your code, but in one case you pass it to where_in() while in the other you pass it to where() (while the methods are on two different classes, I'm assuming they're at least somewhat consistent with $this->db->where() and $this->db->where_in()).

$customer_id_count is not going to change inside your loop (with the current code), so you can safely get it out of the way by moving it out of the loop.

In this section:
PHP Code:
$data['item_count_item'] = $items->result_count();  //1
$data['items'] = $items;  
//$data['dates3'] = array();
$data['dates2'] = array();
$data['dates'] = array();
$data['total'] = array();
$data['total_cost'] = array();
$data['total_for_cost'] = array(); 

You initialize a bunch of arrays, eliminating whatever was stored in them on the previous run through the loop. Then, after looping through $dates, you copy and manipulate a bunch of data and store it in those arrays, right before the end of the loop:

PHP Code:
$data['dates2'][$i]=$data['dates'];  
$data['dates3'][$i]=$data['dates2'][$i];  
$data['total_for_cost'] = array_reduce($data['total'], create_function('$total, $next''$total += $next["total_cost"]; return $total;')); 

So, these last 3 assignments are meaningless for all but the last iteration of the loop, as are $data['item_count_item'], $data['items'], $data['dates'], $data['total'], and $data['total_cost'].

You have the following:
PHP Code:
foreach ($items as $item) {
    for(
$count 0$count $customer_id_count[$item->id]; $count++) {
        
$item->id;
        if (
$item->id == $new_date) {
            
$data['dates'][$date->id]['jobs'] = array(
                
'name' => $item->support_item
            
);
        }
    }


The for loop in this section is, at best, just wasting cycles. At worst, it might be preventing the loop from ever satisfying the $item->id == $new_date condition. The $item->id call inside the for loop isn't doing anything, either (unless you have a magic method doing something in the class).
Reply
#10

I am studying your responses - had a complete 10 ay where electricity out at house then yesterday was out all day, so absolutely not ignoring your input - Thanks and thanks for taking time to let me know the issues (many) with the code and like i said i will study it to learn a better way to get thi working - more soon
Reply




Theme © iAndrew 2016 - Forum software by © MyBB