Change Database Output - mysql

I have a form setup and the data goes into a mysql database. There are several parts where the choice is both a numerical and a written rating - 1 - poor, 2 - fair, 3 - good, 4 - excellent. The field in the database is set to varchar with a length of 20. However, the output only returns the written portion - poor, fair, good, excellent, dropping the numerical. I want to either include the numerical with the written or just use the numerical itself. Can someone explain how I would set this up.
I am assuming the changes I need to make are in the php form itself - I am supplying a sample of the code and what I think I need to change - just want to verify before I do so.
<select name="programRating" class="pure-input-1">
<option value="">Select Rating...</option>
<option value="Poor" <?php if($_POST['programRating'] == "Poor") echo "selected"; ?>>1 - Poor</option>
<option value="Fair" <?php if($_POST['programRating'] == "Fair") echo "selected"; ?>>2 - Fair</option>
<option value="Good" <?php if($_POST['programRating'] == "Good") echo "selected"; ?>>3 - Good</option>
<option value="Excellent" <?php if($_POST['programRating'] == "Excellent") echo "selected"; ?>>4 - Excellent</option>
</select>
<select name="programRating" class="pure-input-1">
<option value="">Select Rating...</option>
<option value="Poor" <?php if($_POST['programRating'] == "1-Poor") echo "selected"; ?>>1 - Poor</option>
<option value="Fair" <?php if($_POST['programRating'] == "2-Fair") echo "selected"; ?>>2 - Fair</option>
<option value="Good" <?php if($_POST['programRating'] == "3-Good") echo "selected"; ?>>3 - Good</option>
<option value="Excellent" <?php if($_POST['programRating'] == "4-Excellent") echo "selected"; ?>>4 - Excellent</option>
</select>

The values which are stored in your database are the ones you specify in the "value" attribute of each "option".
So put the numbers in the "value" attribute instead of the text.

Related

can we make slider in select options?

