codeigniter form_dropdown return wrong value - codeigniter

My Model,
function get_partners_name()
{
$q = $this->db->select("id,fname,lname")
->from('partner');
$all_partners = $q->get()->result();
$data = array();
foreach($all_partners as $partner)
{
$data[$partner->id] = $partner->fname.' '.$partner->lname;
}
return $data;
}
my controller var_dump($partners) data is,
array (size=2)
38 => string 'Mahesh' (length=6)
40 => string 'Rahul' (length=4)
My view,
<?php array_unshift($partners, "-Select-");?>
<?php echo form_dropdown('parnter', $partners,'',''); ?>
so in dropdown i want to display,
<option value='0'>-Select-</option>
<option value='38'>Mahesh</option>
<option value='40'>Rahul</option>
But it display,
<option value='0'>-Select-</option>
<option value='1'>Mahesh</option>
<option value='2'>Rahul</option>

from the PHP manual.
'array_unshift() prepends passed elements to the front of the array. Note that the list of elements is prepended as a whole, so that the prepended elements stay in the same order. All numerical array keys will be modified to start counting from zero while literal keys won't be touched.'
so your keys are being renumbered when using unshift.
since the select option should never be selected I would suggest hardcoding it into the view. also don't give it a value if you don't want users to select it.
<select name='foo'>
<option disabled>-Select-</option>
<?php
foreach($partners as $partner){
echo '<option value="';
echo $partner->id;
echo '">';
echo $partner->name;
echo '</option>';
}
?>
</select>

Related

Laravel search logic

For the past two days I was trying to build this relatively simple search system but I just can't figure this out..
Basically, I have a few select tags which contain options to filter the users..
Something like this:
<div class="col-sm-4">
<label for="price">Price:</label><br>
<select class="browse-select" name="price">
<option>Any</option>
<option value="1">Less than 100$</option>
<option value="2">More than 100$</option>
</select>
</div>
<div class="col-sm-4">
<label for="delivery">Delivery:</label><br>
<select class="browse-select" name="delivery">
<option>Any</option>
<option value="1">1 month or less</option>
<option value="7">7 days or less</option>
<option value="3">3 days or less</option>
</select>
</div>
Those are in a form (get request form) and here's my controller.
public function index(Request $request){
$users = User::paginate(10);
$service = Service::all();
//Search Logic
$category = $request->input('category');
$price = $request->input('price');
$delivery = $request->input('delivery');
$searchQuery = Service::with('user');
$searchQuery->where('category','=',$category);
if ($price == 1) { //price input has a value of 1 which means (less than 100)
$searchQuery->where('price','<',100)->where('category','=',$category);
}elseif($price == 2){
$searchQuery->where('price','>',100)->where('category','=',$category);
}
$result = $searchQuery->get();
return view('browse.index', compact('users','service','result'));
}
As you can see I have a relationship between Users and Services. Because I need to access user's services in order to compare the values and query the database.
What I have so far, works but only for one select. If I select a category, that works fine and the user is displayed. But, if I try for example to choose a category and also a price then it returns all the users.
How should I do this properly? I feel like I'm missing something here..
I believe you need to add additional checks and split your query. Something like
$searchQuery = Service::with('user');
if($request->has('category'))
{
$searchQuery->where('category', $request->input('category'));
}
if($request->has('price'))
{
$operator = (1 === $request->input('price')) ? '>' : '<';
$searchQuery->where('price', $operator, 100);
}
$result = $searchQuery->get();

Optimize query for mysql and php

