how to concatenate two columns from mysql in PHP - php

I'm trying to get all the names from MySQL into a dropdown list using PHP.
I connected to MySQL using PDO. currently I can get only the first name, But I want the names to be first name + last name in the drop down list but I couldn't concatenate them.
I tried to concatenate them like this:
<select class="un">
<option class="op" value="" disabled selected style="color:gray">Username</option>
<?php foreach ($result as $output) { ?>
<option class="op"> <?php echo $output["firstname"+"lastname"]; ?></option>
<?php } ?>
</select>
but that didn't work out for me.
$query="select * from user_details";
$exec = $conn->prepare($query);
$exec->execute();
$rc = $exec->rowCount();
$result=$exec->fetchAll();
<select class="un">
<option class="op" value="" disabled selected style="color:gray">Username</option>
<?php foreach ($result as $output) { ?>
<option class="op"> <?php echo $output["firstname"]; ?></option>
<?php } ?>
</select>
it worked only with one column which is the firstname but I want it to be both firstname + lastname

Update this view code:
<select class="un">
<option class="op" value="" disabled selected style="color:gray">Username</option>
<?php foreach ($result as $output) { ?>
<option class="op"> <?php echo $output["firstname"] .' '.$output["lastname"]; ?></option>
<?php } ?>
</select>

With Mysql you can try this concat()
$query="select concat(firstname,' ',lastname) as fullname from user_details";
$exec = $conn->prepare($query);
$exec->execute();
$rc = $exec->rowCount();
$result=$exec->fetchAll();
<select class="un">
<option class="op" value="" disabled selected style="color:gray">Username</option>
<?php foreach ($result as $output) { ?>
<option class="op"> <?php echo $output["fullname"]; ?></option>
<?php } ?>
</select>

Related

space between 2 column names with php from sql

I ve got the idea of basic Concat with sql but i cant get a space between first and surname on my drop down menu. The code all works its just a basic thing i cant find to give me a space between the first name and the last name.
<option value="" >Valuer of this Property</option>
<?php
$res=mysqli_query($conn, "SELECT CONCAT(first,surname) from listalot_users");
while($row=mysqli_fetch_array($res))
{
?>
<option><?php echo $row['CONCAT(first,surname)']; ?></option>
<?php
}
?>
</select>
<option value="" >Valuer of this Property</option>
<?php
$res=mysqli_query($conn, "SELECT CONCAT(first,' ',surname) as full_name from listalot_users");
while($row=mysqli_fetch_array($res))
{
?>
<option><?php echo $row['full_name']; ?></option>
<?php
}
?>
</select>
Update you code like this:
<option value="" >Valuer of this Property</option>
<?php
$res=mysqli_query($conn, "SELECT CONCAT(first, ' ', surname) as fullname from listalot_users");
while($row=mysqli_fetch_array($res))
{
?>
<option><?php echo $row['fullname']; ?></option>
<?php
}
?>
</select>

How will it show the selected value (SELECTED) in CodeIgniter 3.x?

I am trying to populate a drop-down list of the database. In my view file I have the following code
Here is my controller
$query = $this->interprete_model->interpreteID($this->session->userdata('user_id'));
print_r($query);
$data['interprete'] = $query;
Aqui esta mi vista, usa set_select.
<select class="form-control" name="regionI" id="regionI">
<option value="">- Select -</option>
<?php foreach($result as $row):?>
<option value="<?php echo $row->id;?>"
<?php echo set_select('regionI', $row->id, TRUE); ?>><?php echo $row->name;?></option>
<?php endforeach; ?>
</select>
Result:
enter image description here
Many selected, I need one selected to modify (update) the data.
You can try this :
<select class="form-control" name="regionI" id="regionI">
<option value="">- Select -</option>
<?php foreach($users as $row):
$selected = FALSE;
// 1 is the id u want to be selected u can change it according to you
if ($row->id == 1){
$selected = TRUE;
}
?>
<option value="<?php echo $row->id;?>"
<?php echo set_select('regionI', $row->id, $selected); ?>><?php echo $row->name;?></option>
<?php endforeach; ?>
</select>
You can also use form_dropdown as
// FOR ids
$ids = array(1,2,3,4); // array of user ids
echo form_dropdown('regionI',$ids,1,array('class'=>'form-control'));
// FOR name
$names= array('name1','name2','name4','name3'); // array of user names
echo form_dropdown('regionI',$names,'name1',array('class'=>'form-control'));
For More :
https://www.codeigniter.com/user_guide/helpers/form_helper.html
i write this way for edit time selection
<?php foreach ($select_single as $select_single_show):?>
<select class="form-control" name="regionI">
<?php foreach ($users as $row):?>
<option <?php if($row->id==$select_single_show->regionI)echo "selected";?> value="<?php echo $all_branch_show->id?>"><?php echo $row->name?>
</option>
<?php endforeach;?>
</select>
<?php endforeach;?>

