Php contained inside HTML inside PHP - php

Having a bit of an issue with PHP thats contained inside HTML thats inside PHP.
I have a script running that's using an SQL query to obtain Titles, Storys, urls (etc.)
Then
<?php
#Random code here to obtain SQL results
#$id = $row['id'];
#$story_Title["$id"] = $row['story_Title'];
#end of random code block for reference
echo '<!-- BEGIN content -->
<div id="content">
<div class="post">
<p class="details1">$date["{$id}"]/(echo $story_Title["{$id}"];)
The code :
$date["{$id}"]
Is printing to the web page literally , as apposed to returning the results requested.
Is there anyway to get around this , or is it not possible.
If not possible, what would be a better solution?

Concatenate the string with .:
<?php
echo '
<!-- BEGIN content -->
<div id="content">
<div class="post">
<p class="details1">'.$date[$row['id']].'/'.$story_Title[$row['id']].'</p>
</div>
</div>';
?>
Or you can break in and out of PHP like:
<!-- BEGIN content -->
<div id="content">
<div class="post">
<p class="details1">
<?php
echo $date[$row['id']].'/'.$story_Title[$row['id']];
?>
</p>
</div>
</div>
Read about, String Operators
There are two string operators. The first is the concatenation
operator ('.'), which returns the concatenation of its right and left
arguments. The second is the concatenating assignment operator ('.='),
which appends the argument on the right side to the argument on the
left side.

Anything wrong with this?
<?php
// some php code here...
?>
<!-- BEGIN content -->
<div id="content">
<div class="post">
<p class="details1">
<?php
echo $date[$row['id']] . '/' . $story_Title[$row['id']];
?>
</p>
</div>
</div>

PHP is an HTML 'templating' language as well as a programming language.
When you have sizable amounts of HTML to send then i suggest you start using PHP in that 'mode'. Never echo 'lots' of HTML in 'chunks'. Just drop out of PHP mode and 'switch' in and out of PHP mode as required. It is a lot easier...
You are also working rather hard when accessing stuff. All the PHP code in a script is linked together so your '$row' variable is available with direct access from mostly everywhere.
the '<?= ' is short for '<?php echo ', it is always available.
If you want to do 'control statements' like 'if, foreach etc' then look at the control-structures.alternative-syntax
Tested code:
<?php
#Random code here to obtain SQL results
$row = array('id' => 1, 'story_Title' => 'how to do stuff!', 'date' => '1960-04-01');
#end of random code block for reference
?>
<!-- BEGIN content -->
<html>
<body>
<div id="content">
<div class="post">
<p class="details1"><?= $row['date']?>/(<?= $row['story_Title']?>)</p>
</div>
</div>
</body>
</html>
Output from the above code:
1960-04-01/(how to do stuff!)

You need to make sure variables inside single quotes are actually not within single quotes.
echo '<!-- BEGIN content -->
<div id="content">
<div class="post">
<p class="details1">' . $date["{$id}"] . '</p';
See this for detailed explanation

// inside a loop run this
echo '<!-- BEGIN content -->
<div id="content">
<div class="post">
<p class="details1">',
$row['date'], $row['id']
'<h1>', $row['story_Title'] , '</h1>',
'</p>
</div>
</div>';

Related

Unable to return output of PHP function, while echo displays string

I am using a PHP function to output a string of HTML. I was initially just echoing the output, but my HTML was displaying too high on the page when I did that. In the past, using ob_start() and returning ob_get_clean() has solved this issue, yet it is not working in this case. In fact, when I use this method, no HTML is output at all. When I use var_dump() on the variable, it is confirming that my HTML string is indeed being saved into the object, yet return still outputs nothing.
<?php
add_shortcode( 'slider-display', 'slider_display' );
function slider_display(){
$slider_html = '';
ob_start();
?>
<!-- Slider main container -->
<div class="swiper-container one-column-swiper" id="swiper">
<div class="swiper-wrapper">
<div class="swiper-slide">
<div class='slide-content'>
<img src="img.jpg">
</div>
</div>
<div class="swiper-slide">
<div class='slide-content'>
<img src="img.jpg">
</div>
</div>
</div>
</div>
<?php
$slider_html = ob_get_clean();
return $slider_html;
} //end function
Any help is very much appreciated!

PHP Loop on website

