Removing "echo" and replacing it with "return"? - php

I have the following code in place, however, I have to strip away all the styling (any css) and have it return rather than echo, otherwise it messes up with some other code I'm using.
The other code I'm using echo's a shortcode that outputs the following determined by if there is items in the cart or not.
Obviously I can't echo (on the echo shortcode end of things), so I must return. If the cart is empty, it returns just fine as per my commented code below. It is when the cart has items, I can't get it to return :( my attempt is below.
//Original code, if there's items
if(is_object($cart) && $cart->countItems()) {
?>
<div id="Cart66scCartContents" style="float:right; text-align: right;">
<a id="Cart66scCartLink" href='<?php echo get_permalink($cartPage->ID) ?>'>
<span id="Cart66scCartCount"><?php echo $cart->countItems(); ?></span>
<span id="Cart66scCartCountText"><?php echo $cart->countItems() > 1 ? ' items' : ' item' ?></span>
<span id="Cart66scCartCountDash">–</span>
<!-- <span id="Cart66scCartPrice"><?php //echo CART66_CURRENCY_SYMBOL .
number_format($cart->getSubTotal() - $cart->getDiscountAmount(), 2); ?> -->
</span></a>
<a id="Cart66scViewCart" href='<?php echo get_permalink($cartPage->ID) ?>'>View Cart</a>
<span id="Cart66scLinkSeparator"> | </span>
<a id="Cart66scCheckout" href='<?php echo get_permalink($checkoutPage->ID) ?>'>Check out</a>
</div>
<?php
}
else {
//My code, if there's no items (which works perfectly as a return)
$emptyMessage = isset( $attrs['empty_msg'] ) ? $attrs['empty_msg'] : 'Your cart is empty';
return "<p id=\"Cart66scEmptyMessage\" style=\"float:right; text-align: right;\">" . $emptyMessage . "</p>";
}
My attempt on setting the "if there's items in the cart" to a return rather than echo..
if(is_object($cart) && $cart->countItems()) {
return "<a href='" . get_permalink($cartPage->ID) . "'>" . $cart->countItems(); . " " . $cart->countItems() > 1 ? ' items' : ' item' . "–" . number_format($cart->getSubTotal() - $cart->getDiscountAmount(), 2); . "</a> <a href='" . get_permalink($cartPage->ID) . "'>View Cart</a> | <a href='" . get_permalink($checkoutPage->ID) . "'>Check out</a>";
}
else {
$emptyMessage = isset( $attrs['empty_msg'] ) ? $attrs['empty_msg'] : 'Your cart is empty';
return "<p id=\"Cart66scEmptyMessage\" style=\"float:right; text-align: right;\">" . $emptyMessage . "</p>";
}
The text "1 item – View Cart | Check out" is not showing up with my attempt.
What have I done wrong?
Thank you!!

You have a semicolon at . $cart->countItems();, remove that and you should be good to go

Try to use HEREDOC
For example:
if (is_object($cart) && $cart->countItems()) {
$items = $cart->countItems() > 1 ? ' items' : ' item';
return <<<HTML
<div id="Cart66scCartContents" style="float:right; text-align: right;">
<a id="Cart66scCartLink" href='{get_permalink($cartPage->ID)}'>
<span id="Cart66scCartCount">{$cart->countItems()}</span>
<span id="Cart66scCartCountText">{$items}</span>
<span id="Cart66scCartCountDash">–</span>
</span></a>
<a id="Cart66scViewCart" href='{get_permalink($cartPage->ID)}'>View Cart</a>
<span id="Cart66scLinkSeparator"> | </span>
<a id="Cart66scCheckout" href='{get_permalink($checkoutPage->ID)}'>Check out</a>
</div>
HTML;
}
else {
$emptyMessage = isset( $attrs['empty_msg'] ) ? $attrs['empty_msg'] : 'Your cart is empty';
return "<p id=\"Cart66scEmptyMessage\" style=\"float:right; text-align: right;\">" . $emptyMessage . "</p>";
}

Maybe
$html = <<<EOD
<tr>
<td>TEST</td>
</tr>
EOD;
Would help here?

Declare the string variable and concatenate all the HTML you trying to echo in that string and return the string variable .
Example $string = '

Related

Wordpress array null

I have a checkout system, within my Wordpress that shows me the following message:
Notice: Undefined index: answers in
/srv/www/vhosts/wordpress/wp-content/themes/../checkout/fragment-header.php
on line 3
Notice: Trying to access array offset on value of type null in
/srv/www/vhosts/wordpress/wp-content/themes/../checkout/fragment-header.php
on line 3
The php code of that file is the following:
<?php
$valorAccesorio = $args['quote']['answers']['vehicleGncValue'] ?? '0';
$cuotaMensual = $args['quote']['answers']['planPremioMensual'] +
(isset($args['quote']['answers']['apPremioMensual']) ? $args['quote']['answers']['apPremioMensual'] : 0);
?>
<header id="header">
<div class="wrap menu-wrap">
<?php
$class = (theme_get_custom_logo()) ? 'image' : 'text';
echo '<h1 class="site-title ' . $class . '">';
echo '<a href="' . esc_url(home_url('/')) . '" rel="home">';
if (theme_get_custom_logo()) echo '<img src="' . theme_get_custom_logo() . '" class="site-logo" alt="' . htmlspecialchars(get_bloginfo('name')) . '" />';
echo '<strong>' . get_bloginfo('name') . '</strong>';
echo '</a>';
echo '</h1>';
?>
<div class="right-section">
<?php if ($args['quote']['product'] == 'seguro-de-motos' || $args['quote']['product'] == 'seguro-de-autos-y-pick-ups') : ?>
<h1>
Estás contratando un plan
<span><?php echo $args['quote']['answers']['planCobertura'] ?></span>,
para tu
<span>
<?php echo $args['quote']['answers']['vehicleBrand'] ?>
<?php if (isset($args['quote']['answers']['vehicleModel'])) {
echo $args['quote']['answers']['vehicleModel'];
} else {
echo $args['quote']['answers']['vehicleVersion'];
} ?>
<?php echo $args['quote']['answers']['vehicleYear'] ?>
</span>
</h1>
<p>
Suma asegurada: $<?php echo number_format(($args['quote']['answers']['vehicleValue'] + $valorAccesorio), 2, ',', '.') ?>
|
Cuota mensual: $<?php echo number_format(($cuotaMensual), 2, ',', '.') ?>
</p>
<?php endif; ?>
</div>
From what I understand I am generating an array with null value, but I do not understand how to generate it by another way
Some help? Thanks!

How can I put a link around my excerpts in PHP?

I'm trying to put a link around my excerpts like on my titles but i'm getting a parse-error on this line:
echo '<div class="excerpt">''' . nectar_excerpt($excerpt_length) . '</div>';?>
Here the whole code of my post-element:
<div class="post-header">
<h3 class="title">
<?php the_title(); ?>
</h3>
<span class="meta-author"><?php the_author_posts_link(); ?> </span>
<span class="meta-category"> | <?php the_category(', '); ?> </span>
<span class="meta-comment-count"> | <a href="<?php comments_link(); ?>">
<?php comments_number( esc_html__( 'No Comments','salient'), esc_html__( 'One Comment','salient'), '% '. esc_html__( 'Comments','salient') ); ?></a>
</span>
</div>
<?php
$excerpt_length = ( !empty( $nectar_options['blog_excerpt_length'] ) ) ?
intval( $nectar_options['blog_excerpt_length'] ) : 30;
echo '<div class="excerpt">''' . nectar_excerpt($excerpt_length) . '</div>';?>
<div class="meta-tags"> <?php the_tags(''); ?> </div>
<div class="tags-divider"></div>
In PHP, text strings are merged using the dot character. So if you want to connect them together, you should do this:
$a = "text1";
$b = "text2";
echo ($a . $b); // prints "text1text2"
Or in your case like this:
echo "text1" . function() . "text3"; // prints text1text2text3
And if you are using function like string, you don't use the ";" character at the end, because it will end the whole line of code.
echo "text1" . "text2"; . "text3"; // wrong
echo "text1" . "text2" . "text3"; // correct
echo "text1" . function(); . "text3"; // wrong
echo "text1" . function() . "text3"; // correct
So, just add the dots and remove the semicolon, and it should work.
echo '<div class="excerpt">' . '' . nectar_excerpt($excerpt_length) . '</div>';?>
Give this a try?
echo '<div class="excerpt">' . nectar_excerpt($excerpt_length) . '</div>';

Adding A Div For Every 4th MySQL Result With Pagination

I have a script that displays images from a database.
Every 4 pictures is contained in a <div class="row">.
I added a pagination script to this page to limit the number of images and I ran into a problem.
When there are 4 or 8 images on a page the script works fine, but if there are only 1 or 7 images on the page, the closing </div> for the <div class="row"> doesn't get added.
This is my entire script:
$conn = getConnected("lucycypher");
$img_start=0;
$img_limit=8;
if(isset($_GET['page'])) {
$page=$_GET['page'];
$img_start=($page-1)*$img_limit;
}
else { $page = 1; }
$img_total=mysqli_num_rows(mysqli_query($conn, "select * from gallery_img"));
$img_total_count=ceil($img_total/$img_limit);
echo '<nav aria-label="Page navigation">
<ul class="pagination">' . PHP_EOL;
if($page>1) {
echo '<li><span aria-hidden="true">Previous</span></li>' . PHP_EOL;
}
for($i=1;$i<=$img_total_count;$i++) {
if($i==$page) { echo "<li class='active'><a href='?page=".$i."'>".$i."</a></li>" . PHP_EOL; }
else { echo "<li><a href='?page=".$i."'>".$i."</a></li>" . PHP_EOL; }
}
if($page!=$img_total_count) {
if(!isset($page)) { echo '<li><span aria-hidden="true">Next</span></li>' . PHP_EOL; }
else { echo '<li><span aria-hidden="true">Next</span></li>' . PHP_EOL; }
}
echo '</ul>
</nav>' . PHP_EOL;
?>
</div>
</div>
<?php
if($_COOKIE['age_verification'] == "adult") {
$img_query = "SELECT img_name, img_category, img_location FROM gallery_img order by img_time desc LIMIT $img_start, $img_limit;";
}
else if($_COOKIE['age_verification'] == "child") {
$img_query = "SELECT img_name, img_category, img_location FROM gallery_img WHERE img_category NOT LIKE '%nude' order by img_time desc LIMIT $img_start, $img_limit;";
}
$img_result = mysqli_query($conn, $img_query);
if (mysqli_num_rows($img_result) > 0) {
// output data of each row
$img_count = 1;
while($img_row = mysqli_fetch_assoc($img_result)) {
$tags = $img_row["img_category"];
if ( $img_count%4 === 1 ) { echo '<div class="row">' . PHP_EOL; } // Create new row for every 4th image
echo '<div class="col-md-3">' . PHP_EOL;
echo '<div class="panel panel-default">' . PHP_EOL;
echo '<img src="http://lucycypher.com/h/400/w/300/a/c/thumb/'.$img_row["img_location"].'" class="img-responsive img-protected">' . PHP_EOL;
echo '<div class="panel-footer"><span class="glyphicon glyphicon-tag"></span> Tags: ' . $tags . '</div>' . PHP_EOL;
echo '</div>' . PHP_EOL;
echo '</div>' . PHP_EOL;
if ( $img_count%4 === 0 ) { echo '</div>' . PHP_EOL; } // Close row
$img_count++;
}
} else {
echo '<div class="col-md-3">' . PHP_EOL;
echo '<div class="panel panel-primary">' . PHP_EOL;
echo '<div class="panel-heading"><span class="glyphicon glyphicon-picture"></span> Sorry</div>' . PHP_EOL;
echo 'No recent uploads.' . PHP_EOL;
echo '</div>' . PHP_EOL;
echo '</div>' . PHP_EOL;
}
?>
<div class="row">
<div class="col-md-12">
<?php
echo '<nav aria-label="Page navigation">
<ul class="pagination">' . PHP_EOL;
if($page>1) {
echo '<li><span aria-hidden="true">Previous</span></li>' . PHP_EOL;
}
for($i=1;$i<=$img_total_count;$i++) {
if($i==$page) { echo "<li class='active'><a href='?page=".$i."'>".$i."</a></li>" . PHP_EOL; }
else { echo "<li><a href='?page=".$i."'>".$i."</a></li>" . PHP_EOL; }
}
if($page!=$img_total_count) {
if(!isset($page)) { echo '<li><span aria-hidden="true">Next</span></li>' . PHP_EOL; }
else { echo '<li><span aria-hidden="true">Next</span></li>' . PHP_EOL; }
}
echo '</ul>
</nav>' . PHP_EOL;
Once I figure this out I'm going to limit the images to 8 per page so on the last page if there are only 3 total images then it's missing the final </div> tag since there isn't a total of 4 which completely throws off the rest of the page layout.
Best way to explain it is a visual I assume:
When images total 4 per row with a limit of 8:
<div class="row">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
</div>
<div class="row">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
</div>
But if the final page only has 3 images:
<div class="row">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
<!--Missing </div> Tag-->
The final </div> tag is set by this line: if ( $img_count%4 === 0 ) { echo '</div>' . PHP_EOL; } // Close row.
You may calculate the total count of images, which will be uploaded.
Then, use if ( $img_count%4 === 0 || $img_count==$total_count )
In my example $img_count is the number of added image (beginning from 1)

Combine get_posts loop with another array and randomise sort by date

I have a page I am building in WordPress which includes tweets by the Twitter API and then a standard get_posts loop.
At the moment they are displaying one after the other but I wish to create a masonry style grid which includes both.
Is there a way to output both arrays in between each other by date so the items are broken up instead of it outputting tweets first then the posts?
Here is my code.
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);
echo '<div class="funny-grid">';
echo '<div class="grid-sizer"></div>';
foreach($string as $items)
{
$string = $items['text'];
$regex = "#(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)#";
echo '<div class="grid-item tweet ">';
echo '<i class="fa fa-lg fa-twitter inverse" aria-hidden="true"></i>';
echo '<div class="tweet-text">' . preg_replace($regex, ' ', $string) . '</div>' ."<br />";
echo '<div class="tweet-user">' . '#' . '' . $items['user']['screen_name'] . '' . '</div>';
echo '<div class="tweet-date">' . $items['created_at'] . '</div>' . "<br />";
echo '</div>';
}
//counts the amount of items in the array.
/*echo "Number of items:" . count($string);*/
?>
<!--<div style="clear: both;"></div>-->
<?php
query_posts('post_type=funny_wall &posts_per_page=8');
if(have_posts()):while(have_Posts()):the_post();
$img = wp_get_attachment_url(get_post_thumbnail_id($post->ID), "full");?>
<div class="grid-item image">
<div class="as">
<img src="<?php echo $img ; ?>">
<a href="<?php the_permalink();?>">
</a>
</div>
</div>
<?php
endwhile;
endif;
wp_reset_query();
?>
</div>
</div>
Instead of echoing the results directly, you could put them into an array, and then use shuffle() to randomise before outputting it in one go.
e.g.:
tweets
$grid_items[] = '<div class="grid-item tweet ">
<i class="fa fa-lg fa-twitter inverse" aria-hidden="true"></i>
<div class="tweet-text">' . preg_replace($regex, ' ', $string) . '</div>' .'<br />
<div class="tweet-user">' . '#' . '' . $items['user']['screen_name'] . '' . '</div>
<div class="tweet-date">' . $items['created_at'] . '</div>' . '<br />
</div>';
posts:
$grid_items[] = "<div class='grid-item image'>
<div class='as'>
<img src=".$img.">
<a href=".the_permalink().">
</a>
</div>
</div>";
then use:
shuffle($grid_items);
foreach($grid_items as $grid_item){
echo $grid_item;
}
when you want to output them.
http://php.net/manual/en/function.shuffle.php

just echo price from this tag

how can i echo just the salesPrice without creating a pricediv? I have tried echo salesPrice but that doesn't seem to be working.
<?php echo "<span class='priceColor2'>" . $this->currencyDisplay->createPriceDiv ('salesPrice', '', $this->cart->pricesUnformatted, FALSE) . "</span>" ?>
the code currently echos the price in a separate div like this:
<span class='priceColor2'><div class="PricesalesPrice" style="display : block;" ><span class="PricesalesPrice" >£20.00</span></div></span>
try this...
<?php echo "<span class='priceColor2'>" . strip_tags($this->currencyDisplay->createPriceDiv ('salesPrice', '', $this->cart->pricesUnformatted, FALSE)) . "</span>" ?>

Categories