How do I Populate Multi Select drop dwon using php

Here I am listing all cars.customers want to compare car so they will select from this drop down. A person can select multiple cars. At the first time he is selecting 'Audi' and Saab' I will store it into data base next if he came I need to populate Saab and audi as select how I can do this using php
<select name="cars" multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
Here is my code
<select id="cars" class="multiselect" multiple="multiple" name="cars[]">
<?PHP
if($carslist->num_rows() >0)
{
foreach($carslist->result_array() as $entry):
?> <option value="<?php echo($entry['ID']); ?>" ><?php echo($entry['car_name']); ?></option>
<?php
endforeach;
}
?>
</select>
Following code I tried $resources contain select cars
<select id="cars" class="multiselect" multiple="multiple" name="cars[]">
<?PHP
if($carslist->num_rows() >0)
{
foreach($carslist->result_array() as $entry):
if($resources->num_rows() >0)
{
foreach($resources->result_array() as $car):
if($entry['ID'] == $employee['car_id'])
{
$select = 'selected="selected"';
}
else
{
$select = '';
}
endforeach;
}
?> <option value="<?php echo($entry['ID']); ?>" <?php echo $select;?> ><?php echo($entry['car_name']); ?></option>
<?php
endforeach;
}
?>
</select>
but it showing error
Here, try something like this, and see if it works:
Here is the controller:
<?php
function something(){
$data = array();
$data['cars'] = $this->some_model->some_function_to_return_cars_array();
$data['selected'] = $some_array_of_selected_cars();
$this->load->view('some_view', $data);
}
?>
And this is the view:
<select id="cars" class="multiselect" multiple="multiple" name="cars[]">
<option value="">Select:</option>
<?php
foreach( $cars as $key => $val ){
?>
<option value="<?php echo $val['some_id'] ?>"
<?php
if( in_array( $val['some_id'], $selected ) ) echo ' selected';
?>
><?php echo $val['some_name'] ?></option>
<?php
}
?>
</select>

php mysql search using boxlist

