PHP If and else statement - - php

I am pulling images and information from a mySQL database and displaying with a few PHP functions. At times all of the information isn't there and I need it to basically display:none; but I can't seem to get it - What am I missing? Here is my display function:
<?php if ($recipe->hassliderimage5 == true) {
$recipe->show_image_carousel5();
} else {
}
?>
And here is the PHP function calling it from the database -
if (trim(mysql_result($this->result,0,"imageCarousel5") != '')) {$this->hassliderimage5 = true;} else {$this->hassliderimage5 = false;}
Here is what I got to work for what I wanted - not sure if it is the best solution or not? I'm still kind of new to PHP.
<?php if ($recipe->hassliderimage5 == true) { ?>
<div id="sliderimageFive" class="item">
<?php
$recipe->show_image_carousel5();
?>
</div>
<?php } ?>

You seem to make the mistake to create your view before collecting the data that has to be displayed. A more logical application structure would be to fetch the data from the database, validate it and then create your view according to the amount and type of data you have.
Also, take a look at this: Why shouldn't I use mysql_* functions in PHP?

I can't comment cause i have not enough reputation, so i try it with an answer.
why you are using the trim on mysql_result($this->result,0,"imageCarousel5") != '' this code will give you either true or false no string you have to trim
Maybe you want to trim the return of mysql_result and then check if its empty
if (trim(mysql_result($this->result,0,"imageCarousel5")) != '') {$this->hassliderimage5 = true;} else {$this->hassliderimage5 = false;}

Try this test:
<?php if (filter_var(recipe->hassliderimage5, FILTER_VALIDATE_BOOLEAN) == true) {
$recipe->show_image_carousel5();
} else {
// do other
}
?>
if return true if the value of 'recipe->hassliderimage5' is "1", "true", "on" and "yes"
Enjoy your code!

Related

Display text if statement matches

I am a beginner when it comes to php, and I have encountered a problem that I cannot find the solution for. I have tried searching for a relevant answer but I haven't found one.
I have the following code in my index.php:
<?php if ($set_status = 2) {
echo 'There is one or more errors';
} else {
echo '';
}
?>
and this a bit further down:
<?php include 'scan/a.php'; ?>
And inside my a.php I have the following code:
<?php
$a = file_get_contents
('http://www.a-random-website/text.html', NULL, NULL, 2, 1);
if ($a == "0") {
include 'fail.php';
} elseif ($a == "1") {
include 'success.php';
} else {
echo 'Offline';
}
?>
And inside my fail.php I have the following code:
<?php
$set_status = 2;
echo 'Failed';
?>
So the idea here is that "a.php" will fetch a number from the website (The correct website has either ["1"] or ["0"] displayed that the code will fetch).
Depending on the result it returns, a.php will include either "fail.php" or "success.php", each containing either a success or a fail-message. If file_get_contents return a 0 I also want fail.php to $set_status = 2; which will cause "There is one or more errors" to be displayed on the front-page (index.php).
The reason that I am using include is that there's going to be a "b.php" and "c.php" and "d.php" and so on, all doing the same thing but fetching data from different pages. I want the success or fail-message to remain easy to edit, without having to edit each and every new x.php file.
So here's where it gets problematic. Everything works beautifully, except for the "There is one or more errors"-message that's supposed to trigger if ($set_status = 2).
I can get as far as the message showing, but when I switch the 1 and 0 in "a.php" (To simulate a specific result) the message will still show. I can't seem to figure it out.
So my question is: What have I done wrong, and what is the correct way to do it?
Thank you in advance!
Best regards,
Marc
use == operator in index.php to compare
<?php if ($set_status == 2) {
echo 'There is one or more errors';
} else {
echo '';
}
?>
<?php if ($set_status ==2) {
echo 'There\'s one or more errors';
} else {
echo '';
}
?>
Just change this code ... see slash\ and == in the echo line .. else of your code id fine

Need help making an if statement in php that creates a html href link when passed correctly

