php passing on output to a variable - php

This might be easy, but i could not get solution for it as is novice and learning on the php
I want to put the output into a variable called $list so that it becomes
<?php $list .= '<h3> <?php echo $this->escape($item->n_make); ?> <?php echo $item->n_model; ?>
</h3>
<p> <?php echo $item->n_month; ?> <?php echo $item->n_year; ?> </p>
<p> <?php echo $item->n_short_description; ?> </p>
<hr/>'
?>
But the syntax is incorrect as its like php within php
How to get value of $list capturing in details
and can then use in explode function
<?php
$listings = explode("<hr/>", $list);
$numberOfListings = count($listings);
---- conditions
echo $listings;
?>

It's just simple concatenation:
$list .= '<h3> ' . $this->escape($item->n_make) . $item->n_model . '</h3>
<p> ' . $item->n_month . ' ' . $item->n_year . '</p>
<p> ' . $item->n_short_description . ' </p>
<hr/>';

Related

Read More and Read Less button with PHP

I'm trying to put a read more button and of course excerpt.
this is my base code to call text <?php the_content();?>
I add something for read more but it doesn't work.
<?php if($excerpt = $desc->the_content):?>
<?php
$num_word = 15;
$excerpt = strip_shortcodes($excerpt);
echo '<p>' . wp_trim_words($excerpt,15,'...') . '</p>';
echo '<p class="property-content">' . wp_trim_words($excerpt,25,'...') . '</p>';
?>
<?php endif;?>
Problem is solved.
<?php if ($excerpt = the_excerpt()):
$num_word = 5;
$excerpt = strip_shortcodes($excerpt);
echo '<p>' . wp_trim_words($excerpt, $num_word, '...') . '</p>';
?>
<?php endif; ?>

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>';

while looping w3-quarter in w3-row-padding

Here is the problem that while looping the php in while loop in w3-row-padding of w3 responsive layout . The layout breaks
Here is the source code
<?php
$r=0;
while($r<ceil($fetch_row_count/4))
{ ?>
<div class="w3-row-padding w3-padding-16 w3-center" style="clear:both" id="food">
<?php
while($row=mysqli_fetch_array($res))
{
?>
<div class="w3-quarter">
<img src="admin/uploads/<?php echo $row['image']; ?>" alt="noodles" style="width:50%">
<h3><?php echo $row['title']; ?></h3>
<p><?php echo $row['description']; ?> </p>
</div>
<?php
}
$r++;
}
?>
</div>
Thanks for reply and comments in advance
That bottom div was not being added for each of your padded containers.
The way the code is written you are adding a padded container and then adding your w3-quarter div for each of your result sets and then repeating that a bunch of times with what looks like to me the same set of data in each one.
What you are probably trying to accomplish is just making one padded div and then echo out your result set with the w3-quarter divs inside of it.
Here is your original way with the bottom div corrected:
<?php
$r=0;
while($r<ceil($fetch_row_count/4)) {
echo
'<div class="w3-row-padding w3-padding-16 w3-center" style="clear:both" id="food">';
while($row=mysqli_fetch_array($res)){
echo
'<div class="w3-quarter">' .
'<img src="admin/uploads/' . $row['image'] . '" alt="noodles" style="width:50%">' .
'<h3>' . $row['title'] . '</h3>' .
'<p>' . $row['description'] . '</p>' .
'</div>';
}
$r++;
echo
'</div>';
}
?>
Here is the way I think you are trying to do it: (Just guessing)
<?php
echo
'<div class="w3-row-padding w3-padding-16 w3-center" style="clear:both" id="food">';
$r = 0;
while($row=mysqli_fetch_array($res)){
echo
'<div class="w3-quarter">' .
'<img src="admin/uploads/' . $row['image'] . '" alt="noodles" style="width:50%">' .
'<h3>' . $row['title'] . '</h3>' .
'<p>' . $row['description'] . '</p>' .
'</div>';
$r++;
//I would not actually try to control how many results you add like this.
//I would get the results from a better formatted query.
if($r < ceil($fetch_row_count/4)){
break;
}
}
echo
'</div>';
?>

Trouble concatenating strings in a loop

I'm try to make some changes to my ZenPhoto installation in order so that at the bottom of an album page I have a text area which displays the "embed" code of the generated images above, so that readers can simply copy it and post the html onto their own sites.
Here is the snippet of code.
<?php $str = ''; ?>
<?php while (next_image()): ?>
<div class="imagethumb"><?php printImageThumb(getAnnotatedImageTitle()); ?></div>
$str .= '<?php printImageThumb(getAnnotatedImageTitle()); ?>;'
<?php endwhile; ?>
<textarea type="text" size="50">
<?php echo $str; ?>
</textarea>
The code I've added is the $str stuff. I'm trying to loop through the images and create the html that is used in the first div in the while loop so that it puts it as text into the str string. This is concatenated for each image in the library and then the end str is posted into a simple text area for the user to copy.
I can't get the $str concatination working.
I'm very new to php and I can't quite get the syntax working.
Any help would be much appreciated.
The concatenation is not inside the <?php tags. For readability, you should use sprintf:
<?php $str = ''; ?>
<?php while (next_image()): ?>
<div class="imagethumb"><?php printImageThumb(getAnnotatedImageTitle()); ?></div>
<?php $str .= sprintf('%s',
html_encode(getImageLinkURL()),
getBareImageTitle(),
printImageThumb(getAnnotatedImageTitle()));
?>
<?php endwhile; ?>
But you are repeating things here (you are creating the link twice). You could restructure the code a bit to avoid that:
<?php
$images = array();
while (next_image()) {
$images[] = sprintf('%s',
html_encode(getImageLinkURL()),
getBareImageTitle(),
printImageThumb(getAnnotatedImageTitle()));
}
?>
<?php foreach($images as $image): ?>
<div class="imagethumb"><?php echo $image; ?></div>
<?php endforeach; ?>
<textarea>
<?php echo implode("", $images); ?>
</textarea>
Reference: implode
Your php code is starting and ending outside your php blocks with random php blocks in between.
<?php $str .= '<a href="';
$str .= html_encode(getImageLinkURL());
$str .= '" title="';
$str .= getBareImageTitle();
$str .= '">';
$str .= printImageThumb(getAnnotatedImageTitle());
$str .= '</a>';
?>
Alternatively:
<?php $str .= ''.printImageThumb(getAnnotatedImageTitle()).''; ?>
One issue is that ' and " strings behave differently in PHP. When you have:
echo '<?php echo $otherString; ?>';
PHP will print:
<?php echo $otherString; ?>
instead of the contents of $otherString.
I would rewrite this line:
<?php $str .= '<?php printImageThumb(getAnnotatedImageTitle()); ?>' ?>
to look more like this:
<?php $str .= '<a href="' . html_encode(getImageLinkURL()) . '" title="' . getBareImageTitle() . '" ' . printImageThumb(getAnnotatedImageTitle() . '</a>;'; ?>
Another issue is that you can't echo into a string and have it concatenate. Instead, you want to return a string value.
The $str variable is not enclose in <?php ?> tags.

Categories