PHP code to block out any code in the page [duplicate] - php

This question already has answers here:
HTML is still reading php code with <!---->
(7 answers)
Closed 8 years ago.
I currently have the following code on my page.
<?php
if($result["r_approved"] == "APPROVED"){
echo "<!--";
}
?>
<div class="main">
<div class="main-sub">
<?php include('http://www.contractorsintelligence.com/contractors-license/includes-page-elements/navigation1.php'); ?>
<div id="mid-top"><img src="https://www.contractorsintelligence.com/images/shadowbg-top.png" width="990" height="20" alt="Top Spacer"/></div>
<div id="mid_shdw">
<?php
if($result["r_approved"] == "APPROVED"){
echo "-->";
}
?>
With this code, I'm trying to block/ignore a block of code with <!-- and -->, but it does not want to ignore php code. How would I use PHP to block out an entire section of the code? I would really appreciate if you would use my current "if" statement variables.

Why not this:
<?php
if($result["r_approved"] != "APPROVED"){
?>
<div class="main">
<div class="main-sub">
<?php include('http://www.contractorsintelligence.com/contractors-license/includes-page-elements/navigation1.php'); ?>
<div id="mid-top"><img src="https://www.contractorsintelligence.com/images/shadowbg-top.png" width="990" height="20" alt="Top Spacer"/></div>
<div id="mid_shdw">
<?php
}
?>

Put the code inside the if. Please note that I also changed the if to a "does not equal"
<?php if($result["r_approved"] !== "APPROVED"): ?>
<div class="main">
...
</div>
<?php endif; ?>
I used the alternative syntax for readability reasons as I think it looks much cleaner than with the brackets if you mix PHP with HTML.

I'd say a much cleaner way to do this would be to use the alternative syntax for the if statement. This removes the obfuscation caused by printing comments from php. In your case, that would be:
<?php if(! $result["r_approved"] == "APPROVED"):?>
<div class="main">
<div class="main-sub">
<?php include('http://www.contractorsintelligence.com/contractors-license/includes-page-elements/navigation1.php'); ?>
<div id="mid-top"><img src="https://www.contractorsintelligence.com/images/shadowbg-top.png" width="990" height="20" alt="Top Spacer"/></div>
<div id="mid_shdw">
<?php endif; ?>
You can read more on the alternative syntax here.

Related

If/or statement throwing error in php 5.3.10

I have an if statement which is throwing a syntax error, unexpected T_VARIABLE error and I have no idea why.
The code:
<?php if($contentTop || $contentLeft || $contentCenter || $contentRight): ?>
<div>a bunch of html</div>
<?php endif; ?>
The variable contains eiter an empty string or a bunch of html.
I have tried changing the || to or but it doesn't seem to change anything. neither does opening and closing the if statement with curly brackets. It only seems to work when i only have one variable in the statement. I have also tried using !empty($contentTop) to check the variables.
The server is running PHP version 5.3.10-1ubuntu3.26 but as far as I can see, that should not change anything regarding if statements
EDIT:
On request of the full markup:
<?php
$contentTop = get_field('section2new_content');
$contentLeft = get_field('section2new_left');
$contentCenter = get_field('section2new_center');
$contentRight = get_field('section2new_right');
if( $contentTop || $contentLeft || $contentCenter || $contentRight ): ?>
<div class="bbh-inner-section teaser-3-col" id="section2temp">
<?php if($contentTop): ?>
<div class="grid-container top">
<div class="row">
<div class="col-sm-12">
<?php echo $contentTop; ?>
</div>
</div>
</div>
<?php endif; ?>
<?php if($contentLeft || $contentRight || $contentCenter): ?>
<div class="grid-container bottom">
<div class="row">
<div class="col-sm-4 left">
<?php echo $contentLeft; ?>
</div>
<div class="col-sm-4 center">
<?php echo $contentCenter; ?>
</div>
<div class="col-sm-4 right">
<?php echo $contentRight; ?>
</div>
</div>
</div>
<?php endif; ?>
</div>
<?php endif; ?>
As i noticed myself i have second if / || statement further down, which seems to work fine. It's also not that I mistakenly inserted a wrong type of space or whateever, I have manually rewritten the code multiple times
I tried #Poiz answer which worked. Being a bit suspicious i tried my own original code again, which now also worked. Human error once again. Maybe it was some illegal version of a space or tab.
Would you be open to trying something a little odd? Anyways, it is essentially the same thing you did except that we turned the if Statements into Ternary Operations. This is because like You said; you've tried almost all combinations of if without success, except when only one Condition is supplied.... The Idea here is simply to crunch down the Line that does the main Comparison to a single, simple Boolean Value, which can then be used within the Subsequent if. Perhaps this works for you since your Magical if works only in the Case you have but One Condition to test for...
<?php $state = ($contentTop || $contentLeft || $contentCenter || $contentRight) ? true : false; ?>
<?php if($state): ?>
<div>a bunch of html</div>
<?php endif; ?>