<?php if ($_SESSION["isLoggedOn"] == True) {?>
LOGOUT
<?php }?>
i want the link to only be created if the IF statement is successful and to not create it if it is not. At the moment no matter what i have tried to change the link will always be created. thanks.
Have you tried
<?php
if ($_SESSION["isLoggedOn"] == True)
{
echo 'LOGOUT';
}
?>
Your this visible code seems to be corrected. May be you would have some error in storing the sessions. Try:
echo $_SESSION['isLoggedOn'];
OR try doing this:
<?php
if ($_SESSION['isLoggedOn']) {
echo "Logout";
}?>
<?php if (isset($_SESSION["isLoggedOn"]) && $_SESSION["isLoggedOn"] == True) {?>
LOGOUT
<?php }?>
Your $_SESSION["isLoggedOn"] is always TRUE.
Also dont use == TRUE for True/False variables.
Also logging out at terms and conditions page is not very good i guess ;-)
Elaborate your code:
<?php
session_start();
if ( !isset($_SESSION) || !isset($_SESSION["isLoggedOn"]) {
echo "an error occured instantiating the session.";
exit 1;
}
if ( $_SESSION["isLoggedOn"] === true ) {
echo 'LOGOUT';
}
?>
You need to start a session as very first action in your PHP.
Check if you actually have a vale in $_SESSION
Then, check its state and output what you want.

PHP remember url parameter value using $_SESSION

I made a script that shows the value of "school_id" in url parameter.
http://mywebsite.com/mygrade?school_id=00000
I use $_GET['school_id'] to display the ID number.
<?php echo $_GET['school_id']; ?>
But I what I want is if the parameter "school_id" is empty, I want to display the previous data entered.
Example, the user already browse http://mywebsite.com/mygrade?school_id=00000 then he browse http://mywebsite.com/mygrade?school_id= which id has no value. It will still display 00000 which is the previous ID he used.
I used this code below but doesn't work.. :(
<?php
session_start();
$_SESSION['schoo_id'] = $_GET['school_id'];
if ($_GET['school_id'] === null || $_GET['school_id'] == ""){
echo $_SESSION['schoo_id'];
}
else{
$_GET['school_id'];
}
?>
Anyone who get my point and could help me?
I'm going to break this down line by line, please let me know in the comments if I need to explain anything further:
Self explanatory:
<?php
session_start();
There is a typo here:
$_SESSION['schoo_id'] = $_GET['school_id'];
But! Fixing it won't resolve your problem. What happens if $_GET['school_id'] is not defined/blank? Guess what, $_SESSION['school_id'] is now blank. Obviously you don't want this behavior, so you'll want to only set $_SESSION['school_id'] if $_GET['school_id'] is defined
accessing $_GET['school_id'] will throw an E_NOTICE error if it isn't defined, so you'll want to instead check its existence, rather than checking to see if it is null.
if ($_GET['school_id'] === null || $_GET['school_id'] == ""){
Oh, that typo was intended. Why misspell school though? No need! :)
echo $_SESSION['schoo_id'];
What is this doing? Nothing! No echo, nothing. Just accessing a variable and doing nothing with it.
}
else{
$_GET['school_id'];
}
?>
Here's what your code should look like, or at least I believe is what you intend:
<?php
session_start();
if (isset($_GET['school_id']) && $_GET['school_id'] !== ""){
$_SESSION['school_id'] = $_GET['school_id'];
}
// $_SESSION['school_id'] will be guaranteed to be what $_GET['school_id'] is (if set)
// or whatever it was last time it was defined
// always echo it.
echo $_SESSION['school_id'];
?>
<?php
session_start();
if ($_GET['school_id'] === null || $_GET['school_id'] == ""){
echo $_SESSION['schoo_id'];
}
else{
$_GET['school_id'];
$_SESSION['schoo_id'] = $_GET['school_id']; //here set the session
}
?>
I agree with Salman A, the simplest way:
<?php
session_start();
if (is_int($_GET['school_id'])) $_SESSION['school_id'] = $_GET['school_id'];
// further use $_SESSION['school_id'] for your needs.
?>
what you need to do here is save the GET value in SESSION only if it is set for later use so this should work
<?php
session_start();
if (!isset($_GET['school_id']) || $_GET['school_id'] === null || $_GET['school_id'] == ""){
echo $_SESSION['schoo_id'];
}
else{
$_SESSION['schoo_id'] = $_GET['school_id'];
echo $_GET['school_id'];
}
?>
You almost have it.
<?php
session_start();
if (isset($_GET['school_id']) && trim($_GET['school_id']) !== '') {
// its a fair assumption to make that 'school_id' is intended to be an integer,
// however I will not make that assumption on the OP's behalf.
$_SESSION['school_id'] = $_GET['school_id'];
}
if (isset($_SESSION['school_id']) {
echo $_SESSION['school_id'];
}
else {
echo 'have not entered a school id yet';
}
?>

PHP search url request

I need some help with this I have for example index.php and and i need to make it something like.
someone access:
index.php?search=blbla
include search.php
else
include home.php
I need an advice with this thanks
Try this
if (isset($_GET['search'])) include('search.php');
else include('home.php');
Well, you could use isset() to see if the variable is set. e.g.
if(isset($_GET['search'])){
include "search.php";
}
else {
include "home.php";
}
$sq = $_GET['search']; //$_GET['']
if (isset($sq) && $sq != '') {
include('search.php');
} else {
include('home.php');
}
I personally prefer to check if a $_GET is set and if it actually equals something like so:
if(isset($_GET['search']) && strlen(trim($_GET['search'])) > 0): include 'search.php';
else: include 'home.php';
This will avoid the problem of putting in the $_GET variable but not actually setting it.
use it like this
if (isset($_GET['search']))
include 'search.php';
else
include 'home.php';
<?php
//if($_GET['search'] > ""){ // this will likely cause an error
if(isset($_GET['search']) && trim($_GET['search']) > ""){ // this is better
include ('search.php');
}else{
include ('home.php');
}
?>
When using isset() you need to be aware that with an empty GET variable like this script.php?foo= that isset($_GET['foo']) will return TRUE
Foo is set but has no value.
So if you want to make sure that a GET variable has a value you might want to use strlen() combined with trim() instead...
if (strlen(trim($_GET['search'])) > 0) {
include('search.php');
} else {
include('home.php');
}
Also you might want to use require() instead of include(). A PHP fatal error is generated if search.php cannot be "required" with just a PHP warning if search.php cannot be "included".

How to check if an include() returned anything?

Is there any way to check if an included document via include('to_include.php') has returned anything?
This is how it looks:
//to_include.php
echo function_that_generates_some_html_sometimes_but_not_all_the_times();
//main_document.php
include('to_include.php');
if($the_return_of_the_include != '') {
echo $do_a_little_dance_make_a_little_love_get_down_tonight;
}
So after I've included to_include.php in my main document I would like to check if anything was generated by the included document.
I know the obvious solution would be to just use function_that_generates_some_html_sometimes_but_not_all_the_times() in the main_document.php, but that's not possible in my current setup.
make function_that_generates_some_html_sometimes_but_not_all_the_times() return something when it outputs something and set a variable:
//to_include.php
$ok=function_that_generates_some_html_sometimes_but_not_all_the_times();
//main_document.php
$ok='';
include('to_include.php');
if($ok != '') {
echo $do_a_little_dance_make_a_little_love_get_down_tonight;
}
If you are talking about generated output you can use:
ob_start();
include "MY_FILEEEZZZ.php";
function_that_generates_html_in_include();
$string = ob_get_contents();
ob_clean();
if(!empty($string)) { // Or any other check
echo $some_crap_that_makes_my_life_difficult;
}
Might have to tweak the ob_ calls... I think that's right from memory, but memory is that of a goldfish.
You could also just set the contents of variable like $GLOBALS['done'] = true; in the include file when it generates something and check for that in your main code.
Given the wording of the question, it sounds as if you want this:
//to_include.php
return function_that_generates_some_html_sometimes_but_not_all_the_times();
//main_document.php
$the_return_of_the_include = include 'to_include.php';
if (empty($the_return_of_the_include)) {
echo $do_a_little_dance_make_a_little_love_get_down_tonight;
} else {
echo $the_return_of_the_include;
}
Which should work in your situation. That way you don't have to worry about output buffering, variable creep, etc.
I'm not sure if I'm missing the point of the question but ....
function_exists();
Will return true if the function is defined.
include()
returns true if the file is inclued.
so wrap either or both in an if() and you're good to go, unless I got wrong end of the stick
if(include('file.php') && function_exists(my_function))
{
// wee
}
try
// to_include.php
$returnvalue = function_that_generates_some_html_sometimes_but_not_all_the_times();
echo $returnvalue;
//main_document.php
include('to_include.php');
if ( $returnvalue != '' ){
echo $do_a_little_dance_make_a_little_love_get_down_tonight;
}

Categories