How to store the database value in array? - codeigniter

im new to codeignitor and suffering to store a database value in array and pass it through other page
$query = $this->db->query("YOUR QUERY");
foreach ($query->result() as $row)
{
echo $row->title;
echo $row->name;
echo $row->body;
}
instead of echo it should be stored in array and pass it to other page as a argument.. is it possible??

This code return the query result as array, now you can use the array inside your controller and pass it to your view.
$data = array();
foreach ($query->result() as $row)
{
$data[] = array(
'title' => $row->title,
'name' => $row->name,
'body' => $row->body
);
}
return $data;

Related

Getting data from mysql in result array and batch updating

I need a quick solutions to this problem as I'm getting uninitialized offset 2 error codes
$this->db->select('bprice');
$this->db->from('items');
$this->db->limit(20);
$query = $this->db->get()->result_array();
$data = array();
foreach($query as $key => $value){
$data = array(
'branch_price' => (float)$value['bprice'][$key] + ((float)$value['bprice'][$key] * 0.30)
);
}
return $this->db->update_batch('items',$data);
$this->db->select('bprice');
$this->db->from('items');
$this->db->limit(20);
$query = $this->db->get()->result_array();
$data = array();
foreach($query as $query){
$data = array(
'branch_price' => (float)$query['bprice'] + ((float)$query['bprice'] * 0.30)
);
}
return $this->db->update_batch('items',$data);

How to remove commas and spaces when submitting a excel file in laravel

I'm trying to save data that is in an excel file to my database and I would like to check if any of those has a comma or spaces in it and if it does I would like to remove it.
I'm using phpoffice/phpspreadsheet and laravel and if possible I would like to use phpoffice/phpspreadsheet if it can do it.
Here is my code
$xl = IOFactory::load($path);
$sheet = $xl->getSheetByName('Products');
$c = $sheet->getHighestDataColumn();
$r = $sheet->getHighestDataRow();
$data = $sheet->rangeToArray("A1:{$c}{$r}", null, false);
$headers = array_shift($data);
$projects = [];
foreach ($data as $row) {
$entry = array_combine($headers, $row);
$projects[] = [
'name' => $entry['Name'],
'description' => $entry['description'],
'price' => $entry['price'],
];
}
return $projects;
try to create a function :
function cleanData($data) {
$data= str_replace( ',', '', $data);
$data= preg_replace('/\s+/', '', $data);
return $data;
}
and use it:
foreach ($data as $row) {
$entry = array_combine($headers, $row);
$projects[] = [
'name' => cleanData($entry['Name']),
'description' => cleanData($entry['description'],
'price' => cleanData($entry['price']),
];
}

uploading image and got error in laravel "Undefined variable: data"

I am trying to update the product and for that I am updating the pictures. I meant if user want to add more pictures to it but when I do this I get below error
Undefined variable: data
Also I want to restrict the total uploading pictures to max 3 pictures or 2 pictures and 1 video
When I add a video it does not show like it does not play
Any help would be great. Thank for the help in advance
public function editProduct($id, Request $request) {
if(Auth::check()) {
$pn = Input::get('pn');
$desc = Input::get('desc');
$price = Input::get('price');
// $products = Product::all();
$products = Product::where('id', $id)->first();
// foreach($products as $p) {
if(($products->user_id == Auth::user()->id) && ($products->id == $id)) {
$this->validate($request, [
'filename.*' => 'required|max:5000',
// 'filename.*' => 'image|mimes:jpeg,png,jpg,mp4|max:5000'
]);
if($request->hasfile('filename'))
{
foreach($request->file('filename') as $image)
{
$name = $image->getClientOriginalName();
$filename = time(). '-' . $image;
$file_path = $image->move(public_path().'/assets/images/', $name);
$data[] = $name;
}
}
$new_name = json_encode($data);
$products = Product::where('user_id', Auth::user()->id)
->where('id', $id)->update([
'product_name' => $pn,
'description' => $desc,
'price' => $price,
'filename' => $new_name,
]);
}
} else {
Session::flash("message", "OOPS! You dont have permission to edit the items. Please login first.");
return redirect("/register-user");
}
I was missing enctype="multipart/form-data" files=true in my form in the view so it was not getting a file from the blade so got it fixed now

How to update multiple rows of a table in Laravel?

Helper,
I wanna update multiple addresses for a particular user_id, with out changing its existing order. Also I'm using relation here.
public function update(Request $request, $id){
$data['id'] = $id;
$user = User::with('addresses')->find($id);
$j = $user['addresses'][0]['id'];
foreach($request->address_id as $key => $i){
$i++;
}
foreach($request->address1 as $key => $v){
if($j<$i){
$user->addresses()->whereUserId($data['id'])->update([
'address1' => $v,
'address2' => $request->address2[$key],
'state' => $request->state[$key],
'country' => $request->country[$key]
]);
}
$j++;}
}
I'm getting input somewhat like this,
{"_method":"PATCH","address_id":["92","93"],"address1":["aaa","xxx"],"address2":["bbb","yyy"],"state":["ccc","zzz"],"country":["India","India"]}
When I do my update the last input, which means, xxx,yyy,zzz,India is getting update for both rows92,93

Update in Codeigniter for Beginner

Can anyone help me to update in Codeigniter?
This is my model code:
function update($data,$userid)
{
$data = array(
'dob' => $dob,
'sex' => $sex,
);
$this->db->where('user_id', $userid);
$this->db->update('profile', $data);
}
Assuming you are passing dob and sex via the data variable in the function you're just trying to access those incorrectly.
function update($data,$userid)
{
$data = array(
'dob' => $data['dob'],
'sex' => $data['sex'],
);
$this->db->where('user_id', $userid);
$this->db->update('profile', $data);
}
And for that matter if that is all that's in that data variable you can lose the first part entirely since it is really just replicating what you already have.
function update($data,$userid)
{
$this->db->where('user_id', $userid);
$this->db->update('profile', $data);
}
$where = array('id'=>'10','email'=>'dude#dude.com');
$data = array('username'=>'lol');
function update($data,$where){
foreach($where as $key=>$value){
$this->db->where($key,$value);
}
$this->db->update('users',$data);
}

Resources