Echo an image tag with site_url() inside PHP tags

I have a loop in my view that outputs all the content gathered from the database:
<?php foreach($content as $contentRow): ?>
<?php
echo $contentRow->value;
?>
<?php endforeach; ?>
This works fine for HTML strings like:
<h2><strong>Example Text</strong></h2>
however I have some image content that I would like to display and I have tried the following database entries to no avail:
<img src="<?php echo site_url('pathToImage/Image.png'); ?>" alt="Cover">"
<img src="site_url('pathToImage/Image.png')" alt="Cover\">"
I feel like I am missing a step on how to use PHP values in this way.
How do I access the URL of the image and use that to show the image?
Full Code Edit
<?php
$CI =& get_instance();
?>
<div class="container">
<div class="row">
<div class="col-md-9">
<div class="col-md-2"></div>
<div class="col-md-20">
<!--<form class="form-center" method="post" action="<?php echo site_url(''); ?>" role="form">-->
<!-- <h2 class="">Title</h2>
<h2 class=""SubTitle/h2>-->
<?php echo $this->session->userdata('someValue'); ?>
<!--//<table class="" id="">-->
<?php foreach($content as $contentRow): ?>
<tr>
<td><?php
echo $contentRow->value;
?></td>
</tr>
<?php endforeach; ?>
<!--</table>-->
<!--</form>-->
</div>
<div class="col-md-2"></div>
</div>
</div>
</div><!-- /.container -->
and the values are being read out in $contentRow->value;
I have to verify this, but to me it looks like you are echo'ing a string with a PHP function. The function site_url() is not executed, but simply displayed. You can execute it by running the eval() function. But I have to add this function can be very dangerous and its use is not recommended.
Update:
To sum up some comments: The use of eval() is discouraged! You should reconsider / rethink your design. Maybe the use of tags which are replaced by HTML are a solution (Thanks to Manfred Radlwimmer). Always keep in mind to never trust the data you display, always filter and check!
I'm not going to accept this answer as #Philipp Palmtag's answer helped me out alot more and this is more supplementary information.
Because I'm reading data from the database it seems a sensible place to leave some information about what content is stored. In the same table that the content is stored I have added a "content type" field.
In my view I can then read this content type and render appropriately for the content that is stored. If it is just text I can leave it as HTML markup, images all I need to do is specify the file path and then I can scale this as I see fit.
I have updated my view to something akin to this and the if/else statement can be added to in the future if required:
<?php foreach($content as $contentRow): ?>
<?php if ($contentRow->type != "image"): ?>
<?php echo $contentRow->value; ?>
<?php else: ?>
<?php echo "<img src=\"".site_url($contentRow->value)."\">"; ?>
<?php endif; ?>
<?php endforeach; ?>

Add HTML elements to the current page using PHP

