Form dropdown is giving me index and not the text - codeigniter

Can you help out a new learner of Codeigniter? for some reason my form dropdown is giving me the index as input->post. I just need the text selected.
Model
function get_items(){
$this->db->select('item_name');
$this->db->from('commissary_items');
$query=$this->db->get();
$result=$query->result();
$item_names=array('-SELECT-');
for($i=0;$i<count($result);$i++){
array_push($item_names,$result[$i]->item_name);
}
return $item_names;
}
View
<div class="form-group">
<div class="row colbox">
<div class="col-lg-4 col-sm-4">
<label for="item_name" class="control-label">Item</label>
</div>
<div class="col-lg-8 col-sm-8">
<?php
$attributes = 'class = "form-control" id = "item_name"';
echo form_dropdown('item_name',$item_name,set_value('item_name'),$attributes);?>
<span class="text-danger"><?php echo form_error('item_name'); ?></span>
</div>
</div>
</div>
Controller
public function new_inventory(){
$data['item_name']=$this->commissary_model->get_items();
$this->form_validation->set_rules('date_added','Date Added','required');
$this->form_validation->set_rules('item_name','Item Name','callback_combo_check');
$this->form_validation->set_rules('quantity','Quantity','required');
$this->form_validation->set_rules('amount','Amount','required|numeric');
$this->form_validation->set_rules('username','User Name');
if($this->form_validation->run()==FALSE){
// $data="";
$this->load->view('new_inventory_view',$data);
}else{
$data=array(
'date_added'=>#date('Y-m-d',#strtotime($this->input->post('date_added'))),
'item_name'=>$this->input->post('item_name'),
'quantity'=>$this->input->post('quantity'),
'amount'=>$this->input->post('amount'),
'username'=>$this->session->userdata('username')
);
$this->db->insert('add_inventory',$data);
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">Item added to inventory.</div>');
redirect('commissary/added_to_inventory');
}
}
Instead of the "text value" inside the form dropdown, I get the index 1 or 2 or 3 or 4 or 5 etc.... Thank you.

That is how <select> options work - the value of the select option is returned.
If you want the text you will have to setup $item_names differently and have the index be the same as the text. Easily accomplished with a little restructuring in the model.
function get_items()
{
$this->db->select('item_name');
$this->db->from('commissary_items');
$query = $this->db->get();
$result = $query->result();
$item_names = array();
foreach($result as $item)
{
$item_names[$item->item_name] = $item->item_name;
}
return $item_names;
}

Related

How to use condition in laravel query

Hello I am new to laravel. I am getting an error saying 'Property [pickupdate] does not exist on this collection instance.', after fowarding to 'overdue_pickup' view.
My Controller code
public function overdue_pickup(){
$id = "2";
$curr_date = date('m/d/yy');
$overdue_pickup = DB::table('archive_pickup')
->where('ridder_id_',$id)
->get();
if($overdue_pickup->pickupdate < $curr_date){
return view('overdue_pickup',['overdue_pickup' =>
$overdue_pickup]);
}else{
return "No Overdue Pickup";
}
}
My overdue_pickup view
<?php $i = 1; ?>
#foreach($overdue_pickup as $overdue_pickup)
<div class="col-md-offset-1 col-md-10">
<div class="box box-success">
<div class="box-header with-border">
<h3 class="box-title" style="padding:5px">Order No: <b><?php echo $i;?> {{$overdue_pickup->status}}</b></h3>
</div>
<div class="box-body">
<div class="col-md-offset-1 col-md-10">
<div style="overflow-x:auto;">
<p><i class="fa fa-dashboard"></i> <b>Pickup Time:</b> {{$overdue_pickup->pickuptime}}</p>
<p><i class="fa fa-dashboard"></i> <b>Delivery Time:</b> {{$overdue_pickup->deliverytime}}</p>
</div>
</div>
</div>
</div>
</div>
<?php $i++; ?>
#endforeach
Considering that your record for $id=2 is one,
Change
$assign_pickup = DB::table('archive_pickup')
->where('ridder_id',$id)
->get();
to first()
$assign_pickup = DB::table('archive_pickup')
->where('ridder_id',$id)
->first();
Then use
if($assign_pickup->pickdate > $curr_date){
return view('Ridder.assign',['assigned_pickup' => $assigned_pickup]);
}else{
return "No Overdue Pickup";
}
If not one then use foreach & no need to change get() statement,
foreach($assign_pickup as $assign_pick){
if($assign_pick->pickdate > $curr_date){
// ... your logic
}else{
// ..your logic
}
}
EDIT:
public function overdue_pickup(){
$id = "2";
$curr_date = date('m/d/yy');
$overdue_pickup = DB::table('archive_pickup')
->where('ridder_id_',$id)
->get();
return view('overdue_pickup',['overdue_pickup' =>
$overdue_pickup]);
}
In your view, you can just foreach loop through it,
#foreach($overdue_pickup as $overdue_pick)
#if($overdue_pick->pickdate > $curr_date)
{{-- show your view --}}
#else
<p class="text-warning">No Overdue Pickup</p>
#endif
#endforeach

