• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Looping Problem

#1
Question 
Hi,

I'm running into a little bit of trouble and would appriciate some guidance.

I currently have a list of statuses held in a database table called 'status'. These are 'Open', 'Closed', 'Pending'.

I also have another table of sales which is in a table called 'sales'.

I'm trying to loop around each status and get the relevant sales, but am having a bit of an issue working out how this is done.

Please note the below code does not work  Undecided

Controller:

PHP Code:
// this returns all the statuses (open, closed, pending)
$data["status"] = $this->sales_model->get_status();

// now i want to loop around these and get the all the items with the that status_id
foreach($data['status'] as $status){
 
   $data["sales"] = $this->sales_model->get_all($status['status_id']);


View:

PHP Code:
<?php foreach ($status as $s){ ?>
    <!-- loop around and get each status -->
    <h1>$s['status_title'];</h1>
    
    <!-- now i want to loop around the results and display them for this status -->
    <?php print_r($sales); ?>

<?php }else{ ?>
    <!-- no status found -->
   <p>Sorry no status found</p>
<?php ?>

Thank you for your time.

MoFish
Reply

#2
(12-13-2014, 07:59 AM)MoFish Wrote: Hi,

I'm running into a little bit of trouble and would appriciate some guidance.

I currently have a list of statuses held in a database table called 'status'. These are 'Open', 'Closed', 'Pending'.

I also have another table of sales which is in a table called 'sales'.

I'm trying to loop around each status and get the relevant sales, but am having a bit of an issue working out how this is done.

Please note the below code does not work  Undecided

Controller:






PHP Code:
// this returns all the statuses (open, closed, pending)
$data["status"] = $this->sales_model->get_status();

// now i want to loop around these and get the all the items with the that status_id
foreach($data['status'] as $status){
 
   $data["sales"] = $this->sales_model->get_all($status['status_id']);


View:






PHP Code:
<?php foreach ($status as $s){ ?>
    <!-- loop around and get each status -->
    <h1>$s['status_title'];</h1>
    
    <!-- now i want to loop around the results and display them for this status -->
    <?php print_r($sales); ?>

<?php }else{ ?>
    <!-- no status found -->
   <p>Sorry no status found</p>
<?php ?>

Thank you for your time.

MoFish


Have you tried some thing like this on controller


PHP Code:
$results  $this->sales_model->get_sales();

foreach (
$results as $result) {
$data['sales'][] = array(
'sales_id' => $result['sales_id'
);




View Example


PHP Code:
<?php foreach($sales as $sale) { ?>

What ever

<code> <?php echo $sale['sale_id'];?> </code>

<?php ?>


Model example


PHP Code:
public function get_sales() {
return 
$this->db->get($this->db->dbprefix .'sales')->result_array();

There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply

#3
I have tried the following but unfortunately am still struggling.

I'm needing to loop around a table called "status" and get all status and display these to the screen in my view. eg.

Code:
<h1>Open</h1>
<h1>Closed</h1>
<h1>Pending</h1>

I then need to loop around another table called "sales" and get all the elements where its 'sale_status_id' equals the 'status_id' from the above loop. This should then display something like.

Code:
<h1>Open</h1>
<p>sale info 1</p>
<p>sale info 2</p>
<p>sale info 3</p>
<h1>Closed</h1>
<p>sale info 4</p>
<h1>Pending</h1>
<p>sale info 5</p>

Regards,

MoFish
Reply

#4
PHP Code:
foreach($data['status'] as $status){
 
   $data["sales"] = $this->sales_model->get_all($status['status_id']);



Simple, you overwrite the previous result at each iteration of your loop, and you end up with only the last one. Store the results in an array like this :


PHP Code:
foreach($data['status'] as $status){
 
   $data["sales"][] = $this->sales_model->get_all($status['status_id']);

Reply

#5
Hi Includebeer,

Thank you for taking the time to respond.

I have tried the following, but unfortunately i'm still not getting the results I require.

PHP Code:
$data["status"] = $this->sales_model->get_status();

foreach(
$data['status'] as $status){
 
   $data["sales"][] = $this->sales_model->get_all($status['salesstatus_id']);


$this->load->view('sale/index'$data); 

In the array returned to the view $sales, this does not contain all the status values with the sales inside each one. I would have expected a result something like the following:

Code:
Open // loop around each status
 array(
      item1(
         // all values in here from sales
      }
      item2(
         // all values in here from sales
      }
 )
Closed // loop around each status
 array(
      item3(
         // all values in here from sales
      }
 )
Pending // loop around each status
 array(
      item4(
         // all values in here from sales
      }

Regards,

MoFish
Reply

#6
Quote:In the array returned to the view $sales, this does not contain all the status values with the sales inside each one. I would have expected a result something like the following:

You can build your result array the way you want. Since I don't know how your model returns the data I can't give you the exact code. One way you can do it is like this :


PHP Code:
$data["status"] = $this->sales_model->get_status();

foreach(
$data['status'] as &$status){
 
   $status["sales"] = $this->sales_model->get_all($status['salesstatus_id']);
}
unset(
$status); 

This will give something like :


PHP Code:
array{
=>
 
'id' => 1,
 
'name' => 'open',
 
'sales' =>
 
   array{
 
     0 =>
 
       'id',
 
       ...
 
     1 => ...
 
     2 => ...
 
   }
 
=>
 
 'id' => 2,
 
 'name' => 'closed'
 
 'sales' =>
 
   array{
 
    ...
 
   }
...

Reply

#7
Hi,

I'm not sure if i'm doing this the correct way, but im returning my results in the model like:

PHP Code:
$query $this->db->get();
        if (
$query->num_rows() > 0) {
            foreach (
$query->result() as $row) {
                
$data[] = $row;
            }
            return 
$data;
        }
        return 
false

I tried the above, but unfortunately still not playing ball.

Regards,

MoFish
Reply

#8
What error do you get?

Note that the foreach in my example is assigning $status by reference. So you can modify the original array and not a copy of it.

Also, you don't have to loop on the results, you can replace
PHP Code:
foreach ($query->result() as $row) {
 
   $data[] = $row;
}
return 
$data
By :
PHP Code:
return $query->result(); 
Reply

#9
I think you may have cracked it! Seems to be appearing as expected. Thanks ever so much for taking the time to respond to me.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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