My textboxes get autocomplete populated from mysql tables.
I want to display the output list from the textbox into a selectable option instead of an list item.
echo '<li onClick="fill(\''.$result->naam_klant.'\');">'.$result->naam_klant.'</li>';
My code so far:
'<select onClick="fill(\''.$result->naam_klant.'\');"><option value=$result->naam_klant></option></select>';
Can you guys help me with this ?
UPDATE
if(isset($_POST['queryString'])) {
$queryString = $db->real_escape_string($_POST['queryString']);
// Is the string length greater than 0?
if(strlen($queryString) >0) {
$query = $db->query("SELECT naam_klant FROM overboekingen WHERE naam_klant LIKE '$queryString%' LIMIT 10");
if($query) {
while ($result = $query ->fetch_object()) {
echo '<li onClick="fill(\''.$result->naam_klant.'\');">'.$result->naam_klant.'</li>';
'<select onClick="fill(\''.$result->naam_klant.'\');"><option value=$result->naam_klant></option></select>';
}
} else {
echo 'ERROR: There was a problem with the query.';
}
} else {
} // There is a queryString.
} else {
echo 'There should be no direct access to this naam_klant script!';
}
}
?>
You don't give enough information, how you is your results structured?
<select onchange="fill(this.value);" onfocus="this.selectedIndex = -1;">
<?
for ($i=0; $i < count(results); $i++) {
$result = results[i];
echo "<option value='{$result->naam_klant}'>option {$result->naam_klant}</option>\r\n";
}
?>
</select>
Update
Since you want a selectable option instead of an list item
if ($query) {
echo '<select onchange="fill(this.value);" onfocus="this.selectedIndex = -1;">\r\n';
while ($result = $query->fetch_object()) {
echo '<option value={$result->naam_klant}>{$result->naam_klant}</option>\r\n';
}
echo '</select>\r\n';
}
Related
0)
{
while($row = mysqli_fetch_array($result))
{
echo $company_data[] = $row;
}
}
else
{
echo 'Data not Found';
}
}
?>
Refer Insert Query.
Your code should have condition-
if ($rest)
I have to relations: one with the times and one where the selected times go in after submitting a form. I'm trying to create a dropdown menu, which should be sorted. If a time slot is already occupied is should still show up in the drop down, but as disabled. E.G. 9 , 10: occupied, 11... and so on.
At the moment the occupied slots are at the bottom of the menu. How can I achieve, that they appear where they should be.
Here is my code so far:
$query = "SELECT stunde FROM zeiten WHERE buchbar = 2 and
NOT EXISTS (SELECT *
FROM raumbuchung
WHERE zeiten.stunde =
raumbuchung.zeitanfang and belegt = 'belegt');
SELECT zeitanfang, belegt from
raumbuchung where belegt = 'belegt'";
echo "Beginn der Veranstaltung: ";
echo "<select name='time' id='t1'>";
if (mysqli_multi_query($conn, $query)) {
do {
if ($result = mysqli_store_result($conn)) {
while ($row = mysqli_fetch_assoc($result)) {
if ($row[belegt]) {
echo "<option value=$row[zeitanfang] disabled>$row[zeitanfang]: $row[belegt]</option>";
}
else {
echo "<option value=$row[stunde]>$row[stunde]</option>";
}
}
}
}
while(mysqli_next_result($conn));
}
Maybe someone can help me out?
First store them in separate arrays then populate as your wish.
$query = "SELECT stunde FROM zeiten WHERE buchbar = 2 and
NOT EXISTS (SELECT *
FROM raumbuchung
WHERE zeiten.stunde =
raumbuchung.zeitanfang and belegt = 'belegt');
SELECT zeitanfang, belegt from
raumbuchung where belegt = 'belegt'";
$occupied_arr = array();
$available_arr = array();
if (mysqli_multi_query($conn, $query)) {
do {
if ($result = mysqli_store_result($conn)) {
while ($row = mysqli_fetch_assoc($result)) {
if ($row['belegt']) {
$occupied_arr[] = $row;
}
else {
$available_arr[] = $row;
}
}
}
}
while(mysqli_next_result($conn));
}
echo "Beginn der Veranstaltung: ";
echo "<select name='time' id='t1'>";
foreach ($available_arr as $key => $value) {
echo "<option value=".$value['stunde'].">".$value['stunde']."</option>";
}
foreach ($occupied_arr as $key => $value) {
echo "<option value=".$value['zeitanfang']."disabled>".$value['zeitanfang'].": ".$value['belegt']."</option>";
}
echo "</select>";
Newbie here... I tried to adapt my code from here
The list is populated properly but I can't get it to pre-select. What am I doing wrong? Thanks in advance!
$q = "SELECT cat_id FROM category_user WHERE cat_id=$d";
while ($row = mysqli_fetch_array($q)) {
$cat = (int)$row['cat_id'];
}
$q = "SELECT cat_id, cat FROM category";
$r = mysqli_query ($dbc, $q);
if (mysqli_num_rows($r)> 0) {
while ($row = mysqli_fetch_array ($r, MYSQLI_NUM)) {
echo "<option value=\"$row[0]\"";
$selected = '';
$cid=(int)$row[0];
if ($cid=$cat) {
$selected='selected="selected"';
echo $selected;
echo ">$row[1]</option>\n";
}else{
//Check for stickyness
if (isset($_POST['category'])&&($_POST['category']== $row[0]))
echo 'selected="selected"';
echo ">$row[1]</option>\n";
}
}
}
category
---------------
|cat_id | cat |
---------------
category_user
-------------------------
|cu_id | user_id | cat_id|
-------------------------
Why overcomplicate the code. You can try something like this
<?php
$result = mysqli_query($dbc, "SELECT cat_id, cat FROM category");
if ($result) {
echo "<select name='whatever_you_want'>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='{$row[0]}'";
if (intval($row[0]) == intval($cat_id)) {
echo " selected";
}
echo ">{$row[1]}</option>";
}
echo "</select>";
}
?>
if($cid=$cat){
should be
if($cid==$cat){
and
$cid=(int)$row[0];
(int) is unnecessary because string to int are compared automatically.
Figured it out. the foreach loop preselects everything for the dropdown menu. The problems were mostly getting the array from the mysql table to iterate as selected. The last piece of the puzzle were the brackets [] after $cats.
Thanks for your help, and sorry I wasn't very clear on what I was trying to accomplish. My bad.
<p><select class=\"box\" name=\"wkType[]\" multiple=\"multiple\">";
$q = "SELECT cat_id FROM category_user WHERE user_id=$d";
$r = mysqli_query ($dbc, $q);
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$cats[]=$row['cat_id'];
}
$q = "SELECT cat_id, cat FROM category";
$r = mysqli_query ($dbc, $q);
if (mysqli_num_rows($r)> 0) {
while ($row = mysqli_fetch_array ($r, MYSQLI_NUM)) {
echo "<option value=\"$row[0]\"";
foreach ($cats as $key =>$cat) {
if ($row[0]==$cat){
echo 'selected="selected"';
//Check for stickyness
if (isset($_POST['category'])&&($_POST['category']== $row[0]))
echo 'selected="selected"';
}
}
echo ">$row[1]</option>\n";
}
}else{
echo '<p>Please select a category.</p>';
}
echo "</select></p>";
Hi I'm attempting to display data retrieved from a mysql table horizontally in an html table using php. The code below works well except for the fact that it leaves out the first record (starts at the second record) in my database. I'm sure it has something to do with the counter but I can't seem to figure out how to get it to stop doing this. If anyone can point out my error I'd really appreciate it!
$items = 5;
$query = "SELECT * FROM members ";
$result = mysql_query($query)
or die(mysql_error());
$row = mysql_fetch_array($result);
if (mysql_num_rows($result) > 0) {
echo '<table border="1">';
$i = 0;
while($row = mysql_fetch_array($result)){
$first_name = $row['first_name'];
if ($i==0) {
echo "<tr>\n";
}
echo "\t<td align=\center\">$first_name</td>\n";
$i++;
if ($i == $items) {
echo "</tr>\n";
$i = 0;
}
}//end while loop
if ($i > 0) {
for (;$i < $items; $i++) {
echo "<td> </td>\n";
}
echo '</tr>';
}//end ($i>0) if
echo '</table>';
}else {
echo 'no records found';
}
try and remove the 1st
$row = mysql_fetch_array($result);
you are calling it twice, that's why it skips 1 row in your while loop
try this simpler.
$items = 5;
$query = "SELECT * FROM members ";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
echo '<table border="1">';
while($row = mysql_fetch_array($result)){
$first_name = $row['first_name'];
echo "<tr>";
for ($i=0 ; $i <= $items ;$i++) {
echo "<td align='center'>".$first_name."</td>";
}
}//end while loop
echo "</tr>";
echo '</table>';
}else{ echo 'no records found'; }
I have run into this issue before. Try the do while loop instead. Example
do {
// code
} while($row = mysql_fetch_array($result)); //end while loop
$row = mysql_fetch_array($result);
1) remove this line of code from ur scripts
2) only use while loop code instead.
I have 3 drop downs I want to display whatever the user will select after he/she has selected a function or a scrpt will do but it must be within the script
<?php
$resource_names = mysql_query("SELECT DISTINCT NAME FROM selections ORDER BY id ASC");
$names = array();
while($row = mysql_fetch_row($resource_names)){
$names[] = $row[0]
}
$resource_surnames = mysql_query("SELECT DISTINCT SURNAME FROM selections ORDER BY id ASC");
$surnames = array();
while($row = mysql_fetch_row($resource_surnames)){
$surnames[] = $row[0];
}
$resource_emails = mysql_query("SELECT DISTINCT EMAIL FROM selections ORDER BY id ASC");
$emails = array();
while($row = mysql_fetch_row($resource_emails)){
$emails[] = $row[0];
}
if(count($emails) <= 0 || count($surnames) <= 0 || count($emails) <= 0){
echo 'No results have been found.';
} else {
// Display form
echo '<form name="form" method="post" action="test.php">';
//Names dropdown:
echo '<select name="id" id="names">';
foreach($names as $name) echo "<option id='$name'>$name</option>";
echo '</select>';
//Surnames dropdown
echo '<select name="id" id="surnames">';
foreach($surnames as $surname) echo "<option id='$surname'>$surname</option>";
echo '</select>';
//Emails dropdown
echo '<select name="id" id="emails">';
foreach($emails as $email) echo "<option id='$email'>$email</option>";
echo '</select>';
echo "<button id='write_in_div'>Click me!</button>";
echo '</form>';
}
?>
Something that will call the write_in_div When Click me! button is press or any other method that can be used to display 3 selection user selected
The Output should be something like You select 1) Name 2)Surname and Email
You have an error in your html selects each select has the same name "id" they each need to be unique so you can detect then.
You need to detect if the user has submitted the form
if(isset($_POST["select_name"])) {
echo $_POST["select_name"];
}
There is a big mistake on you form.
In a form, each select and input MUST have a unique name. You need this name to retrieve the submitted value back in your php script.
I suppose you have this:
<?php
$resource_names = mysql_query("SELECT DISTINCT NAME FROM selections ORDER BY id ASC");
$names = array();
while($row = mysql_fetch_row($resource_names)){
$names[] = $row[0]
}
$resource_surnames = mysql_query("SELECT DISTINCT SURNAME FROM selections ORDER BY id ASC");
$surnames = array();
while($row = mysql_fetch_row($resource_surnames)){
$surnames[] = $row[0];
}
$resource_emails = mysql_query("SELECT DISTINCT EMAIL FROM selections ORDER BY id ASC");
$emails = array();
while($row = mysql_fetch_row($resource_emails)){
$emails[] = $row[0];
}
if(count($emails) <= 0 || count($surnames) <= 0 || count($emails) <= 0){
echo 'No results have been found.';
} else {
// Display form
echo '<form method="post" action="test.php">';
//Names dropdown:
echo '<select name="names">';
foreach($names as $name) echo "<option id='$name'>$name</option>";
echo '</select>';
//Surnames dropdown
echo '<select name="surnames">';
foreach($surnames as $surname) echo "<option id='$surname'>$surname</option>";
echo '</select>';
//Emails dropdown
echo '<select name="emails">';
foreach($emails as $email) echo "<option id='$email'>$email</option>";
echo '</select>';
echo '<button id="write_in_div">Click me!</button>';
echo '</form>';
}
When the form is submitted, test.php will have the posted data: $_REQUEST['names'], $_REQUEST['surnames'] and $_REQUEST['emails'].
You just have to check the content of thoses vars and print them if not null.
Note1: ?> is useless at the end of a file.
Note2: be carefull about ' and " when writing an html file. The value of an html attribute is written with ", not '.