PHP substitute, "If $something = "0" then $something1 = "no"" - php

So basically what am trying to achieve is the following.
I am trying to make it so the following script does something in this instance:
If $something == "0" then $something1 == "no"
If $something == "1" then $something1 == "yes"
else echo "Error."
That is how I would explain what Im trying to do.
This is my current code:
<?php
if(isset($_POST['resolve'])){
$api = "http://test.com/php/";
if(strlen($_POST['name'])==0){
echo "fill in all fields!";
} else {
$response = file_get_contents($api.$_POST['name']);
$array = unserialize($response);
?>
<div align="center"><?php echo "".$array['something1']; ?></div>
<?php
}
}
?>
I would like it to echo "no" if the result of array "something" is "0" and echo "yes" if the result of array "something" is "1".

<?php
if($array['something'] == '0'){echo 'No';}
elseif($array['something'] == '1'){ echo 'Yes';}
else{ echo 'Error!'; }
?>

switch case is the most elegant way to go here:
switch($array['something']) {
case 0: echo 'No';break;
case 1: echo 'Yes';break;
default: echo 'Error.';
}

<?php
if(isset($_POST['resolve'])) {
$api = "http://test.com/php/";
if(!$_POST['name']) {
echo "Please, fill in all fields!";
} else {
$response = file_get_contents($api.$_POST['name']);
$array = unserialize($response);
echo "<div align='center'>";
if($array['something'] == '0') {
echo 'No';
}
elseif($array['something'] == '1') {
echo 'Yes';
}
else {
echo 'Error.';
}
echo "</div>";
}
}
?>
Don't forget to also do a security input check on $_POST['name']

Voila
echo $array['something1'] ? "Yes" : "No";

This sets $array['something1'] to either 'yes' or 'no' depending on the value of $array['something'].
<?php
if(isset($_POST['resolve'])){
$api = "http://test.com/php/";
if(strlen($_POST['name'])==0){
echo "fill in all fields!";
} else {
$response = file_get_contents($api.$_POST['name']);
$array = unserialize($response);
$array['something1'] = $array['something'] == 0 ? 'no' : 'yes';
?>
<div align="center"><?php echo "".$array['something1']; ?></div>
<?php
}
}

$yesno = ['No', 'Yes'];
$something1 = $yesno[$something];
That is the simplest way I know of doing it.

Related

If-else condition gives me errors in PHP

