I'm displayed a dropdown list of initials that come from a table. I insert "--" at the start before getting the initials from the table.
It goes:
AB
CD
EF
I want it to select the default. $init is the defaulkt
The code below works for '--' but not for the initials.
On the While, if I remove the if statement and just leave the 'else'
echo "<option value='" . $row['id'] . "'>" . $row['init'] . " ";
it is OK.
The first half of the if statement should be the same but with 'selected added.
I don't understand why it is not working.
All I get is "--", not the initials, or any other following input.
Thanks for your help.
<?php
echo $init;
// Drop down list initials
$conn=createconnection();
$sql = "SELECT * FROM employees WHERE lef='0' AND man='1' ORDER BY init";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "Updated by and date<br>";
echo "<select name='select_initial' id='select_initial'>";
if ($init==="--"){
echo "<option value='" . '0' . "'selected>" .'--' . " </option>";
} else {
echo "<option value='" . '0' . "'>" . '--' . " </option>";
}
// output data of each row
while($row = $result->fetch_assoc()) {
if ($init===$row('init')) {
echo "<option value='" . $row['id'] . "'selected>" . $row['init'] . " </option>";
} else {
echo "<option value='" . $row['id'] . "'>" . $row['init'] . " </option>";
}
}
echo "</select>";
//echo "<br><br>";
}
$conn->close();
?>
I found if I used square brackets round the $row function it worked:
if ($init===$row['init']) {
Related
My dropdown list displays user names that I get from the database. I want to implement a displayInfo function so that when someone selects a user, it will automatically display his/her info below.
How can I display a user's info when someone selects their name?
This is my dropdown code:
<?php
//connect
$conn = mysqli_connect("localhost","user","123abc");
mysqli_select_db($conn, "users");
//query
$sql= mysqli_query($conn, "SELECT person_id,first_name FROM users");
echo "<select name='dropdown' onchange='displayInfo' id='dropdown'>";
while ($row = mysqli_fetch_array($sql))
{
//display friends' first names on dropdown
if($row['person_id'] == $row['first_name']) {
echo "<option value='" . $row['person_id'] . "' selected>" . $row['list_name'] . "</option>";
} else {
echo "<option value='" . $row['person_id'] . "'>" . $row['first_name'] . "</option>";
}
}
echo "</select>";
Best way to call a Javascript function on option select like this
<?php
//connect
$conn = mysqli_connect("localhost","user","123abc");
mysqli_select_db($conn, "users");
//query
$sql= mysqli_query($conn, "SELECT person_id,first_name FROM users");
echo "<select name='dropdown' onchange='displayInfo' id='dropdown'>";
while ($row = mysqli_fetch_array($sql))
{
//display friends' first names on dropdown
if($row['person_id'] == $row['first_name']) {
**echo "<option onchange='myFunction(this);' value='" . $row['person_id'] . "' selected>" . $row['list_name'] . "</option>";**
} else {
echo "<option onchange='myFunction(this);' value='" . $row['person_id'] . "'>" . $row['first_name'] . "</option>";
}
}
echo "</select>";
<script>
function myFunction(control){
$(control).val() //to access value or any other functionality
}
</script>
I am new to PHP and have just started the professional development in this language. I have created a dynamic dropdown list in php as under:
$sql="select DISTINCT State from branchinfo";
$result = $conn->query($sql);
if($result->num_rows > 0)
{
echo "< SELECT NAME='states'>";
while($row=$result->fetch_assoc())
{
echo "< OPTION NAME = '" . $row['State'] . "'" . " VALUE = '" . $row['State'] . "'>" . $row["State"];
}<br>
echo "< /SELECT>";
}
echo "< INPUT TYPE='submit' name='submit' value='submit'>";
Problem is when I select a state and click on submit, the list reloads and my selection is lost, the first option gets selected by default. I have tried to embed the script within OPTION but it didn't worked, I tried it as under:
echo "< OPTION NAME='" . $row['State'] . "'" . " VALUE='" . $row['State'] . "'" . if(isset($_POST["submit"])){ if($_POST["states"] == $row['State']) echo "selected";} . " >" . $row["State"];<br>
I am not using any javasrcipt / jquery till now on this page and not planning to use it either. plz provide a solution within this code. Please help.
Some additional information
The mthod i tried as mentioned above, works fine on hardcoded static drop downlist items written in html form. It stops working for dynamically generated list.
You have to add dropdown inside form tag, then it will work.
$sql = "select DISTINCT State from branchinfo";
$result = $conn->query($sql);
echo "<form method='POST'>";
if ($result->num_rows > 0) {
echo "<SELECT NAME='states'>";
while ($row = $result->fetch_assoc()) {
$sel = ( $_POST['states'] == $row['State'] ) ? "selected" : "";
echo "< OPTION NAME = '" . $row['State'] . "'" . " VALUE = '" . $row['State'] . "' " . $sel . ">" . $row["State"] . "</OPTION>";
}
echo "</SELECT>";
}
echo "<INPUT TYPE='submit' name='submit' value='submit'>";
echo "</form>";
Use session to remember your choice. And when the page is reloaded just retrieve your previous selected value.
like
while($row=$result->fetch_assoc())
{
if($sessionvalue==$row['State']){
echo "< OPTION SELECTED NAME = '" . $row['State'] . "'" . " VALUE = '" . $row['State'] . "'>" . $row["State"];
}else{
echo "< OPTION NAME = '" . $row['State'] . "'" . " VALUE = '" . $row['State'] . "'>" . $row["State"];
}
}
I'm trying to Update a SQL Field so what i got is a Select and
$row = $stmt->fetch(PDO::FETCH_ASSOC);
....
$country_ausgabe = $row['country'];
So country_ausgabe as for example a value = de for german.
Also i have a array with all country codes in it.
So with this code i get all in a Select. $countries_de = the array with all country codes.
<select>
foreach ($countries_de as $key=>$country) {
echo "<option value='" . $key . "'>" . $country . "</option>";}
</select>
So but how can i use my $country_ausgabe to set this value in the select to selected?
use this code
<select>
foreach ($countries_de as $key=>$country) {
$selected = ($country_ausgabe == $key)?"SELECTED":"";
echo "<option value='" . $key . "' ".$selected." >" . $country . "</option>";}
</select>
try this
echo "<select>";
foreach ($countries_de as $key=>$country)
{
$selected="";
if($key==$country_ausgabe)
{
$selected = "selected";
}
echo "<option value='" . $key . "' " . $selected . " >" . $country . "</option>";
}
echo "</select>";
I run a query to create a drop down.
$sql_course = "SELECT * FROM hc_course";
$result_course = mysql_query($sql_course);
echo "<select name='course_num'>";
while ($row = mysql_fetch_array($result_course)) {
echo "<option value='" . $row['course_num'] . "'>" . $row['course_name'] . "</option>";
}
echo "</select>";
This produces the list with all the names correctly.
However, I would like to inject an "any" entry that would then turn the next query based off of this from a 'course_num' into a *.
You mean like this?
echo "<select name='course_num'>";
echo "<option value='*'>Any</option>";
while ($row = mysql_fetch_array($result_course)) {
....
echo "<select name='course_num'>";
echo "<option value='*'>Any</option>";
while ($row = mysql_fetch_array($result_course)) {
echo "<option value='" . $row['course_num'] . "'>" . $row['course_name'] . "</option>";
}
echo "</select>";
You should never trust user input so you would need to add some form of PHP sanitisation to process the request.
Initial code:
$sql_course = "SELECT * FROM hc_course";
$result_course = mysql_query($sql_course);
echo "<select name='course_num'>";
echo "<option value="">Any</option>"; // New code for all courses
while ($row = mysql_fetch_array($result_course)) {
echo "<option value='" . htmlspecialchars($row['course_num']) . "'>" . htmlspecialchars($row['course_name']) . " </option>";
}
echo "</select>";
And the code on the page handling this request should work something like this:
if (empty($_POST["course_num"]))
{
// Run SQL to select all courses
}
else
{
// Sanitise the $_POST["course_num"]
// Run SQL to select a specific course item
}
I want to print mysql_query result in a table. I know how to do it but I am just confused. I tried this.
<?php
mysql_connect("localhost","root","") or die("Could not Connect.");
mysql_select_db("check") or die("Could not Select DB");
$table = "cc";
$i = 1;
$query = "select * from $table";
$sql = mysql_query($query);
if($sql){
echo "<table border='5'><tr>";
while($i<=2 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " . $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
while($i<=4 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " . $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
while($i<=6 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " . $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
while($i<=8 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " . $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
echo "</tr></table>";
}
?>
As you can see it is written again and again with a slight change of 2,4,6,8 in the while loop. It works but the problem is I cant rewrite it again and again as when the website will go live it will have more than 1000 entries. Could You guys help me out by suggesting another way to do this?
""** I need it to be like these dots (dots represent records in the database) **"""
. . . .
. . . .
. . . .
THANKS in Advance.
Ramzy
<?php
mysql_connect("localhost","root","") or die("Could not Connect.");
mysql_select_db("check") or die("Could not Select DB");
$table = "cc";
$query = "select * from $table";
$sql = mysql_query($query);
if($sql){
echo "<table border='5'><tr>";
while($row = mysql_fetch_array($sql)){
echo "<td>" . $row['id'] . " : " . $row['name'] . "</td>";
}
echo "</tr></table>";
}
?>
while($row = mysql_fetch_array($sql)) {
echo "<td>" . $row['id'] . " : " . $row['name'] . "</td>";
}
I don't really see what's the problem here.
By the way you should never call you're array like this $row[id] but you should quote the key instead $row['id']; Because if a constant id exists it will screw up your code and also for performance reason.
Just use
$limit = 1000;//place any value you need here to limit the number of rows displayed
while ($i<=$limit && $row = mysql_fetch_array($sql)){
echo "<td>" . $row['id'] . " : " . $row['name'] . "</td>";
++$i;
}
Also, that limit is unnecessary if all you want is to flush every record to the output. You could just do
while ($row = mysql_fetch_array($sql)){
echo "<td>" . $row['id'] . " : " . $row['name'] . "</td>";
}
And it will stop as soon as there are no more records.
To print all database rows into an HTML-table, use:
echo '<table>';
$i = 0; // $i is just for numbering the output, not really useful
while($row = mysql_fetch_array($sql))
{
echo '<tr><td>' . $i . ' - ' . $row['id'] . ' : ' . $row['name'] . '</td></tr>';
$i++;
}
echo '</tr></table>';
here is a general function I use:
function query_result_to_html_table($res, $table_id = NULL, $table_class = NULL, $display_null = true)
{
$table = array();
while ($tmp = mysql_fetch_assoc($res))
array_push($table, $tmp);
if (!count($table))
return false;
echo "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" "
. ($table_id ? "id=\"$table_id\" " : "")
. ($table_class ? "class=\"$table_class\" " : "") . ">";
echo "<tr>";
foreach (array_keys($table[0]) as $field_name) {
echo "<th>$field_name";
}
foreach ($table as $row) {
echo "<tr>";
foreach ($row as $col => $value) {
echo "<td>";
if ($value === NULL)
echo "NULL";
else
echo $value;
}
echo "\n";
}
echo "</table>\n";
return true;
}
I Got The Answer.. I wanted it to be like this. I made this and It Actually Works.
<?php
$i = 1;
mysql_connect("localhost" , "root" , "") or die('Could not Connect.');
mysql_select_db("db") or die('Could not select DB.');
$query = "select * from `check`";
$sql = mysql_query($query) or die(mysql_error());
echo "<table border='5' width='50%'><tr><th>Name</th><th>Gender</th></tr></table><table border='5' width='50%'><tr>";
if($i<3){
echo "<td align='center'>".$row['name']."</td>";
echo "<td align='center'>".$row['gender']."</td>";
++$i;
} else {
echo "<td align='center'>".$row['name']."</td><td align='center'>".$row['gender']."</td>";
echo "</tr>";
$i = 1;
echo "<tr>";
}
}
echo "</table>";
?>
</div>
Thank You Guys For Your Support