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';
}
?>
Related
I have a simple PHP code, as below.
When I try the URL localhost/df.php?result1=bharat, I get the result Bharat, exactly as I want it. But when I try the URL localhost/df.php?result2=bharat, I get an error, meaning my result2 variable was not read like my result1 variable did.
Could you please correct my code so that it works?
<?php
if(isset($_GET['Result1']))
{
$file = $_GET['Result1'];
}
else
{
echo "Error"; exit;
}
echo "$result1";
?>
elseif(isset($_GET['Result2']))
{
$file = $_GET['Result2'];
}
else
{
echo "Error"; exit;
}
echo "$result2";
?>
You have way too many errors in your code. The following is the solution to your problem:
<?php
if(isset($_GET['result1']))
{
$result1 = $_GET['result1'];
echo $result1;
}
elseif(isset($_GET['result2']))
{
$result2 = $_GET['result2'];
echo $result2;
}
else
{
echo "Error";
exit();
}
?>
For the future, I would recommend you to learn PHP and be familiar with the basic syntax, at least, before posting questions about it here.
<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';
}
?>
I was wondering if it's possible to have an if statement within an echo.
I have if statement which works fine when echoing results through the a while loop... This is the statement:
<div><?php if ($row['image'] == '') {}
else {echo "<img src='data:image/jpeg;base64,".base64_encode($row['image'])."'>";} ?>
<?php if ($row['video'] == '') {}
else {echo "<iframe src={$row['video']}></iframe>";} ?></div>`
So basically it's either a video or an image which works fine but then I implemented an infinite scroll to my blog which echoes the data from the database through and if statement like so:
if ($results) {
while($obj = $results->fetch_object())
{
echo '
<div><h3>'.$obj->headline.'</h3> </div>
<div><img src='data:image/jpeg;base64,".base64_encode('.$obj->image.')."'></div>'
So I wondering if anyone knows if it's possible to transfer that if statement within this echo so that it display an image firstly and then knows whether one is present or when a video is present within the database.
Thanks in advance for any help.
PS: I'm very new to coding/php!
Of course. Just split up the echo into multiple statements:
while($row = $results->fetch_object()) {
echo '<div>';
if ($row['image'] == '') {
} else {
echo "<img src='data:image/jpeg;base64,".base64_encode($row['image'])."'>";
}
if ($row['video'] == '') {
} else {
echo "<iframe src={$row['video']}></iframe>";
}
echo '</div>';
}
Try this one.
//first initialize a variable as a string
$result="";
while($obj = $results->fetch_object()) {
$result.="<div>";
if (!empty($obj['image'])){
$result.="<img src='data:image/jpeg;base64,".base64_encode($obj['image'])."'>";
}
elseif (!empty($obj['video'])){
$result.="<iframe src={$obj['video']}></iframe>";
}else{
//show some notification or leave it
//echo 'not Found';
}
$result.="</div>";
}
//finally you need to print the result variable.
echo $result;
for($c=1;$c<=$num;$c++)
{
$row=mysql_fetch_array(mysql_query("SELECT * FROM `$quiztitle` WHERE id=$c"));
if($row['answer']==$_POST['answer'][$c]) // NOT WORKING
{
echo "correct";
echo "<br>";
}
else
{
echo "incorrect";
echo "<br>";
}
}
on the line where it says "NOT WORKING",
the index [$c] does not get the value from the loop.
but when i specify it and change it to $_POST['answer1'], it is working.
what is the correct syntax for this?
Try this.
for($c=1;$c<=$num;$c++)
{
$row=mysql_fetch_array(mysql_query("SELECT * FROM `$quiztitle` WHERE id=$c"));
if($row['answer']==$_POST['answer'.$c]) // NOT WORKING
{
echo "correct";
echo "<br>";
}
else
{
echo "incorrect";
echo "<br>";
}
}
You're treating 'answer' as an array here, looking for an index within.
You want to concatenate the value.
if($row['answer']==$_POST["answer{$c}'])
Based on your note, it looks like you want:
$_POST["answer$c"]
Correct is:
if ($row['answer'] == $_POST['answer' . $c]) {
....
}
I cannot get my PHP script to echo the second else statement if the first result empty.
The way my script currently works is "Print Addresses (from another array) > List Comments", however even if a comment is empty for an address is will either print the comments or nothing, I cannot make the script echo the word "No comment".
if(!empty($row['id']))
{
echo "$row[comment]<br/>";
}
else
{
echo "no comment<br/>";
}
Any help appreciated. Thanks.
Assuming this comes from a database and each row has an id, this will always be true:
if(!empty($row['id']))
Try:
if(!empty($row['comment']))
If id is something else, the same logic applies: check the value that you intend to print:
if (!empty($row['id']) && !empty($row['comment']))
{
echo $row['comment'].'<br/>';
}
else
{
echo "no comment<br/>";
}
EDIT: If this code is looping through all comments attached to a post or something, there will never be any output if there are no comments to loop through. In that case try something like this:
if (count($comments) === 0)
{
echo "no comments<br />";
}
else
{
foreach ($comments as $row)
{
if (!empty($row['comment']))
{
echo $row['comment'].'<br />';
}
else
{
echo "no comment<br />";
}
}
}
OR:
$comment_count = 0;
foreach ($comments as $row)
{
if (!empty($row['comment']))
{
echo $row['comment'].'<br />';
$comment_count++; // We have at least one comment
}
else
{
echo "no comment<br />";
}
}
if ($comment_count === 0) echo 'no comments<br />';