Hiding output with 0 query value with php [duplicate] - php

This question already has answers here:
PHP check for not > 0
(3 answers)
Closed 1 year ago.
Can anyone help with this? I have a query that displays a value on the website but I would like to hide the output if it is 0 (zero). There are 2 fields, one called COI and the other AVK. They display as a percent such as COI: 12.00% AVK: 34.56%.
If the value is 0 then they display as COI: 0.00% and/or AVK 0.00%. I would like to not show this at all.
The code is specific and I can't find examples that would apply to this situation. Thought maybe an an else if but I just can't quite figure it out.
Appreciate any help!
UPDATE: Found a solution & have edited the code to show what was added. Hopefully this will help someone else if they have a similar issue.
echo "<span class='subjectdetails'>";
$nIndex = 11;
$nMarkIndex = -1;
foreach ($aExtraFields as $extraField)
{
$extraField = trim($extraField);
$subField = trim($details[$nIndex]);
if (($extraField == 'COI') and ($subField > 0)) <----- added this
$subField = sprintf("%.2f%%", $subField * 100);
if (($extraField == 'AVK') and ($subField > 0)) <----- added this
$subField = sprintf("%.2f%%", $subField * 100);
if (!empty($subField) && $extraField[0] != '_')// Don't print extra fields if they begin with underscore
if ((!empty($subField) && $extraField[0] != 'C') || $extraField == 'COI')
if ((!empty($subField) && $extraField[0] != 'A') || $extraField == 'AVK')
if (!empty($subField) && $extraField[0] != 'M')// Don't print extra fields if they begin with M
echo "$extraField: $subField<br/>" ;
if ($extraField == '_Marks')
$nMarkIndex = $nIndex;
$nIndex++;
}

You can just check $subfield with another if. If it's 0 then set $subfield to '';
if ($extraField == 'COI') {
if($subField) {
$subField = sprintf("%.2f%%", $subField * 100);
} else {
$subField = '';
}
}

Related

if isset statement is incorrect

I want to check if the reaction count is 0 then go in the if statement. The part without && ($app->count_reactie($topic['id']) == 0) does work but when I add this it won't work.
In the line above this code I use: implode($app->count_reactie($topic['id']))and it works so it is not that this code doesnt work. This code will return the number of reactions.
But this code down below is what I am trying to get to work.
if(isset($actiefboardid)){
$toppic = $app->get_topics($actiefboardid);
foreach($toppic as $topic){
if(isset($_SESSION['klant_id']) && ($_SESSION['klant_id'] == $topic['klant_id']) && ($app->count_reactie($topic['id']) == 0)){
}}}
Hopefully someone can help me.
Try this:
$count = $app->count_reactie($topic['id']);
if(isset($_SESSION['klant_id']) && ($_SESSION['klant_id'] == $topic['klant_id'])
&& ($count['COUNT(reactie)'] == 0)){

Yahtzee Php Code 3 of a Kind

if($_SESSION['valueofdie1'] != 0 && $_SESSION['valueofdie2'] != 0 && $_SESSION['valueofdie3'] != 0 && $_SESSION['valueofdie4'] != 0 && $_SESSION['valueofdie5'] != 0)
{
if((($_SESSION['valueofdie1'] == $_SESSION['valueofdie2']) && ($_SESSION['valueofdie2'] == $_SESSION['valueofdie3']||$_SESSION['valueofdie4']||$_SESSION['valueofdie5'])) || (($_SESSION['valueofdie1'] == $_SESSION['valueofdie3']) && ($_SESSION['valueofdie3'] == $_SESSION['valueofdie4']||$_SESSION['valueofdie5'])) || (($_SESSION['valueofdie1'] == $_SESSION['valueofdie4']) && ($_SESSION['valueofdie4'] == $_SESSION['valueofdie5']))
|| (($_SESSION['valueofdie2'] == $_SESSION['valueofdie3']) && ($_SESSION['valueofdie3'] == $_SESSION['valueofdie4']||$_SESSION['valueofdie5'])) || (($_SESSION['valueofdie2'] == $_SESSION['valueofdie4']) && ($_SESSION['valueofdie4'] == $_SESSION['valueofdie5']))
|| (($_SESSION['valueofdie3'] == $_SESSION['valueofdie4']) && ($_SESSION['valueofdie4'] == $_SESSION['valueofdie5'])))
{
if($_POST['choose'] == 'choose 3oaK')
{
$_SESSION['g'] = 5;
$_SESSION['scoretkind'] = $_SESSION['valueofdie1'] + $_SESSION['valueofdie2'] + $_SESSION['valueofdie3'] + $_SESSION['valueofdie4'] + $_SESSION['valueofdie5'];
unset($_SESSION['3oaKBut']);
echo '<input type="hidden" name="choose" value="Clear" onLoad="form.submit();">';
$_POST['sub'] = 'reset';
$_POST['choose'] = '';
}
if(empty($_SESSION['g']))
{
$_SESSION['3oaKBut'] = '<input type="submit" name="choose" value="choose 3oaK">';
echo $_SESSION['3oaKBut'];
}
}
}
if($_SESSION['g'] == 5)
{
echo $_SESSION['scoretkind'];
}
So here is the code we have. We are trying to check if 3 of the 5 die values are equal. If they are equal we echo out a button that allows the user to choose to score his 3 of a kind, which is the total of all of the dice. Everything works except in some cases the 3 of a kind button would echo out when there isnt a 3 of a kind. Halp PLS
I'm sorry I didn't answer your question by actually solving your bug, but I think your code is hard to read and your approach makes it cumbersome to program all the rules.
General advice: Put $_SESSION['valueofdie1'] and the other dice into an array of values. That's much easier to work with. After that, it should be pretty easy to check how many times each value occurs. Even when you keep your approach, you could make variables like $die1, which is already a lot shorter and more readable than $_SESSION['valueofdie1'].
But with an array, you could roughly start like this:
// Put all dice into an array.
$dice = array(
$_SESSION['valueofdie1'],
$_SESSION['valueofdie2'],
etc.... );
// Count how many times each die is rolled.
$diceCount = array();
foreach($dice as $die) {
$count = 0;
if (isset($diceCount[$die])) {
$count = $diceCount[$die];
}
$diceCount[$die] = $count + 1;
}
// Check possible results simply by looking at those counts.
// If one die value is rolled 5 times, it's Yahtzee...
if (array_search(5, $diceCount) !== false) {
echo 'Yahtzee!';
}
if (array_search(4, $diceCount) !== false) {
echo 'Four of a kind';
}
// Full house takes two types.
if (array_search(3, $diceCount) !== false && array_search(2, $diceCount) !== false) {
echo 'Full house';
}
for ($diceCount as $die => $count) {
echo "$count times $die";
}
... etc ...
You'll need to expand this list, and take some other rules into account. After all, a Yahtzee could also count as a Four of a Kind. But by checking all those rules, you can generate a new array of possible combinations, which you can check against the previously chosen options. And the outcome of that determines which options the player can choose.

Conditionals operation helps in php

Hello everybody i put this code but and when the value referal is < 0 running }else{ but i dont do.
if($referal > 0 && is_numeric($referal) && $site['refsys'] == 1 && $site['aff_click_req'] == 0){
//code
if($user['id'] > 0){
//code
}
}
}
I want to put }else{ but not how, }else{ $referal="$referal2"; ????
if($referal > 0 && is_numeric($referal) && $site['refsys'] == 1 && $site['aff_click_req'] == 0){
//code
if($user['id'] > 0){
//code
}
}}else{ $referal="$referal2";
}
Regards and thanks
First of all this the if elseif statement syntax.
if($condition1){
// code if the first condition is true
}elseif($condition2){
// code if the first condition is false and the second one is true
}else{
// code if both the first and second condition are false
}
it is described in the php official website
If I have understood your question you want, when the $referal1 is equal to 0, to set the value of $referal2 to $referal1.
In this case your code should be something like this
if($referal > 0 && is_numeric($referal) && $site['refsys'] == 1 && $site['aff_click_req'] == 0){
//code
if($user['id'] > 0){
//code
}
}elseif($referal==0){
$referal=$referal2;
}
Hope this will solve your problem.

