Problems with elseif conditions - php

I have a section of my code with a few elseif condtions that don't seem to be working properly. I've built a conditional drop down form that's working for the most part so far except for when it reach a couple elseifs.
I'm using JS to send the dropdown option name to my PHP script with GET which is where it seems to be going weird. For most of the form it pulls the options for the next dropdown from a MySQL query but for the first drop I've just used a few elseifs to make it less complex. you can check the live script here. Here's the section giving me trouble (it's the last two elseifs):
if (isset($_GET['setpoint'])) {
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row{'stp'} . "'>" . $row{'stp'} . "</option>";
}
} elseif (isset($_GET['power'])) {
echo "<option>Please Choose Setpoint Options</option>";
while ($row = mysql_fetch_array($result)) {
$row{'stp'} = ucfirst($row{'stp'}); //capitalizes the first letter; necessary?
echo "<option value='" . $row{'stp'} . "'>" . $row{'stp'} . "</option>";
}
} elseif (isset($_GET['source'])) {
echo "<option>Please Choose Input Range</option>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row{'sir'} . "'>" . $row{'sir'} . "</option>";
}
} elseif (isset($_GET['type']) && $_GET['type'] = "Digital") {
echo "<option>Please Choose Input Source</option>";
echo "<option value='RS232C'>RS232C</option><option value='RS422'>RS422</option><option value='RS485'>RS485</option><option value='current loop'>current loop</option>";
} elseif (isset($_GET['type']) && $_GET['type'] = "Analog") {
echo "<option>Please Choose Input Source</option>";
echo "<option value='DC current'>DC Current</option><option value='DC voltage'>DC Voltage</option><option value='AC current'>AC Current</option><option value='AC voltage'>AC Voltage</option><option value='process'>Process</option><option value='thermocouple'>Thermocouple</option><option value='RDT'>rdt</option>";
}
The first dropdown will set $_GET['type'] to either 'Analog' or 'Digital' but no matter which option $_GET['type'] is set to it will just run the first elseif that checks if $_GET['type'] is set. If $_GET['type'] = 'Analog' then it should be returning the last elseif and not the second to last.
This is most of the script for reference:
//prevents injections
//any order
isset($_GET['type'])?$type = urldecode($_GET['type']):"";
//$type = mysql_real_escape_string(urldecode($_GET['type']));
isset($_GET['source'])?$source = mysql_real_escape_string(urldecode($_GET['source'])):"";
isset($_GET['range'])?$power = mysql_real_escape_string(urldecode($_GET['range'])):"";
isset($_GET['setpoint'])?$setpoint = mysql_real_escape_string(urldecode($_GET['setpoint'])):"";
//forms the query depending on what data is recieved through GET
//first option on the bottom; last option on the top to avoid conflicts
if (isset($_GET['setpoint'])) {
$query = "SELECT DISTINCT stp FROM meters WHERE sio='$range' AND pso='$power' AND stp='$setpoint' ORDER BY model";
} elseif (isset($_GET['power'])) {
$query = "SELECT DISTINCT stp FROM meters WHERE sio='$range' AND pso='$power' ORDER BY model";
} elseif (isset($_GET['range'])) {
$query = "SELECT DISTINCT pso FROM meters WHERE sio='$range' ORDER BY model";
} elseif (isset($_GET['source'])) {
$query = "SELECT DISTINCT sir FROM meters WHERE sio LIKE '%$source%' ORDER BY sir";
}
//creates a result array from query results
isset($query)?$result = mysql_query($query):"";
//outputs dropdown options dependent on what GET variables are set
//first option on the bottom; last option on the top to avoid conflicts
if (isset($_GET['setpoint'])) {
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row{'stp'} . "'>" . $row{'stp'} . "</option>";
}
} elseif (isset($_GET['power'])) {
echo "<option>Please Choose Setpoint Options</option>";
while ($row = mysql_fetch_array($result)) {
$row{'stp'} = ucfirst($row{'stp'}); //capitalizes the first letter; necessary?
echo "<option value='" . $row{'stp'} . "'>" . $row{'stp'} . "</option>";
}
} elseif (isset($_GET['source'])) {
echo "<option>Please Choose Input Range</option>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row{'sir'} . "'>" . $row{'sir'} . "</option>";
}
} elseif (isset($_GET['type']) && $_GET['type'] = "Digital") {
echo "<option>Please Choose Input Source</option>";
echo "<option value='RS232C'>RS232C</option><option value='RS422'>RS422</option><option value='RS485'>RS485</option><option value='current loop'>current loop</option>";
} elseif (isset($_GET['type']) && $_GET['type'] = "Analog") {
echo "<option>Please Choose Input Source</option>";
echo "<option value='DC current'>DC Current</option><option value='DC voltage'>DC Voltage</option><option value='AC current'>AC Current</option><option value='AC voltage'>AC Voltage</option><option value='process'>Process</option><option value='thermocouple'>Thermocouple</option><option value='RDT'>rdt</option>";
Again, you can check the live script here.

$_GET['type'] = "Digital" is assignment, not a check.
What you need is
$_GET['type'] == "Digital"
Same with $_GET['type'] = "Analog"
It should be
$_GET['type'] == "Analog"
Also, like Darkbee mentioned, use PDO or MySQLi

You're assigning $_GET['type'] when I think you want the comparison operator equals:
$_GET['type'] = "Digital"
becomes
$_GET['type'] == "Digital"

try
elseif (isset($_GET['type']) && $_GET['type'] == "Digital")
and
elseif (isset($_GET['type']) && $_GET['type'] == "Analog")
Two Equal signs is an equal one is an assignment (will return true always)

Related

Set previously selected value in Dropdown box using PHP

So, what I'm trying to achieve that after the user hits submit, the dropdown retains the value that the user have chosen. This is my code for the dropdown, I understand that I have to selected = 'selected', but I couldn't figure out how it fits in my code.
if (mysqli_num_rows($result) > 0) { // output data of each row
while($row_branch = mysqli_fetch_array($result)) {
$menu_branch .= "<option value='".$row_branch["0"]."'>" .
$row_branch["0"]. "</option>";
}
}
echo $menu_branch;
You cannot just add selected = selected inside the loop because every option will be selected.
You might need the if statement to select the option of your choice.
For example:
if($row_branch["0"] == 'bar'){
$menu_branch .= "<option value='".$row_branch["0"]."' selected>" .
$row_branch["0"]. "</option>";
}else{
$menu_branch .= "<option value='".$row_branch["0"]."'>" .
$row_branch["0"]. "</option>";
}
When the user select an option, Where you submit this information?
If this information return from a database
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row_branch = mysqli_fetch_array($result)) {
$select = ($row_branch["0"]==$value_db)? "selected='selected'" : "";
$menu_branch .= "<option value='".$row_branch["0"]."' $select>" .
$row_branch["0"]. "</option>";
}
}
if i understand right:
// $yourvalue is previous dropdown value
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row_branch = mysqli_fetch_array($result)) {
if ($row_branch["0"] == $yourvalue)
$selected = "selected";
else
$selected = "";
$menu_branch .= "<option value='".$row_branch["0"]."' $selected>" .
$row_branch["0"]. "</option>";
}
}
echo $menu_branch;