I have table with fields like id,name and values like (1,"sony,nokia,htc"),(2,"sony")(3,"Samsung").I fetch the records but my logic goes longer than my expection.I want to exlode name if it contains "," and fetch unique records.My result will look like
Name array will be { 0 =>"sony",1=>"HTC",2=>"Nokia"}.My dummy logic is here (This is incomplete not working properly.)
<?php
$row = array("sony,htc,nokia","htc","sony");
foreach ($row as $key => $valuek) {
if(strpos($valuek['value'], ",") !== false)
{
$str[] = explode(",",$valuek['value'] )
?>
<option value="<?php echo $valuek['value']?>"><?php echo $valuek['value']?></option>
<?php
}
?>
<option value="<?php echo $valuek['value']?>"><?php echo $valuek['value']?></option>
<?php } $i++;}?>
You don`t need to do explode that
At the start where you get all your data and stored in array you can just used array_unique()
$row = array("sony,htc,nokia","htc","sony"); // all fetch data from db.
$row = implode(',',$row); // first convert the array -> strings
$row = explode(',',$row); // then return back to array and you can now use array_unique
$row = array_unique($row); // this will filter all data with uniqueness and drop some data that have same value
then you can use this to loop the data on.
for($i=0;$i<count($row)){
echo '<option value="'.$row[$i].'">'.$row[$i].'</option>';
}
this will make your code shorter and easy to manage..

Codeigniter: Using set_select() if dropdown data comes from the db

I have a dropdown <select> field which gets its values from the db. I'm currently looking for a way to use set_select() but have been unsuccessful. Here's my existing view:
<select name="userbase">
<?php echo $userbase_list; ?>
</select>
The variable $userbase_list is a string containing all the <option value="">text</option> html to be inserted. Since this <select> is generated from the db, does that mean I can't use the CI set_select() anymore?
$array_of_options= array("key_one"=>"val_one", "key_two"=>"val_two");
foreach($array_of_options as $k => $v)
{
$selected = ($v === $db_result) ? 'selected=selected' : '';
echo '<option '.$selected.' value='.$k.'>' . $v . '</option>';
}

CodeIgniter function for returning special dropdown

I want to create a function in my model that does something a bit different and need a bit of help.
I have a database table with names, description, ids etc etc .... there is only one of each name, but i want to return 3 entries for every 1 name, prefixed with a number on it.
So, for example, if i have 2 entries in my database with the names 'joe' and 'bob' .... i want my dropdown box to look like the following
<select>
<option value="joe1">joe1 - description</value>
<option value="joe2">joe2 - description</value>
<option value="joe3">joe3 - description</value>
<option value="bob1">bob1 - description</value>
<option value="bob2">bob2 - description</value>
<option value="bob3">bob3 - description</value>
and so on for additional entries in the DB.
i understand how to do a normal dropdown box, but not one like this.
Any help would be grand :)
Try something like this:
function get_users()
{
$this->db->select("id,name,desc");
$this->db->from("table_name");
$q = $this->db->get();
$rows = $q->row_array();
}
In controller method:
$data = array();
$data['users'] = $this->model_name->get_users();
$this->load->view('view_name',$data);
In view:
<?php
echo "<SELECT>";
foreach($users as $user)
{
for ($i=1; $i<=3; $i++)
{
$id = $user['id'].$i;
echo '<option value="'.$id.'">'.$id.' - '.$user['desc'].'</option>';
}
}
echo "</SELECT>";
?>

Repopulating Select form fields in CodeIgniter

I want to re-populate (i.e refill) values posted from a select form field after validation. I know codeigniter user guide proposed using set_select() function but their example from the guide assume you've hard-coded (in HTML) the array of values for the 'select' form element. In my case I used the Form Helper and my select field values are pulled from an array() like so:
$allstates = array('blah','blah2','blah3','blah4','blah5');
echo form_label('Select State Affiliate', 'state');
echo form_dropdown('state', $allstates);
Needless to say, $allstates array is dynamic and changes over time. So how do I code such that my select field can be re-populated with the value the user selected?
You can set the value that should be preselected via the third paramater of form_dropdown. The value that the user selected in the previous step is in $this->input->post('state'). So you would use:
echo form_dropdown('state', $allstates, $this->input->post('state'));
From the user guide:
The first parameter will contain the name of the field, the second parameter will contain an associative array of options, and the third parameter will contain the value you wish to be selected
$options = array(
'small' => 'Small Shirt',
'med' => 'Medium Shirt',
'large' => 'Large Shirt',
'xlarge' => 'Extra Large Shirt',
);
$shirts_on_sale = array('small', 'large');
echo form_dropdown('shirts', $options, 'large');
// Would produce:
<select name="shirts">
<option value="small">Small Shirt</option>
<option value="med">Medium Shirt</option>
<option value="large" selected="selected">Large Shirt</option>
<option value="xlarge">Extra Large Shirt</option>
</select>
Sometimes, especially when working with objects, it might be easier to do this without the form helper:
<select name="my_select>
<?php foreach ( $my_object_with_data as $row ): ?>
<option value="" <?php if ( isset( $current_item->title ) AND $row->title == $current_item->title ) { echo 'selected="selected"';} ?> >
<?php echo $row->title; ?>
</option>
<?php endforeach; ?>
</select>
I know it's very ugly, but in a lot of cases this was the easiest way of doing it when working with objects instead of an associative array.

Resources