echo php variabel in HTML(wordpress) - php

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.

Related

How can I use a php parameter in HTML markup?

So I have tried researching this before jumping into asking this question. Given that I have an index.php, and a cookie which stores the username, saved as $name, most answers tell me its simple to do this:
echo '<h3>'.$name.'</h3>'
But this doesnt work for me, and I assume its because im doing strange syntax for an if statement first, and I want to use the parameter within this if statement. My exact code looks more like this:
<?php
//store the cookie
$name=$_COOKIE['user'];
//check that it is set
if(isset($_COOKIE['user'])):
<section id="login">
<h1> Welcome</h1>
echo '<h3>'.$name.'</h3>';
</section>
else: //prompt to login
endif;
?>
It is meant to show a welcome message to a user that is logged in, adressing them by name, otherwise prompt the user to login.
So my question is: Why doesn't the parameter reflect at all? (shows nothing when the page is loaded) and How can I get this to work as intended?
ps. Please don't worry about the security risk of using cookies to do this etc. It is purposefully vulnerable.
pps. I am 100% sure the cookie is set, I viewed it with a cookie browser.
It doesn't work because the previous two lines are invalid PHP so it would throw an error and stop working.
If you want to use the echo approach then the correct syntax would be:
echo '<section id="login">';
echo '<h1> Welcome</h1>';
echo '<h3>'.$name.'</h3>';
It is important to never insert external data as if it were raw HTML. This can render you vulnerable to XSS attacks. Treat the input as plain text and convert it to HTML before outputting it into an HTML document.
$name_html = htmlspecialchars($name);
echo '<section id="login">';
echo '<h1> Welcome</h1>';
echo '<h3>'.$name_html.'</h3>';
You can make the code easier to read by using variable interpolation:
$name_html = htmlspecialchars($name);
echo '<section id="login">';
echo '<h1> Welcome</h1>';
echo "<h3>$name_html</h3>";
And when outputting large chunks of HTML, it is easier to just drop out of PHP mode entirely:
$name_html = htmlspecialchars($name);
?>
<section id="login">
<h1> Welcome</h1>
<h3><?=$name_html?></h3>
<?php
Aside
Check to see if the cookie is set before you try to use it, not afterwards!
if(isset($_COOKIE['user'])):
$name=$_COOKIE['user'];
you should use php + html like this:
<?php $var = 'my string'; ?>
<?php if ($var == 'my string') : ?>
<h1><?php echo $var; ?></h1>
<?php endif;?>
this breaks up the if statement, allowing you to add html inbetween the conditional statement without having to echo and html from php
You should post any errors you get - But your can't just write html inside php, without either echoing it out, or ending the php for a while.
<?php
//store the cookie
$name=$_COOKIE['user'];
//check that it is set
if(isset($_COOKIE['user'])):?>
<section id="login">
<h1> Welcome</h1>
<?php echo '<h3>'.$name.'</h3>'; ?>
</section>
<?php else: //prompt to login
endif;
?>
To display HTML code only if a php statement is true, just close the php tag right after you have opened the if statement and then put your HTML code between.
<?php
//store the cookie
$name=$_COOKIE['user'];
//check that it is set
if(isset($_COOKIE['user'])) {
?>
<section id="login">
<h1> Welcome</h1>
<h3><?php echo $name; ?></h3>
</section>
<?php
} else {
echo 'Please login';
}
?>

Why is this XML attribute showing up twice

