I'm trying to specify that my logo appears on some pages and not on others, the only pages I do not want it showing on are the homepage and /index.php. I have the big logo appearing and disappearing as I want it and so I presumed I could just do the opposite for the small logo but I must be doing something wrong. Here is my current code:
<?php
$dunpage = $_SERVER['REQUEST_URI']; if ($dunpage != '/index.php' || $dunpage != '/') {?>
<h1 class="small-logo">
<span><?php echo $siteName; ?>
</h1>
<?php }
?>
<?php
$currentpage = $_SERVER['REQUEST_URI']; if ($currentpage == '/' || $currentpage == '/index.php') {?>
<h1 class="logo";>
<span><?php echo $siteName; ?>
</h1>
<?php }
?>
Instead of a logical OR in your first condition, you should be using a logical AND:
$dunpage = $_SERVER['REQUEST_URI']; if ($dunpage != '/index.php' || $dunpage != '/') {?>
// Should be
$dunpage = $_SERVER['REQUEST_URI']; if ($dunpage != '/index.php' && $dunpage != '/') {?>
Effectively, you were saying "act if the page is either not index.php, or not /." So in either of those cases, the opposite would be true. If it wasn't index.php, it could be /, for example.
try
if ($dunpage != '/index.php' && $dunpage != '/') {
for your small logo.
You correctly negated the == operator, but not the || operator. You're saying that if dunpage is not /index.php OR dunpage is not /, do that, which basically means do that always. Change || to && for the small logo.
Why don't you use an else?
<?php
$currentpage = $_SERVER['REQUEST_URI']; if ($currentpage == '/' || $currentpage == '/index.php') { ?>
<h1 class="logo";>
<span><?php echo $siteName; ?>
</h1>
<?php
} else {
?>
<h1 class="small-logo">
<span><?php echo $siteName; ?>
</h1>
<?php
}
?>
(You don't actually mention that it's either a large or a small logo that you want to display, but I have to assume that is the case here.)
Or you can use in_array() in this case.
<?php
$displayMainLogo = array('/', '/index.php');
if ( in_array($_SERVER['REQUEST_URI'], $displayMainLogo) ) { ?>
<h1 class="logo";>
<span><?php echo $siteName; ?>
</h1>
<?php
} else {
?>
<h1 class="small-logo">
<span><?php echo $siteName; ?>
</h1>
<?php
}
?>
If you understand how to do a ternary (http://php.net/manual/en/language.operators.comparison.php), then this is as terse as you can make it.
<h1 class="<?php echo (in_array($_SERVER['REQUEST_URI'], $displayMainLogo)) ? 'logo' : 'small-logo' ?>";>
<span><?php echo $siteName; ?>
</h1>
Related
I want to ask about how to change div id in if else condition?
My code
<html>
<div id="div1">
<?php
$page = isset($_GET['page']) ? $_GET['page'] : "";
$page2 = isset($_GET['page2]) ? $_GET['page2] : "";
if ($page == "If1") {
include ".../some.php";
}else if ($page2 == "If2") {
echo '<div id="div2">';
include ".../some2.php";
echo '</div>';
}
?>
</div>
</html>
if im going to page in https://localhost/index.php?page2=If2 i want change <div id="div1> to <div id="div2>
can i get some solution?
sorry for my bad english
Here is one approach for doing that, using the If-else statement before printing the <div ... >
<html>
<?php
$page = isset($_GET['page']) ? $_GET['page'] : "";
$page2 = isset($_GET['page2']) ? $_GET['page2'] : "";
if ($page == "If1") {
echo '<div id="div1">';
}else if ($page2 == "If2") {
echo '<div id="div2">
}
include ".../";
?>
</div>
</html>
Declare variable as html attribute
Declare your variable as a html attribute and assign it via php to your <div>.
<?php
// check if request method is "get"
if ($_SERVER["REQUEST_METHOD"] === "GET") {
if (isset($_GET['page'])) { // if "page"
$divId = 'id="div1"';
} else if (isset($_GET['page2'])) { // if "page2"
$divId = 'id="div2"';
}
}
?>
<html>
<div <?php echo $divId ?>>
<?php include ".../" ?>
</div>
</html>
If you'd like to play around with my code in the browser directly, you can do so here.
Here's the simple answer
<html>
<div id="<?php echo isset($_GET['page2']) && $_GET['page'] == 'IF2' ? 'div2' : 'div1' ?>" >
<!-- rest content -->
</div>
</html>
OR
<html>
<?php $id = isset($_GET['page2']) && $_GET['page'] == 'IF2' ? 'div2' : 'div1' ; ?>
<div id="<?php echo $id; ?>">
<!-- rest content -->
</div>
</html>
Note: You can also use PHP short_open_tag to echo id <?= $id; ?> . If short_open_tag enable in php.ini in server
everyone!
My website has a list of office contacts being dynamically generated via PHP (partial snippet below). As you can see, each list result/child has a name, address and, on a following div, a google maps frame.
What I'd like to achieve is to call the latest result of the array and have that one NOT TO display the google maps div.
I think I can probably call it with the "end()" command, but I'm not being able to wrap my head around the right way to proceed... Can anyone point me in the right direction, please?
Thank you!
<header id="standardheader">
<div class="wrap">
<nav class="standardsubnav">
<ul class="standardsubnav_list vanilla"><!--
<?php include_partial( 'contact/citynav', array('class' => 'standardsubnav') ); ?>
--></ul>
</nav>
<h1 class="standardpage_title"><?php echo $current->getHeadline(); ?></h1>
</div>
</header>
<div class="wrap">
<section class="">
<ul class="list4 vanilla">
<?php $contacts = $current->getChildren(); ?>
<?php foreach($contacts as $contact): $settings = $contact->getSettings(); ?>
<li class="item4">
<a name="<?php echo $settings['city']; ?>"></a>
<div class="link4">
<div class="link4_inner">
<h2 class="title4"><?php echo $settings['city']; ?></h2><!--
--><address class="address4">
<?php echo $settings['name']; ?><br />
<?php if(isset($settings['address1']) && $settings['address1'] != "") echo $settings['address1'] . '<br />'; ?>
<?php if(isset($settings['address2']) && $settings['address2'] != "") echo $settings['address2'] . '<br />'; ?>
<?php if(isset($settings['address3']) && $settings['address3'] != "") echo $settings['address3'] . '<br />'; ?>
<?php if(isset($settings['address4']) && $settings['address4'] != "") echo $settings['address4'] . '<br />'; ?>
<?php if(isset($settings['phone']) && $settings['phone'] != ""): ?>t. <?php echo $settings['phone']; ?><br /><?php endif; ?>
<?php if(isset($settings['fax']) && $settings['fax'] != ""): ?>f. <?php echo $settings['fax']; ?><br /><?php endif; ?>
<?php if(isset($settings['email']) && $settings['email'] != ""): ?>e. <?php echo $settings['email']; ?><?php endif; ?>
</address>
</div><!--
--><div class="link4_inner">
<?php
$mapurl = (!empty($settings['mapurl'])) ? $settings['mapurl'] : 'https://www.google.com/maps/#' . $settings['latitude'] . ',' . $settings['longitude'] . ',' . $settings['zoom'] . 'z'; ?>
<div class="gmap4" data-gmap="<?php echo htmlspecialchars( '{"lat":"'.$settings['latitude'].'","lng":"'.$settings['longitude'].'", "zoom":"'.$settings['zoom'].'", "mapurl":"'.$mapurl.'"}' ); ?>">
<noscript><?php echo __('Please enable javascript to see the map.'); ?></noscript>
<?php end($contacts)
Count the $contacts array. Then in the foreach loop check if the element beeing currently in the loop is equals the items in the array, if it's equal to the items, it's the last element, then exit the foreach:
$items = count($contacts );
$i = 0;
foreach($contacts as $contact) {
if(++$i === $items ) {
continue;
}
}
I'm trying to check if the current URL equals the desired URL for making the link class active (Bootstrap).
Example
<?php
If ($currenturl = "show.php?name=123") {
?><li class="active">123</li><?php
}else{
?><li class="">123</li><?php
}
?>
Current URL -> $currenturl = $_SERVER['REQUEST_URI']
Now, I have this if I'm on show.php?name=456
Make sure you have the correct comparison inside the if. You're currently using the assignment operator:
Note the difference:
if($currenturl = "show.php?name=123") // assignment =
// changes $currenturl to "show.php?name=123"
// and then tests if("show.php?name=123") equivalent to if(true)
// see also http://stackoverflow.com/questions/5940626/evaluation-of-assignment-in-php
if($currenturl == "show.php?name=123") // comparison ==
Second: You have set $urlpage but comparing $currenturl. Use $urlpage
Code:
<?php $urlpage = $_SERVER['REQUEST_URI']; ?>
<?php if ($urlpage == "/show.php?nom=123") { ?>
<li class="active">123</li>
<?php } else { ?>
<li class="">123</li>
<?php } ?>
An alternative using a ternary operator:
<li <?php echo ($urlpage == "/show.php?nom=123") ? 'class="active"' : ''; ?>>123</li>
Applying to all pages:
<?php $pages = array('123', '456', '789'); ?>
<ul>
<li <?php echo (!isset($_GET['nom']) ? 'class="active"' : ''); ?>>Home</li>
<?php foreach($pages as $page): ?>
<?php if (isset($_GET['nom']) && $_GET['nom'] == $page) { ?>
<li class="active"><?php echo $page; ?></li>
<?php } else { ?>
<li class=""><?php echo $page; ?></li>
<?php } ?>
<?php endforeach; ?>
</ul>
An alternate way is to use the $_GET
if(isset($_GET['name']))
if($_GET['name'] == '123')
...
and so forth
I am trying to return a $user_id variable from file to if(isset(...)) statement. I will perform a different if statement but just trying to echo out the $user_id variable to test that I am setting the page.
<?php
include 'core/init.php';
include 'init.image.php';
protect_page();
include 'includes/overall/overall_header.php';
if(isset($_GET['username']) === true && empty($_GET['username']) === false){
$username = $_GET['username'];
if(user_exists($username) === true){
$user_id = user_id_from_username($username);
$profile_data = user_data($user_id, 'first_name','last_name','email', 'username');
?>
<h1><?php echo $profile_data['first_name']; ?>'s Yor Page</h1>
<div id="navWrapper">
<ul>
<li>
<img src="uploads/profile/blank_profile.gif" width="150" height="150" id="blank_profile">
</li>
<nav>
<ul>
<li>
Albums
</li>
<li>
Music
</li>
</ul>
</nav>
</ul>
</div>
<?php
if(isset($_GET['action']) && $_GET['action']=='albums'){
$albums = get_profile_albums($user_id);
if(empty($albums)){
echo 'No Albums';
}else{
foreach($albums as $album){
if (empty($album['image'])) {
$album['image'] = 'uploads/profile/blank_profile.gif';
}
?>
<p><?php echo $album['name'],' (', $album['count'], ')'?> <br />
<a href="<?php echo $profile_data['username'];?>?action=album_id=<?php echo $album['id'];?>">
<img src="uploads/thumbs/<?php echo $album['id'];?>/<?php echo $album['image'];?>" />
</a><br />
<?php echo $album['description'];?>...<br />
</p>
<?php
}
}
}
if(isset($_GET['action']) && $_GET['action']=='album_id=$album['id']'){
echo $user_id;
}
if(isset($_GET['action']) && $_GET['action']=='music'){
echo'<h1>Music</h1>';
}
}else{
echo 'Sorry, that user doesn\'t exist';
}
}else{
header('Location: index.php');
exit();
}
include 'includes/overall/overall_footer.php';
?>
Single quotes do not parse the string but double quotes do. For example:
$album_id = 1;
echo 'album_id=$album_id ';
echo "album_id=$album_id";
Will result in album_id=$album_id album_id=1
Thus, $_GET['action']=='album_id=$album_id' is checking if $_GET['action'] is equal to 'album_id=$album_id' and not what $album_id is evaluated to.
Your check should be more along these lines:
if (/* ... */ && $_GET['action']=="album_id=$album_id") {
Edit: Suggestion.
You should check $_GET['action'] once and store $action. From there create an if/else if/else statement checking each possible type of action (music, albums, album, etc). If a type of action requires extra user supplied data, include that in the respective if block. For example:
$action = $_GET['action'];
// URL: /script.php?action=albums
if ($action == 'albums') {
// ...
}
// URL: /script.php?action=album&album_id=12
else if ($action == 'album') {
if (!empty($_GET['album_id'])) {
$album_id = $_GET['album_id'];
echo "User with id $user_id is trying to access album with id $album_id";
}
// ...
}
// ...
No more $_GET['action']=="album_id=$album_id"
You are passing the variable correctly.
What I'm more concerned with in the following line is:
if(isset($_GET['action']) && $_GET['action']=='album_id=$album_id'){
'album_id=$album_id' doesnt look right and is probably why your if is not firing... It looks like you're trying to use the variable $album_id wrapped in single quotes and that will not substitute the value and rather keep $album_id in tact. I'm not sure if this is what you intended but if not its likely the source of your problem.
if(isset($_GET['action']) && $_GET['action']=='album_id=$album['id']')
Now I am sure all of you can see the error here I am trying to check if this link is clicked
<a href="<?php echo $profile_data['username'];?>?action=album_id=<?php echo $album['id'];?>">
Now this link works and displays as it should in the url but I can not figure out how to pass $album['id'] to the if statements isset?
EDIT
ok i did this:
<a href="<?php echo $profile_data['username'];?>?action=album_id&action_id=<?php echo $album['id'];?>">
and
if($_GET['action'] == 'album_id' && $_GET['action_id'] == $album['id'])
and returned undefined variable in the if statement says album is undefined variable
This is my htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /lr/profile.php?username=$1 [QSA]
Here is my entire php file:
<?php
include 'core/init.php';
include 'init.image.php';
protect_page();
include 'includes/overall/overall_header.php';
if(isset($_GET['username']) === true && empty($_GET['username']) === false){
$username = $_GET['username'];
if(user_exists($username) === true){
$user_id = user_id_from_username($username);
$profile_data = user_data($user_id, 'first_name','last_name','email', 'username');
?>
<h1><?php echo $profile_data['first_name']; ?>'s Yor Page</h1>
<div id="navWrapper">
<ul>
<li>
<img src="uploads/profile/blank_profile.gif" width="150" height="150" id="blank_profile">
</li>
<nav>
<ul>
<li>
Albums
</li>
<li>
Music
</li>
</ul>
</nav>
</ul>
</div>
<?php
if(isset($_GET['action']) && $_GET['action']=='albums'){
$albums = get_profile_albums($user_id);
if(empty($albums)){
echo 'No Albums';
}else{
foreach($albums as $album){
if (empty($album['image'])) {
$album['image'] = 'uploads/profile/blank_profile.gif';
}
?>
<p><?php echo $album['name'],' (', $album['count'], ')'?> <br />
<a href="<?php echo $profile_data['username']; ?>?action=album&album_id=<?php echo $album['id']; ?>">
<img src="uploads/thumbs/<?php echo $album['id'];?>/<?php echo $album['image'];?>" />
</a><br />
<?php echo $album['description'];?>...<br />
</p>
<?php
}
}
}
else if (isset($_GET['action']) && $_GET['action']=='album' && isset($_GET['album_id'])){
echo 'albums';
}
if(isset($_GET['action']) && $_GET['action']=='music'){
echo'<h1>Music</h1>';
}
}else{
echo 'Sorry, that user doesn\'t exist';
}
}else{
header('Location: index.php');
exit();
}
include 'includes/overall/overall_footer.php';
?>
the proper way would be to have the url properly encoded:
<a href="<?php echo $profile_data['username'];?>?action=<?php echo urlencode('album_id='.$album['id']); ?>">
and then decode it
if(isset($_GET['action']) && urldecode($_GET['action'])=='album_id='.$album['id'])
You don't have a destination, where's the php file you try to access? You started with username. Check here href="<?php echo $profile_data['username'];?>?
Try separate the action and the id, it's probably just me, I found it kinda weird to put it like action=album=1
you can try like this
$_GET['action'] == 'album' && $_GET['action_id'] == $album['id']
so the url would be looking like
?action=album&action_id=1
I think #Dreen answered to your question, but I would separate the action parameter to something like action and id:
<a href="<?php echo $profile_data['username'];?>?action=album&id=<?php echo $album['id'];?>">
and check
if(isset($_GET['action']) && $_GET['action']=='album' && isset($_GET['id']) && $_GET['id']==$album['id'])