I'm trying to insert a data inside my log_tb
table using the set_log()
method inside the User_model
class. But I can't seem to make it work, thinking because of the way I parameterize a user id to set_log()
( No error are showing even though I set the $config['log_threshold']
to 4
in the config.php ).
User id comes from a query from get_user
method and run through the result using foreach()
to get the user id.
Take a look at my class below:
class User_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function set_log($userid) /* INSERT METHOD */
{
$date = date('Y-m-d H:i:s'); /* GET THE DATE AND TIME */
$data = array(
'user_id' => $userid,
'log_date' => $date
);
return $this->db->insert('log_tb', $data); /* INSERT THE DATA TO log_tb */
}
public function get_user($pinno = FALSE)
{
if ($pinno === FALSE)
{
$query = $this->db->get('user_tb');
return $query->result_array();
}
$query = $this->db->get_where('user_tb', array('pin_no' => $pinno));
foreach($query->result() as $query_item) /* RUN THROUGH THE RESULT */
{
$this->User_model->set_log($query_item["user_id"]); /* GET THE USER ID AND PARAMETERIZE IT TO set_log METHOD */
}
return $query->row_array();
}
}
How can I get the user id from $query
variable to make it as a parameter to set_log()
method? Is this the right approach to this type of logic? Or should I just call the set_log()
outside?
since you use $query->result the outcome is an object try this
public function get_user($pinno = FALSE)
{
if ($pinno === FALSE)
{
$query = $this->db->get('user_tb');
return $query->result_array();
}
$query = $this->db->get_where('user_tb', array('pin_no' => $pinno));
foreach($query->result_array() as $query_item) /* RUN THROUGH THE RESULT */
{
$this->set_log($query_item['user_id']); /* GET THE USER ID AND PARAMETERIZE IT TO set_log METHOD */
}
return $query->result_array();
}
In this function get_user()
you are using $query->result()
for getting userid as $query_item["user_id"]
, it will return you result in object form not in array.
Modified Function:
public function get_user($pinno = FALSE)
{
if ($pinno === FALSE)
{
$query = $this->db->get('user_tb');
return $query->result_array();
}
$query = $this->db->get_where('user_tb', array('pin_no' => $pinno));
foreach($query->result() as $query_item) /* RUN THROUGH THE RESULT */
{
$this->User_model->set_log($query_item->user_id); /* GET THE USER ID AND PARAMETERIZE IT TO set_log METHOD */
}
return $query->row_array();
}
There are two solutions:
Solution 1:
If you want to use
$query->result()
Than you need to use userid like:
$query_item->user_id // as property in foreach() body
Solution 2:
If you want to use
$query->result_array()
Than no need to change anything in foreach()
body:
$query_item["user_id"] // as array index
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.