Time is not update in Laravel 5

I have a form in which i input two fields Activity_datetime_from and Activity_datetime_to. the problem is that when i update the form if i update these two fields then update function is working fine but when i update other fields not these two than it update these two fields automatically to 0000-00-00 00:00:00 like that. Here is the code of my update function:
public function update(UpdateActivity $request, $id)
{
$activity = Activity::find($id);
if(!$activity) {
return response()->json(['message'=>'Data not found', 404]);
}
$activity->update($request->except(['activity_type', 'activity_picture']));
if($request->hasFile('activity_picture')) {
$activity_picture = $request->file('activity_picture');
$activity_picture_name = time().'.'.$activity_picture->getClientOriginalExtension();
$path = public_path('Storage/ActivityImages');
$activity_picture->move($path, $activity_picture_name);
$activity->activity_picture = 'Storage/ActivityImages/'.$activity_picture_name;
}
if ($request->has('activity_address')) {
$location = $request->input('activity_address');
$client = new \GuzzleHttp\Client();
$geocoder = new Geocoder($client);
$geocoder->setApiKey(config('geocoder.key'));
$address = $geocoder->getCoordinatesForAddress($location);
$latitude = $address['lat'];
$longitude = $address['lng'];
}
$activity->latitude = $latitude;
$activity->longitude = $longitude;
$activity->save();
return redirect()->route('activities.index')
->with('success','Activity updated successfully');
}
Here is my view edit.blade.php
<div class="form-group">
{!!Form::label('activity_datetime_from','Activity From*:',['class'=>'col-md-2'])!!}
<div class="col-md-10">
<p>{{$activity->activity_datetime_from}}</p>
{!!Form::date('activity_datetime_from', null, ['class'=>'form-control','activity_datetime_from'])!!}
<!-- {!!Form::text('dateTime-local', null, ['class'=>'form-control',])!!} -->
</div>
</div>
<div class="form-group">
{!!Form::label('activity_datetime_to','Activity To*:',['class'=>'col-md-2'])!!}
<div class="col-md-10">
<p>{{$activity->activity_datetime_to}}</p>
{!!Form::date('activity_datetime_to', null, ['class'=>'form-control','activity_datetime_to'])!!}
</div>
</div>
<div class="form-group">
{!!Form::label('activity_address','Address*:',['class'=>'col-md-2'])!!}
<div class="col-md-10">
{!!Form::text('activity_address', null, ['class'=>'form-control','required'])!!}
</div>
</div>
I want if i will update other fields than these two field will not change it contains the existing value. Please help Thanks in Advance.
replace
{!!Form::date('activity_datetime_from', null, ['class'=>'form-control','activity_datetime_from'])!!}
by
{!!Form::date('activity_datetime_from', $activity->activity_datetime_from, ['class'=>'form-control','activity_datetime_from'])!!}

Laravel Select the First Row From HasMany Relation

