How do I get this while loop to dynamically produce this html? - php

I am building a picture gallery, that uses this code to display each product I have:
<div class="feature">
<imagetag alt="Image Caption"srcs="">
<div>
<p>
This is some information that can go along with an image.
Anything can be placed here, including images.
</p>
</div>
</div>
I need to create a while loop, that takes all the products in my database, and creates a div of the "feature" class for every instance. I have problems know exactly which symbols need to be escaped and etc. Your help is greatly appreciated.
here is my start:
<?php
($product_set = mysql_fetch_assoc($query)) {
print("<div class="feature"> <imagetage alt="Image Caption" srcs=$product_set[products_image]>"
);}
?>

If you are in a string, every doublequote should be escaped. Because it will close your string.
<?php
($product_set = mysql_fetch_assoc($query))
{
print "<div class=\"feature\"><img alt=\"Image Caption\" src=" . $product_set['products_image'] . ">";
}
?>
Fun thing is, I got a link from someone on stackOverflow about PHP templating. Which was using Smarty. So you don't have to use these print states anymore.

Have you tried:
print(htmlentities($my_html_string))
or htmlspecialchars? htmlentities converts all characters that have one to their HTML escape sequence, while htmlspecialchars converts only those that have meaning in HTML.

Related

Echoing un-escaped HTML

Very unusual question.
I came across some code some years in the pass that was including conditions and normal PHP syntax while echoing all content.
My question is how is that Technic/syntax called. I have been googling with some very broad terms and can't find what im looking for.
If my memory is correct, The code I viewed long time ago had un-escaped HTML and it was not required to start and stop PHP processing with <?php ?>
I Have a method within a class called Template\Labels::User()
the only purpose of that method is to echo the proper html to create a label within my webapp so that the pages are lighten of code and clear to anyone viewing the code.
Id like to avoid, having to <?php ?> for very simple boolean if
Any one know what I am looking for ?
static function User($UserObj,$isLink = true){
?>
<div class="image label bg-purple" style="margin: 4px;">
<?php if($isLink){
?><a href=""><?php
} ?>
<img src="<?php echo $UserObj -> ProfilePicture; ?>" style="height: 2em;" class="img-circle" alt="User Image">
<label style="font-size: 90%"><?php echo $UserObj->FirstName{0}.$UserObj->LastName{0}; ?></label>
<?php if($isLink){
?></a><?php
} ?>
</div>
<?php
}
Edited
After some more research by going through PHP documentation on Operator
I found Nowdoc string quoting
Can someone shed some light onto Nowdocs are to single-quoted strings what heredocs are to double-quoted strings. A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc. The construct is ideal for embedding PHP code or other large blocks of text without the need for escaping. It shares some features in common with the SGML
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc
Its good that you added code to your question so that we can all see what you are dealing with here. Now to me what I understand with your question is that you want to avoid using php tags to echo some html code based on if condition.
<?php
static function User($UserObj,$isLink = true){
$html = '<div class="image label bg-purple" style="margin: 4px;">';
if($isLink) $html .= '<a href="">';
$html .= '<img src="'.#$UserObj->ProfilePicture.'" style="height: 2em;" class="img-circle" alt="User Image">';
$html .= '<label style="font-size: 90%">'.#$UserObj->FirstName[0].#$UserObj->LastName[0].'</label>';
if($isLink) $html .= '</a>';
echo $html;
}
?>
In my thinking I thought you should just have to run php tags once and use a simple variable to add your html code to so that you can print at the end of the function.
I didn't understand some of your images but all the same your issue is printing unescaped html in PHP. In other words you want to have raw html.
There are two functions am thinking of right now which you can use depending on your desired output: html_entity_decode() and htmlentities().
html_entity_decode() is the opposite of htmlentities() in that it converts all HTML entities in the string to their applicable characters.
<?php $orig = "I'll \"walk\" the <b>d
$a = htmlentities($orig);
$b = html_entity_decode($a);
echo $a; // I'll "walk&quot
echo $b; // I'll "walk" the <b>
?>
Ref: http://www.php.net/html_entity_decode
I hope this helps solve your issue of unescaped html.

