header() not working in a very specific line - php

I have 2 li tags combined with PHP that is used for dictionary purposes:
<li <?php echo "title='".$this->text['tt-statistics']."'" ;?> ><a <?php if($location == "analytics") {echo 'id="current"'; } ?> href="analytics-dash.php"><?php echo $this->text['statistics'];?></a></li>
<li <?php echo "title='".$this->text['tt-logins']."'" ;?> ><a <?php if($location == "settings") {echo 'id="current"'; } ?> href="settings-producer.php"><?php echo $this->text['manage-logins'];?></a></li>
If I put a header() command right after $this->text['statistics']; (at the end of the first li) Like this:
<li <?php echo "title='".$this->text['tt-statistics']."'" ;?> ><a <?php if($location == "analytics") {echo 'id="current"'; } ?> href="analytics-dash.php"><?php echo $this->text['statistics']; header('Location: http://www.example.com/');?></a></li>
<li <?php echo "title='".$this->text['tt-logins']."'" ;?> ><a <?php if($location == "settings") {echo 'id="current"'; } ?> href="settings-producer.php"><?php echo $this->text['manage-logins'];?></a></li>
The header command works fine.
But If i put it right on the start of the second li, like this:
<li <?php echo "title='".$this->text['tt-statistics']."'" ;?> ><a <?php if($location == "analytics") {echo 'id="current"'; } ?> href="analytics-dash.php"><?php echo $this->text['statistics'];?></a></li>
<li <?php header('Location: http://www.example.com/'); echo "title='".$this->text['tt-logins']."'" ;?> ><a <?php if($location == "settings") {echo 'id="current"'; } ?> href="settings-producer.php"><?php echo $this->text['manage-logins'];?></a></li>
It doesn't work.
I know that header "must be called before any actual output is sent". But I cant understand why or where am I printing the output that mess up the header() between those 2 places.

The very first character of your example is actually output being already sent.

Your output starts before <?php tag.

Related

Is there a reason why some of my nav items are underlined when active, and others are not?

I'm working on moving some nav items around on an exiting site but am unfamiliar with php (though I assumed this would just me a css issue). The issue is that some of the nav items are underlined correctly when clicked (active) while other are not.
I've tried working with the code outside of the site but because it is also php I cant get it to render properly in the browser
#main_nav .active{
text-decoration: underline!important;
}
<nav id="main_nav" class="nav-mobile col-md-8 col-md-pull-4 col-md-float-break">
<ul>
<li id="home_link"><a href="/<?php echo $data['lang']['language']; ?>" <?php if ($data['page_info']['disc'] == 'home') echo 'class="active"'; ?> ><?php echo $data['lang']['home_link']; ?></a></li>
<li id="started_link"><a href="/<?php echo $data['lang']['language']; ?>/get-started" <?php if ($data['page_info']['disc'] == 'get_started') echo 'class="active"'; ?> ><?php echo $data['lang']['started_link']; ?></a></li>
<li id="new_vehicle_link"><a href="/<?php echo $data['lang']['language']; ?>/lease-or-purchase" <?php if ($data['page_info']['disc'] == 'lease-or-purchase') echo 'class="active"'; ?> ><?php echo $data['lang']['new_vehicle_link']; ?></a></li>
<li id="keep_vehicle_link"><a href="/<?php echo $data['lang']['language']; ?>/purchase-your-vehicle" <?php if ($data['page_info']['disc'] == 'purchase-your-vehicle') echo 'class="active"'; ?> ><?php echo $data['lang']['keep_vehicle_link']; ?></a></li>
<li id="vehicle_link"><a href="/<?php echo $data['lang']['language']; ?>/turn-in-your-vehicle" <?php if ($data['page_info']['disc'] == 'turn_in_your_vehicle') echo 'class="active"'; ?> ><?php echo $data['lang']['vehicle_link']; ?></a></li>
</ul>
</nav>
It's because you switch from dashes to underscores.
get-started -> get_started
turn-in-your-vechicle -> turn_in_your_vehicle
These will never match, so they will never be active

How do i set up an OR or || when using a ?: conditional statemetn

