简体   繁体   中英

Opencart pass variable from controller to model

(hi) everyone!

I'm using OpenCart v.2.2.

First I have a trouble with PHP Event Calendar, which I try to develop.

So, my question to you is how can I pass data variable from controller to model where this variable is set in SQL query?

I will show some piece of code.

Model:

<?php 
$year = '';
$month = '';
$dateYear = ($year != '') ? $year : date("Y");
$dateMonth = ($month != '') ? $month : date("m");
...
$result = $this->db->query("SELECT * FROM " . DB_PREFIX . "product WHERE event_start = '" . $currentDate . "' AND status = 1");
$event_num = $result->num_rows;

?>

So, when I choose from dropdown any month or year

<select name="month_dropdown" class="month_dropdown dropdown"><?php echo $get_months; ?></select>
<select name="year_dropdown" class="year_dropdown dropdown"><?php echo $get_years; ?></select>

I just want to get selected month or/and year and then pass the value to controller. After this the data should be passed to model to set selected month. If I choose 10th month then

Model:

<?php
$month = '10';
?>

I tried to pass the variable hardcode, but there is no result in the model :(

Controller:

<?php
$data['month'] = '10';
$data['year'] = '2017';
?>

Model:

<?php 
if (isset($data['month'])) {
   $month = $data['month'];
}
if (isset($data['year'])) {
   $year= $data['year'];
}
?>

But I got the following notice:

Notice: Undefined variable: data in C:\xampp\htdocs\events\catalog\model\event_calendar\event.php on line 11

But when I hardcode the model $month = '10' I get successful all of days for this month.

Thanks in advance for all replies :)

Try below :

View (.tpl file)

<select name="month_dropdown"><?php echo $your_varialbe; ?></select>

Controller file

if(isset($this->request->post['month_dropdown'])) {
$month_dropdown = $this->request->post['month_dropdown'];
} else {
$month_dropdown = 0;
}
$info = $this->model_catalog_product->your_method_name($month_dropdown);

Model file (assuming you are using model - catalog/model/catalog/product.php)

public function your_method_name($your_method_name) {
//use $your_method_name here 
}

Hope this help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM