how can i validate a multi dimentional array in codeigniter? - codeigniter

I have a form with dynamic fields that result in a multidimensional array format,
like this:
name="cart[1][qty]"
name="cart[2][qty]"
name="cart[3][qty]"
...
I use this rule:
$this->form_validation->set_rules('cart[][qty]','number','is_natural');
but it fails to validate. If i change the rule to :
$this->form_validation>set_rules('cart[1][qty]','number','is_natural');
it just validates the first field, rather than all of the fields.
I would like to prevent from inserting the decimal numbers in this form.
How can I validate it in Codeigniter using form_validation?
Thanks in advance.

You can try example below using foreach loop
$cart = $this->input->post('cart');
if(!empty($cart)) {
foreach($cart as $number => $data) {
$this->form_validation->set_rules('cart[' . $number . '][qty]', 'Number', 'is_natural');
}
}

Related

Laravel Validation Passing Variable to Rule::in() method

I am importing a Excel file in Laravel, It is working fine. I need some validation like whether team ids are available in database or not. I make a query each time file uploaded and create an array and then implode it to pass in rule condition. Can you please help. Is it possible to pass and variable to rule method as am trying to do in my code?
Thanks in advance.
public function rules(): array
{
.......
//Some Codes here
.....
$team_id = implode("', '", $team_id_array);
return [
'*.ext_or_int' => Rule::in(['Internal','External']),
'*.team_id' => Rule::in(['$team_id']),
];
}

Getting error as stated below while passing three array values from controller to the view(i.e., to the same page)

Find below the controller code:
public function domaincheck()
{
$response = file_get_contents('https://resellertest.enom.com/interface.asp?command=check&sld=unusualTVname&tld=tv&uid=resellid&pw=resellpw&responsetype=xml');
$data = simplexml_load_string($response);
$configdata = json_encode($data);
$final_data = json_decode($configdata,true);// Use true to get data in array rather than object
// dd($final_data);
$response1 = file_get_contents('http://resellertest.enom.com/interface.asp?command=GETNAMESUGGESTIONS&UID=resellid&PW=resellpw&SearchTerm=hand&MaxResults=5&ResponseType=XML');
$data1 = simplexml_load_string($response1);
$configdata1 = json_encode($data1);
$final_data1 = json_decode($configdata1,true);// Use true to get data in array rather than object
//dd($final_data1);
$response2 = file_get_contents('https://resellertest.enom.com/interface.asp?command=gettldlist&UID=resellid&PW=resellpw&responsetype=xml');
$data2 = simplexml_load_string($response2);
$configdata2 = json_encode($data2);
$final_data2 = json_decode($configdata2,true);
//dd($final_data2);
return view('clientlayout.main.test',array('final_data1' =>
$final_data1), array('final_data' => $final_data),
array('final_data2' => $final_data2));
}
Find the view code given below:
{{print_r($final_data)}}
<br><br><br>
{{print_r($final_data1)}}
<br>
{{print_r($final_data2)}}
Find the route code given below:
Route::get('/test','EnomController#domaincheck');
I need to return all the three arrays to the view page but When use the return view code giveb above I'm getting error as "Undefined variable: final_data2 " for the third array alone.If I declare only two array statements in the return view it works correctly. Suggest me a solution to solve this error.
in addition to #achraf answer if you dont want to use same variable names in your view as of your controller, you can pass data to your view like this. where final1, final2 and final3 will be the names of the variable in your view.
return view('clientlayout.main.test',['final1' =>$final_data ,'final2'=> $final_data1,'final3' => $final_data2]);
View take second param as array, your sending multiple arrays,
the solution is to create an array of arrays like this
return view('clientlayout.main.test',compact('final_data','final_data1','final_data2'));

Convert Auth item to array before use it Laravel 4

I've working on User custom permission in Laravel 4, After login permissions(json string) stored in Auth::user()->permissions. as following:
$permissions =array(101,102);
DB::table('user')->where('id', Auth::user()->id)->update(['permissions' => json_encode($permissions)]);
But while I've checking permissions every time need to decode it to array:
if(in_array(1000, json_decode(Auth::user()->permissions)){
}
but I want something that make it to work like following:
if(in_array(1000, Auth::user()->usr_rights){
}
You can add a Accessor to your model :
public function getPermissionsAttribute($value)
{
return json_decode($value);
}
and all you have to do is this :
if(in_array(1000, Auth::user()->permissions){
}
You could use Implode
This isnt the nicest way of doing it but works.
Example:
$array = array('lastname', 'email', 'phone');
comma_separated = implode(",", $array);
echo $comma_separated; // lastname,email,phone
U could save this string in an additional row in user table.
and then convert it back with Explode
$pizza = "piece1, piece2, piece3, piece4, piece5, piece6";
$pieces = explode(",", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2

How to create user-friendly and seo-friendly urls in codeigniter?

i want to change my codigniter url which looks like
http://dev.hello.com/about/updateVision/9
to
http://dev.hello.com/about/updateVision/this-is-test-edit/
i have no idea how to make looks like that anyone can help please
thanks !
This all depends on you database structure. I would introduce a field called reference or slug. This will contain a unique string for your record. When creating the record you can use the url_title function to remove spaces as mentioned by #Rooneyl
This reference must be unique so I would create a function that creates a reference in a loop, checks it against the database. When it returns false, it is unique.
public function unique_reference($name)
{
$name = trim(strtolower($name));
$unique_reference = url_title($name);
$counter = '';
do {
$result = $this->db->select('id')
->from('your_table')
->where('reference', $unique_reference)
->get()->row();
if ($result)
$counter++;
$unique_reference = url_title(trim("{$name} {$counter}"));
} while ($result);
return $unique_reference;
}
You can then retrieve record using this unique value

codeigniter database how to limit the output

Hello how can i like if i use substr(); do so i only get like 400 number of characters from the database out?
You have to use core mysql function SUBSTRING to achieve this.
In codeigniter the query can be written as -
$this->db->select("SUBSTRING('COLUMN_NAME',5)");
$query = $this->db->get('TABLE_NAME');
foreach ($query->result() as $row)
{
//process result here.
}
You can use codeigniters limiter (test helper) to display only what you want
$string = "Here is a nice text string consisting of eleven words.";
$string = character_limiter($string, 400);
You can pull the entire string out of the database but only use the number of characters you need.
Or
take a look at this tutorial using "left" in mysql
http://net.tutsplus.com/tutorials/php/how-to-create-blog-excerpts-with-php/
It too late, but this is for someone like me looking for solution
public function getDetails(){
// mytable(id,name,about,...,status)
$this->db->select(array('id', 'name', 'SUBSTRING(about,1,180) AS about', 'status'));
$result=$this->get('mytable');
return result_array();
}

Resources