I'm trying to pull in the parameter of a URL and use that to determine what information to display on page, but for some reason the information is being read wrong. The first thing I do is check for the parameter below and assign it to $page
<?php
if(isset($_GET["page"])) {
$page=$_GET["page"];
}
?>
I then check if the $page is equal to 2 or 3. For some reason, if I echo out $page, I get the proper value of the parameter but it displays incorrect info.
<?php
if(isset($page) == '2') { ?>
DISPLAY INFO A
ECHO $PAGE RETURNS 2
<?php } elseif(isset($page) == '3') { ?>
DISPLAY INFO B
ECHO $PAGE RETURNS 3
<?php } else { something here } ?>
For some reason, even though $page returns 3, I receive INFO A that's supposed to be displayed on page 2. Am I pulling the parameter wrong? The URL Looks like this:
feed.php?page=3
php isset function return Boolean.
You should change code to:
<?php
if(isset($page) && $page== '2') {
?>
DISPLAY INFO A
ECHO $PAGE RETURNS 2
<?php } elseif(isset($page) && $page== '3') { ?>
DISPLAY INFO B
ECHO $PAGE RETURNS 3
<?php } else { something here } ?>
This is wrong:
if(isset($page) == '2') {
It should be
if( $page == '2') {
This will only seem to work on page 1, because isset($page) returns true, which truthy gets converted to 1. The isset() function is only to check if the variable has been set or not
if($page == '3')
Why the isset()? You already do that when you assign it. Maybe this as well:
if(isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page = '0'; // or something
}
isset() return a TRUE/FALSE, yet you're comparing it against normal integers. Boolean TRUE in mysql is equivalent to integer 1, but will fail the rest of your tests. You need to have:
if (isset($_GET['page'])) {
if ($_GET['page'] == 1) { ... 1 stuff }
else if ($_GET['page'] == 2) { ... 2 stuff }
}
Related
<?php if (isset($_GET['p'])) { $variable = $_GET['p']; ?>
<?php if ($variable == '1'): ?>
<?php include('pages/home.php');?>
<?php endif;?>
<?php };?>
<?php if (isset($_GET['p'])) { $variable = $_GET['p']; ?>
<?php if ($variable == '2'):
include('pages/about.php');
else: include('pages/home.php'); endif; };?>
The above is what I have used to try and fix it, but if I don't put it, errors show up. For example if I used "ifelse", and it tells me to change it to else, or endif. But when I use endif, it tells me to use ifelse. I'm trying to make it so http://localhost/?p=PAGE_ID just simply shows the page using an include(FILE_PATH) statement.
Does anyone know where I went wrong? Or how I can fix it :)
You seem to have thoroughly tied yourself in knots. To my mind, this is a much cleanrer and more flexible and maintainable way to approach the whole thing:
$pages = [
"1" => "pages/home.php",
"2" => "pages/about.php"
];
if (isset($_GET['p'])) {
if (isset($pages[$_GET['p']]))
{
$page = $pages[$_GET['p']];
}
else
{
$page = $pages["1"];
}
}
else
{
$page = $pages["1"];
}
include($page);
This way, you have a list of pages, and the system simply looks up the required page in the array by its index number (as passed in via the GET parameter). If it can't find that index, it just uses the default page.
You can now easily add pages by just adding items to the array, without needing to write more if/else logic. You could even store the array data in a database instead of it being hard-coded.
You dont need or want an elseif in this flow, simple do
<?php
if (isset($_GET['p'])) {
if ($_GET['p'] == '1') {
include('pages/home.php');
}
if ($_GET['p'] == '2') {
include('pages/about.php');
}
}
If the value of $_GET['p'] can only be 1 or 2 you could do
<?php
if (isset($_GET['p'])) {
if ($_GET['p'] == '1') {
include('pages/home.php');
} else {
include('pages/about.php');
}
}
Or if $_GET['p'] could be multiple values a switch may be more useful
<?php
if (isset($_GET['p'])) {
switch($_GET['p']) {
case 1:
include('pages/home.php');
break;
case 2:
include('pages/about.php');
break;
default:
include('pages/somethingelse.php');
}
}
Note I also left out the intermediate variable, as its not needed. The $_GET and $_POST arrays are, once filled by PHP all yours to use as you want. There is no need to waste CPU cycles and memory creating unnecessary variables
I have this code: it works fine
<?php
if(!isset($_GET['id']) || $_GET['id'] != '1000'){
header('Location: http://website.com?id=1000');
exit;
}
?>
what i need is to add a second value like this
<?php
if(!isset($_GET['id']) || $_GET['id'] != '1000' or $_GET['id'] !='2000'){
header('Location: http://website.com?id=1000');
exit;
}
?>
Use in_array and add all the values you want to check against into the array:
if(!isset($_GET['id']) || !in_array($_GET['id'], array('1000', '2000'))){
echo 'not 1k or 2 k';
} else {
echo 'is 1k or 2k';
}
Functional example: https://3v4l.org/Q2uao
Divide your condition in to two parts.
First use the !isset condition and then
Put the rest of condition inside if condition
Your Code Look like this :
if(!isset($_GET['id'])){
$id = $_GET['id'];
if($id == 1000 || $id == 2000) {
// Action
}
}
I'm trying to write a statment that looks at the value in a boolean field (field_solo) and returns one of two template files that I have created in Drupal 7.
My field "field_solo" is correctly outputting a value of 0 or 1 and I have cleared the cache.
Can someone tell me if I am doing this correctly? Right now I am not getting it to display when the statement is TRUE.
function motg_preprocess_node(&$vars) {
$node = $vars['node'];
if($node->field_solo[0]['value'] == 1)
{
$vars['theme_hook_suggestion'] = 'node__solo';
} else
{
$vars['theme_hook_suggestion'] = 'node__video';
}
}
Instead of
if($node->field_solo[0]['value'] == 1)
Make it
if($node->field_solo['und'][0]['value'] == 1)
// OR
if($node->field_solo[LANGUAGE_NONE][0]['value'] == 1)
Have a look at this it could be helpful:
http://www.computerminds.co.uk/articles/rendering-drupal-7-fields-right-way
This worked for me
<?php if ($content["field_hide_title"]["#items"][0]["value"] == 0) { ?>
<?php echo $node->title; ?></h2>
<?php } ?>
I have a test.php and i have the below code
<?php
if(isset($_GET['p']) or $_GET['p'] != null) {
echo $_GET['p'];
} else {
echo "Not found";
}
?>
I have listed out below urls then required output are show
Test 1 : http://localhost/example/test.php
output : Notice: Undefined index: p in R:\xampp\htdocs\example\test.php on line 3
Not found
Test 2 : http://localhost/example/test.php?p
output : blank page
Test 3 : http://localhost/example/test.php?p=
output : blank page
Test 4 : http://localhost/example/test.php?p=1
output : 1
I accept that Test 1 and Test 2 are true
But when Test 2 and Test 3 fails out the solution.
<?php
if(!empty($_GET['p']))
echo $_GET['p'];
else
echo "Not found";
?>
if(isset($_GET['p']) and $_GET['p'] != null) {
echo $_GET['p'];
} else {
echo "Not found";
}
you need and as you want to check that it is set and is not null.
Like others have pointed out - you could use empty() :
if(!empty($_GET['p'])) {
echo $_GET['p'];
} else {
echo "Not found";
}
This will return true when the value is empty, the following is considered empty :
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)
<?php
if(isset($_GET['p']) and $_GET['p'] != null) {
echo $_GET['p'];
} else {
echo "Not found";
}
?>
The above code you mentioned as or so it will take any one of the following as true
try with and
<?php
if(isset($_GET['p']) and $_GET['p'] != null) {
echo $_GET['p'];
} else {
echo "Not found";
}
?>
And it will work perfect.
You are using an OR, so the second test will ALWAYS run. Meaning, if it's not set it will still try access it. You can use an AND instead (&&) so it will only check the value if it exists.
You should use empty() instead of isset().
<?php
if(empty($_GET['p'])) {
echo "Not found";
} else {
echo $_GET['p'];
}
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.