So I have the need to dynamically add html content using php which isnt the tricky part but I'm trying to put the HTML into a different location in the document than where the PHP is being run. So for example:
<div id="firstDiv">
<?php
echo "<div id=\"firstDivA\"></div>";
echo "<div id=\"secondDivA\"></div>";
?>
</div>
<div id="secondDiv">
</div>
But I want to be able to place the some HTML inside "secondDiv" using the PHP that is executed in the "firstDiv". The end result should be:
<div id="firstDiv">
<div id="firstDivA"></div>
</div>
<div id="secondDiv">
<div id="secondDivA"></div>
</div>
But I have no idea how to go about doing that. I read about some of the DOM stuff in PHP 5 but I couldn't find anything about modifying the current document.
You can open/close "blocks" of PHP wherever you like in your HTML
<div id="firstDiv">
<?php echo '<div id="firstDivA"></div>'; ?>
</div>
<div id="secondDiv">
<?php echo '<div id="secondDivA"></div>'; ?>
</div>
You can also capture the output if necessary with ob_start() and ob_get_clean():
<?php
$separator = "\n";
ob_start();
echo '<div id="firstDivA"></div>' . $separator;
echo '<div id="secondDivA"></div>' . $separator;
$content = ob_get_clean();
$sections = explode($separator, $content);
?>
<div id="firstDiv">
<?php echo $sections[0]; ?>
</div>
<div id="secondDiv">
<?php echo $sections[1]; ?>
</div>
Why not just move the relevant code to the right place?
<div id="firstDiv">
<?php
echo "<div id=\"firstDivA\"></div>";
?>
</div>
<div id="secondDiv">
<?php
echo "<div id=\"secondDivA\"></div>";
?>
</div>
The .php file is continuous thus if you have two separate <?php ?> tags they will be able to share the same variables.
<div id="firstDiv">
<?php
echo "<div id=\"firstDivA\"></div>";
$div2 = "<div id=\"secondDivA\"></div>";
?>
</div>
<div id="secondDiv">
<?php echo $div2 ?>
</div>
This will give the desired effect. (Demonstrates the use of variables)
I'm not sure what you're asking. Perhaps just add an echo statement to the second div.
<div id="firstDiv">
<?php echo "<div id=\"firstDivA\"></div>"; ?>
</div>
<div id="secondDiv">
<?php echo "<div id=\"secondDivA\"></div>"; ?>
</div>
Or do you mean you want to make DIV changes after PHP? Try jQuery!
Or do you mean you want to make DIV changes before PHP is finished? Perhaps phpQuery is good for you then.
If you want to work with XML-data (read XHTML), you'd rather use an appropriate XML processor.
DomCrawler is an excellent to work with DOM. It works with the native DOM Extension and therefore is fast and widely used.
Here an example from the doc on how to add content:
$crawler = new Crawler();
$crawler->addHtmlContent('<html><div class="foo"></div></html>');
$crawler->filter('div')->attr('class') // returns foo

Unexpected bracket - PHP