echo php variabel in HTML(wordpress)

I have created a custom wordpress post type everything works but my client asked me to insert a function that doenst show the button if the link field is empty that is also working but when I want to display the tekst or link the part where the php is inserted just doesnt shows up what am I doing wrong
I am able to get the data on other parts of this php file but not in this part of the page
<?php
$linktitle = $day_aray=get_field("under_shoe_button_title");
$linkexist = get_field("under_shoe_button_link");
echo($linktitle);
if (empty($linkexist)) {
echo '<html> <p></p></html>' ;
}
else {
echo '<html>
<a href="google.nl" class="button primary is-bevel box-shadow-3 box-shadow-4-hover expand" style="border-radius:5px;"
</html> <?php echo($linktitle); ?> <html><span></span>
<i class="icon-shopping-cart"></i></a>
</html>';
}
?>
If you would look carefully, you would notice, that you are echoing a string where, inside the string, you are trying to echo again. Even with little programming knowledge, you should understand, that it is not logical to do that.
The same goes for php opening <?php tag. You opened the tag at start of the page and later on, inside a string, you are trying to open it again. This does not work.
Instead, close the string (or escape it) and then add the echo option.
echo '<html>
<a href="google.nl" class="button primary is-bevel box-shadow-3 box-shadow-4-hover expand" style="border-radius:5px;"
</html>';
echo($linktitle);
echo '<html><span></span>
<i class="icon-shopping-cart"></i></a>
</html>';
And please, read the comments to you question and learn basic HTML
There are so many things wrong in your code
Firstly you are using echo inside echo you should use concatenation instead.
so you want to echo it like this
echo '<your html code>'.$linktitle.'<your other html code>';
Also your html code is wrong coz u are using many html tags.

Cannot echo sting with WP value "the_title()" in string - order of string changes

I'm having an issue with echo-ing a string with mutiple values/variables in it.
Here's the PHP:
echo '" alt="'.the_title ().'"height="500px" width="940px"data-caption="#'.$post->ID.'" />';
echo '
<span class=" orbit-caption" id="'.$post->ID.'"><h3>'.the_title ().'</h3><br /><p>'.wp_trim_excerpt().'</p></span>
';
And this results in this (one of five):
<img src= "http://msc-media.co.uk/wp-content/uploads/2012/07/wpid-IMAG00421.jpgDay Thirteen – Undissapointing French castles" alt=""height="500px" width="940px"data-caption="#178" />Day Thirteen – Undissapointing French castles
<span class=" orbit-caption" id="178"><h3></h3><br /><p>We finally decided to take advantage our free entry to Peyrepetouse, the pictures say it all really! Was super windy near the top, so much so that the falconry show in the castle lost a few birds, which we watched fight the wind to try to get back to their masters for over 30 minutes, [...]</p></span>
The issue with this is that .the_title(). isn't appearing in the alt="" tag, but before, merging with the img src.
The second issue is that for span orbit-caption, the_title () has no output, as it should appear between the tags.
Let me know where I'm going wrong? - Or should I just split up this string into more echo-s?
Try to use get_the_title();
Because
the_title() doing something like, and doesn't return a string.
function the_title() { echo get_the_title(); }
So it called before your echo call.

PHP String Concatenation Issue

