If else for css - php

Can i use if else statement for css?
This is where i want the color of the text to change:
<?php echo $status; ?>
There will be 2 status: Pending & Delivered
Pending will be red color and delivered will be green
Can i do something like (for CSS):
.pending {text-decoration:underline; color:red;}
.delivered {text-decoration:underline; color:green;}
and if else statement:
if ($status==delivered)
{
//this is where i don't know what to do and code
}
else
{
//and here
}
What should i put there? Or any other solution?

If the $status variable in PHP actually matches your class names, just use it in your PHP when displaying whatever the thing is that's being styled:
e.g. if $status == 'pending', then:
<div class="<?= $status ?>">...</div>
will render
<div class="pending">...</div>
and match your .pending rule.

Output html with php / javascript / any other language, and assign classes to the whatever element you want.
pure PHP example:
<?php
if(true) {
echo '<div class="pending">content</div>';
} else {
echo '<div class="delivered">content</div>';
}
?>
Another way using variables (PHP + html):
<?php
if(true) {
$status = 'pending';
} else {
$status = 'delivered';
}
?>
<html>
<head>
</head>
<body>
<div class="<?php echo $status; ?>">content</div>
</body>
</html>

Related

Can i use HTML instead of echo in IF-ELSE statement in PHP?

if( $user->username == 'XYZ' )
{
echo "hello, XYZ";
}
else
{
echo "hello, guest";
}
In the above code, can i use pure html code which will get executed incase the IF statement is true instead of using echo ?
Yes, you can do it:
if( $user->username == 'XYZ' )
{
?>
hello, XYZ
<?
}
else
{
?>
hello, guest
<?
}
Sometimes it looks better and simplier. But in fact it is much better to put php and html code in different files (separate logic, styles and data).
if( $user->username == 'XYZ' )
{ ?>
<p>Hello <b><i>XYZ</i></b>
<?php }
else { ?>
<p>Hello <b><i>Guest</i></b>
<?php
}
I hope You Got it.
Of course you can write HTML directly by closing the <?php ?> tags.
<?php if( $user->username == 'XYZ') { ?>
hello, XYZ<br>
<?php } ?>
<?php else { ?>
hello, guest<br>
<?php } ?>
You can of course heredoc as well:
<?php
$str = <<<FOO
This is a
demo message
FOO;
echo $str;
?>
but i dont use it since i it messes with any highlighting editor (it thinks it's a text and i like my HTML highlighted)
What i like best is this, especially when my outputs are big:
<?php if( $user->username == 'XYZ') {
include("user_template.php");
}
<?php else { ?>
include("guest_template.php");
<?php } ?>
which are actually just rendering HTML contained there.
Can also be written like this, more readable for several lines.
<?php if ($user->username == 'XYZ') : ?>
Hello, XYZ
<?php else : ?>
Hello, Guest
<?php endif; ?>

PHP function that decides which HTML code to use?

I'm looking for a function like and if else statement for php which will execute certain html code.
For example:
<?php>
$result = 1;
if ($result == 1)
<?>
html code
else
html code
So, based off the result variable gotten from php scripts, a certain html page is output. I've tried echoing the entire html page, but it just displays the html code-> tags and such.
Hopefully you get what I'm trying to get across, ask if you need any clarification questions. Thanks!
That should work:
<?php
$result = 1;
if($result==1) {
?>
html code
<?php
} else {
?>
html code
<?php
}
?>
The problem I'm facing with the if else statement, is in order to display the html, I have to exit php coding. Thus, the if else statement will not work. (Link)
This is not entirely true. You can use the approach below:
<?php
// Do evaluations
if ( $result == "something" )
{
?>
<p>Example HTML code</p>
<?php
} elseif ( $result == "another thing")
{
?>
<span>Different HTML code</p>
<?php
} else {
?>
<h4>Foobar.</h4>
<?php
}
// Rest of the PHP code
?>
Or, if you don't want to exit PHP coding, you can use the echo or print statements. Example:
<?php
// Evaluations
if ( $result == "foo" )
{
echo "<p>Bar.</p>";
} else {
echo "<h4>Baz</p>";
}
// Some else PHP code
?>
Just be careful with proper sequences of ' and " characters. If your HTML tags are to have arguments, you should watch your step and use either of the following approaches:
echo "<span class=\"foo\">bar</span>";
echo '<span class="foo">bar</span>";
If you want to evaluate some PHP and print the HTML results later, you could use something like this
<?php
$output = "";
if ( $result == "something" ) {
$output = '<p>Example HTML code</p>';
} else if ( $result == "another thing") {
$output = '<span>Different HTML code</p>';
} else {
$output = '<h4>Foobar.</h4>';
}
// Output wherever you like
echo $output;
?>
EDIT (because I'm not sure what you;re trying to do so i'm just putting out different ideas):
If you're trying to output an entire page, it may be useful to use header('location: newPage.html'); instead of $output. This redirects the browser to an entirely new web page. Or you can likely include newPage.html as well.
very close:
<?php
$result = 1;
if ($result == 1){
?>
html code
<?php } //close if
else {
?>
html code
<?php
} //close else
?>
you can echo html code something like this
<?php
$result = 1;
if ($result == 1){
echo "<h1>I love using PHP!</h1>";
}
?>
this would output if Result is 1
**I love using PHP!** //but slightly bigger since its H1

Keeping track of language in a session variable

What my current code does is, while checking the DB if the versions (FR and EN) are either True or False, display the proper content and if both exist to display a link so that users can switch languages. If only one language exists, the content is shown in that language and there is no link displayed.
the 2 functions in javascript are like this, here`s the FR one:
function makeVisibleFR()
{
document.getElementById('bbqc_contentFR').style.display="inline";
document.getElementById('bbqc_contentEN').style.display='none';
document.getElementById('vFrancais').style.display='none';
document.getElementById('vAnglais').style.display="inline";
}
What i`d like to add to this is the option of memorizing the user's choice and displaying the following pages with the same language version.
I imagine i'd need to create a $_SESSION['language'] variable and store in it either "FR" or "EN" but i`m not sure how to go about implementing that within my current code.
<?php
if($versionFR == true)
{
if($versionEN == true)
{
?>
Version Anglaise
<div id="bbqc_contentFR">
<h2><?php echo $titleFR; ?></h2>
<?php echo $contentFR; ?>
</div>
Version Française
<div style="display:none" id="bbqc_contentEN">
<h2><?php echo $titleEN; ?></h2>
<?php echo $contentEN; ?>
</div>
<?php
}
else
{
?>
<div id="bbqc_contentFR">
<h2><?php echo $titleFR; ?></h2>
<?php echo $contentFR; ?>
</div>
<?php
}
}
else
{
if($versionEN == true)
{
?>
<div id="bbqc_contentEN">
<h2><?php echo $titleEN; ?></h2>
<?php echo $contentEN; ?>
</div>
<?php
}
else
{
?>
<h2>Erreur, il n`y a aucun texte</h2>
<?php
}
}
?>
Here's a simplistic example:
// assuming that databaseHas() queries available languages
session_start();
$langs = array('ENG', 'FR');
$showlang = '';
if (databaseHas($_SESSION['lang']))
{
$showlang = $_SESSION['lang'];
}
else
{
foreach ($langs as $l)
{
if (databaseHas($l))
{
$showlang = $l;
break;
}
}
}
if ($showlang == '')
{
die('No languages found!');
}
echo databaseContent($showlang);
// print links to alternate languages
foreach ($langs as $l)
{
if (databaseHas($l) && $l != $showlang)
{
// print link to this language
}
}
put session_start() at the top of your file then:
if($versionFR == true) $_SESSION['lang'] = 'FR';
else $_SESSION['lang'] = 'ENG';
//later on (could be in a whole other page with session_start() on top)
if($_SESSION['lang'] == 'FR'){/*display FR stuff*/}
elseif($_SESSION['lang'] == 'ENG'){/*display ENG stuff*/}
Something like that should work well for ya ^_^
How would i go about adding that if 1)
both versions exist 2) lang is ENG 3)
english part is display:none by
default
1) You'd just add the clause: if($_SESSION['lang'] == 'FR' && $_SESSION['lang'] == 'ENG')
2) Not sure what you mean here, Neal explained it well from what I can see
3) If ENG is display:none by default, you'd want to fire off a javascript function to toggle it back on.
But let's take a step back here, consider this: Make two language files that define each piece of content. So for your english.php you might have variables such as $GREETING = 'Hello'; $YES = 'YES'; and then in your french.php you'd define these variables as $GREETING = 'Bonjour'; $YES = 'WEE'; (I'm not even sure if wee means yes, but you get the idea!). So now you can choose to include the appropriate language file based on the user's language, and you make it easy to add another language down the road. Be flexible!

PHP if/else statement

How can I write the following statement in PHP:
If body ID = "home" then insert some html, e.g.
<h1>I am home!</h1>
Otherwise, insert this html:
<p>I'm not home.</p>
Doing it with native PHP templating:
<?php if ($bodyID==='home') { ?>
<h1>I am home!</h1>
<?php } else { ?>
<p>I'm not home!</p>
<?php } ?>
You can try using this :
$html = '';
if ( $body_id === 'home' )
{
$html .= '<h1>I am home!</h1>';
}
else
{
$html .= '<p>I\'m not home.</p>';
}
echo $html;
This will echo the html code depending on the $body_id variable and what it contains.
You can use a switch command like so:
switch($body)
{
case 'home': //$body == 'home' ?
echo '<h1>I am home!</h1>';
break;
case 'not_home':
default:
echo '<p>I'm not home.</p>';
break;
}
The default means that if $body does not match any case values, then that will be used, the default is optional.
Another way is as you say, if/else statements, but if within template / view pages you should try and use like so:
<?php if ($body == 'home'):?>
<h1>I am home!</h1>
<?php else:?>
<p>I'm not home!</p>
<?php endif; ?>
Assuming $bodyID is a variable:
<?php
if ($bodyID==='home') {
echo "<h1>I am home!</h1>";}
else {
echo "<p>I'm not home!</p>";}
?>
Personally I think that the best way to do that without refreshing and without having to set a variable (like $body or something like that) is to use a javascript code, this because "communications" between JS & PHP is a one-way communication.
<script language="javascript">
<!--
if( document.body.id === "home" ){
window.document.write("<h1>I am home!</h1>") ;
}
else{
window.document.write("<p>I'm not home!</p>") ;
}
-->
</script>
otherwise you can build a form and then take the body.id value using $_GET function... It always depends on what you've to do after you now body.id value.
Hope this will be usefull & clear.
you can try in the following way:
$body_id = "home";
if ($body_id == "home") {
echo "I am home!";
} else {
echo "I am not home!";
}
or
$body_id = "home";
if (strcmp($body_id, "home") !== 0) {
echo 'I am not home!';
}
else {
echo 'I am home!';
}
Reference:
https://www.geeksforgeeks.org/string-comparison-using-vs-strcmp-in-php/

If-statement: how to pull 2nd GET variable

How do I get this to pull my 2nd variable? (I already have a switch setup)
<body id="<?php if (! isset($_GET['page'])) { echo "home"; } else { $_GET['page']; echo $page; } ?>">
I have a switch statement that pulls the pages from
index.php?page=#####
and I have just added this part to my switch:
index.php?page=####&section=#####
Right now, if I am on page=photos, my code ends up being:
<body id="photos">
I need to make it so that if any link has the "sections" variable on it like this page=photos&section=cars it uses the same ID:
<body id="photos">
First of all, a HTML element can only have one id. So if you want to create a hybrid (e.g. page-section) you can do something like this:
<body id="<?php echo isset($_GET['page']) ? $_GET['page'] : "home"; echo isset($_GET['section']) ? ("-".$_GET['section']) : ''; ?>">
For more information on Ternary Operators in PHP (the ? and : I used in the echo statement) see http://php.net/manual/en/language.operators.comparison.php
I am not entirely sure I understand your question, but where you're doing:
$_GET['page']; echo $page;
What do you think is happening? You're echoing a variable that has no definition. If you want to echo the value passed in the url, just do:
echo $_GET['page'];
GET doesnt mean your getting the varible, its the method by which the variable was passed to he page. The possible methods are get (in the url) or post (not).
Wouldn't that be an if to find out it if the section was defined? i.e.
if(isset($_GET['section'])){
//create div
} elseif(isset($_GET['page']){
//create fallback div
}
Move the PHP code outside the body's id attribute for readability, and use else if. Make sure your code isn't vulnerable to injection by sanitizing or validating input from $_GET. For example:
<?php
function isValidID($x) {
return preg_match('/^[A-Z][-_.A-Za-z0-9]$/i', $x);
}
if (isset($_GET['section']) && isValidID($_GET['section'])) {
$bodyID = $_GET['section'];
} else if (isset($_GET['page']) && isValidID($_GET['page'])) {
$bodyID = $_GET['page'];
} else {
$bodyID = 'home';
}
?>
...
<body id="<?php echo $bodyID; ?>">
Alternatively,
<?php
function isValidID($x) {
return preg_match('/^[A-Z][-_.A-Za-z0-9]$/i', $x);
}
$bodyID='home';
foreach (array('section', 'home') as $key) {
if (isset($_GET[$key]) && isValidID($_GET[$key])) {
$bodyID = $_GET[$key];
break;
}
}
?>
...
<body id="<?php echo $bodyID; ?>">
In this case, I'd use the first, unrolled version. If you had to check more input keys, use the loop-based approach.
If you decide you want both page & section in the ID, you can try something like:
<?php
function isValidID($x) {
return preg_match('/^[A-Z][-_.A-Za-z0-9]$/i', $x);
}
if (isset($_GET['page']) && isValidID($_GET['page'])) {
$bodyID = $_GET['page'];
} else {
$bodyID = 'home';
}
if (isset($_GET['section']) && isValidID($_GET['section'])) {
$bodyID .= '_' . $_GET['section'];
}
?>
...
<body id="<?php echo $bodyID; ?>">

Categories