Selected in option from while PHP

I have a problem to select the preferred value
<?php
...
if ($result->num_rows > 0) {
$selected_rep = ($row['ID']=67) ? 'selected="selected"' :'';
while($row = $result->fetch_assoc()) {
echo "<option $selected_rep value='". $row["ID"]."'>" . $row["NUME"]. ' '. $row["PRENUME"]. "</option>\n";
}
}
...
?>
I want to select the value with ID=67.
Not function $selected_rep ,select all values.
Simply put the selected test inside the loop where it has access to the loaded $row for each of the resultset rows you are processing
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$selected_rep = ($row['ID'] == 67) ? 'selected="selected"' :'';
echo "<option $selected_rep value='". $row["ID"]."'>" . $row["NUME"]. ' '. $row["PRENUME"]. "</option>\n";
}
}
Ahhh and you write test using == and not =
== is this equal to that
= set this to that

Find previously selected option in while loop

I'm trying to get a drop down menu to keep its selected value when the user hits submit, but it fails due to errors on the form.
I have a while loop returning values from a database to build the options for the drop down, but how do I echo "selected" on the right option?
I have tried if($district == $row["name"]) { echo "selected";} as you see below, but it doesn't work.
<?php
$result = mysql_query("SELECT dist.name FROM districts AS dist JOIN int_bd AS ibd ON dist.id = ibd.districts_id WHERE banners_id = 6 GROUP BY dist.id ORDER BY dist.id ASC", $connection);
if (!result) {
die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
echo '<option value="{$row["name"]}"'; if($district == $row["name"]) { echo "selected";} ; echo '>' . $row["name"] . "</option>";
}
?>
Sorry for the delay. None of the suggested answers worked for me. Any other ideas?
Can you try this,
<?php
$result = mysql_query("SELECT dist.name FROM districts AS dist JOIN int_bd AS ibd ON dist.id = ibd.districts_id WHERE banners_id = 6 GROUP BY dist.id ORDER BY dist.id ASC", $connection);
if (!result) {
die("Database query failed: " . mysql_error());
}
$district = $_REQUEST['name']; // You need pass the value you have been submitted
while ($row = mysql_fetch_array($result)) {
$selected ="";
if(trim($district) == trim($row["name"])) { $selected = "selected";}
echo '<option value="{$row["name"]}" '.$selected.' >' . $row["name"] . "</option>";
}
?>
Try this..
if($district == $row["name"])
{
echo "<option value='$district' selected>$district</option>";
}
I just found the answer. This is what I did:
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row["name"] . '"';
if($row["name"] == $district) { echo 'selected';} ;
echo '>' . $row["name"] . '</option>';
}
It seems to have been this line
echo '<option value="{$row["name"]}"';
that was causing the problem.

