Codeigniter get where clause from input select - php

I need to get database information from table by using where clause from a select options.
My view:
<div>
<select name="noSeats" class="form-control" placeholder="Seats">
<option value="">Seats</option>
<option value="45">45</option>
<option value="49">49</option>
<option value="51">51</option>
<option value="53">53</option>
<option value="56">56</option>
<option value="57">57</option>
<option value="61">61</option>
</select>
</div>
My Model:
public function getSeats($noSeats) {
$query = $this->db->get('seat');
if ($query->num_rows() > 0) {
return $query->result();
}
}
Also I have another problem with listing checkboxes; I need to list checkboxes in row of limit to 4 since I have many chechboxes.

public function getSeats($noSeats){
$query = $this->db->from('seat')->where('seat_col', $noSeats)->get();
if($query->num_rows() > 0){
return $query->result();
}
}

You can use
$query = $this->db->where('COLUMN_NAME', $noSeats )->get('seat');

Related

How can I only show distinct data from database where user_id is the same as current user in Laravel

I have a user table and position table.
Position Table contains
Schema::create('positions', function (Blueprint $table) {
$table->id();
$table->string('name_position');
$table->boolean('is_head');
$table->string('name_department');
$table->boolean('is_top');
$table->string('top_name_department');
$table->string('position_report');
$table->integer('assigned_emp_id');
$table->timestamps();
Positions Controller contains
public function createorg()
{
$user_id = auth()->user()->id;
$user = User::find($user_id);
return view('positions.createorg')->with('positions',$user->positions);
}
Blade contains
<select id="name_department" name="name_department">
<option value="0">Please select a department</option>
#foreach($positions as $row)
{
<option value="name_department">{{$row->name_department}}</option>
}
#endforeach
</select>
My FE is showing
<select id="name_department" name="name_department">
<option value="0">Please select a department</option>
{
<option value="name_department">Company</option>
}
{
<option value="name_department">Finance</option>
}
{
<option value="name_department">Finance</option>
}
</select>
So my problem here is I do not want to show duplicate departments since the department is only one. But in my position table, I have multiple data entry with the same department. How can I achieve it?
You can create another variable with the department names from the $user->positions and pass it along to the view
public function createorg()
{
$user_id = auth()->user()->id;
$user = User::find($user_id);
$departments = $user->positions->pluck('name_department')->unique();
return view('positions.createorg', [
'positions' => $user->positions,
'departments' => $departments]
);
}
Then use $departments in blade view to populate select options
<select id="name_department" name="name_department">
<option value="0">Please select a department</option>
#foreach($departments as $department)
<option value="name_department">{{$department}}</option>
#endforeach
</select>
Or you can do it in blade view directly without passing $departments from controller
<select id="name_department" name="name_department">
<option value="0">Please select a department</option>
#foreach($positions->pluck('name_department')->unique() as $department)
<option value="name_department">{{$department}}</option>
#endforeach
</select>

Codeigniter get selected values of form_multiselect

i'm working on a codeigniter website and i need to use form_multiselect the select shows fine but when i post it and i try to get the selected value on the controller when i do :
$category = $this->input->get_post('category');
var_dump($category);
i get string(0) "".
here is my codes :
View
<?php echo form_multiselect('category[]', $categories); ?>
Controller
$data['categories'] = $this->blog_category_model->getblogcatDropDown($app_key);
$category = $this->input->get_post('category');
var_dump($category);
die;
Model
public function getblogcatDropDown($app_key)
{
$query = $this->db->query('SELECT * FROM `webapp_blog_categories` WHERE `app_key`=('.$this->db->escape($app_key).') ORDER BY `id` ASC')->result();
$return[''] = 'Choose blog category';
foreach ($query as $row) {
$return[$row->id] = $row->cat_name;
}
return $return;
}
update
when i use html select on the view :
<select multiple name="tst[]">
<option value="" disabled selected>Choose your option</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
it works fine , but the generated form not working
can any one help me ? thanks
Make sure the form method="post" and simply check the whether the value is posted when you submit the form using developer tools.
for getting the value in controller. it's simply.
$category = $this->input->post('category');

Form validation for multiselect dropdown list in codeigniter

i have tried using various methods for setting form validation using codeigniter for a multiselect dropdown list,but is unable to set the rules properly. under here is the code i am using,kindly help. p.s. i have already tried implementing codes suggested on stackoverflow but in vain.
<select class="" name="travel_cat[]" multiple="multiple" id="travel_lst" data-placeholder="Select Travel Categories" style="width: 100%;">
<option value="adventure">Adventure</option>
<option value="arts&culture">Arts & Culture</option>
<option value="events">Events</option>
<option value="backpacking">Backpacking</option>
<option value="beach_holidays">Beach Holidays</option>
<option value="budget_travel">Budget Travel</option>
<option value="city_travels">City Travels</option>
<option value="day_trips">Day Trips</option>
<option value="honeymoons">Honeymoons</option>
<option value="family_trips">Family Trips</option>
<option value="weekend_gateway">Weekend Gateway</option>
<option value="history&architecture">History & Architecture</option>
<option value="luxury">Luxury</option>
<option value="nature">Nature</option>
<option value="road_trips">Road Trips</option>
<option value="wildlife">Wildlife</option>
<option value="religious">Religious</option>
</select>
$choice = $this->input->post("travel_cat");
if(is_null($choice))
{
$choice = array();
}
$travel_cat = implode(',', $choice);
form validation:-
$this->form_validation->set_rules('travel_cat', 'Travel Category', 'callback_check_default');
function check_default($array)
{
foreach($array as $element)
{
if($element == '0')
{
return FALSE;
}
}
return TRUE;
}
i have replaced the callback function with the below mentioned code,and it worked fine.
function check_default()
{
$choice = $this->input->post("travel_cat");
if(is_null($choice))
{
$choice = array();
}
$travel_cat = implode(',', $choice);
if($travel_cat != '')
return true;
else
return false;
}

Insert values into using mysql and bootstrap?

here is the bootstrap that i need to put into the database well im
new to bootstrap and mysql so i have no idea whether im doing it right
or wrong? i need some help badly. btw im making a simple online reservation system
<div class = "panel-group" id = "Accordion">
<div class = "panel panel-info">
<div class = "panel-heading">
<h4 class = "panel-title">
</span> Reserve Now!
</h4>
</div>
<!-- Panel Body -->
<div class ="panel-body">
<div class = "row">
<form class="form-inline" role="form" action ="" method = "post">
<div class="form-group">
<div class="input-group" name = "from">
<div class="input-group-addon">FROM</div>
<select class="form-control input-sm">
<option selected disabled>Select Destination</option>
<option value="BCD">Bacolod</option>
<option value="BSO">Basco(Batanes)</option>
<option value="USU">Busuanga(Coron)</option>
<option value="BXU">Butuan</option>
<option value="CGY">Cagayan de Oro</option>
<option value="CYP">Calbayog</option>
<option value="CRM">Catarman</option>
<option value="BOR">Catician(Boracay)</option>
<option value="CEB">Cebu</option>
<option value="COT">Cotabato</option>
<option value="DVO">Davao</option>
<option value="DPL">Dipolog</option>
<option value="DGT">Dumaguete</option>
<option value="GES">General Santos</option>
<option value="ILO">Iloilo</option>
<option value="JOL">Jolo</option>
<option value="KLO">Kalibo</option>
<option value="LAO">Laoag</option>
<option value="LGP">Legazpi</option>
<option value="MNL">Manila</option>
<option value="MBT">Masbate</option>
<option value="NAG">Naga</option>
<option value="OZC'">Ozamiz</option>
<option value="PPS">Puerto Princesa</option>
<option value="RXS">Roxas</option>
<option value="SUG'">Surigao</option>
<option value="TAC">Tacloban</option>
<option value="TAG">Tagbilaran(Bohol)</option>
<option value="TWT">Tawi Tawi</option>
<option value="TUG">Tuguegarao</option>
<option value="ZAM">Zamboanga</option>
</select>
</div>
</div>
and here is the mysql query that im tweaking right now . im using PDO. what should i do ? i edited this is now my full db.php its from my previous small blog post project.
<?php namespace ars\DB; // Global annoucement of location within the this page
$config = array(
'username' =>'root',
'password' => 'kel',
'database' => 'ars'
); // function can be reusable
//function will be put to class namespace (real world)
function connect($config)
{
try {
$conn = new \PDO('mysql:host=localhost;dbname=' .
$config['database'],
$config['username'],
$config['password']);
$conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
return $conn;
} catch(exception $e ) {
return false;
}
}
//helper function
function query($query, $bindings, $conn)
{
$stmt = $conn->prepare($query);
$stmt->execute($bindings);
return ( $stmt -> rowCount() > 0 ) ? $stmt : false;
}
function get($tableName, $conn)
{ try{
$result = $conn->query("SELECT * FROM $tableName ORDER BY id ");
return( $result -> rowCount() > 0 )
? $result
: false;
}catch(Execption $e){
return false;
}
}
// passing the id
function get_by_id($id, $conn)
{
// querying the id
$query = query(
'SELECT * FROM posts WHERE id = :id LIMIT 1',
array('id' => $id),
$conn
);
if ($query) return $query->fetchAll();
// else
}
there are many errors in this code. i cant help you with your problem but i can just point out some of these errors. first off,
there is no need for order by in insert
your function is get but you are inserting. (can i assume you are supposed to use select? if so, learn more about it. a little google search will save you).
you can help us help you by showing your connect statement. (like
mysqli_connect() function) note:dont use mysql_connect as it is
depreciated.
a note to point out is your
has no name attribute. add a name="some_Select" in your select tag

Show same item only once in drop down list?

I have the following drop down list, that has a common item 'a'. This item is shown two times in output, but it want to have it only once. Can anybody help me?
<select name="">
<option value="a">a</option>
<option value="a">a</option>
<option value="b">b</option>
</select>
My code is
<select name="brand" id="brand" class="txtfld" >
<option value="">Select</option>
<?php
$country_sql="SELECT DISTINCT(brand) FROM customer where status='A' and brand<>''";
$result_country=executequery($country_sql);
while($country_array=ms_stripslashes(mysql_fetch_array($result_country)))
{
$sel_con=($country_array['brand']==$_REQUEST['brand'])? " selected='selected'" : " ";
$brand=$country_array['brand'];
$brand2=explode('|',$brand);
if(count($brand2)<2)
{
?><option value="<?=$brand;?>" ><?=$brand;?></option><?php
}
else
{
for($i=0;$i<count($brand2);$i++)
{
?><option value="<?=$brand2[$i];?>" ><?=$brand2[$i];?></option><?php
}
}
}
?>
</select>
You can use array_unique() which will keep only unique values of your array :
$brand2 = explode('|',$brand);
$brand2 = array_unique($brand2);
EDIT
Try :
$new_array = array();
while($country_array=ms_stripslashes(mysql_fetch_array($result_country)))
{
$brand=$country_array['brand'];
$brand2=explode('|',$brand);
$NewArray = array_unique (array_merge ($NewArray, $brand2));
}
Now you have all unique value in array, loop through to show the list.
NOTE: It might be a overhead if you have big data.

Categories