A line of code is showing as well as the webpage - php

<?php
session_start();
include_once "incfiles/connectdb.php";
include_once "incfiles/func.php";
$page="gamepoints.php";
logincheck();
$username=$_SESSION['username'];
$fetch=mysql_fetch_object(mysql_query("SELECT * FROM accounts WHERE username='$username'"));
if ($_GET['item']){
$item=strip_tags($_GET['item']);
if ($item == "LHP"){ $cost="50"; $amount="10000"; }
elseif ($item == "FMJ"){ $cost="70"; $amount="15000"; }
elseif ($item == "rankpoints"){ $cost="150"; $amount="10000"; }
elseif ($item == "awp"){ $cost="300"; $amount="1"; }
elseif ($item == "credits"){ $cost="50"; $amount="10"; }
elseif ($item == "money"){ $cost="300"; $amount="5000000"; }
From if if ($item == "LHP") to the end is what shows up on my webpage along with the page and I can't seem to figure out why

Not sure if this is all of your code - if it is, looks like you might be missing the last }.
<?php
session_start();
include_once "incfiles/connectdb.php";
include_once "incfiles/func.php";
$page="gamepoints.php";
logincheck();
$username=$_SESSION['username'];
$fetch=mysql_fetch_object(mysql_query("SELECT * FROM accounts WHERE username='$username'"));
if ($_GET['item']){
$item=strip_tags($_GET['item']);
if ($item == "LHP"){ $cost="50"; $amount="10000"; }
elseif ($item == "FMJ"){ $cost="70"; $amount="15000"; }
elseif ($item == "rankpoints"){ $cost="150"; $amount="10000"; }
elseif ($item == "awp"){ $cost="300"; $amount="1"; }
elseif ($item == "credits"){ $cost="50"; $amount="10"; }
elseif ($item == "money"){ $cost="300"; $amount="5000000"; }
}

Related

Which is equivalent for PHP Include at CodeIgniter3?

Please explain to me how to do it:
<?php
if(!isset($_GET['s'])) {
$_GET['s'] = ""; }
if ($_GET['s'] == "") { include("Templates/Tutorial/1.tpl"); }
if ($_GET['s'] == "1") { include("Templates/Tutorial/1.tpl"); }
if ($_GET['s'] == "2") { include("Templates/Tutorial/2.tpl"); }
if ($_GET['s'] == "3") { include("Templates/Tutorial/3.tpl"); }
if ($_GET['s'] == "4") { include("Templates/Tutorial/4.tpl"); }
if ($_GET['s'] == "5") { include("Templates/Tutorial/5.tpl"); }
?>
Thank you very much!
I think this will also work
<?php
$inc = 1;
if(isset($_GET['s']) && $_GET['s'] != ""):
$inc = (int)$_GET['s'];
endif;
include("Templates/Tutorial/$inc.tpl");
?>

How to condense multiple IF statements simply in PHP

Here if my code. It works and runs and does everything I want it to do, but its too long. How can I condense it? My PHP level is basic.
Would I use an array? or a function? Can you kindly post the code you would use to make this more efficient?
<?php
$product = $result[CategoryId];
if ($product == "1") {
echo "Tonneau Cover to suit";
} elseif ($product == "2") {
echo "Clip On Tonneau Cover to suit";
} elseif ($product == "3") {
echo "INSERT";
} elseif ($product == "4") {
echo "INSERT";
} elseif ($product == "5") {
echo "INSERT";
} elseif ($product == "6") {
echo "INSERT";
} elseif ($product == "7") {
echo "INSERT";
} elseif ($product == "8") {
echo "INSERT";
} elseif ($product == "9") {
echo "INSERT";
} elseif ($product == "10") {
echo "INSERT";
} elseif ($product == "11") {
echo "INSERT";
} elseif ($product == "12") {
echo "INSERT";
} elseif ($product == "13") {
echo "INSERT";
} elseif ($product == "14") {
echo "INSERT";
} elseif ($product == "15") {
echo "INSERT";
} elseif ($product == "16") {
echo "INSERT";
} elseif ($product == "17") {
echo "INSERT";
} else {
echo ".";
}
?>
yes you can use array in this manner
$product = $result[CategoryId];
$ogh=["Tonneau Cover to suit","Clip On Tonneau Cover to suit","INSERT"];
if(count($ogh)>$product)
echo $ogh[$product];
else
echo "Invalid";
Based on what you have in the example:
<?php
$product = (int) $result['CategoryId'];
if ($product === 1) {
echo "Tonneau Cover to suit";
} elseif ($product === 2) {
echo "Clip On Tonneau Cover to suit";
} elseif ($product >=3 || $product <=17) {
echo "INSERT";
} else {
echo ".";
}
?>

Hide group if user is not logged in and all items in group require login