I have the below code which works perfectly. When the title is JohnPage, it does not display in the li.
<?php foreach($this->pages AS $page) { ?>
<li <?php echo $page['title'] == ('JohnPage') ? 'style="display:none";' : ''; ?>>
<a<?php echo ($page['active']?' class="active"':'');?> href="<?php echo $page['href'];?>"><?php echo $page['title'];?></a>
</li>
<?php } ?>
However, I want to say: if the title is either JohnPage or MyPage, do not display in the li. I tried the following:
<li <?php echo $page['title'] == ('JohnPage' OR 'MyPage') ? 'style="display:none";' : ''; ?>>
<li <?php echo $page['title'] == ('JohnPage' || 'MyPage') ? 'style="display:none";' : ''; ?>>
Neither works. Thanks for your help.
Instead of putting your conditional statements there. Try it this way.
<?php
foreach($this->pages AS $page) { ?>
<li <?php if($page['title'] == 'JohnPage' || $page['title'] == 'MyPage') { ?>style="display:none<?php } ?>">
<a <?php echo ($page['active']?' class="active"':'') ?> href="<?php echo $page['href'] ?>"><?php echo $page['title'] ?></a>
</li>
<?php
} // end foreach
?>

PHP Notice: Undefined variable: http_type

I get the following PHP Notice in my opencart log file.
Undefined variable: http_type in /home/AAA/public_html/vqmod/vqcache/vq2-catalog_view_theme_template_product_product.tpl on line 3
Here is what I have in my product.php file's few first few lines
<?php if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) {
$http_type = "https:";} else {$http_type = "http:";}
<!---THIS IS LINE 3---> ?>
<?php echo $header; ?><?php echo $column_left; ?><?php echo $column_right; ?>
<div id="content"><?php echo $content_top; ?>
<div class="breadcrumb" xmlns:v="<?php echo $http_type;?>//rdf.data-vocabulary.org/#" id="brd-crumbs" >
<ul>
<?php foreach ($breadcrumbs as $breadcrumb) { ?>
<li typeof="v:Breadcrumb">
<?php echo $breadcrumb['separator']; ?><a property="v:title" rel="v:url" href="<?php echo $breadcrumb['href']; ?>"><span><?php echo $breadcrumb['text']; ?></span></a></li>
<?php } ?>
</ul>
</div>
Any help is highly appreciated
Firstly, it's just a notice so I expect everything is working as you would expect.
The notice is generated because when the $http_type variable is echoed, it hasn't necessarily been set to anything. If you add $http_type = ''; before the initial if statement, that will get rid of the notice.

If statement to not show div / h2

This for each / if statement displays changes, and then right below it it displays the changes made.
I am trying to write an if statement that tells it not to show the h2 and the #change_box if there are no changes.
Help would be greatly appreciated.
<h2 class="changes"> Changes: </h2>
<div id="change_box">
<? foreach ($audit['Events'] as $event):?>
<?if ( $event['Type'] != 'Comment'):?>
<span class="field">
<?= $event['Field']?>
</span>:
<?= $event['Value'] ?>
<?=($event['Previous'])?>
<?endif?>
<?endforeach?>
</div>
<?php
if ($changes) { // You'll have to set this variable
//Echo all of your HTML
else {
//Echo the stuff you'd rather show if they didn't change anything
}
?>
To give you an idea of how I'd write your code
<?php
if ($changes) {
echo '<h2 class="changes">Changes:</h2>';
echo '<div id="change_box">';
foreach ($audit['Events'] as $event) {
if ($event['Type'] != 'Comment') {
echo $event['Field'] . </span> . $event['Value'] . $event['Previous'];
}
}
echo "</div>"
}
?>
You could wrap your code with an if like
if(count($audit['Events'])>0)
{
//Your code
}
Wny do you open and close php tags every time? Would be better to make everything in PHP and echo whenever you want to print HTML code.
<?php
echo "<h2 class=\"changes\"> Changes: </h2>";
echo "<div id=\"change_box\">";
foreach ($audit['Events'] as $event) {
if ( $event['Type'] != 'Comment') {
echo "<span class=\"field\">".$event['Field']."</span>";
echo $event['Value'];
echo "(".$event['Previous'] .")";
}
}
echo "</div>";
?>
please make space after each <? and make sure you enabled short tags
<?php if(is_array($audit['Events']) && $audit['Events']):?>
<h2 class="changes"> Changes: </h2>
<div id="change_box">
<?php foreach ($audit['Events'] as $event):?>
<?php if ( $event['Type'] != 'Comment'):?>
<span class="field">
<?php echo $event['Field'];?>
</span>:
<?php echo $event['Value']; ?>
<?php echo $event['Previous'];?>
<?php endif;?>
<?php endforeach;endif;?>
</div>

How do I hide HTML tags with PHP?

How do I hide the html tags when the user is not logged on for the users name to be display?
<li><?php if (isset($_SESSION['user_id'])) { echo $_SESSION['first_name']; } ?></li>
<?php if (isset($_SESSION['user_id']) { ?>
<li><?php echo $_SESSION['first_name']; } ?></li>
<?php } ?>
or with short tags:
<? if (isset($_SESSION['user_id']) { ?>
<li><?= $_SESSION['first_name']; } ?></li>
<? } ?>
You can also use the alternate PHP control structures, which arguably make it more readable:
<?php if (isset($_SESSION['user_id']): ?>
<li><?php echo $_SESSION['first_name']; } ?></li>
<?php endif; ?>
With PHP you determine whether to spit out any content, you don't need to "hide" it per se like CSS...
<?php
if ( isset( $_SESSION['user_id'] ) ) {
?>
<li><?php echo $_SESSION['user_id'];?></li>
<?php
} ?>
Like this:
<?php if (isset($_SESSION['user_id'])) { echo "<li>"; echo $_SESSION['first_name']; echo "</li>"; } ?>
Like this.
<?php if ($_SESSION['user']['id']):?>
<p>
Hi <?=$_SESSION['user']['name'];?>, you are logged in!
</p>
<?php else:?>
Sign in or Register
<?php endif;?>

Categories