Updating to MySQLi and having some trouble

So I've spent a bit of time looking at MySQLi and I'm having trouble updating a script with the new functions. This script is used for a dynamic dropdown form, using data sent to it using JS. You can find a live version of the script here to check out what I'm takling about. I've looked up and down my code and have compared it to other MySQLi examples and I'm just not sure where I'm going wrong.
Now, the first dropdown doesn't even initiate a query, all the PHP does is return predefined results as it's just simpler for the first option. What's weird, to me, is that even the first drop down is now not working when it does not rely at all on the MySQLi connection. It all worked before updating, just for reference.
Here's my script:
$db = new mysqli($dbHost, $dbUser, $dbPass, $dbDatabase);
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
//prevents injections
//any order
isset($_GET['type'])?$type = urldecode($_GET['type']):"";
isset($_GET['source'])?$source = $db->real_escape_string(urldecode($_GET['source'])):"";
isset($_GET['range'])?$power = $db->real_escape_string(urldecode($_GET['range'])):"";
isset($_GET['setpoint'])?$setpoint = $db->real_escape_string(urldecode($_GET['setpoint'])):"";
//forms the query depending on what data is recieved through GET
//first option on the bottom; last option on the top to avoid conflicts
if (isset($_GET['setpoint'])) {
$query = "SELECT DISTINCT stp FROM meters WHERE sio='$range' AND pso='$power' AND stp='$setpoint' ORDER BY model";
} elseif (isset($_GET['power'])) {
$query = "SELECT DISTINCT stp FROM meters WHERE sio='$range' AND pso='$power' ORDER BY model";
} elseif (isset($_GET['range'])) {
$query = "SELECT DISTINCT pso FROM meters WHERE sio='$range' ORDER BY model";
} elseif (isset($_GET['source'])) {
$query = "SELECT DISTINCT sir FROM meters WHERE sio LIKE '%$source%' ORDER BY sir";
}
//creates a result array from query results
isset($query)?$result = $db->query($query):"";
//outputs dropdown options dependent on what GET variables are set
//first option on the bottom; last option on the top to avoid conflicts
if (isset($_GET['setpoint'])) {
while ($row = $result->fetch_assoc()) {
echo "<option value='" . $row['stp'] . "'>" . $row['stp'] . "</option>";
$result->free();
}
} elseif (isset($_GET['power'])) {
echo "<option>Please Choose Setpoint Options</option>";
while ($row = $result->fetch_assoc()) {
$row{'stp'} = ucfirst($row{'stp'}); //capitalizes the first letter; necessary?
echo "<option value='" . $row['stp'] . "'>" . $row['stp'] . "</option>";
$result->free();
}
} elseif (isset($_GET['source'])) {
echo "<option>Please Choose Input Range</option>";
while ($row = $result->fetch_assoc()) {
echo "<option value='" . $row['sir'] . "'>" . $row['sir'] . "</option>";
$result->free();
}
} elseif (isset($_GET['type']) && $_GET['type'] == "Digital") {
echo "<option>Please Choose Input Source</option>";
echo "<option value='RS232C'>RS232C</option><option value='RS422'>RS422</option><option value='RS485'>RS485</option><option value='current loop'>current loop</option>";
$result->free();
} elseif (isset($_GET['type']) && $_GET['type'] == "Analog") {
echo "<option>Please Choose Input Source</option>";
echo "<option value='DC current'>DC Current</option><option value='DC voltage'>DC Voltage</option><option value='AC current'>AC Current</option><option value='AC voltage'>AC Voltage</option><option value='process'>Process</option><option value='thermocouple'>Thermocouple</option><option value='RDT'>rdt</option>";
$result->free();
}
edit: This is my old code using the deprecated method.
$con = mysql_connect($dbHost, $dbUser, $dbPass) or trigger_error("Failed to connect to MySQL Server. Error: " . mysql_error());
mysql_select_db($dbDatabase) or trigger_error("Failed to connect to database {$dbDatabase}. Error: " . mysql_error());
//prevents injections
//any order
isset($_GET['type'])?$type = urldecode($_GET['type']):"";
//$type = mysql_real_escape_string(urldecode($_GET['type']));
isset($_GET['source'])?$source = mysql_real_escape_string(urldecode($_GET['source'])):"";
isset($_GET['range'])?$power = mysql_real_escape_string(urldecode($_GET['range'])):"";
isset($_GET['setpoint'])?$setpoint = mysql_real_escape_string(urldecode($_GET['setpoint'])):"";
//forms the query depending on what data is recieved through GET
//first option on the bottom; last option on the top to avoid conflicts
if (isset($_GET['setpoint'])) {
$query = "SELECT DISTINCT stp FROM meters WHERE sio='$range' AND pso='$power' AND stp='$setpoint' ORDER BY model";
} elseif (isset($_GET['power'])) {
$query = "SELECT DISTINCT stp FROM meters WHERE sio='$range' AND pso='$power' ORDER BY model";
} elseif (isset($_GET['range'])) {
$query = "SELECT DISTINCT pso FROM meters WHERE sio='$range' ORDER BY model";
} elseif (isset($_GET['source'])) {
$query = "SELECT DISTINCT sir FROM meters WHERE sio LIKE '%$source%' ORDER BY sir";
}
//creates a result array from query results
isset($query)?$result = mysql_query($query):"";
//outputs dropdown options dependent on what GET variables are set
//first option on the bottom; last option on the top to avoid conflicts
if (isset($_GET['setpoint'])) {
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row{'stp'} . "'>" . $row{'stp'} . "</option>";
}
} elseif (isset($_GET['power'])) {
echo "<option>Please Choose Setpoint Options</option>";
while ($row = mysql_fetch_array($result)) {
$row{'stp'} = ucfirst($row{'stp'}); //capitalizes the first letter; necessary?
echo "<option value='" . $row{'stp'} . "'>" . $row{'stp'} . "</option>";
}
} elseif (isset($_GET['source'])) {
echo "<option>Please Choose Input Range</option>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row{'sir'} . "'>" . $row{'sir'} . "</option>";
}
} elseif (isset($_GET['type']) && $_GET['type'] == "Digital") {
echo "<option>Please Choose Input Source</option>";
echo "<option value='RS232C'>RS232C</option><option value='RS422'>RS422</option><option value='RS485'>RS485</option><option value='current loop'>current loop</option>";
} elseif (isset($_GET['type']) && $_GET['type'] == "Analog") {
echo "<option>Please Choose Input Source</option>";
echo "<option value='DC current'>DC Current</option><option value='DC voltage'>DC Voltage</option><option value='AC current'>AC Current</option><option value='AC voltage'>AC Voltage</option><option value='process'>Process</option><option value='thermocouple'>Thermocouple</option><option value='RDT'>rdt</option>";
}
You are freeing your $result inside the while loop. This will cause the loop to fail on the second iteration.
Since you are freeing the result in all of the ifs, why don't you just do it once at the end?
...
} elseif (isset($_GET['type']) && $_GET['type'] == "Analog") {
echo "<option>Please Choose Input Source</option>";
echo "<option value='DC current'>DC Current</option><option value='DC voltage'>DC Voltage</option><option value='AC current'>AC Current</option><option value='AC voltage'>AC Voltage</option><option value='process'>Process</option><option value='thermocouple'>Thermocouple</option><option value='RDT'>rdt</option>";
}
$result->free();
However this doesn't explain why analog and digital won't work still..

