What I need to do is echo a PHP variable but It needs to be on the bottom of a certain DIV in my HTML page, not just the bottom of the page. Putting it in that DIV id should cover the formatting because that div is formatted using CSS.
Does anyone know how to do this?
I believe what you are looking to do is something similar to the following.
<div id="divName"><?php echo($variable); ?> </div>
<div><?php echo $variable; ?> </div>
Am I not understanding what you're trying to do or does that work?
You can also write like this:
<?php echo "<div id='idName'> Hello World </div>"?>
<?php echo "<b>This text is in bold.</b>"?>
Related
We write PHP code inside the HTML code via <?php ... ?> tags. So normally it would not make sense to write HTML code inside PHP code that is already inside HTML code, if you can just exit the PHP for the lines you need. But what if you need the HTML code in the same line as you have the PHP code?
My example would go like this:
<div>
<?php ($bool) ? <script>...</script> : <script></script> ?>
</div>
Is this:
<div>
<?php if($bool): ?>
<script>...</script>
<?php else: ?>
<script>...</script>
<?php endif; ?>
</div>
the only way?
Note: instead of <script> you could have <h1>, <strong>, <title> or any other "one-liner".
Thank you in advance.
Sure, alternative syntax would be the way to go when you have multiple lines of HTML, as you already stated...
However, for one liners, you can shorten <?php echo '...' ?> with <?= '...' ?> and wrap your HTML within single or double quotes, depending if you are already using double quotes within your HTML syntax. You may also escape them if you like, but that'd be messy.
<div>
<?= ($bool) ? "<script>...</script>" : "<script></script>" ?>
</div>
In order to print any string into your html code from PHP snippets use echo function.
http://php.net/manual/en/function.echo.php
So you just need to add echo
<div>
<?php
if($bool) {
echo '<script>...</script>';
} else {
echo '<script>...</script>';
} ?>
Stumbled upon this and decided to answer my own question just to point other newbies in the right direction.
Important note: Nowadays I'm using Laravel Framework and if you don't know it, you should definitely get to know it (there are alternatives though).
But I started following MVC architecture strongly even before that. So even before Laravel's Blade templates, my views looked something like the following.
<html>
<body>
<?php if ($isUserAuthenticated) : ?>
<div>
<span>Welcome <?= $username ?>
</div>
<?php else : ?>
Login
<?php endif ?>
</body>
</html>
As you can see there is absolutely no data manipulation in the view.
I also tried my best not to store any HTML strings into variables, but sometimes it makes for less code, so I did something like the following.
$alert = match($errorCode) {
1 => <<<HTML
<div class="alert alert-danger">Big error</div>
HTML,
2 => <<<HTML
<div class="alert alert-warning">Small error</div>
HTML,
default => ""
};
That way I can keep syntax highlighting (in VSCode) for HTML.
Note: match expression is new in PHP8, but you could achieve the same before with a switch statement.
I am using kohana framework.
I am trying wrap a url in a div in other to make the div clickable.
I normally do it like this
<a href "www.test.com">
<div class="foo1>
<div class="foo2">
<?php ?>
</div>
</div>
</a>
but in this case I am using this framework.
the div I am trying to wrap as other div and those div as php tags
I am trying to wrap this
<?php echo HTML::anchor($link .$test1->getid(). '/travel'); ?>
any idea how I will wrap this in in a div
or Make <?php echo HTML::anchor($link .$test1->getid(). '/travel'); ?>
work on
jquery window.location
The second parameter is the text that will be shown as the title/text inside the a tag, check the documentation for HTML::anchor
Maybe this will work.
<?php echo HTML::anchor($link .$test1->getid(). '/travel', <div class="foo1"><div class="foo2">your content</div></div>); ?>
Please note that putting divs in anchor tags may not be semantically right.
How can I display a certain HTML code in a php generated page.
For example, php will automatically display the name and category of an item, yet I want to add custom pictures to this generated page. Is there any way to do this without creating a separate HTML page to include into the generated page?
I'm hoping for something like a database entry where to add the html part to display. Is it possible?
ps. Sorry if I didn't express my idea in a completely understandable way.
Store the image URLs just like you are storing the names and categories. Then you would just do something like:
<span id="name"><?= $name ?></span>
<span id="category"><?= $category ?></span>
<img src="<?= $image ?>" />
Maybe you need to clarify - why do you need different HTML for each page, if the only thing changing are the images?
Keep in mind here I use <?= $foo ?> which is generally considered bad practice, and is just shorthand for <?php echo $foo; ?>.
Just wrap the PHP in your HTML:
<h1>My Test</h1>
<?= $myCategory; ?> - <?= $myItem; ?>
<img src ="mySource" alt="myImg" width="15" height="15" />
This also works for your HTML in your Database:
<p><?= $myDatabaseHTMLContent ?></p>
Cheers,
Max
<?php
//Your php code
?>
<img src="" />
<?php
//Your php code
?>
You can put html in a php file, like the example above.
I know this code isn't correct, but I would like to inject the HTML and PHP into the title of the below tag.
<p class="related_articles" title="<?php echo the_field(related_articles_1_title); ?>"Related Articles</p>
I am using a jQuery tooltip plugin (http://flowplayer.org/tools/tooltip/index.html) that grabs content from the title tag and displays it as the tooltip content. I have Advanced Custom Fields setup (WordPress plugin) that allows me to publish custom field content. In effect, the content I post in these custom fields will end up in the tooltip.
My goal is to produce a tooltip when the user hovers over "Related Articles", that displays a link that is clickable. Again, the above jQuery tooltip plugin grabs the content from the title, which is why this is causing difficulty
Ok, as we understand what you are trying to do. Lets get some things clear.
<p class="related_articles" title="<?php echo the_field(related_articles_1_title); ?>"Related Articles</p>
Is SO wrong on so many levels. First of all things, its not valid in any way. Second of all, you are not ending your <a>. You are also missing one echo and target="", inside title="" was not escaped: target=\"\".
So in a nutshell to straight up answer your question, this maybe will work (maybe, because its seriously uncommon and nonstandard)
<p class="related_articles" title="<?php echo the_field(related_articles_1_title); ?>">Related Articles</p>
Also, as one of the users already mentioned. If your server server enables short open tags, then you could make the <?php echo $foo; ?> shorter: <?= $foo ?>. So in your codes case it would look like:
<p class="related_articles" title="<?= the_field(related_articles_1_title) ?>">Related Articles</p>
However, as probably mentioned already. This is not recommended method and may produce all sort of issues. I recommend to research for a better solution.
Use the short form if your server supports it:
<?=the_field(related_articles_1_link)?>
or else, you should echo it:
<?php echo the_field(related_articles_1_link); ?>
<p class="related_articles" title="<?php echo htmlspecialchars(''. the_field(related_articles_1_title) .''); ?>">Related Articles</p>
While this should work, I am not sure if your mentioned jQuery plugin is able to interpret the given html in a title attribute as HTML (you need to test it). Most likely this will be interpreted as text and all tags will be visible to the end user, but this is not what you want.
I found an example of how to implement this in a different manner: http://flowplayer.org/tools/demos/tooltip/any-html.html
Is it possible that this is what you want?
<p class="related_articles">
Related Articles:
<a href="<?php echo the_field(related_articles_1_link); ?>" target="_blank">
<?php echo the_field(related_articles_1_title); ?>
</a>
</p>
Now that you've clarified that your question was regarding the jQuery Tooltip plugin, you should do the following:
<p class="related_articles">Related Articles</p>
<a class="tooltip" href="<?php the_field(related_articles_1_link); ?>" target="_blank">
<?php echo the_field(related_articles_1_title); ?>
</a>
Javascript:
$('.related_articles').tooltip();
The reason this works:
The tooltip plugin looks at the element right after the one which .tooltip() was applied to. If that next element has a class name of tooltip, it'll use that as the contents of the tooltip (instead of the title attribute).
Given that you are placing html content inside an HTML property it should be escaped. As mentioned by #Karolis do this:
<p class="related_articles" title="<?php echo htmlentities(''.the_field(related_articles_1_title).''); ?>">Related Articles</p>
This should generate a tooltip with the link in it. If you see stuff like "& lt;a href="... & gt;" on the tooltip then the plugin is not unescaping html values. You could unescape the values but unfortunately javascript has no function. For this you can check out php.js
which provides php's functions in js but that seems kinda overkill. You could instead replace the troublesome characters like this:
<?php
// Generate complete tootltip content
$tootltipContent = ''.the_field(related_articles_link_1).'';
// Search for these
$search = array('<','>','"');
// And replace them with these
$replacements = array('##','##','\"');
$tooltipContent = str_replace($search,$replacements,$tooltipContent);
?>
<p class="related_articles" title="<?php echo $tooltipContent; ?>">Related Stuff</p>
Then look for the line in the plugin where the title is extracted from the element. (tip: on the plugin source code do a search for 'title'. There should be one place where the value is being extracted) It may look something like this tooltipContent = $(someVar).attr('title'); Now run the inverse replacement in js:
tooltipContent = tooltipContent.replace('##','<').replace('##','>').replace('\"','"');
I am trying to insert this search form into my wordpress header. I found where php calls for the user-uploaded logo, and would like to insert the form immediately after.
Search form include:
<?php include ('wp-content/themes/thematic/searchform.php'); ?>
Functions.php echo:
function childtheme_override_blogtitle(){
global $up_options;
echo '<div id="blog-title"><span><img src="' . $up_options->logo . '" alt="" /></span></div>';
}
add_action('thematic_header','childtheme_override_blogtitle',3);
function childtheme_override_blogdescription(){
I tried inserting the form as a separate div, but this keeps the form from centering with the rest of the main content. So I am trying insert the form where php creates the div. You can see my current progress here: texturly.com
There's a function for including the theme's searchform..
http://codex.wordpress.org/Function_Reference/get_search_form
Just call it inside your function or appropriate file.
<?php get_search_form(); ?>
There's an example filter on the above codex page to, should you want to override searchform markup from within your functions file.
EDIT: Regarding placement inside the function, i'd write it a little something like this.
function childtheme_override_blogtitle(){
global $up_options;
?>
<div id="blog-title">
<span><img src="<?php echo $up_options->logo; ?>" alt="" /></span>
<?php get_search_form(); ?>
</div>
<?php
}
Of course move that search form where you want it, i just reformatted how the HTML is generated so you'll have an easier time adjusting it.
Hope that helps.