I am trying to add an if else statement to a file in Magento.
Basically I want it to display different content depending on the body class.
For example, if the body element had a specific class, such as in:
<body class="specific-class">
Then within that body block, then the contents of it might display:
<section class="banner banner-inner <?php Mage::helper('function')->convert_category_name($category_name); ?>">
<div class="wrapper">
<?php echo $this->getChildHtml('breadcrumbs') ?>
<?php echo $this->getLayout()->getBlock('breadcrumbs')->toHtml(); ?>
</div>
<div class="bottom-rip"></div>
</section>
But if the body had a different class, or no class at all, it would default to:
<div class="no_content">
<?php echo="<h1>No content</h1>"; ?>
</div>
Does anyone know the syntax for performing such an operation?

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; ?>

How to get a div class to be displayed on certain pages using a template and php

I have a template for all my web pages but i want a div class not to be seen on certain pages. I use the URL to get the content out of a database with php so an example of my URL
= index.php?categorie=navigatie_bar&onderwerp=startpagina
I need a code to say get catagory from the url and if catagory is not navigation_bar
echo '<div class"fb-comments"></div>';
can someone help ?
<div id="container">
<div id="pagina_text">
{{ CONTENT }}
</br>
<div class="rw-ui-container"></div>
</br></br>
<?php
if(strcmp($_GET['categorie'], "navigatie_bar") != 0)
{
echo '<div class="fb-comments" data-href="http://alledaagsetips.nl" data-numposts="10" data-colorscheme="light"></div>';
}
?>
</div> <!-- end pagina_text -->
</div><!-- end container -->
You can access the URL variables via $_GET['name_of_parameter'].
if(strcmp($_GET['category'], "navigation_bar") != 0)
{
echo '<div class="fb-comments"></div>';
}
In this case, only if index.php?category=navigation_bar the div will be echoed.
Take attention to your spelling, in your example you wrote catagory instead of category
EDIT:
You can do $_GET['category'] == 'navigation_bar', but for string comparisons it is sometimes safer to use the strcmp (string compare) function.
See String comparison using == vs. strcmp
try this
<?php
if($_REQUEST['category'] != 'navigation_bar'){
echo '<div class="fb-comments"></div>';
}else{ //do this }
?>
I think this is just basic PHP, a simple condition. But you seem to have also errors in our template. I would suggest running a w3c validator check, so you can see what you have wrong (wrongly nested tags, syntax mistakes etc..)
W3C Validator Online
UPDATE:
<div id="container">
<div id="pagina_text">
{{ CONTENT }}
<br />
<div class="rw-ui-container"></div>
<br /><br/>
<?php
if(strcmp($_GET['categorie'], "navigatie_bar") != 0)
{
echo '<div class="fb-comments" data-href="http://alledaagsetips.nl" data-numposts="10" data-colorscheme="light"></div>';
}
?>
</div> <!-- end pagina_text -->
</div><!-- end container -->

PHP Code Within Element Causes Repetition

I'm finding that placing php code snippets within my website's & tags is causing a problem with the code after it's placement. I first noticed it when placing avatars on my site in a list format where it works fine up to a point and then the same image is repeated for the rest of the list. Here is the code that I'm uisng for that:
<?php
$show_user .= "
<div class=\"section\">
<div class=\"sectionInner\">
<div class=\"searchAvatar\"><img class=\"searchAvatarSize\" src=\"uploads/avatars/$member_avatar\"></div>
<div class=\"searchInformation\"><div class=\"searchInformationPrimary\">$member_name</div><div class=\"searchInformationSecondary\"><i>"$member_summary"</i></div></div>
<div class=\"searchInformation\"><div class=\"searchInformationPrimary\">$member_subtype $member_type</div><div class=\"searchInformationSecondary\">$member_city, $member_county</div><div class=\"searchInformationThird\">View Details</div></div>
<div class=\"clearLeft\"></div>
</div>
</div>
<div class=\"searchResultSplitter\"></div>
";
?>
<?php echo $show_user; ?>
I'm now noticing it when placing php within my navigation bar where it's not closing the tag. Here is my code for the bar:
<div id="pageSubNavigation" class="page<?php echo $thispage; ?>SubNavigation">
Next Page
</div>
After this, all of the other tags show the same link. Any ideas why this might be?
You need to either jump out of the HTML to add the variables or wrap them in {} so they appear correctly.
Example:
<div class=\"searchAvatar\"><img class=\"searchAvatarSize\" src=\"uploads/avatars/".$member_avatar."\"></div>
or
<div class=\"searchAvatar\"><img class=\"searchAvatarSize\" src=\"uploads/avatars/{$member_avatar}\"></div>
Check out this article about string formatting with variables in PHP.

Categories