can we make slider/carousel in select options.below is the code and image.anyone help plz.
<select class="" id="project_type" name="project_type" title="Project Type" multiple
style="width: 410px;">
<option value="Residential" <?php if($selected_project_type == 'Residential') echo "selected";
?> >Residential</option>
<option value="Commercial" <?php if($selected_project_type == 'Commercial') echo "selected"; ?
> >Commercial</option>
<option value="Residential & Commercial" <?php if($selected_project_type == 'Residential &
Commercial') echo "selected"; ?> >Residential & Commercial</option>
<option value="Infrastructure" <?php if($selected_project_type == 'Infrastructure') echo
"selected"; ?> >Infrastructure</option>
<option value="Others" <?php if($selected_project_type == 'Others') echo "selected"; ?>
>Others</option>
enter image description here
i want all select options inline and put slider.
Selects are pretty hard to customize
A way easier will be just make slider
and change an input or even div properties when click on slide

Select Value fetched from mysql

I have a select tag contains option male and female , I'm making an update form but when I load and set value of select (from mysql) it always display the first option which is "Male".
<select value="<?php echo $gender ; ?> name="gender"
id="gen">
<option > Male </option>
<option > Female </option>
</select>
I know there's a javascript way in this case but I can't find the correct one and it's not working. I'm also thinking about if statement comparing $gender to string male or female then if possible I might change attribute of option selected=selected.
What you want to do is set the option that is correct to "selected"
A drop-down list with a pre-selected option:
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="vw">VW</option>
<option value="audi" selected>Audi</option>
</select>
https://www.w3schools.com/TAGS/att_option_selected.asp
So your best bet is to drop the PHP value into Javascript and handle it from there.
Alternatively, if you really want to use just PHP
$html = '<select name="gender" id="gen">';
if($gender == "Male") {
$html .= "<option selected> Male </option>";
$html .= "<option> Female </option>";
}else{
$html .= "<option> Male </option>";
$html .= "<option selected> Female </option>";
}
$html .= "</select>";
echo $html;

mysql_fetch_array and while loop accidentally returning every other record

Question title says it all: I want to loop through every row, but I get only every other row. Funny enough, when I use mysql_num_rows as can be seen below, it gets the right count. Whats up?
$query = "SELECT * FROM `staff`";
$result = mysql_query($query) or die(mysql_error());
$count = mysql_num_rows($result);
echo '<form action="" method="post"><ul>';
while ($row = mysql_fetch_array($result)){
echo '<li>' . $row['name'] . '
<select>
';
$i = 1;
while ($i <= $count) {
echo '<option value="' . $i . '">' . $i . '</option>';
$i++;
}'
</select>
</li>';
}
Can it be because I am counting as well? I need to count as well. This is a reordering app so I need to be able to change the position of every person, but the options can only be the total number of positions available. Maybe there is a more logical way to do this anyway?
You have are not outputting the string that closes the </select> tag:
while ($i <= $count) {
echo '<option value="' . $i . '">' . $i . '</option>';
$i++;
}'
'// ^--- NB missing echo
As it stands, PHP currently parses the string as a literal expression that performs no operation.
Consequently, your resulting HTML will look like:
<li>foo
<select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<li>bar
<select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<li>baz
<select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
How such broken HTML is rendered will depend upon one's browser. It would appear that your browser is only producing every other select box?

Selecting Data from select tag

i have to select data from select tag rather then selecting value as its selects value for default
<select name="time" >
<option selected="selected" >timings</option>
<option value="155">9:00AM - 12:00PM</option>
<option value="244">12:00AM - 15:00PM</option>
</select>
I want to select 12:00AM - 15:00PM values and store it in my DB. How to do it any ideas.
Thanks in advance
Ameeth
<select name="time">
<option selected="selected" >timings</option>
<option value="9:00AM - 12:00PM">9:00AM - 12:00PM</option>
<option value="12:00AM - 15:00PM">12:00AM - 15:00PM</option>
</select>
<?php
$value = $_POST["time"]; //what method you are using
?>
or
<select name="time" >
<option selected="selected" >timings</option>
<option value="155">9:00AM - 12:00PM</option>
<option value="244">12:00AM - 15:00PM</option>
</select>
<?php
$value = "";
switch($_POST['time']){
case '155':
$value="9:00AM - 12:00PM";
break;
case '244':
$value = "12:00AM - 15:00PM";
break;
default:
$value = "No data found";
break;
}
?>
You've two options;
Use 12:00AM - 15:00PM inside the value="" parameter.
OR
Do something like this in PHP file for collecting data and inserting it into db;
if ($_POST['time'] == "155")
{
$time = '9:00AM - 12:00PM';
}
elseif ($_POST['time'] == "244")
{
$time = '12:00AM - 15:00PM';
}
// do rest of the data insertion into db

Select option default based on database variable [duplicate]

This question already has answers here:
HTML <Select> <Option> default based on MySQL data
(4 answers)
Closed 7 months ago.
I have a form, with a simple select box, and I also have a text field that pulls a value. I want to use the value in $Defaultselection as the default value for the select options. So that if $Defaultselection = 4 then the default selected option would be D.
Using PHP/HTML
`$Defaultselection`
contains an integer between 1-4.
<select >
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
<option value="4">D</option>
</select>
That should do it:
<select >
<option value="1" <?php echo ($Defaultselection == 1)?"selected":""; ?>>A</option>
<option value="2" <?php echo ($Defaultselection == 2)?"selected":""; ?>>B</option>
<option value="3" <?php echo ($Defaultselection == 3)?"selected":""; ?>>C</option>
<option value="4" <?php echo ($Defaultselection == 4)?"selected":""; ?>>D</option>
</select>
or this other one:
<select >
<option value="1" <?php if ($Defaultselection == 1) echo "selected"; ?>>A</option>
<option value="2" <?php if ($Defaultselection == 2) echo "selected"; ?>>B</option>
<option value="3" <?php if ($Defaultselection == 3) echo "selected"; ?>>C</option>
<option value="4" <?php if ($Defaultselection == 4) echo "selected"; ?>>D</option>
</select>