I am looking for a way to search for data from the database using input type box list, I tried make the code but it doesn't display anything:
html code:
<form action="users.php" method="post" name="searching">
<select name="users">
<option selected="selected" value="">-- select --</option>
<option value="1">user1</option>
<option value="2">user2</option>
<option value="3">user3</option>
</select>
<input type="submit" name="search" value="find">
</form>
php code:
if (isset($_POST['users'])) {
$key = trim ($_POST['users']);
$s = "SELECT * FROM users where user_name LIKE '%$key %'";
$res = mysql_query($s) or die('query did not work');
while($row = mysql_fetch_array( $res ))
{
?>
User ID: <?php echo $row['user_id'] ?>
User Name: <?php echo $row['user_name'] ?>
<?php
}
?>
when I try the code I didn't get any result and when I remove the while loop and put this instead of it :
<?php echo $key; ?>
it gives me the numbers of the selected value, for example if I select user2 the result will be 2. and I want the result to be user id and user name.
you need to fetch all the user name in your drop down select box
<select name="users">
<option selected="selected" value="">-- select --</option>
<?php $s2 = "SELECT * FROM users";
$q2=mysql_query($s2) or die($s2);
while($rw=mysql_fetch_array($q2))
{
echo '<option value="'.$rw['userid'].'">'.$rw['username'].'</option>';
}</select>
?>
<?php if (isset($_POST['search'])) { // submit button name here
$key = $_POST['users'];
$s = "SELECT * FROM users where user_id='".$key."'";
$res = mysql_query($s) or die($s);
while($row = mysql_fetch_array( $res ))
{
?>
User ID: <?php echo $row['user_id'] ?>
User Name: <?php echo $row['user_name'] ?>
<?php
}
?>
edit your html to this,you will get the in $_POST which will be in value='something'
<form action="users.php" method="post" name="searching">
<select name="users">
<option selected="selected" value="">-- select --</option>
<option value="user1">user1</option>
<option value="user2">user2</option>
<option value="user3">user3</option>
</select>
<input type="submit" name="search" value="find">
</form>
Or if value is the id of user then change query to this
$s = "SELECT * FROM users where user_id='".$key."'";

PHP while in edit mode show selected value in to drop down

This question was asked already, but my question is very simple.
In the my account page, I have the employee country in a dropdown.
How to select a value in the combo, when in edit mode?
Let's assume you have the user's country in $user_country and the list of all countries in $all_countries array:
<select id="country">
<?php
foreach ( $all_countries as $country ):
$selected = "";
if ( $country == $user_country )
$selected = "selected";
?>
<option value="<?php echo $country; ?>"
selected="<?php echo $selected; ?>">
<?php echo $country; ?>
</option>
<?php
endforeach; ?>
</select>
should work.
An option tag will be the default for a select list when the selected attribute is set. In the following code option 2 will show up as the current selected option when the page loads:
<select>
<option value="1">1</option>
<option value="2" selected="selected">2</option>
<option value="3">3</option>
</select>
To achieve this in your PHP code conditionally display the selected attribute on your options against what the current value is:
<option value="1"<?php if($user['country'] == '1') { ?> selected="selected"<?php } ?>>1</option>
<option value="2"<?php if($user['country'] == '2') { ?> selected="selected"<?php } ?>>2</option>
<option value="3"<?php if($user['country'] == '3') { ?> selected="selected"<?php } ?>>3</option>
function p_edit_combo($cCurstatus,$h_code_default,$h_name=NULL){
<select name="<?php echo $cCurstatus;?>" id="<?php echo $cCurstatus;?>" class="main_form_select">
<option value="">Select</option>
<?php
$sql_h = "SELECT h_code,h_name FROM med_hl WHERE status = 1";
$sql_h_result = mysql_query($sql_h);
while($row=mysql_fetch_array($sql_h_result)){
$h_code = $row['h_code'];
$h_name = $row['h_name'];
?>
<option <?php if($h_code_default==$h_code){ ?> selected="selected" <?php }?> value='<?php echo $h_code; ?>' >
<?php echo $h_code."|".$h_name; ?>
</option>
<?php } ?>
</select>
<?php
}
**i have two table
" users" colmns(fname,lname,...as on ohther_infomation,hobbies datatype(int))
"info" columns (id (primary_key),hobbies(varchar 200)); in which i stored for hobbies name
In my case i am storing values in from (1,2,3,4) in hobbies (int) filled of users table which i matached them through join after time of fetch them,
in my info table i stored hobbies by their name (reading, writing,playing,gyming)
$row has our users selected hobbies (int)
$rows has list of our hobbies(varchar)
edit.php i need Dropdown value selected :==== And i am Doing Like this :--- (100% Working)**
<div class="form-control">
<label for="hobbies">Hobbies</label>
<select name="hobbies">
<?php
$query = "SELECT * FROM info";
$results = mysqli_query($connect, $query);
while ($rows = mysqli_fetch_array($results)) {
?>
<option <?php if ($rows['id'] == $row['hobbies']) { ?> selected="selected" <?php } ?> value='<?php echo $rows['id']; ?>'>
<?php echo $rows['hobbies']; ?>
</option>
<?php
}
?>
</select>
<span class="text-danger"><?php if (isset($err_hobbies)) echo $err_hobbies; ?></span>
</div>

Categories