I am trying to create this filter thing with check boxes.
I am trying to toggle the state of the check boxes to be true or false
<x-input.checkbox wire:click="$toggle(free_trial)"/>
public $free_trial = false;
public $free_forever = false;
public $pay_per_user = false;
public $progressive = false;
When I do this approach, I am not sure how to make the inverse work, so if it is unchecked It would be false. Is there a way to add something to this, so if it's clicked again it would change it to false?
<x-input.checkbox wire:click="$set('free_trial','true')"/>
Any help much appreciated :)
<x-input.checkbox wire:click="$set('free_trial',{{ $free_trial ? 'false' : 'true' }})"/>
I prefer dev this kind of login in a backend method.
<x-input.checkbox wire:click="toggleProperties('free_trial')"/>
public function toggleProperties($property)
{
if($property == 'free_trial')
$this->free_trial = !$this->free_trial;
elseif(...) //.....
}
Related
I was wondering why my checkbox fails even after it has already been ticked. It is a checkbox for terms and conditions
Controller:
// Load the registration page
public function registration_page(){
$this->load->view('includes/main_index');
$this->load->view('recycler/forms/register');
$this->load->view('includes/footer');
}
public function accept_terms_conditions()
{
// Checkbox name is 'agree' which checks if the value of $checked
// is equal to 1
$checked = $this->input->post('agree');
return (int) $checked == 1 ? TRUE : FALSE;
}
$this->form_validation->set_rules('agree', '', 'callback_accept_terms_conditions');
if($this->form_validation->run() == FALSE)
{
// After form submission, it always goes here
$this->registration_page();
}
else{ /* Proceed to data insertion */ }
I printed out the post value of it using print_r function and it returned 1, yet the validation still fails. $this->form_validation->run() == FALSE means that if the form validation has errors, it will go back to the register view, which is the $this->register() method call.
print_r
ticked checkbox
Does anyone know why it keeps failing?
EDIT:
Added the method for loading the registration view which is the register() method
try
public function accept_terms_conditions() {
$checked = ($this->input->post('agree')) ? TRUE : FALSE;
return $checked;
}
A checkbox brings "on" as value when it is checked not 1.
public function accept_terms_conditions()
{
// returns true if the checkbox 'agree' has been checked
return ($this->input->post('agree') === 'on');
}
And your condition is wrong, you are saying that if the validation has failed then call method register I think it should be the other way round.
I am front of reflexion about my php developments. I'm trying to optimize my code.
I have often condition like this :
if($userConnected->getType() == User::BUYER_ACCOUNT_TYPE || $userConnected->getType() == User::ADMIN_ACCOUNT_TYPE){//Mycode}
My question is : Is it possible to have something like this :
if($userConnected->getType() == User::BUYER_ACCOUNT_TYPE || User::ADMIN_ACCOUNT_TYPE)
Actually the best way I found to do this is :
if(in_array($userConnected->getType(), array(User::BUYER_ACCOUNT_TYPE, User::ADMIN_ACCOUNT_TYPE)))
And I want to know if there is a better way ?
Thank you in advance
Thomas
You can add some public methods to your User class to check if the user is a buyer or an admin:
public function isBuyer()
{
return $this->type === self::BUYER_ACCOUNT_TYPE;
}
public function isAdmin()
{
return $this->type === self::ADMIN_ACCOUNT_TYPE;
}
Having these methods you can simply check:
if ($userConnected->isBuyer() || $userConnected->isAdmin())
You can go further and do a single method if the condition above is used very often:
public function isAllowed() // just an example of a method name
{
return $this->isBuyer() || $this->isAdmin();
}
So I've built a small conditional to evaluate which button is pressed in my form (as there are 2). This works fine and fires off the correct method and writes the appropriate data to the DB, however my redirect is not working. It saves() to the DB and then simply stays on the page designated as the POST route.
I suspect the problem has something to do with my conditional and the use of $this.
Here is my check_submit method:
public function check_submit()
{
if(!is_null(Input::get('add_to_invoice'))){
$this->invoice_add_item();
} elseif(!is_null(Input::get('complete_invoice'))) {
$this->invoice_complete();
}
}
Here is one of the 2 methods which I am currently testing:
public function invoice_add_item()
{
$input = Request::all();
$invoice_items = new Expense;
$invoice_items->item_id = $input['item_id'];
$invoice_items->category_id = $input['category'];
$invoice_items->price = $input['price'];
$invoice_items->store_id = $input['store'];
if(Input::has('business_expense'))
{
$invoice_items->business_expense = 1;
}
else{
$invoice_items->business_expense = 0;
}
$invoice_items->save();
return redirect('/');
}
Perhaps there is a better way of handling this in my routes(web) file, but I'm not sure how to go about this.
You should add the return to the check_submit() method. Something like
public function check_submit()
{
if(!is_null(Input::get('add_to_invoice'))){
return $this->invoice_add_item();
} elseif(!is_null(Input::get('complete_invoice'))) {
return $this->invoice_complete();
}
}
Better yet, you should probably return a boolean on invoice_add_item() and based on that, redirect the user to the correct place (or with some session flash variable with an error message)
I am not actually login in to the page.But when i click on the services on index page for more services , show logout button instead of login.Please provide solution for this issue.
Controller:
public function more_services($serviceid)
{
$data = $this->data;
$this->load->helper(array('form','url'));
$id=$this->session->userdata('id');
$this->load->model('realpropertiesmodel');
$data['service']=$this->realpropertiesmodel->getServicesbyID($serviceid);
$this->smarty->view('service/service_viewdetails.tpl',$data);
}
public function logout()
{
$this->session->sess_destroy();
$data = $this->data;
redirect("realestate/index" ,'refresh');
}
In the view i add the following code:
[~if $id==''~]
[~include file="common/realestate_header.tpl"~]
[~else~]
[~include file="home/realestate_header.tpl"~]
[~/if~]
But i got the same error when add this much f code.Please provide solution for this issue
your id variable might be false so your check with "" might fail.
First solution:
Change the id variable as -1 if no login information is present:
$this->load->helper(array('form','url'));
$id= $this->session->userdata('id');
$id = ($id) ? $id : -1;
$this->load->model('realpropertiesmodel');
And then check for:
[~if $id!=-1~]
Second solution
Directly check for false:
[~if $id==false~]
I'm working on creating a callback function in codeigniter to see if a certain record exists in the database, and if it does it'd like it to return a failure.
In the controller the relevent code is:
function firstname_check($str)
{
if($this->home_model->find_username($str)) return false;
true;
}
Then in the model I check the database using the find_username() function.
function find_username($str)
{
if($this->db->get_where('MasterDB', array('firstname' => $str)))
{
return TRUE;
}
return FALSE;
}
I've used the firstname_check function in testing and it works. I did something like
function firstname_check($str)
{
if($str == 'test') return false;
true;
}
And in that case it worked. Not really sure why my model function isn't doing what it should. And guidance would be appreciated.
if($this->home_model->find_username($str)) return false;
true;
Given that code snippet above, you are not returning it true. If that is your code and not a typo it should be:
if($this->home_model->find_username($str)) return false;
return true;
That should fix it, giving that you did not have a typo.
EDIT:
You could also just do this since the function returns true/false there is no need for the if statement:
function firstname_check($str)
{
return $this->home_model->find_username($str);
}
So the solution involved taking the query statement out of if statement, placing it into a var then counting the rows and if the rows was > 0, invalidate.
Although this is a more convoluted than I'd like.
I find your naming kind of confusing. Your model function is called 'find_username' but it searches for a first name. Your table name is called 'MasterDB'. This sounds more like a database name. Shouldn't it be called 'users' or something similar? I'd write it like this :
Model function :
function user_exists_with_firstname($firstname)
{
$sql = 'select count(*) as user_count
from users
where firstname=?';
$result = $this->db->query($sql, array($firstname))->result();
return ((int) $result->user_count) > 0;
}
Validation callback function :
function firstname_check($firstname)
{
return !$this->user_model->user_exists_with_firstname($firstname);
}