Use only function parameter if variable has a value - php

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

Related

Echo of Option Menu Shows as "%"

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()

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.

post method not working on condition

I have created a page on which a form posts its value. So I add some lines to get those values like
$parameter = mysqli_real_escape_string($con,$_POST['Parameter']);
But when I open that page without that form it shows Notice that
Undefined index parameter in page on line.
So I want to make something like if I post the values then only specific area will work.
Otherwise remaining area will work just like if condition.
For ex.
if(post)
{}
else
{}
How can I do this?
First you would need to check if the values are set properly..You can do it with the if condition which would be like
if(isset($_POST) && array_key_exists('name_of_your_submit_input',$_POST)){
// do the things after form processing
}else{
//things you want to do after form breaks.
}
Use isset() to check
if(isset($_POST['Parameter'])){
//desired tasks
//$parameter = mysqli_real_escape_string($con,$_POST['Parameter']);
}else{
//other task
}
You can use isset() function. In your case if should be like
if(isset($_POST['param']))
{
//Do something
}
else
{
//Do something else
}

PHP + MsSQL = Checkbox problems. Need HELP!

I have been working on this one topic for weeks. I'm creating a webpage that pulls information from MsSQL server. My problem is where there is a section with few check boxes.
The checkboxes are suppose to be checked if they are found in the SQL database.
If the borrower used money from "Savings", "Checking" and "Stock" those checkboxes should be checked in HTML page. In my case it is only checking whatever is on the first row of the SQL search list. So if the list has "Saving" on the first row, only the "Saving" checkbox will be checked not the rest on the list. I tried using loop (while($r->EOF)), but then it picks whatever is on the end of the list. Here is what I am using to pull data from the SQL server. Thank you in advance for your help. Really appreciate it!
function __construct($_ldid, $_lrid)
$this->ldid = $_ldid;
$this->lrid = $_lrid;
//$loan is defined in the PHP.class (which is shown below).
$q = ("SELECT * FROM Tabel_name where loan_id = 885775")
$v = array($_ldid, $_lrid);
$r = $db->Execute($q, $v);
$this->downpaymenttype = $r->fields; //<- I think I have this line done wrong because it is suppose to store the outputs to an array.
//So wherever the "$loan" is in HTML page, it will take the outputs from the above statement.
//The above code, which is in PHP.class file, outputs the following(or suppose to):
1 885775 Checking
2 885775 Saving
3 885775 Gift
In the HTML webpage, the following codes should check mark the boxes according to the outputs:
<input type="checkbox" name="DPS1[]" id="DPS1-{$key}" {if $loan-> downpaymenttype.downpaymentsource == "Checking"} checked {/if}/>
<input type="checkbox" name="DPS2[]" id="DPS2-{$key}" {if $loan-> downpaymenttype.downpaymentsource == "Saving"} checked {/if}/>
<input type="checkbox" name="DPS3[]" id="DPS3-{$key}" {if $loan-> downpaymenttype.downpaymentsource == "Gift"} checked {/if}/>
Only the "checking" checkbox is getting checked because that's the one on the first row.
I also tried to loop, but it is not storing in array (I guess I don't know how to use the array for session)
$q = ("SELECT Tabel_name FROM loan_downpaymentsource where loan_id = 885775");
$v = array($_ldid, $_lrid));
$r = $db->Execute($q, $v);
while(!$r->EOF) {
$this->downpaymenttype = $r->fields;
$r->MoveNext();
}
Add a loop to go through your results before displaying and set flags. $r->fields is only going to grab the fields for the current row. You need to go through your entire rowset to see if the others are set.
Something like (It looks like you're using adodb or something like that)
$checking = false;
$saving = false;
$gift = false;
while(!$r->EOF){
$row = $r->fields;
switch($row->downpaymenttype){
case 'Checking':
$checking=true;
break;
case 'Saving':
$saving=true;
break;
case 'Gift':
$gift=true;
break;
default:
break;
}
$r->moveNext();
}
Now on your view check the flags. I've not used smarty but you'll have to pass those flags to your template as well as I'm sure you know, and then change your checkbox lines to check if each flag is true/false.
Hope that helps!

checkbox status "checked" by default problem

I have a page with search form on it and table with search results below. In search form i have checkbox "Search in this category". What i'm doing to check it by default :
if(!isset($_SESSION['inthiscat'])){
$_SESSION['inthiscat'] = 'on' ;
$checked = 'checked';
}
$_GET['inthiscat'] = $_SESSION['inthiscat'];
checkbox code : INPUT type="checkbox" name="inthiscat"<?=$checked?>.
Link to next page of results index.php?inthiscat=$_GET['inthiscat'].
So the problem is when i uncheck "Search in this category" its still checked when i going to next page of results. How to fix it and what i'm doing wrong? Session startet of course.
Firstly, do you really need SESSION variables for this? If you want box to be checked when GET parameter is not specified, you do not need SESSIONs at all.
Assuming you want to preserve the behaviour in case someone removes the GET parameter:
<?php
session_start();
//......
//......
$checked='checked';
if(isset($_REQUEST['inthiscat'])) {
// Form input and url GET parameters take precedence
if($_REQUEST['inthiscat'] != 'checked') { $checked = ''; };
} else if(isset($_SESSION['inthiscat'])) {
// Next, use session variable if it exists
if($_SESSION['inthiscat'] != 'checked') { $checked = ''; };
};
$_SESSION['inthiscat']=$checked;
?>
Note:
1) Assigning values to GET array is not a good practice.
2) I assume you are using correct syntax for your FORM submit.
3) IMO, you could remove the SESSION variable as you are explicitly passing as GET parameter in the subsequent urls. Or dont use the GET parameter in urls.
Problem is: when you uncheck the checkbox and go to the next page, $_SESSION['inthiscat'] will still be unset - where did you change it?
Here is the code:
if (isset($_GET['inthiscat'])) {
$_SESSION['inthiscat'] = $_GET['inthiscat'];
}
if (!isset($_SESSION['inthiscat'])) {
$checked = 'checked';
} else {
if ($_SESSION['inthiscat'] == 'on') {
$checked = 'checked';
} else {
$cheked = '';
}
}
Assuming this HTML: <INPUT type="checkbox" name="inthiscat" checked="<?=$checked?>" value="on" />
So what it does is:
Looks for the GET data and, if there is, assigns it (can be 'on' or '') to the SESSION;
If there is no SESSION (that means, no GET as well) it's the first page of that kind the user visits, so checked;
If there is a SESSION for inthiscat, it means it's not the first page and GET data has been assigned to the SESSION. So, if it's on, it displays the mark; else, it does not.

Categories