I am learning PHP by myself and I wonder if you guys can help me to solve my problem. I want to concatenate 2 variables that are align right in the form of an hyperlink with a word. I can do that no problem, but the word is not getting aligned right as well. I have tried somethings, but it doesnt work. Please see my code:
<?php
$log = ( '<a style="float:right; "href="login.php">login </a>' ) ;
$reg = ( '<a style="float:right; "href="login.php">register </a>' );
echo $log ." or " . $reg;
?>
I need login or register to be displayed on the top right of the page, but I dont want to hyperlink the word "or".
Also, if you guys have any links for good tutorials on PHP or tutorials on how to create a good website design, maybe templates, please paste here. I am starting with websites now and I am learning by myself. Any help is appreciatted. :-)
You probably are thinking wrong in terms of HTML/CSS markup. HTML code that will be generated by your script looks as follows:
<a style="float:right; "href="login.php">login </a> or <a style="float:right; "href="login.php">register </a>
It will first float "login" to the right, then append "register" to the left of "login".
I would suggest doing it this way:
<?php
$log = 'login ';
$reg = 'register ';
echo sprintf("<div style='float:right;'>%s or %s</div>", $log, $reg);
?>
I would wrap them in a container div thats floated right,
float the links left and make the or a span that is floated left as well.
<?php
$log = ( '<a style="float:left; "href="login.php">login </a>' ) ;
$reg = ( '<a style="float:left; "href="login.php">register </a>' );
echo '<div style="float:right;">'.$log .'<span style="float:left;"> or </span>'.$reg.'</div>';
also, its better to assign classes. you could write all that markup just by assigning a class to the container div
Try encapsulating both of your anchor tags in a div tag that is floated to the right.
<?php
$log = 'login' ;
$reg = 'register';
echo "<div style='float:right; width:200px;'>$log or $reg</div>";
?>
also, notice how inside of double quotes, you can put a variable as i've done in my echo statement.
If you need a CMS try some existing distributions instead of inventing the wheel again.
For templates use something like Mustache or Smarty.

CSS Class on end MySQL row

I am having trouble creating a solution that will target the end row of a MySQL query. Currently I have a foreach function that works through the query and displays each one as a div with the information inside:
<?php $residents = Resident::find_all();
foreach($residents as $resident): ?>
<div class="submenu">
<p class="menuitem submenuheader"><?php echo $resident->name; ?></p>
<img src="images/<?php echo $resident->image_path(); ?>" width="250" class="image" />
<p><?php echo $resident->info; ?></p>
</div>
.submenu currently has a bottom border. I need to remove this on the last row returned. I have looked at DESC LIMIT 1, however this requires another MySQL query and could make things very messy...
Addd this to your CSS:
.submenu:last-child { border-bottom: 0; }
Note: this is not supported by IE < 9.
You could switch to putting the border on the top of the element, and use the :first-child pseudo selector in CSS to remove it.
http://reference.sitepoint.com/css/pseudoclass-firstchild
The :last-child selector would be nice, but it's not supported in IE before version 9, so it's not a good idea to use it if you want compatibility.
If you separate your HTML and PHP a little this is easily achieved:
<?php
function echoBlock($resident,$pClass="menuitem submenuheader") {
echo "<div class=\"submenu\">\n<p class=\"$pClass\">\n";
echo $resident->name;
echo "</p>\n<img src=\"images/";
echo $resident->image_path();
echo "\" width=\"250\" class=\"image\" />\n<p>";
echo $resident->info;
echo "</p>\n</div>\n\n";
}
$residents = Resident::find_all();
$last=count($residents)-1;//2 element array last pos is 1
for ($i=0;$i<$last;$i++) {
echoBlock($residents[$i]);
}
echoBlock($residents[$last],"menuitem");
?>
echoBlock (which could easily be a method on a class) requires the calling code to know about the classes it uses, which isn't really separating intent but it does prevent the need for an if branch on every loop. That being said it would be less efficient but perhaps more usable to set it up as:
function echoBlock($resident,$isLast=false) {
$pClass="menuitem".($isLast?"":" submenuheader");
//...
Which then doesn't need the caller to know anything about what echoBlock does.
You could try and pop the array using array_pop(), to get the last value out of the array and then inputing it using the special class after the foreach loop.
What about
Instead of echo'ing each line one by one, create one big string in PHP, search for the last entry of "submenu" and change the class.

Categories