Echo of Option Menu Shows as "%" - php

I have an option menu with several options including show all records. The option menu is fed by a query on a MySQL table. I am trying to echo the option selected and everything works until I select all records. Then the echo shows as %. Would like for it to echo ALL when All records is selected from option menu. I am using the following to echo:
<?PHP echo isset($_POST['selGrade']) ? $_POST['selGrade'] : 'ALL'; ?>
Currently the default option is all records, which echos ALL correctly with my PHP statement above. Problem happens when I select another option and then select all records again. Then it echos "%". What can I add to the PHP statement to echo ALL when a user selects All records?

With the help of Peter Darmis, the solution to my question is:
<?PHP
if(!isset($_POST['selGrade']) || empty($_POST['selGrade']) ||
$_POST['selGrade']=="%"){
$text = "ALL";
} else {
$text = $_POST['selGrade'];
}
echo $text;
?>
I asked a similar question at Option Menu Showing Undefined Index. Thanks to all.

I'm guessing "all records" empties the $_POST['selGrade'] but doesn't unset it. Consider using !empty($_POST['selGrade']) instead of isset().
Function empty()

Related

Use only function parameter if variable has a value

I'm developing a small system to add records which be can opened later. On the page view.php it shows all records of all different country. I just extend this to only show one selected countries.
I call this function with the following argument:
$toxRecords->getStatsCountry();
If I want to show only one country, I just simple add the county code as parameter. Example:
$toxRecords->getStatsCountry('NL');
Because I use one page to show all countries but also one specific country it needs to check if there is an variable.
I check my POST with the following argument: toxInput::get('country');
This just simple return: $_POST['country'].
Now I just want that it will only use the function parameter if value country exists. This can be very simple, see below:
if($country = toxInput::get('country')) {
$toxList = $toxRecords->getRecords($country);
} else {
$toxList = $toxRecords->getRecords();
}
But I was wondering if it possible to shorten this to one single lane?
Example what I tried and will explain what I want:
$toxRecords->getRecords(if($country = toxInput::get('country')){ echo $country; });
this statement returns only one value through the condition:
condition ? value if condition is true : value if condition is false
for example:
$toxRecords->getRecords($country == toxInput::get('country') ? $country : "");
Try something like this:
($val1 == $val2)? Echo "true" : Echo "False";
For more info check this post:
https://stackoverflow.com/a/1506621/7116840

Disable button on conditions