I have 2 table where 1 products have many prodphotos
I can retrieve all prodphotos from products with the same id, but my case is listing all the products but only take 1 photo from prodphotos.
Controller :
public function daftarproduk()
{
$produks = Product::orderBy('created_at', 'desc')->get();
$select = Product::with('prodphotos')->firstorfail();
$photo = $select->prodphotos->pluck('photo');
$kategori = Category::orderBy('created_at', 'asc')->get();
return view('guest.daftarproduk')
->with('produks',$produks)
->with('kategori',$kategori)
->with('select',$select)
->with('photo',$photo);
}
View :
#foreach($produks as $value)
<div class="col-xs-6 col-md-4 col-lg-3 box-product-outer">
<div class="box-product">
<div class="img-wrapper">
<a href="detail/{{$value->id}}/{{str_slug($value->name)}}">
<img alt="Product" src="images/gambarproduk/thumb_{{ i dont know what i must throw here to only get first picture }}">
</a>
</div>
<h6>{{$value->name}}</h6>
</div>
</div>
#endforeach
I dont know what function I must use to get the first photo name from $select or $photo from controller. or my foreach logic is wrong? Please help me.
Add a featured photo relation with hasOne type to your Product model.
Product model
public function featuredPhoto() {
return $this->hasOne(PhotosModel);
}
In your controller
public function daftarproduk()
{
// Get the products with featured image
$produks = Product::with('featuredPhoto')->orderBy('created_at', 'desc')->get();
$kategori = Category::orderBy('created_at', 'asc')->get();
return view('guest.daftarproduk')
->with('produks',$produks)
->with('kategori',$kategori);
}
View
#foreach($produks as $value)
<div class="col-xs-6 col-md-4 col-lg-3 box-product-outer">
<div class="box-product">
<div class="img-wrapper">
<a href="detail/{{$value->id}}/{{str_slug($value->name)}}">
<img alt="Product" src="images/gambarproduk/thumb_{{ $value->featuredPhoto->photo }}">
</a>
</div>
<h6>{{$value->name}}</h6>
</div>
</div>
#endforeach

Call to a member function tasks() on null on laravel 5

Let me explain with a couple of word my problem.
On my controller i have this line:
$tasks = $student->group->tasks()->orderBy('created_at', 'desc')->withPivot('id')->get();
This works for existing users, but when i try to create new ones i receive
Call to a member function tasks() on null
Can i with something like this or what do you suggest ?
if(!is_null($tasks))
$tasks = $student->group->tasks()->orderBy('created_at', 'desc')->withPivot('id')->get();
}
This i my show function on controller
public function show(){
$user = Auth::user();
if(!$user)
return redirect('/')->withErrors(config('constants.NA'));
$countries = Country::all()->lists('name', 'id')->toArray();
$profile = $user->profile;
$student = $profile->student;
// Tasks showed on students profile
if ($student->group) {
$tasks = $student->group->tasks()
->orderBy('created_at', 'desc')
->withPivot('id')
->get();
}
// Classmates
if ($student->group) {
$classmates = $student->group->students()
->where('id', '!=', $student->id)
->get();
}
// Activated books
$books = Auth::user()->books()->orderBy('grade_id', 'asc')->get();
if(!is_null($student->group))
$iTasks = $student->group->tasks->count();
else {
$iTasks = 0;
}
$iTodos = $user->todos->count();
return view('student.profile.show',compact('user','profile', 'countries', 'iTasks', 'iTodos', 'tasks', 'classmates', 'books'));
}
This is my show view, for the tasks
<div class="tab-pane <?php if(isset($tab) && $tab == 'timeline'): ?> active <?php endif; ?>" id="timeline">
#if($tasks->count())
<div class="timeline">
#foreach($tasks as $task)
<!-- TIMELINE ITEM -->
<div class="timeline-item">
<div class="timeline-badge">
<div class="timeline-icon">
<i class="mdi mdi-clipboard-text font-red-intense"></i>
</div>
</div>
<div class="timeline-body">
<div class="timeline-body-arrow"> </div>
<div class="timeline-body-head">
<div class="timeline-body-head-caption">
<span class="timeline-body-title font-blue-madison">
{{ $task->professor->profile->user->name }}
</span>
<span class="timeline-body-time font-grey-cascade">ju ca caktuar një detyrë të re.</span>
</div>
</div>
<div class="timeline-body-content">
<span class="font-grey-cascade">
{{ $task->pivot->comment }}
</span>
</div>
<hr>
Lenda: <span class="timeline-body-time font-grey-cascade sbold">{{ $task->subject->name }}</span>
<div class="pull-right">
Krijuar më: <span class="timeline-body-time font-grey-cascade sbold">{{ $task->created_at->format(Config::get('klasaime.date_format')) }}</span>
</div>
</div>
</div>
<!-- END TIMELINE ITEM -->
#endforeach
</div>
#else
<div class="alert">
Ju nuk keni asnje detyrë të caktuar!
</div>
#endif
</div>
Looks to me like you haven't added the student to a group yet, or that somehow hasn't been persisted. If you have added the student to a group after creating and saving the student, try this:
$student->load('group')
Before running:
$tasks = $student->group->tasks()->orderBy('created_at', 'desc')->withPivot('id')->get();
I'll need to see more of your code to give a more accurate answer. But the error you're getting is related to the ->group, of your student, not your student itself.
Do a simple check to see if the group exists and then carry on with whatever action you need to perform.
// controller
$tasks = null;
if ($student->group) {
$tasks = $student->group->tasks()
->orderBy('created_at', 'desc')
->withPivot('id')
->get();
}
// view
#if($tasks)
{{ $tasks->id }}
#else
No tasks found
#endif
Edit : In your controller add $tasks = null; and $classmates = null; at top. And in your view change #if($tasks->count()) to #if($tasks). I'm not sure where your use the classmates variable, but add a check to see if it's null before you use it.

