I am a bit stuck I am attempting to build out a table with HTML and PHP. Ok got that done. Now I want to add a dynamic dropdown as one of the column options but cant seem to figure out how to mix the PHP, the HTML and the needed FOR EACH loop.
I know its currently wrong but am posting a variable of what I have been trying below.
echo '<td>' . "<select>". "<option value =" . $sf_name . ' '. $sl_name . ">" . $sf_name . ' '. $sl_name . "</option>" .
foreach($Staff_On_Duty as $person){
"<option value =" . $sf_name_option=$person->Staff_First_Name . ' '. $sl_name_option=$person->Staff_Last_Name . ">" . $sf_name_option . ' '. $sl_name_option . "</option>"
}
. "</select>" .'</td>';
I need to have the currently selected individual at the top of the dropdown with the option to change that person out. the first .sf_name comes from higher in the code and gives me the name of the individual currently selected. The foreach runs from the $Staff_On_Duty query to give me everyone working right now. What is the right way to do this?
This is how I would do it, essentially only fill the dropdown and mark the option that should have been selected (other stuff is a matter of preference):
echo("<td><select>");
foreach($Staff_On_Duty as $person){
$sf_name_option=$person->Staff_First_Name; // separated for clarity, you could also use $person->Staff_First_Name everywhere
$sl_name_option=$person->Staff_Last_Name;
echo("<option value = $sf_name_option $sl_name_option");
if (($sf_name_option == $sf_name) && ($sl_name_option == $sl_name)) echo (" selected"); // this is the relevant part, but make sure variable values match!
echo(">$sf_name_option $sl_name_option</option>");
}
echo("</select></td>");
Note that I did not put any quotation marks cause I have no idea what your variables entail, but it would need them if you don't have them in yet, i.e.:
... value=\"$sf_name_option $sl_name_option\" ...
Related
I'm new to PHP programming and working with backend. I know what I am doing, but having trouble finding a proper solution online which matches my case.
Basically, I have a form (2 text fields, 3 dropdowns) that filters the data that I am getting from SQL Server (that works). However, when I press the "filter" button and I get the desired results, the page reloads and resets the form. Now I have the filtered records but I don't know what filters I used to get that. I could insert a <?php ... ?> if else, but the thing is I already am using php to get the dropdown values. Don't think I can put php inside php.
By including if-else I mean this:
echo '<option value="' . $row['status'] . '"' . if($prod_status == $row['status']): echo ' selected="selected"' . '>' . $row['status'] . '</option>';
Here's the code:
<label for="prod_status_filter">Prod Status</label>
<select class="form-control" id="prod_status_filter" name="prod_status_filter">
<?php
$query_status = 'select DISTINCT status FROM analytics.laborrecords ORDER BY status ASC';
$result = sqlsrv_query($conn, $query_status);
while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC))
{
echo '<option value="' . $row['status'] . '">' . $row['status'] . '</option>';
}
?>
</select>
This is one of the dropdowns.
I want:
1. Either stop the page from reloading so the filter dropdowns remain the same. (AJAX won't work, I have 3 dropdowns with a lot of options and it's doesn't seem like a good practice to make so many getElementById lines)
2. Let the page reload but keep the values as I set until I want to reset them, with a "reset" button.
You have to populate the form using the submitted form data. It should be either in $_POST or $_GET.
Check if the value of $_POST['prod_status_filter'] matches the value in $row['status'] and if it does, echo selected.
Be sure to sanitize the form data. Never trust user input.
Is there any simple way to add none value in a dropdown list in php?? I tried the below but it doesn't work. It shows all lname values but not the 'None'.
I do not want to enter 1 none value in database and retrieve it. So if it is possible through php itself.
while ($row = mysqli_fetch_array($result))
{
echo "<option value=None>" . $row['lname'] . "</option> <br>";
}
Please help me.
Just add the "none"-option above the others.
echo '<option value="">None</option>';
while ($row = mysqli_fetch_array($result))
{
echo '<option value="' . $row['lname'] . '">' . $row['lname'] . '</option>';
}
Two things odd here:
- You need quotes around the None in value=None --
- a inside of a select options list isn't ever a smart thing to do.
while ($row = $arr_result ) {
echo "<option value='" . $row['fallacytype'] . "'>" . $row['fallacytype'] . "</option>";
}
I am trying to create a and menu in html by using the string values in my SQL table.
I've confirmed that the rest of my code (not shown here, but can be provided if needed to diagnose), is working. The only thing that goes wrong is that my loop up there becomes infinite (i know this because it creates infinite sql value).
I've looked up and tried all the variations of quotation marks to no avail.
I've checked in w3shool.com, and other video tutorials that my loop syntax is correct.
It seems that the $row variable is not incrementing or moving on to the next value in the loop for some curious reason.
Thanks in advance.
You have to loop via following way for all the elements:
foreach($arr_result as $row) {
echo "<option value='" . $row['fallacytype'] . "'>" . $row['fallacytype'] . "</option>";
}
I'm looking to create a formatted product list from an SQL database. My aim is to have a store on my website with a series of small boxes containing some shorthand information about each product, that when clicked will open a pop-up containing detailed information. (I have a working Javascript/JQuery code to create the pop-ups.)
Here is the PHP code so far, simply to get the information from the database and display it on a webpage...
(I've been using XAMPP to provide an environment for me to test the code in)
<?php
mysql_connect("localhost", "root", "") or die (mysql_error ());
mysql_select_db("Database1") or die(mysql_error());
$strSQL = "SELECT * FROM Products";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {
echo $row['Brand'] . " " . $row['ProductName'] . " " . $row['Image'] . "<br />";
}
mysql_close();
?>
I want the echoed line to be displayed in a divider, with a divider generated for each record in the SQL database (say I have 10 products available, there would be ten dividers, and 10 different boxes on the webpage). The divider's class is "ProductBox".
echo "<div class=\"ProductBox\">"; $row['Brand'] . " " . $row['ProductName'] . " " . $row['Image'] . "</div>";
This was the closest I have come to a solution, which was simply managing to write a code with no syntax errors - alas, nothing actually displays on the webpage.
If I'm going about this entirely the wrong way please tell me - I'm fairly sure I need to use a SQL database to dynamically update stock on a live website, but if I need to implement a different programming language or whatever then just tell me what you think would work and help me with a solution.
You have an extra semicolon in your code
echo "<div class=\"ProductBox\">"; $row['Brand'] . " " . $row['ProductName'] . " " . $row['Image'] . "</div>";
Replace with
echo "<div class=\"ProductBox\">". $row['Brand'] . " " . $row['ProductName'] . " " . $row['Image'] . "</div>";
mysql_fetch_array needs to be used like this (see PHP Doc):
while($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
}
or you could just use "mysql_fetch_assoc" instead.
HOWEVER, if you're new to PHP, I HIGHLY RECOMMEND that you get started on the right foot. mysql_query functions are soon to be deprecated. DON'T USE THEM. Most recommend using "PDO" for querying your database. Here's a great tutorial to teach you: http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers
Also, as mentioned, you have an extra semi-colon.
Dont forget these basics markups :
`<HTML>
<HEAD>
</HEAD>
<BODY> put in here your divs
</BODY>
</HTML>`
I am aware there are examples out there that hover around the issue of dynamically setting the selected tag in a HTML option tag, but I am hitting a pretty difficult issue of break statements and quotations in what I am trying to accomplish.
while($info = mysql_fetch_array($companydesc))
{
$output3 .= '<option value="'. $info['company_code'] . if ($result['company']==$info['description']){echo 'selected=\"selected\"'} . '">' . $info['description'] . '</option>';
}
echo $output3;
The error that I receive is a unexpected T_IF on the line with the if statement. Is it not legal to put an if statement in there? Or is it a matter of doing proper breaks? Any help would be greatly appreciated (and hopefully the formatting for the code worked)
Use a ternary statement:
while($info = mysql_fetch_array($companydesc)) {
$output3 .= '<option value="'. $info['company_code'].'"'.($result['company']==$info['description'] ? ' selected=\"selected\"' : '') . '>' . $info['description'] . '</option>';
}
echo $output3;
yeah, it is illegal, my suggestion is:
while($info = mysql_fetch_array($companydesc))
{
if($result['company']==$info['description'])
{
$output3 .= '<option value="' . $info['company_code'] . '" selected = selected>' . $info['description'] . '</option>';
}
else
{
$output3 .= '<option value="' . $info['company_code'] . '">' . $info['description'] . '</option>';
}
}
echo $output3;
Trying to fix everything with an individual solution in the way that you originally intended to with stuff like
while($info = mysql_fetch_array($companydesc))
is, I feel, one of the big things that is holding php back and eventually makes the code extremely difficult to maintain, although it's very easy on the learning curve.
This function takes an optional selected parameter and will build out your select box. This is much much better than doing every single select box for every query with its own custom code.
There might be a syntax error in here, but generally,
function selectBox($array, $selected=false){
foreach($array as $name => $value){
$options .= '
<option value=" . $name . '"'
. ($selected != false && $selected == $value) ? 'SELECTED' : ''
. '>".$name."</option>";
}
}
Worth mentioning that select box is requiring you to pass a predefined array of key=>value pairs, so you'll need to have an additional helper function (you can't just pass it your database return)
Also probably worth mentioning is that using mysql_fetch_array($companydesc) is not abstracting the database level at all. Down the line, code like this will prohibit a migration to something like Postgres or a different database system entirely.