Here is some PHP mixed with HTML, I apologize that it's quite messy.
<div class="media"><?php echo ($inf['post_url']) ?><img src="<?php echo($inf['photos'][0]['alt_sizes'][0][url]); ?>" /></div>
{block:Caption}<?php if (array_key_exists('caption', $inf))?><div class="copy"><?php Echo ($inf['caption']);?></div><?php }; ?>
<?php }; ?>
The second line produces this error
Parse error: syntax error, unexpected '}' on line 1708
I do not see anything wrong with the brackets. What is producing this error?
If you'd format your code properly so it'd actually be readable, the superfluous bracket (and missing opening bracket) would be easy to spot:
<div class="media">
<?php echo ($inf['post_url']) ?>
<img src="<?php echo($inf['photos'][0]['alt_sizes'][0][url]); ?>" />
</div>
{block:Caption}
<?php if (array_key_exists('caption', $inf))?>
<div class="copy">
<?php Echo ($inf['caption']);?>
</div>
<?php }; ?>
<?php }; ?>
It greatly helps if you indent your code correctly.
<div class="media">
<?php echo ($inf['post_url']) ?>
<img src="<?php echo($inf['photos'][0]['alt_sizes'][0][url]); ?>" />
</div>
{block:Caption}
<?php if (array_key_exists('caption', $inf)) ?>
<div class="copy">
<?php Echo ($inf['caption']);?>
</div>
<?php }; ?>
<?php }; ?>
Very basic, the closing brackets do not have corresponding opening brackets.
Some more stylistic advice: echo is a PHP construct, so you don't need to call it like a function, that is echo something is equivalent to echo(something) but the former is preferred. Also, it pays to be consistent with the capitalisation of reserved words, i.e. if you're using lowercase, always use lowercase for reserved words.
There was an extra bracket at the end as well as some superfluous semicolons, and "echo" was capitalized. Give this a shot:
<div class="media">
<?php echo ($inf['post_url']); ?>
<img src="<?php echo($inf['photos'][0]['alt_sizes'][0][url]); ?>" />
</div>
{block:Caption}
<?php if (array_key_exists('caption', $inf)) {?>
<div class="copy">
<?php echo ($inf['caption']);?>
</div>
<?php } ?>
If you boil off all the HTML, lines 2 and 3 come out to this:
if (array_key_exists('caption', $inf)) Echo ($inf['caption']); }; };
You don't need either of those closing right curly braces.

How to remove anchor from active navigation page using PHP?

I am sure this is a fairly simple question to answer, but I am new to PHP, so I was hoping someone could help me solve this problem.
I have a dynamic navigation menu that works really well, but I want to remove the link from the current page in the menu.
Here is my code:
<div id="navigation_menu">
<?
foreach($pagedata->menu as $menuitem){
$class = ($menuitem->uri == $requesteduri) ? 'navigation selection' : 'navigation page_select';
?>
<div id="<?=$menuitem->uri?>" class="<?=$class?>">
<img class="nav_icon" src="<?=PROTOCOL?>//<?=DOMAIN?>/img/<?=$menuitem->uri?>.png">
<h1><?=$menuitem->title?></h1>
<h2><?=$menuitem->description?></h2>
<img class="go" src="<?=PROTOCOL?>//<?=DOMAIN?>/img/go.png">
</div>
<?
}
?>
</div>
Any help would be greatly appreciated. Thanks!
UPDATED CODE: (this is what works for me now)
<div id="navigation_menu">
<?
foreach($pagedata->menu as $menuitem){
$class = ($menuitem->uri == $requesteduri) ? 'navigation selection' : 'navigation page_select';
?>
<div id="<?=$menuitem->uri?>" class="<?=$class?>">
<img class="nav_icon" src="<?=PROTOCOL?>//<?=DOMAIN?>/img/<?=$menuitem->uri?>.png">
<h1>
<?php if ($menuitem->uri == $requesteduri):?>
<?=$menuitem->title;?>
<?php else: ?>
<?=$menuitem->title?>
<?php endif;?>
</h1>
<h2><?=$menuitem->description?></h2>
<img class="go" src="<?=PROTOCOL?>//<?=DOMAIN?>/img/go.png">
</div>
<?
}
?>
</div>
I don't know what your loop is outputting, but you want to match your page name with the menuitem->uri. So you'd get your page name like.. (Put this outside the loop)
<?php echo base_name($_SERVER['REQUEST_URI']); ?>
find out what your loop is outputting (Put this in the loop):
<?php echo $menuitem->uri; ?>
Then you'd create an if statement to compare the current menuitem in the loop and the page request, this is just an example:
<h1>
<?php if (base_name($_SERVER['REQUEST_URI']) == $menuitem->uri):?>
<?=$menuitem->title?>
<?php else: ?>
<?=$menuitem->title;?>
<?php endif;?>
</h1>
Put a conditional around the anchor text to see if $menuitem->uri is equal to the current page URL, accessible from `$_SERVER['REQUEST_URI'] before outputting the anchor tags.

Categories