In PHP i'm using the following code to put a banner at the top of my website.
$eventinfo = simplexml_load_file("eventinfo.xml");
<div id="eventinfo"><?php foreach($eventinfo->children() as $child){ $final = $child["name"]."...<a href='".$child["adr"]."'>more info...</a>"; } ?>
</div>
The XML doc is available at the following: http://eastsidespeedway.raceresults.co/eventinfo.xml
If you go to http://eastsidespeedway.raceresults.co/index.php you'll see that the more info... link is showing up twice. One with the correct link, and the other with a link to the same page (index.php).
Can anyone shed some light on what I'm doing wrong?
Also. If you see anything that I'm doing wrong, or you know something that's easier - let me know! This is my first time using XML/PHP so I'm kinda just wingin it. Haha.
this will work for you
<?php
$doc = new DOMDocument();
$doc->load('http://eastsidespeedway.raceresults.co/eventinfo.xml');
$title = $doc->getElementsByTagName('title');
$link = $doc->getElementsByTagName('link');
//print_r($eventinfo);
?>
<div id="eventinfo">
<?php echo $title->item(0)->getAttribute('name'); ?>
<a href='<?php echo $link->item(0)->getAttribute('adr'); ?>'>More Infoo..</a>
</div>
If you look at your source:
<div id="eventinfo">5/18/2013 - Al Smiley Memorial...<a href=''>more
info...</a>...<a href='http://www.eastsidespeedway.com/dirt.html'>more
info...</a></div>
You've got two hyperlinks- one href is blank meaning that it will redirect to the current page, check your HTML code first to see if you've accidentally duplicated the element, otherwise look at the construction of your string in the php code

make phone number clickable to dial screen on mobile?

How can i make a phone number that's being echoed in php from mysql database?
I'm new to php and mysql and still learning, please can someone explain or show me how to make a phone number clickable to go to the dialing screen on a mobile.
Here's the code im using below, i some how need to adapt the hyperlink aroung my '
<?php
$user_type_set = user_type_profile();
while ($user = mysql_fetch_array($user_type_set)) { ?>
<div class="contact-buddy"></div><div class="contact-buddy2"></div>
<div class="contact_details_phone"><p><strong>Phone: <div class="phone_font"><strong><?php echo $profile['contact_number'] ?></strong></div></div></p>
<div class="contact_details_email"><p><strong>Email: <?php echo $profile['public_email'] ?></strong></p></div>
<? } ?>
This should definitely work on iphones, but I believe it would work for androids as well. Your php should output as it's final product something similar to:
1-800-555-5555
If you want to make the phone number returned by <?php echo $profile['contact_number'] ?> clickable then you can use a straight HTML code block with inline PHP:
<?php echo $profile['contact_number']; ?>
Or clean it up a bit with this PHP:
<?php
$phone = $profile['contact_number'];
echo "$phone";
?>
Whichever method you want.

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.

Changing Text in PHP

I haven't found anytihng in Google or the PHP manual, believe it or not. I would've thought there would be a string operation for something like this, maybe there is and I'm just uber blind today...
I have a php page, and when the button gets clicked, I would like to change a string of text on that page with something else.
So I was wondering if I could set the id="" attrib of the <p> to id="something" and then in my php code do something like this:
<?php
$something = "this will replace existing text in the something paragraph...";
?>
Can somebody please point me in the right direction? As the above did not work.
Thank you :)
UPDATE
I was able to get it working using the following sample:
Place this code above the <html> tag:
<?php
$existing = "default message here";
$something = "message displayed if form filled out.";
$ne = $_REQUEST["name"];
if ($ne == null) {
$output = $existing;
} else {
$output = $something;
}
?>
And place the following where ever your message is to be displayed:
<?php echo $output ?>
As far as I can get from your very fuzzy question, usually you don't need string manipulation if you have source data - you just substitute one data with another, this way:
<?php
$existing = "existing text";
$something = "this will replace existing text in the something paragraph...";
if (empty($_GET['button'])) {
$output = $existing;
} else {
$output = $something;
}
?>
<html>
<and stuff>
<p><?php echo $output ?></p>
</html>
but why not to ask a question bringing a real example of what you need? instead of foggy explanations in terms you aren't good with?
If you want to change the content of the paragraph without reloading the page you will need to use JavaScript. Give the paragraph an id.<p id='something'>Some text here</p> and then use innerHTML to replace it's contents. document.getElementById('something').innerHTML='Some new text'.
If you are reloading the page then you can use PHP. One way would be to put a marker in the HTML and then use str_replace() to insert the new text. eg <p><!-- marker --></p> in the HTML and $html_string = str_replace('<!-- marker -->', 'New Text', $html_string) assuming $html_string contains the HTML to output.
If you are looking for string manipulation and conversion you can simply use the str_replace function in php.
Please check this: str_replace()
If you're using a form (which I'm assuming you do) just check if the variable is set (check the $_POST array) and use a conditional statement. If the condition is false then display the default text, otherwise display something else.

Categories