I have a list of items, sorted into groups. When the user is not logged in, I print only the items that don't require login.
$previous_group = '';
foreach ($arr as $item) {
if($previous_group != $item['group']) {
// Add dividers
if($previous_group != '') echo '</ul>';
echo '<h3>'.$item['group'].'</h3>';
echo '<ul>';
}
$previous_group = $item['group'];
if($item['login_required'] !== 'true' || ($item['login_required'] == 'true' && $isLoggedIn != false)) {
echo '<li>'.$item['title'].'</li>';
}
}
echo '</ul>';
PhpFiddle
How can I hide the header for a group that doesn't have any items because the user isn't logged in? For example, the "food" category in the PhpFiddle example.
I could just go through the array twice, but is there a more efficient way to do it?
try
$previous_group = '';
foreach ($arr as $item) {
if($previous_group != $item['group'] && ($item['login_required'] == 'false' || $isLoggedIn == 'true')) {
// Add dividers
if($previous_group != ''){echo '</ul>';}
echo '<h3>'.$item['group'].'</h3>';
echo '<ul>';
}
$previous_group = $item['group'];
if($item['login_required'] !== 'true' || ($item['login_required'] == 'true' && $isLoggedIn != false)) {
echo '<li>'.$item['title'].'</li>';
}
}
echo '</ul>';
If you want just one iteration:
$previous_group = '';
$group_content = '';
$last_key = end(array_keys($arr));
foreach ($arr as $key => $item) {
if (!$previous_group) $previous_group = $item['group'];
if ($key === $last_key && $previous_group == $item['group'] && ($item['login_required'] !== 'true' || ($item['login_required'] == 'true' && $isLoggedIn != false))) $group_content .= '<li>'.$item['title'].'</li>';
if ($group_content) {
echo '<h3>'.$item['group'].'</h3>';
echo '<ul>';
echo $group_content;
echo '</ul>';
}
$previous_group = $item['group'];
$group_content = '';
}
if($item['login_required'] !== 'true' || ($item['login_required'] == 'true' && $isLoggedIn != false)) {
$group_content .= '<li>'.$item['title'].'</li>';
}
}
However, doing more than one iteration will give you cleaner code.

PHP if/else statement issue

I'm having some issues with this statement,
<?php
$cert_mess = $vehicle['make'];
if ($vehicle["make"] == "Honda" && $vehicle["Certified"] == "0") {
$cert_mess = "DFWCertAutos";
}
elseif ($vehicle["make"] == "Cadillac" && $vehicle["Certified"] == "0") {
$cert_mess = "DFWCertAutos";
}
elseif (!in_array($vehicle['make'], array('Cadillac','Honda') )) {
$cert_mess = "DFWCertAutos";
}
?>
<div style="font-size:10px; padding:10px; padding-top: 0px;">*This car is <?php
echo $cert_mess ?> certified.</div>
Any suggestions? currently it just displays $cert_mess as 'make' and ignores the if / else if statements.
A simpler code can be the following:
$cert_mess = $vehicle['make'];
if (($vehicle["make"] == "Honda" && $vehicle["Certified"] == "0")
|| ($vehicle["make"] == "Cadillac" && $vehicle["Certified"] == "0")
|| !in_array($vehicle['make'], array('Cadillac','Honda'))
) {
$cert_mess = "DFWCertAutos";
}
Simpler still:
$cert_mess = $vehicle['make'];
if (!in_array($vehicle['make'], array('Cadillac', 'Honda')) || $vehicle['certified'] == '0')
{
$cert_mess = 'DFWCertAutos';
}

How to go to 'else if' loop after 'if' condition is sucessfully entered

Is there any way to go to the else if body after the if condition is sucessfully entered?
Is there any way to go to the else if body after the if condition is sucessfully entered?
if ($axisX == $axisXOne) { /* Main IF Statement */
if($axisY =='0' && $axisYOne == '1') { $second = '2';}
else if($axisY =='0' && $axisYOne == '2') { $second = '1';}
else if($axisY =='1' && $axisYOne == '0') { $second = '2';}
else if($axisY =='1' && $axisYOne == '2') { $second = '0';}
else if($axisY =='2' && $axisYOne == '0') { $second = '1';}
else if($axisY =='2' && $axisYOne == '1') { $second = '0';}
if (($_POST['button'.$axisX.$second] == null) && ($curVal != $axisX.$second)){ /* Inner IF Statement */
echo $axisX.$second;
}
}
else if ($axisY == $axisYOne) { /* Main ELSE IF statement */
if($axisX =='0' && $axisXOne == '1') { $second = '2';}
else if($axisX =='0' && $axisXOne == '2') { $second = '1';}
else if($axisX =='1' && $axisXOne == '0') { $second = '2';}
else if($axisX =='1' && $axisXOne == '2') { $second = '0';}
else if($axisX =='2' && $axisXOne == '0') { $second = '1';}
else if($axisX =='2' && $axisXOne == '1') { $second = '0';}
if($_POST['button'.$second.$axisY] == null) {/* Inner IF Statement */
echo $second.$axisY;
}
}
else if ($xAxis == $yAxis && $xAxisOne == $yAxisOne) { /* other Main ELSE IF Statement*/
if($comVal[0] == '00' && $comVal[1] == '11') { $diagon = '22';}
else if($comVal[0] == '00' && $comVal[1] == '22') { $diagon = '11';}
else if($comVal[0] == '11' && $comVal[1] == '00') { $diagon = '22';}
else if($comVal[0] == '11' && $comVal[1] == '22') { $diagon = '00';}
else if($comVal[0] == '22' && $comVal[1] == '00') { $diagon = '11';}
else if($comVal[0] == '22' && $comVal[1] == '11') { $diagon = '00';}
if($_POST['button'.$diagon] == null) {
echo $diagon;
}
}
If Main IF Statement evaluates true and Inner IF Statement evalautes false then go to Main ELSE IF ladder. If Main IF Statement evaluates true and Inner IF Statement evalautes true then stop loop . If Main IF Statement evaluates false, directly go to Main ELSE IF Statement
If Main ELSE IF Statement evaluates true and Inner IF Statement evalautes false then go to Main ELSE IF ladder. If Main ElSE IF Statement evaluates true and Inner IF Statement evalautes true ,then stop loop .If Main ELSE IF Statement evaluates false, directly go to other Main ELSE IF Statements
Try this:
$success = true;
if($a == $Xaxis && $b == $yaxis) {
$zAxis = $Xaxis + $yaxis;
$success = $Xaxis != $zAxis;
}
if( ($b == $Xaxis && $a == $yaxis) || !$success) {
// do stuff here
}
Or something along those lines.

Categories