I tried adding HTML inside a PHP echo, and it works.
But the echo is not working if with a PHP function,
The original code:
if( ! function_exists("rframework_cart_button") ){
function rtframework_cart_button(){
$show_cart = get_theme_mod( RT_THEMESLUG.'_top_shortcut_buttons_cart' ) ? true : false ;
if( ! class_exists('Woocommerce') || ! $show_cart ){
return;
}
global $woocommerce;
echo '<li class="cart"><span class="icon-shopping-bag"><sub class="number empty">'. $woocommerce->cart->cart_contents_count .'</sub></span></li>'."\n";
}
}
The PHP code I want to put inside or after the echo is:
<?php $wishlist_count = YITH_WCWL()->count_products(); ?><span class=”your-counter-selector“><?php echo $wishlist_count; ?></span>
Can anyone help?
with echo, you need to change all the double quotes inside the html stuff to single quotes and keep double quotes outside.
Try this:
return "<li class='cart'><a href='#' class='cart-menu-button'><span class='icon-shopping-bag'><sub class='number empty'>{ $woocommerce->cart->cart_contents_count }</sub></span></a></li>'.'\n";
your question was quite confusing but I got an update based on your comment.
echo within a function called from another function will just output the echo to your page before the end of the other function, using return instead of echo will place that code snippet to the other function. it is the preferred way.
If in html, <?php functionWithEcho() ?> or <?php echo functionWithReturn()
if in php, call function normally
try this
echo "<li class='cart'><a href='#' class='cart-menu-button'><span class='icon-shopping-bag'><sub class='number empty'>'". $woocommerce->cart->cart_contents_count ."'</sub></span></a></li>.'\n'";
echo "<li class='heart'><a href='#' class='heart-button'><span class='icon-new-heart-1'><sub class='number empty'>0</sub></span></a></li>";
Related
why I am not getting output as a uid ?
this is my index.
<a href='my_posts.php?u_id=$user_id' class='list-group-item'>My Posts(<?php echo $post_count; ?>)</a>`
and this is my my_posts.php
<?php
include('function.php');
user_posts();
?>
and this is my function.php
<?PHP
function user_posts(){
global $con;
if(isset($_GET['u_id'])){
$u_id = $_GET['u_id'];
echo $u_id;
}
}
?>`
Maybe you should change you code like this:
<a href='my_posts.php?u_id=<?php echo $user_id;?>' class='list-group-item'>My Posts(<?php echo $post_count; ?>)</a>
`
Here's how your index should look:
<?php
$user_id = 12; // Example value
$post_count = 3; // Example value
echo "<a href='my_posts.php?u_id=$user_id' class='list-group-item'>My Posts($post_count)</a>";
You can remove the $user_id=12 and $post_count=3 in your implementation.
Your my_posts.php and function.php can stay as they are.
Edit: in your function.php you could additionally check if $_GET['u_id'] is of type integer using is_int()
There are several errors in your provided code
$user_id is not initialized anywhere, the function user_posts() echoes $u_id and not $user_id which you are printing.
Echoing inside a function is incorrect if you want to access its output, return should be used instead. Read more about return in php manual
<a href='my_posts.php?u_id=**$user_id**' class='list-group-item'>
This syntax is also incorrect as it is, php variables need to be present inside <?php ?> tags else they will be treated like simple text.
Solution:
<?php
include('function.php');
$user_id = user_posts(); // You can now use $user_id to print the output of user_posts()
echo "<a href='my_posts.php?u_id={$user_id}' class='list-group-item'>My Posts({$post_count})</a>";
?>
function.php
<?PHP
function user_posts(){
global $con;
if(isset($_GET['u_id'])){
$u_id = $_GET['u_id'];
return $u_id; // echo changed to return
}
}
?>
simply change single quotes to double quotes in anchor tag. In single quotes you can't put php variables.
<a href="my_posts.php?u_id=$user_id" class='list-group-item'>My Posts(<?=$post_count?>)</a>
I'm trying to pass an URL parameter in an IF and ELSE statement. This is how the initial code looks like with just the link and without the parameter:
<?php
if ($row_checklist_finding_final['item_class'] == "Major")
{
echo 'Go to NCR';
}
else
{
echo 'Go to OFI';
}
?>
Usually when I'm trying to pass an URL parameter I use this code (this is without if and else statement:
<a href="UserNCR.php?item_id=<?php echo $row_checklist_finding_final['item_id']; ?>">
Go to NCR
</a>
So what I did is, I use my usual way of putting the parameter in the link like this.
The parameter name is item_id which is equal to value $row_checklist_finding_final['item_id']
<?php
if ($row_checklist_finding_final['item_class'] == "Major")
{
Go to NCR
}
else
{
'Go to OFI';
}
?>
I also tried removing the redundant <?php ?> like this:
<?php
if ($row_checklist_finding_final['item_class'] == "Major")
{
Go to NCR
}
else
{
'Go to OFI';
}
?>
But there are still error.
Did I use the wrong syntax?
What should I do to pass the URL parameter in an IF and ELSE statement?
Change your code like this below
<?php
if ($row_checklist_finding_final['item_class'] == "Major")
{
echo ' Go to NCR';
}
else
{
echo 'Go to OFI';
}
?>
You can use the . concatenation operator to join variables together, e.g. as suggested in https://stackoverflow.com/a/45666738/5233704
Another way is to use double quotes for your string as these allow inclusion of variables. Arrays need to be surrounded by curly brackets when doing this:
<?php
if ($row_checklist_finding_final['item_class'] == "Major")
{
echo "Go to NCR';
}
else
{
echo 'Go to OFI';
}
?>
Note that any double quotes inside your string need to be escaped with a backslash.
I have an IF statement stating the following...
<?php
if (empty($data['footer_text'])) {
echo'<p>© ';
print(Date("Y"));
echo'<span class="sep"> | </span><a href="';
echo get_settings('home');
echo'" title="';
bloginfo( 'name' );
echo'" rel="home">';
bloginfo( 'name' );
echo'</a></p>';
}
else{
echo'<p>';
global $data;
echo $data['footer_text'];
echo'</p>';
}
?>
The problem I'm running into is that when I call it like this.
<p><?php global $data; echo $data['footer_text']; ?>;</p>
It displays my text correctly. But when I use the IF statement it always defaults to the showing the site name even when I know it's displaying the text correctly.
Is my syntax screwed up? I can't figure out why it thinks nothing is there but still shows up when I display in a p tag.
You don't declare $data to be global until you're INSIDE the if(), meaning that $data is undefined at the point you're doing the
if (empty($data[...])) {
you probably want
global $data;
if (empty($data[...])) {
instead.
Try to debug the variable $data['footer_text']:
<?php var_dump($data['footer_text']); ?>
just before IF statement. Remember, when empty() returns true.
I'm looking for a function like and if else statement for php which will execute certain html code.
For example:
<?php>
$result = 1;
if ($result == 1)
<?>
html code
else
html code
So, based off the result variable gotten from php scripts, a certain html page is output. I've tried echoing the entire html page, but it just displays the html code-> tags and such.
Hopefully you get what I'm trying to get across, ask if you need any clarification questions. Thanks!
That should work:
<?php
$result = 1;
if($result==1) {
?>
html code
<?php
} else {
?>
html code
<?php
}
?>
The problem I'm facing with the if else statement, is in order to display the html, I have to exit php coding. Thus, the if else statement will not work. (Link)
This is not entirely true. You can use the approach below:
<?php
// Do evaluations
if ( $result == "something" )
{
?>
<p>Example HTML code</p>
<?php
} elseif ( $result == "another thing")
{
?>
<span>Different HTML code</p>
<?php
} else {
?>
<h4>Foobar.</h4>
<?php
}
// Rest of the PHP code
?>
Or, if you don't want to exit PHP coding, you can use the echo or print statements. Example:
<?php
// Evaluations
if ( $result == "foo" )
{
echo "<p>Bar.</p>";
} else {
echo "<h4>Baz</p>";
}
// Some else PHP code
?>
Just be careful with proper sequences of ' and " characters. If your HTML tags are to have arguments, you should watch your step and use either of the following approaches:
echo "<span class=\"foo\">bar</span>";
echo '<span class="foo">bar</span>";
If you want to evaluate some PHP and print the HTML results later, you could use something like this
<?php
$output = "";
if ( $result == "something" ) {
$output = '<p>Example HTML code</p>';
} else if ( $result == "another thing") {
$output = '<span>Different HTML code</p>';
} else {
$output = '<h4>Foobar.</h4>';
}
// Output wherever you like
echo $output;
?>
EDIT (because I'm not sure what you;re trying to do so i'm just putting out different ideas):
If you're trying to output an entire page, it may be useful to use header('location: newPage.html'); instead of $output. This redirects the browser to an entirely new web page. Or you can likely include newPage.html as well.
very close:
<?php
$result = 1;
if ($result == 1){
?>
html code
<?php } //close if
else {
?>
html code
<?php
} //close else
?>
you can echo html code something like this
<?php
$result = 1;
if ($result == 1){
echo "<h1>I love using PHP!</h1>";
}
?>
this would output if Result is 1
**I love using PHP!** //but slightly bigger since its H1
I'm trying to call WordPress's 'the_time' using a conditional statement that checks the category. I want to call the custom field 'event_date' if the category is '3' and 'the_time()' if the category is '4'... This is what I have, and it echoes fine if I use "is_single()" instead of "is_category()" but for some I'm getting no echo when I use "is_category()"... any ideas?
<?php
if (is_category('4')) {
echo "<span>";
the_time('');
echo "</span>";
} elseif (is_category('3')) {
echo "<span>";
get_post_meta ('event_date');
echo "</span>";
} else {
echo "<p>Pedro offers you his protection.</p>";
} ?>
In this instance you want to use in_category instead of is_category.