Show combo data when loading the page for the first time


(Santiago01) #1

Good afternoon.
I made a combo which shows the respective data of an option, but this is only loaded when navigating in the other options and returning to the first option.

I need help to show the combo data when the page is reloaded for the first time without having to click on the other options.

For your help, thanks.

Attached code.

Combo Code
$cat = ArrayHelper::map(Category::find()->all(), ‘id’, ‘name_category’);
echo $form->field($model, ‘category_id’)->dropDownList(
$cat,
[
‘prompt’=>‘All the products’,
‘onchange’=>‘javascript:this.form.submit();’,
]
);
?>

    <tbody>
    <?php

    if (isset($produc) && count($produc) > 0)
    {
        foreach ($produc as $products)
        {
    ?>

    <tr>    
      <td><center><?php echo $products->name_product ?></center></td>
      <td><center><?php echo $products->description ?></center></td>
      <td><center><?php echo $products->price ?></center></td>
    </tr>
    <?php  }}
    elseif(isset($pt)){

        foreach ($pt as $produ)
        {
    ?>

    <tr>    
      <td><center><?php echo $produ->name_product ?></center></td>
      <td><center><?php echo $produ->description ?></center></td>
      <td><center><?php echo $produ->price ?></center></td>
    </tr>
    <?php  }}
     ?>     
</tbody>
Name Description Price
<?php ActiveForm::end(); ?>

Combo function
public function actionCategoryProduct()
{
$model = new Product();

    if ($model->load(Yii::$app->request->post()) && empty($model->category_id)) {

    $pt = Product::findBySql("SELECT * FROM product")->all();

    return $this->render('category-product', ['model' => $model, 'pt' => $pt]);

}elseif ($model->load(Yii::$app->request->post()) ) {

    $produc = Product::findBySql("SELECT * FROM product WHERE category_id = ".$model->categoria_id."")
        ->all();

        return $this->render('category-product', ['model' => $model, 'produc' => $produc]);
    }
return $this->render('category-product', ['model' => $model]);

}