space between 2 column names with php from sql - php

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>

Related

how to get the data on script and load it in query

Why can't I get the value of the element? sorry I'm new at coding and trying to learn
<script>
function getdll() {
document.getElementById('lblmess').innerHTML =
(formid.listid[formid.listid.selectedIndex].value)
$element = document.getElementById("lblmess");
console.log($element.innerHTML)
}
</script>
<form name="formid">
<select class="custom-select" name="listid" onchange="getdll()">
<option value="0"></option>
<?php
$hall_qry = $conn->query("SELECT * FROM `office_hall` WHERE o_id !=0 order by `office_name` asc");
while ($row = $hall_qry->fetch_assoc()) : ?>
<option value="<?php echo $row['o_id'] ?>"><?php echo $row['office_name'] ?></option>
<?php
endwhile;
?>
</select>
<br><br>
<select class="custom-select">
<?php
$hall_qry = $conn->query("SELECT * FROM `assembly_hall` WHERE o_id = '$element' order by `room_name` asc");
while ($row = $hall_qry->fetch_assoc()) : ?>
<option value="<?php echo $row['id'] ?>"><?php echo $row['room_name'] ?></option>
<?php
endwhile;
?>
</select>
<label id="lblmess"></label>
</form>
this is where I use this code sorry if my coding id is like that .............................................................
Try:
onchange="getdll(this.value)"
THEN in your script, put a parameter
function getdll(val){
alert(val);
}
Should be able to get the value you selected

how to concatenate two columns from mysql in 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>

selected value get from db into Data list option using php mysql

I need to get selected value from db into datalist box.Tell me how to do it. Here is the code.
<input list="Rank_Name" class="form-control" required>
<datalist id="Rank_Name">
<?php
$sel_cus = "select Rank_Name from ranks where Rank_Status=1";
$res_cus = mysqli_query($connection, $sel_cus);
while ($row = mysqli_fetch_array($res_cus)) {
?>
<option value="<?php echo $row['Rank_Name'];?>"></option>
<?php
}
?>
</datalist>
If i understood right , you need to select value in dropdownlist with other value also. You can achieve this by doing this
<?php
$select1="select Rank_Name from ranks where Rank_Status=1";
$q=mysqli_query($select1) or die($select1);
$row=mysqli_fetch_array($q); //here you are getting name of person whose rank is 1
?>
<datalist id="Rank_Name">
<?php
$s="select * from ranks ";
$q=mysqli_query($s) or die($s);
while($r=mysqli_fetch_array($q))
{ ?>
<option value="<?php echo $r['Rank_Name']; ?>"<?php if($row['Rank_Name']==$r['Rank_Name']) echo 'selected="selected"'; ?>>
<?php echo $r['Rank_Name']; ?>
</option>
<?php } ?>
</datalist>
In above code, this line <?php if($row['Rank_Name']==$r['Rank_Name']) echo 'selected="selected"'; ?> check if value are same from first query ,and if same then that option will be get selected automatically
<input list="Rank_Name" class="form-control" required>
<datalist id="Rank_Name">
<?php
$sel_cus = "select Rank_Name from ranks where Rank_Status=1";
$res_cus = mysqli_query($connection, $sel_cus);
while ($row = mysqli_fetch_array($res_cus)) {
echo "<option value=".$row['Rank_Name']."></option>";
}
?>
</datalist>
try this code.
im using echo <option> with while loop

PHP dropdown populated from database

I need to pull down a column of color codes for a color picker site I'm making but it won't work:
<select name="select" id="dropdown">
<option id="0">-- Select Color --</option>
<?php
$getColors = mysql_query("SELECT * FROM color_codes");
while($list = mysql_fetch_array($getColors)){
?>
<option id="<?php echo $list ['colorID']; ?>">
<?php echo $list['color_code'] ?></option>
<?php } ?>
</select>
Any ideas?
just did a couple of changes to your code and it worked for me, Hope it works for you.
<?php
$mysqli= new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME) or die(mysqli_connect_error());
$sql="select * from test";
$result=$mysqli->query($sql);
?>
<select name="select" id="dropdown">
<option id="0">-- Select Color --</option>
<?php
while($list = $result->fetch_array(MYSQLI_ASSOC)){
?>
<option id="<?php echo $list['id']; ?>">
<?php echo $list['name'] ?></option>
<?php } ?>
</select>

Populate html <select> with array data from mysql in PHP

I can see the query returning results, but I can't seem to be able to put them into a html dropdown box. Also, the dropdown box has just as many entries as the query returns, but THEY ARE ALL WHITE SPACES. HOWEVER, the page source shows correct option values such as
<option value="3 John"></option>
<option value="Jude"></option>
<option value="Revelation"></option>
Can somebody help me out? Why dont they actually show in the dropdown box?
<html>
<?php
//Connect to the database
$mysqli = new mysqli("localhost", "root", "", "bible");
//Return an error if we have a connection issue
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
//Query the database for the results we want
$query = $mysqli->query("select distinct bname as Name from kjv limit 1");
//Create an array of objects for each returned row
while($array[] = $query->fetch_object());
array_pop($array);
//Print out the array results
print_r($array);
?>
<h3>Dropdown Demo Starts Here</h3>
<select name="the_name">
<?php foreach($array as $option) : ?>
<option value="<?php echo $option->Name; ?>"></option>
</select>
<?php endforeach; ?>
Try This
<select name="the_name">
<?php foreach($array as $option) : ?>
<option value="<?php echo $option['Name']; ?>"><?php echo $option['Name']; ?></option>
<?php endforeach; ?>
</select>
After the query is executed use the while loop to add the options to select
$query = $mysqli->query("select distinct bname as Name from kjv limit 1"); ?>
<select>
<?php while($option = $query->fetch_object()){ ?>
<option><?php echo $option->Name; ?></option>
<?php } ?>
</select>
Not sure what the array_pop is doing in the code
AS TIM WAX SAID THIS IS THE SOLUTION
$query = $mysqli->query("select distinct bname as Name from kjv limit 1"); ?>
<select>
<?php while($option = $query->fetch_object()){ ?>
<option><?php echo $option->Name; ?></option>
<?php } ?>
</select>
<select name="the_name">
<?php foreach($array as $option) : ?>
<option value="<?php echo $option->Name; ?>"></option>
<?php endforeach; ?>
</select>
You ended your loop in a way that it also create <select> tag again and again. Change it and try again. I don't know much about .php but it could be a problem in showing your dropdown box.
here is mine .. im a beginner but it works for me,
$query = $mysqli->query("SELECT * FROM `student_type_db`"); //table of student type
echo "<select>";
while($row = $query->fetch_array()){
echo "<option>";
echo $row['student_type'] . " - " . $row['student_description'];
echo "</option>";
}
echo "</select>";
// student type = 1 | student description = regular
// output : 1 - regular

Categories