0

I am trying to pass two result sets from separate queries executed within one model method to a view file.

In my controller, I am just printing the array by print_r($data) function to express the payload. I don't know how to loop this result in my view.

Array (
    [value] => Grocery 
    [products] => Array (
        [0] => Array (
            [query1] => Array (
                [0] => stdClass Object ( [parent_category] => Beverages )
                [1] => stdClass Object ( [parent_category] => Household )
            )
            [query2] => Array (
                [0] => stdClass Object ( [product_name] => Britannia Cake - Fruity Fun )
                [1] => stdClass Object ( [product_name] => Tetley Green Tea - Long Leaf )
                [2] => stdClass Object ( [product_name] => Tetley Green Tea - Lemon )
                [3] => stdClass Object ( [product_name] => AVT Gold Cup Premium Dust Tea )
                [4] => stdClass Object ( [product_name] => Brooke Bond Red Label Tea )
                [5] => stdClass Object ( [product_name] => sd sdsds s )
                [6] => stdClass Object ( [product_name] => sds dfcs )
                [7] => stdClass Object ( [product_name] => fhgg )
                [8] => stdClass Object ( [product_name] => hearts )
                [9] => stdClass Object ( [product_name] => banana )
                [10] => stdClass Object ( [product_name] => hearts )
            )
        )
    )
)

Model

$query3 = $this->db
    ->select('product_name')
    ->from('materials')
    ->where('category', $value)
    ->get();
$data = array();
if ($query3->num_rows() > 0) {
    //$data['category'] = $this->test($value,$location);
    $query4 = $this->db
        ->select('parent_category')
        ->distinct()
        ->from('materials')
        ->where('category', $value)
        ->where('warehouse_id', $location)
        ->group_by('parent_category')
        ->get();
    if ($query4->num_rows() > 0) {
        $data['query1'] = $query4->result();
    }
    $data['query2'] = $query3->result();
             
    return $data = array($data);
}

Controller

$data['value'] = $this->input->post('newparam');
$data['products'] = $this->Search_model->getItems($data['value'], $location);
print_r($data);

View

<table>
    <?php foreach ($query2 as $product){ ?>
        <tr>
            <td><?php echo $product->product_name?></td>
        </tr>
    <?php } ?>
</table>
0

5 Answers 5

2

Assuming your $products variable has the following data structure :

[products] => Array ( 
            [0] => Array ( 
                    [query1] => Array ( 
                            [0] => stdClass Object ( [parent_category] => Beverages ) 
                            [1] => stdClass Object ( [parent_category] => Household ) ) 
                    [query2] => Array ( 
                            [0] => stdClass Object ( [product_name] => Britannia Cake - Fruity Fun ) 
                            [1] => stdClass Object ( [product_name] => Tetley Green Tea - Long Leaf ) 
                            [2] => stdClass Object ( [product_name] => Tetley Green Tea - Lemon ) 
                            [3] => stdClass Object ( [product_name] => AVT Gold Cup Premium Dust Tea ) 
                            [4] => stdClass Object ( [product_name] => Brooke Bond Red Label Tea ) 
                            [5] => stdClass Object ( [product_name] => sd sdsds s ) 
                            [6] => stdClass Object ( [product_name] => sds dfcs ) 
                            [7] => stdClass Object ( [product_name] => fhgg ) 
                            [8] => stdClass Object ( [product_name] => hearts ) 
                            [9] => stdClass Object ( [product_name] => banana ) 
                            [10] => stdClass Object ( [product_name] => hearts ) 
                    ) 
            ) 
        ) 

Then to loop through the data in the query2 array, you could do :

<?php foreach($products[0]['query2'] as $product) { ?>
    <tr>
        <td><?php echo $product->product_name;?></td>
    </tr>
<?php } ?>
Sign up to request clarification or add additional context in comments.

Comments

1

pass data in controller like this

$this->load->view('myview', $data);

try this in view

<?php
foreach ($products as $prod){
    $query1 = $prod['query1'];
    $query2 = $prod['query2'];
}
foreach($query2 as $product){?>
    <tr>
                <td><?php echo $product->product_name?></td>

        </tr>
<?php
}
?>

Comments

1

You have to pass data to your view, like below

/* This is in your controller */
$data['value'] = $this->input->post('newparam');
$data['products']=$this->Search_model->getItems($data['value'],$location);

/* say your view file is myview.php then */
$this->load->view('myview', $data);

in you view (myview.php), you get two variables

<?php print_r($products);?>
<?php print_r($value);?>

and iterate like below

<table>
<?php foreach ($products[0]['query2'] as $product): ?>
        <tr>
                <td><?php echo $product->product_name?></td>
        </tr>
<?php endforeach;?>
</table>

2 Comments

i am able print the data but i just want to loop through it
see added how to loop also for $products[0]['query2'], since you got return $data=array($data); in your model, otherwise you can just use $products['query2']
0

Pass the data from controller to view then loop it inside the view

CONTROLLER

  function index(){
    $data['value'] = $this->input->post('newparam');
    $data['products']=$this->Search_model->getItems($data['value'],$location);
    $this->load->view("pages/extras/php/content", $data);
  }

VIEW

<?php

  echo $value;

  foreach ($products as $key => $value) {
    //do whatever you want
  }
?>

See https://www.codeigniter.com/user_guide/general/views.html for more examples

2 Comments

how to loop throght inside associative array
<?php echo $products[$key]["product_name"]; ?> this is wnot working
0

Your model method getItems() is violating the Single Responsibility Principle and has a non-indicative method name. Even the Search_model is non-indicative (there is no sense of context).

Rename the model Search_model to Material_model.

Split the two processes into individual methods with meaningful names. Store their return values in $data using meaningful keys (not query1 and query2).

Model:

public function getProductNames(string $category): array
{
    return array_column(
        $this->db
            ->get_where('materials', ['category' => $category])
            ->result(),
        'product_name'
    );
}

public function getParentCategories(string $category, int $warehouseId): array
{
    return array_column(
        $this->db
            ->distinct()
            ->select('parent_category')
            ->get_where('materials', [
                 'category' => $category,
                 'warehouse_id' => $warehouseId
            ])
            ->result(),
        'parent_category'
    );
}

Controller

$data['category'] = $this->input->post('newparam');
$data['products'] = $this->Material_model->getProductNames($data['category']);
$data['parentCategories'] = $this->Material_model->getParentCategories($data['category'], $location);
$this->load->view('myview', $data);

View:

echo "Category selected: $category";

if ($products) {
    echo 'Products: ';
    echo '<table><tr><td>' . implode('</td></tr><tr><td>', $products) . '</td></tr></table>';
}

if ($parentCategories) {
    echo 'Parent Categories: ';
    echo '<table><tr><td>' . implode('</td></tr><tr><td>', $parentCategories) . '</td></tr></table>';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.