PHP String Concatenation Issue - php

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.

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.

PHP deep inside HTML tags and attributes

I've come across a situation where there's a gnarly mix of HTML and PHP (well at least it seems that way to me because I'm not an expert in PHP). Currently, there's a hard-coded URL that I'd like to generalize using a PHP function. However, this is where I'm running into issues as this mix is getting rather complex.
After spending over 2 hours on this, I think I'm at a point where looking through topics on this doesn't seem to discuss this particular use case, and lots of trial-and-error isn't yielding the desired results.
Inside my template, I have the following code for my sidebar:
<h4>About <?php the_title(); ?> </h4>
<div id="about-this-waterfall-acf">
<?php
// First attempt HTML in PHP
$home_url = get_home_url();
echo '<div class="field-title"><a class="field-value rating" target="_blank" href=' . $home_url . '/rating-criteria/' . '>Rating:</a> <span class="rating">' . the_field('rating') . '</span></div>'; ?>
// Second attempt PHP in HTML
<div class="field-title"><a class="field-value rating" target="_blank" href="<$php $homeurl = get_home_url(); echo $homeurl; ?>/rating-criteria">Rating:</a> <span class="rating"><?php the_field('rating'); ?></span></div>
// The hard-coded URL that I'm trying to generalize
<div class="field-title"><a class="field-value difficulty" target="_blank" href="https://s1.temporary-access.com/~allacros/sandbox2/difficulty-criteria/">Difficulty:</a> <span class="difficulty"><?php the_field('difficulty'); ?></span></div>
...
The results of this code can be seen in the sidebar in:
https://s1.temporary-access.com/~allacros/sandbox2/california-switzer-falls.html
In that sidebar (beneath "About Switzer Falls" below the Hero Image), you can see 2 Ratings.
The first one has the correct link, but the formatting is off as the "2" is not where it's supposed to be and it's unformatted.
The second one has the correct formatting, but it has the incorrect link.
Anyone know what I'm doing wrong?
Thanks
For the first line, looks like there is "echo" statement in your function the_field("rating"), which cause the rating "2" output first before your "echo" execute.
For the second line, there is error at "$php", which should be "?php".
Change your 2nd statement to the following:
<div class="field-title"><a class="field-value rating" target="_blank" href="<$php $homeurl = get_home_url(); echo $homeurl; ?>/rating-criteria">Rating:</a> <span class="rating"><?php echo the_field('rating'); ?></span></div>
In addition to what others have mentioned about the $ where a ? should be, you could maybe even simplify things for yourself by doing something like this for the link. All you've got to do is echo the $home_url variable you're creating at the start.
link
It's quite simple, really. There's a number of things you are doing on one single line that you don't actually need to do...
<h4>About <?php the_title(); ?> </h4>
<div id="about-this-waterfall-acf">
<div class="field-title">
<a class="field-value rating" target="_blank" href="<?php echo home_url(); ?>/rating-criteria/">Rating:</a>
<span class="rating"><?php the_field('rating'); ?></span>
</div>
Now, things to note... I split the HTML over multiple lines instead of trying to keep it on just one. I have also removed the bulk of it from PHP processing altogether, as the static HTML doesn't need to go through PHP. This cleans it up immensely.
Lastly, if you look back at your code, you were echoing out the return value of the_field. This is where your random '2' is coming from, most likely. the_field should be outputting the value itself, so you should not concatenate it's return value on then output it.

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.

Passing DIV through $data variable, but with an Echo Statement in String

Confused by the title? hehe. Not sure how to explain this one, but I think my snippet of code should explain things a little easier.
This is what I'm trying to pass through the $data variable. div is displayed as it should be, but the echo statement inside (which I need) is NOT displayed.
Where am I messing up?
$data['packagename'] = '<div class="somedoodoo"> echo $row->subscription </div>';
You can just use string concatenation:
$data['packagename'] = '<div class="something">' . $row->subscription . '</div>';
you can't execute code inside a string like that, plus, it's the wrong quotes:
$data['packagename'] = <<<EOL
<div class="somedoodoo">{$row->subscription}</div>
EOL;
relevant docs on heredocs: http://php.net/heredoc

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

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.

Categories