I have a buttons which are displayed from sql query:
$username = new User();
$name = $username->data()->username;
$sql1 = DB::getInstance()->query("SELECT names FROM list WHERE username = '$name'");
if (!$sql1->count()) {
echo 'No data';
} else {
foreach ($sql1->results() as $sql1) {
?>
<p><button class="" > <?php echo $sql1->names; ?></button></p>
<?php
}
}
This displays two buttons which match the conditions from the query, so I'm trying to disable one of the displayed buttons if it doesn't match another condition.
For example, there are two buttons, John and Poodle. And a query to match if one of the buttons is an animal.
So if the button john does not match the query, it should be disabled.
I will try to express the idea sketched in the comments to the question, as you asked me to and explain it step by step:
foreach ($sql1->results() as $set) {
echo sprintf('<p><button %s>%s</button></p>'."\n",
in_array($set->name, array('poodle','cat','sheep')) ? 'disabled' : '',
$set->name);
}
Here $set is a different object (set of attributes) for each iteration of the foreach loop. You said you have two entries in that queries result, so two buttons get generated. Each $set has a name ($set->name) if you understand your code correct (I don't know your database...). This name is used twice for generating each button: first the name is used as text in the button and second it is used in a conditional to decide if the button should be disabled or not. That condition is implemented as a trinary expression, line 3 in the example above. In the line a function is called: in_array(). That returns true or false. If true, then the attribute "disabled" is added to the button, if false then the empty string ('') is added instead, so the button does not have the disabled property.
This is obviously not finished code. It is meant to give you the idea, so you should understand it, not just copy and try it. Feel free to ask if questions arise!
Try the following:
<button class="<?php if($sql1->names != "required_name") echo "disabled" ?>">
<?php echo $sql1->names; ?>
</button>
To disable a button ith php+html, you can try with a query value returned "disabled".
For example: in your query use a CASE WHEN statement to return disabled value when your condition is defined:
SELECT filed1,
CASE
WHEN field1= '19' THEN 'disabled'
WHEN field1='20' THEN ''
WHEN filed1='21' THEN ''
WHEN field1='22' THEN ''
ELSE 'disabled'
END AS for_button
FROM table1 AS tbl1
WHERE tbl1.field1_param = '$param'
Put in the HTML TAG button this:
<input type="submit" value="Button OnOff" <?=$disableButton['for_button']; ?>>
If the value returned is disabled, in the for_button variable, write the valure returned from query and the button will are disabled.

selected value in select should be at top when editing a form

I am editing my form but i am facing a problem in select tag. I want the already selected value at the top of the option and plus same value should not be shown twice. Below i am getting the selected value at the top but i don't know how to overcome the duplicate value. I am new to php and i don't know if this is a ridiculous question or not. Any help would be nice.
I am using two tables Status and other is Cases. First option is from Status and second one is from Cases.
<select name='cstatus' style="width: 100%;">
<option><?=$row['CASE_STATUS']?></option>
echo "<option value='".$row['CASE_NAME']."'>".$row['CASE_NAME'].'</option>';
<select>
You can use selected="selected" on the option you want to be selected.
That way you don't have to duplicate the option to put it at the top.
<?php
if($row['CASE_STATUS'] == "")
{
echo "<option>Under Investigation</option>";
echo "<option>Under Trial</option>";
}
else if($row['CASE_STATUS'] == "Under Investigation")
{
echo '<option>'.$row["CASE_STATUS"].'</option>';
echo "<option>Under Trial</option>";
}
else
{
echo '<option>'.$row["CASE_STATUS"].'</option>';
echo "<option>Under Investigation</option>";
}
?>

how to write a if statement to check if a value has been entered

How would i go about writing a if statement to check if the value of a option menu is empty and if so not to show it.
<option value="">GUYS AND GIRLS</option>
if value has nothing in it it wont show this option in the select menu
but if.
<option value="http://test.com">GUYS AND GIRLS</option>
It has something there it will show up in the drop down.
I see two possible solutions. The first is if have already have the value separated somewhere, then check it and then use php to replace the empty value. If you only have access to that statement in your if statement, then use a substring to extract the value and then compare it.
Put your options in an array and run a foreach loop looking for an empty value:
//put the dropdown option in an array
$options = array('<option value="">GUYS AND GIRLS</option>',
'<option value="http://test.com">GUYS AND GIRLS</option>');
//run a for each
foreach($options as $currentOption){
//look for empty value
if(!strstr('value=""', $currentOption)){
echo $currentOption;
}
}
EDIT, BASED ON COMMENTS
Try this, if it is possible for you to implement.
<?php
if(meta('guys') != ""){
echo '<option value="' . meta('guys') . '">GUYS</option>';
}
?>

Using MySQL Databases and PHP to Populate forms

I am contemplating taking the next step with my PHP applications and making the option fields dynamic. That would open the doors for more automation.
I have drop downs throughout my project, they are used to select a specific user and I update them manually when a new user is added (which is also a manual process). But if i take the first step and make these drop downs become populated by a MySQL Database, then i can move on to dynamic user creation.
I know how I can achieve this, but I am curious about some other alternatives (If there is any).
Here is what I would do..
$query = ** MySQL Select * From Database Query **
echo '<select name="usernames">';
while($row == mysql_fetch_array($query))
{
echo '<option>' . $row['username'] . '</option>';
}
echo '</select>';
So my questions is, would you do this differently? And why? Thanks!
What you are doing will work fine. I like to make it into a function so that if I ever need that dropdown on another page I dont have to write a lot of code over again.
function userDD()
{
$query = ** MySQL Select * From Database Query **
$html = '<select name="usernames">';
while($row == mysql_fetch_array($query))
{
$html .= '<option>' . $row['username'] . '</option>';
}
$html .= '</select>';
return $html;
}
This code does exactly what your code does except it doenst use echo. Instead you use a variable ($html) to store all of the data then when you are done you return it.
Your way is fine, but two things need to be changed:
- Run htmlentities() or htmlspecialchars() on all echoed HTML to avoid XSS. Unless you already sanitized it at database entry time but I find this practice silly.
- Add a value attribute to each <option> tag, otherwise you won't be able to retrieve the username selected. I suggest using the username's corresponding ID or something else that's unique to that user. If it's a string, use htmlentities/htmlspecialchars on it too.
php file
$users = getUsers();
include('template.tpl');
template
<select name="username">
<?php foreach( $users as $user ): ?>
<li><?= e( $user['username'] ) ?></li>
<?php endforeach; ?>
</select>
e is a function that escapes strings to prevent xss attacks
I wouldn't put an SQL query in the same document as my output...
I'd create a document containing all SQL queries, in functions, and include that file. Just to keep things seperated.

Categories