php loop display result if isset and matches the given value

I am sorry to ask such a question but am bit confused about this.
I am having simple variables defined.
$a =1;
$b=2;
$c=3;
$d="";
for($i=0;$i<10;$i++)
{
$testa = 1;
$testb = 4;
$testc = 3;
$testd = 7;
if($a!="" || $b!="" || $c!="" || $d!="") {
if($a==$testa && $b==$testb && $c==$testc && $d==$testd) {
echo $testa;
echo $testb;
echo $testc;
echo $testd;
}
}
}
This is sample php code.
what I need is that I have variables defined at top. SO in my loop i want to display result if user has any 1 variable but in below loop display result based on "and" parameter.
I actually want to skip the empty variable. SO in this case, I want as $d is empty, so it should be prevented somehow from if($a==$testa && $b==$testb && $c==$testc && $d==$testd) from here.
Any help is really appreciated.
To skip the empty values from the check, use ||
if ( ... && (empty($d) || $d == $testd)) {
Then if you also want to skip it in your echos :
echo !empty($d) ? $testd : '';

Idea with php logic using operators

I am new to PHP and find it very hard to explain.
I have a PHP navigation script with 5 categories and two variables relevant to my question:
$catname = 'used-cars'; // the same value for all categories
$currentpage; // pages 1 to 5
index.php of my site has $currentpage == '1'
The issue is that I need a logic that will say:
If $catname IS NOT 'used-cars', do something, BUT, IF $currentpage is equal to 1, even if $catname is 'used-cats' do it anyway
I am thinking of something like this:
if($catname != 'used-cars' && !($currentpage > '1')):
endif;
Hope you can help!
This is merely a single or condition. On the right side, $currentpage === 1 will evaluate to TRUE without regard to the value of $catname. If either part of the condition is TRUE, you'll enter the if () block to execute your code there.
if ($catname !== "used-cars" || $currentpage === 1) {
// do your thing
}
This is just:
if (strcmp($catname, 'used-cars') != 0 || $currentpage == 1)
(Careful with the string comparison.)
Alternatively, you could declare it as a boolean first:
$proceed = false;
if($catname != 'used-cars')
$proceed = true;
if($currentpage == 1)
$proceed = true;
if($proceed){
// whatever you want
}
$doflag = 0;
if($catname != 'used-cars')
{
$doflag = 1;
} else if($currentpage == 1) {
$doflag = 1;
}
if($doflag == 1) {
//do something
}
Basically instead of trying to do everything with the block, use the block to set a flag and use the flag to do something.

Categories