use mysql data and put into a drop down

i have be been trying to put a count query into a drop down as i am creating a rating type system and based on the number of rows in a database i want to return a scale for example 1-5
<?php
// Ruth Gammack
session_start();
$page = 'index.php';
mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db('group4') or die(mysql_error());
//echo "Done";
require_once "./includes/choices.inc.php";
if(isset($_GET["action"])){
switch($_GET["action"]) {
case "add":
addmodule();
break;
case "remove":
removemodule();
break;
case "empty":
emptychoice();
break;
}
reviewChoices();
// close database connection
dbClose($conn);
}
function modules(){
$get = mysql_query('SELECT * FROM module');
if (mysql_num_rows($get) == 0){
echo '<p>There are no Modules available at this time please check back later</p>';
}
else {
while ($get_row = mysql_fetch_assoc($get)) {
echo '<p>'.$get_row['moduleID'].'<br />'.$get_row['ModuleName'].'<br />'.$get_row['ModuleDesc'].'<br />'.$get_row['LecturerID'].'</p>';
// printing the list box select command
$query_disp=('SELECT moduleID, COUNT(*) FROM module');
$result_disp = mysql_query($query_disp);
echo "<select name=”selRating”>";
while($row = mysql_fetch_array($result_disp))
{
echo "<option value='" . $row['moduleID'] . "'>" . $row['moduleID'] . "</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
}
}
}
?>
i have tried many different ways but I have run out of ideas.
Maybe something like this (not tested)?
$query_disp=('SELECT moduleID, ModuleName, ModuleDesc, LecturerID, COUNT(*) AS cnt FROM module GROUP BY moduleID');
$select = "<select name=”selRating”>";
while($row = mysql_fetch_array($query_disp))
{
echo '<p>'.$get_row['moduleID'].'<br />'.$get_row['ModuleName'].'<br />'.$get_row['ModuleDesc'].'<br />'.$get_row['LecturerID'].'</p>';
$select += "<option value='" . $row['moduleID'] . "'>" . $row['ModuleName'] . " " . $row['cnt'] . "</option>";
}
$select += '</select>';
echo $select;

Categories