Code igniter batch_insert - codeigniter

I have an issue where I want to make a survey app where questions are retrieved from the database and looped in a form with radio buttons where by on submission I get a multi dimensional array. Could some one help with the process of doing such a process. From form to controller and then to the model?
Here is my method
public function registerSelfAdmin(){
$fieldsData = $this->db->field_data('response');
foreach ($fieldsData as $key => $field){
$datacc = array( $field->name => $this->input->post($field->name) );
$this->db->insert('response', $datacc);
}
return ($this->db->affected_rows() != 1) ? false : true;
// $query=$this->db->insert_batch('response',$newData,'question_id');
// return ($this->db->affected_rows() != 1) ? false : true;
}

Related

Laravel 5.6 foreach not showing any data

I want to ask. I use foreach for showing my data from database. But nothing happen on view. Here is my code :
public function page($page_url = null){
if($page_url == "villas"){
$villa = Pages::where(['url_page' => $page_url])->first();
$postvilla = Posts::where(['id_page' => $villa->id_page]);
return view('front.villa', compact(array('villa', 'postvilla')));
}else if($page_url == "admin"){
return redirect('/admin/home');
}
}
You can pass variables in compact like
return view('front.villa', compact('villa', 'postvilla'));

How to reinitialize model when client side validation fails in Yii 2?

I am working on Yii 2 form and I want to reinitialize model when client side validation fails. For example with certain rules like below:
public function rules()
{
return [
[['username'], 'required', 'message' => 'You must enter your username'],
['username','email'],
[['password'], 'required', 'message' => 'You must enter your password'],
];
}
When validation fails I want all fields to be empty (for example when user enters invalid email address). How can I do that?
I assume you use standard Yii 2 way of loading the model:
$model = new SomeModel();
if ($model->load(\Yii::$app->request->post()) && $model->save()) {
// ...
}
return $this->render('view', ['model' => $model]);
Set fields to null when validation fails. You don't want to create new instance (which would be easier) because you would lost all validation messages.
$model = new SomeModel();
if ($model->load(\Yii::$app->request->post())) {
if ($model->save()) {
// ....
} else {
$model->username = null;
$model->password = null;
}
}
return $this->render('view', ['model' => $model]);
UPDATE: for the client side validation add this JS code in view:
$("#form-ID").on("afterValidateAttribute", function (event, attribute, messages) {
if (event.result === false) {
attribute.value = "";
}
});
Replace #form-ID with proper form element JS identifier.

codeigniter join table ss

I have a problem with codeigniter,
I want to do a join in the addition of a carrier,
when I add I assign a truck this driver
I want the state of truck changes from 0 to 1,
but I do not know,
public function add($email, $password , $nom , $prenom , $telephone,$id_camion)
{
$query = $this->db->get_where('transporteur', array('email' => $email));
if ($query->num_rows == 1) {
return FALSE;
}
$this->db->insert('transporteur', array('email' => $email,'password' => md5($password),'nom' => $nom ,'prenom'=>$prenom ,'telephone' => $telephone,'id_camion' => $id_camion));
return TRUE;
}
If I understand your question correctly, now that you've inserted a new carrier you want to set some state in a table of trucks. You already have the truck ID as a parameter so in theory all you need to do is:
//update only on a given camion_id
$this->db->where('id', $camion_id);
$this->db->update('camions', array('state' => 1));
Here I assume your table is called camions, its ID is id and the state column you're trying to change from 0 to 1 is called state.
If that's not quite right, please update your question. If you have trouble translating it into english, I can help with that, too. ;)
I'm confused about your question but you have (num_rows should be num_rows()) following code:
if ($query->num_rows == 1) {
return FALSE;
}
It should be:
if ($query->num_rows() == 1) {
return FALSE;
}
It's a method not a property. You may also use it like this way:
if ($query->num_rows()) {
return FALSE;
}

find('first') within a Model custom validate rule

I want to validate a field to be within a range of numbers dependant on another field selection.
eg.
'rating' => array(
'within class range' => array(
'rule' => 'withinClassRange',
'message' => 'number not in range'
)
),
and withinClassRange
public function withinClassRange($data) {
// get class range
$classRange = $this->Edition->Aclass->find('first', array('conditions' => array('Aclass.id' => $this->data['Edition']['aclass_id'])));
if($data['rating'] < $classRange->minRange) {
return false;
}
if($data['rating'] > $classRange->maxRange) {
return false;
}
return true;
}
but this find is only do-able within the controller. How do I implement this?
You shouldn't really assign data to the request object. Either assign the request object to another array such as $data and do your updates there or find a way to pass the range to your validation function.
Also, the find statement from your question would return an array, not an object. So your syntax isn't valid there. That's probably the cause of your original problem.
Something like this should work:
public function withinClassRange($data) {
// get class range
$classRange = $this->Edition->Aclass->find('first', array('conditions' => array('Aclass.id' => $this->data['Edition']['aclass_id'])));
if($data['rating'] > $classRange['Aclass']['minRange'] && $data['rating'] < $classRange['Aclass']['maxRange']) {
return true;
}
return false;
}
I got it!
What I did was retrieve the data and set the range limits as extra vars in the request object in the Controller just before the 'save' call. Then the comparison can be done in the Model.
//controller
$classRange = $this->Edition->Aclass->find('first', array('conditions' => array('Aclass.id' => $this->request->data['Edition']['aclass_id'])));
$this->request->data['Edition']['minRange'] = $classRange['Aclass']['minRange'];
$this->request->data['Edition']['maxRange'] = $classRange['Aclass']['maxRange'];
//model
public function withinClassRange($data) {
if($data['rating'] < $this->data['Edition']['minRange']) {
return false;
}
if($data['rating'] > $this->data['Edition']['maxRange']) {
return false;
}
return true;
}

Custom errors e.g more than 1 record when using Codeigniter method

I have a codeigniter application.
My controller calls a function of a method.
I am checking if the row it is trying to select actually exists, and then returning true (and data) or false (and error message) respectively to my controller.
Within my controller there are multiple calls to this function.
I only want to load the 'success' view if all of these method calls return true.
Otherwise i want to display an error..
Given that i want to show the error within my layout, i could simply create an error view and pass an error message to it.. if there are multiple errors i want to display them all..
Is the correct/most efficient way to do this simply:
if($resultone['result'] == FALSE or $resulttwo['result'] == FALSE)
{
$data['error']['0']=$resultone['error'];
$data['error']['1']=$resulttwo['error'];
$this->load->view('custom_error',$data);
} else {
//load success view
}
I'd rather do this:
$data = array(
'data' => array(),
'errors' => array()
);
...
$result = call_that_function($arg1);
if (isset($result['error'])) {
$data['errors'][] = $result['error'];
} else {
$data['data'][] = $result['data'];
}
...
$result = call_that_function($arg2);
if (isset($result['error'])) {
$data['errors'][] = $result['error'];
} else {
$data['data'][] = $result['data'];
}
...
$result = call_that_function($arg3);
if (isset($result['error'])) {
$data['errors'][] = $result['error'];
} else {
$data['data'][] = $result['data'];
}
...
if (count($errors['errors']) > 0) {
$this->load->view('custom_error',$data);
} else {
$this->load->view('success',$data);
}

Resources