I have two websites, each with it's own domain name. On load I have execute a php file that is used on both sites and has the same functionality.
Here is the code of that file:
<?php
echo '1';
if ($_SESSION["template"]=="template1";)
{
echo '2';
if ($_REQUEST["cmdAction"]=='A')
echo file_get_contents('http://localhost/images/template1/a.php');
else if ($_REQUEST["cmdAction"]=='B')
echo file_get_contents('http://localhost/images/template1/b.php');
else if ($_REQUEST["cmdAction"]=='C')
echo file_get_contents('http://localhost/images/template1/c.php');
}
else if ($_SESSION["template"]=="template2";)
{
echo '3';
if ($_REQUEST["cmdAction"]=='A')
echo file_get_contents('http://localhost/images/template2/a.php');
else if ($_REQUEST["cmdAction"]=='B')
echo file_get_contents('http://localhost/images/template2/b.php');
else if ($_REQUEST["cmdAction"]=='C')
echo file_get_contents('http://localhost/images/template2/c.php');
}
else {
echo 'NO DATA';
}
echo '4';
?>
On each of the two sites I set a session variable but in the above code it doesn't seem to work as I expect it to.
Am i missing something?
remove semicolon from if() and else if() statement, also add brackets when you using nested if else because it makes someone to understand easier and looks better
<?php
echo '1';
if ($_SESSION["template"]=="template1")
{
echo '2';
if ($_REQUEST["cmdAction"]=='A')
{
echo file_get_contents('http://localhost/images/template1/a.php');
}
else if ($_REQUEST["cmdAction"]=='B')
{
echo file_get_contents('http://localhost/images/template1/b.php');
}
else if { ($_REQUEST["cmdAction"]=='C')
{
echo file_get_contents('http://localhost/images/template1/c.php');
}
}
else if ($_SESSION["template"]=="template2")
{
echo '3';
if ($_REQUEST["cmdAction"]=='A')
{
echo file_get_contents('http://localhost/images/template2/a.php');
}
else if ($_REQUEST["cmdAction"]=='B')
{
echo file_get_contents('http://localhost/images/template2/b.php');
}
else if ($_REQUEST["cmdAction"]=='C')
{
echo file_get_contents('http://localhost/images/template2/c.php');
}
}
else {
echo 'NO DATA';
}
echo '4';
?>
After reviewing your code I edited my answer. The below code will do exactly the same as your code but requires alot less code.
<?php
if (isset($_SESSION['template']) && isset($_REQUEST['cmdAction'])) {
echo file_get_contents('http://localhost/images/'.$_SESSION['template'].'/'.strtolower($_REQUEST['cmdAction']).'.php');
} else {
echo 'NO DATA';
}
?>

PHP if - else - else statement

<html>
<input type='text' name='mobile phone' value='
<?php if (strpos($phone_number, '07') === 0) {
echo $phone_number;
} else {
echo $alt_phone;
}?>'
</html>
Works fine. I would like to combine the above with:
<?php if (!empty($alt_phone)) {
echo $alt_phone;
} else {
echo '07777777777';
}?>'`
I have tried ELSEIF with the new condition, and a completely separate <?php ?> section and both times I get a blank page, instead of a textbox with a telephone number in it.
I am trying to achieve this: If $phone_number is a mobile, enter this number, otherwise enter the alt_phone, unless $alt_phone is blank, then enter '07777777777'.
try
<?php
if (!empty($phone_number)) {
echo $phone_number;
}
elseif(!empty($alt_phone))
{
echo $alt_phone;
}
else{
echo '07777777777';
}
?>'`
This will do the trick
<?php
if (strpos($phone_number, '07') === 0) {
echo $phone_number;
}
else if (!empty($alt_phone)) {
echo $alt_phone;
}
else {
echo '07777777777';
}
?>

Assigning a table to a session in PHP

The following validates only one text box (txt1). If I have a series of text boxes which lies in side a table (which is dynamically populated) how do I assign that entire values of the text boxes to one single session?
Is it possible if at all?
Or else can you suggest me a way out?
<?php
if(isset($_POST['sendtwo']))
{ if($_POST['txt1'] == '')
{echo 'txt1 is empty!'; return;} else {$_SESSION['loged'] = $_POST['txt1'];}
}
if(isset($_POST['sendt']) || isset($_POST['sendtwo']))
{if(isset($_SESSION['loged'])) echo $_SESSION['loged']; echo "<table border='1'><tr><td>form 2 is here!</rd></tr>";
?>
//try this
<?php
if(isset($_POST['sendtwo']))
{ if($_POST['txt1'] == '')
{echo 'txt1 is empty!';} else {$_SESSION['loged'] = $_POST['txt1'];}
}
if(isset($_POST['sendt']) || isset($_POST['sendtwo']))
{
if(isset($_SESSION['loged'])) {
echo $_SESSION['loged'];
echo " border='1'>form 2 is here!";
}
}
?>
Something like this? I wasn't sure what you meant:
<?php
if(isset($_POST['sendtwo']))
{
if($_POST['txt1'] == '')
{echo 'txt1 is empty!'; return;}
else{
foreach($_POST as $Key => $Posted)
$_SESSION[$Key] = $Posted;
}
}
if(isset($_POST['sendt']) || isset($_POST['sendtwo']))
{if(isset($_SESSION['loged'])) echo $_SESSION['loged']; echo "<table border='1'><tr><td>form 2 is here!</rd></tr>";
?>

PHP custom error issue

Currently, I have this code:
<?php
if (isset($_GET['s'])) {
$itemid = $_GET['s'];
$search = "$itemid";
$query = ucwords($search);
echo "<title>Results for $query</title>";
$string = file_get_contents('http://clubpenguincheatsnow.com/tools/newitemdatabase/items.php');
if ($itemid == "") {
echo "Please fill out the form.";
} else {
echo '<div id="content">';
$string = explode('<br>', $string);
foreach ($string as $row) {
preg_match('/^(.+)\s=\s(\d+)\s=\s(\D+)\s=\s(\d+)/', trim($row), $matches);
if (preg_match("/$query/i", "$matches[1]")) {
echo "<a href='http://clubpenguincheatsnow.com/tools/newitemdatabase/info.php?id=$matches[2]'>";
echo $matches[1];
echo "</a><br>";
}
}
echo '</div>';
}
} else {
echo "Item does not exist!";
}
?>
If "$matches[1]" has nothing in it, I want my code it to echo "Item does not exist!" How do I do this though? Please help!
I've tried something like if ($matches[1]=="") { echo "Item does not exist!"; } before, but it didn't work. This is what I got:
http://img685.imageshack.us/img685/998/28990b2c12d0423292d3574.png
See works fine right? Look what happens if $matches[1] DOES exist:
http://img528.imageshack.us/img528/3690/71472c9de6ec49118ee8d48.png
It still comes up! How can I make my code so it only echos the error if there is nothing for $matches[1]? PLEASE HELP ME!
If you are wondering, this is my code when I added the if ($matches[1]=="") { echo "Item does not exist!"; } in:
<?php
if (isset($_GET['s'])) {
$itemid = $_GET['s'];
$search = "$itemid";
$query = ucwords($search);
echo "<title>Results for $query</title>";
$string = file_get_contents('http://clubpenguincheatsnow.com/tools/newitemdatabase/items.php');
if ($itemid == "") {
echo "Please fill out the form.";
} else {
echo '<div id="content">';
$string = explode('<br>', $string);
foreach ($string as $row) {
preg_match('/^(.+)\s=\s(\d+)\s=\s(\D+)\s=\s(\d+)/', trim($row), $matches);
if (preg_match("/$query/i", "$matches[1]")) {
echo "<a href='http://clubpenguincheatsnow.com/tools/newitemdatabase/info.php?id=$matches[2]'>";
echo $matches[1];
echo "</a><br>";
}
}
echo '</div>';
if ($matches[1] == "") {
echo "Item does not exist!";
}
}
} else {
echo "Item does not exist!";
}
?>
Any help to my question would be VERY HIGHLY APPRECIATED!
Look at PHP's comparison operators
if (empty($matches[1])) { echo "Item does not exist!"; }
else {
echo "Item does not exist!";
}
This "else" is in regards to isset($_GET['s']). Is there a variable called s in your query string? If there isn't, then it's normal that you get the message.
Maybe you put that else block in the wrong place?

Puzzling around with Get Page ID

Please have a quick look at the code below:
<?php
$pagexfoot = $_GET[page_id];
?>
<?php
if ($pagexfoot == '5' OR !isset($_GET['page_id'])) {
echo 'Hello';
} else {
echo 'Bye';
}
?>
So, if the user is on index.php?page_id=5 then it will echo "Hello" and it will echo "Bye" anywhere else. Now, how do I echo "Hello" on page index.php?page_id=5 and index.php and echo "Bye" on all other pages? Who can solve this puzzle...
<?php
if(isset($_GET['page_id']) && $_GET['page_id'] != 5)
{
echo 'Bye';
}
else
{
echo 'Hello';
}
?>
<?php
if (!isset($_GET['page_id']) || $_GET['page_id'] == 5) {
echo 'Hello';
} else {
echo 'Bye';
}
We are using the || operator to check if either it isn't set or the value is 5, if so tell "Hello" and else, Bye.
$pagexfoot = $_GET[page_id];
if($pagexfoot != '5' || isset($_GET['page_id'])) { echo 'Bye'; } elseif($pagexfoot == '5') { echo 'Hello'; }
You code loks fine to me by try the above code :)

Categories