how search between date using post in codeignitier

Dear Expert need Help first see my view code in codeigniter :
<div class="form-group">
<label for="tglawal" class="col-sm-2 control-label">Periode</label>
<div class="col-sm-3">
<div class="input-group date">
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div>
<input type="date" class="form-control" name="tglawal" id="tglawal">
</div>
</div>
<div class="col-sm-3">
<div class="input-group date">
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div>
<input type="date" class="form-control" name="tglakhir" id="tglawal1">
</div>
</div>
</div>
and this my model code :
private function _get_datatables_query()
{
//add custom filter here
if($this->input->post('tglawal'))
{
$this->db->where('b.tglawal', $this->input->post('tglawal'));
}
if($this->input->post('tglakhir'))
{
$this->db->where('b.tglakhir', $this->input->post('tglakhir'));
}
}
public function get_datatables()
{
$this->_get_datatables_query();
if($_POST['length'] != -1)
$this->db->limit($_POST['length'], $_POST['start']);
$query = $this->db->get();
return $query->result();
}
and my controller if i get the important code is:
public function index()
{
$this->load->helper('url');
$this->load->helper('form');
$this->load->view('infokunjungan_view', $data);
}
else redirect(base_url());
}
public function ajax_list()
{
$list = $this->Infokunjungan->get_datatables();
$data = array();
$no = $_POST['start'];
foreach ($list as $infokunjungan) {
$no++;
$row = array();
$row[] = "<td style='vertical-align:middle'><center>{$no}<center></td>";
$row[] = "<td style='font-size:9px; vertical-align:left;'>{$infokunjungan->tglawal}<center></td>";
$row[] = "<td style='font-size:9px; vertical-align:left;'>{$infokunjungan->tglakhir}<center></td>";
$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => $this->Infokunjungan->count_all(),
"recordsFiltered" => $this->Infokunjungan->count_filtered(),
"data" => $data,
);
//output to json format
echo json_encode($output);
}
the problem is if searching between two date tglawal and tglakhir
im using between 2016-12-04 and 2016-12-04 output display will empty
but if using between 2016-12-04 and 2016-12-06 output success where is my problem or maybe im using where or i have to use like?
You need to use the >= and <= operator.
In your model try the below.
if($this->input->post('tglawal'))
{
$this->db->where('b.tglawal >=', $this->input->post('tglawal')); //assuming this is your begining (from) date
}
if($this->input->post('tglakhir'))
{
$this->db->where('b.tglakhir <=', $this->input->post('tglakhir')); //assuming this is your end(to) date
}
The above will search for the between dates including the dates selected.
Use the operator depending on the beginning and ending variable.

Resources