How to concatenate a string with a variable to include an image - php

for($i ; $i < $rowcount ; $i++){
/*here i want to add an image to my page.*/
echo"<li class='streep mix'><img src='img/'".$productnaam."'.jpg'\>
<button>
<i class='fa fa-2x fa-shopping-cart' aria-hidden='true'></i>
</button>
</li>";
echo"<li class='gap'></li>";
}
i dont understand how to concatenate the image part

Try like this :
echo "<li class='streep mix'><img src='img/" . $productnaam . ".jpg'\>";
echo "<button>";
echo "<i class='fa fa-2x fa-shopping-cart' aria-hidden='true'></i>";
echo "</button>";
echo "</li>";
echo "<li class='gap'></li>";
Hope it helps.

Most probably; You are looking for something like this:
<?php
// OBSERVE THE PART: <img src='img/" . $productnaam . ".jpg' />
// AS WELL AS THE FORWARD SLASH BEFORE CLOSING THE <IMG /> TAG
echo"<li class='streep mix'><img src='img/" . $productnaam . ".jpg' />
<button>
<i class='fa fa-2x fa-shopping-cart' aria-hidden='true'></i>
</button>
</li>";
echo"<li class='gap'></li>";
However, It would be ideal for you (at this stage) to build up your Output-String in sequences so that you could easily spot any typos or potential quote-related errors without much fuss.
<?php
$output = "";
for($i; $i < $rowcount; $i++){
// EMBED AN IMAGE WITHIN A LIST ITEM
$output .= "<li class='streep mix'>";
$output .= "<img src='img/{$productnaam}.jpg' />"; //<== FORWARD SLASH HERE
// THEN, ADD THE SHOPPING-CART BUTTON
$output .= "<button>";
$output .= "<i class='fa fa-2x fa-shopping-cart' aria-hidden='true'></i>";
$output .= "</button>";
$output .= "</li>";
$output .= "<li class='gap'></li>";
}
echo $output;

Related

How to insert HTML using foreach() without echo

I have this working code in my view:
<?php
$task_num = 0;
foreach ($curent_day->getTasksList() as $task){
echo '<div class="task">';
echo '<span class="task_id">'.($task_num+1).'.'.'</span>';
echo '<div class="task_time">';
echo '<span class="task_time_start">'.$task->getStartTime().'</span>';
echo '<span class="task_time_finish">'.$task->getFinishTime().'</span>';
echo '</div>';
echo ''.$task->name.'';
echo 'Start';
echo 'Finish';
echo '<div class="status_round '.$task->status.'"></div>';
echo '</div>';
$task_num++;
}
?>
Is there any way to get rid of 'echo'?
P.S. and is the way to insert HTML with HTML helper more correct even if it takes more space?
You can drop echo completely and use native HTML syntax:
<?php $task_num = 0 ?>
<?php foreach ($curent_day->getTasksList() as $task): ?>
<div class='task'>
<span class='task_id'><?= ++$task_num ?></span>
<div class='task_time'>
<span class='task_time_start'><?= $task->getStartTime() ?></span>
<span class='task_time_finish'><?= $task->getFinishTime() ?></span>
</div>
<a href='/' class='task_name'><?= $task->name ?></a>
<a href='/' class='btn task_start btn_disabled'>Start</a>
<a href='/' class='btn task_finish btn_disabled'>Finish</a>
<div class='status_round <?= $task->status ?>'></div>
</div>
<?php endforeach ?>
This will give you better syntax highlighting and autoformatting support in your IDE/editor.
There are several different ways that you can concatenate the string by using single quotes and double quotes, or by using a variable and the ".=" operator to append text onto the end of a string. Just google php strings & concatenation and it will have more than you need to figure this out.
But using you example here is a method:
$task_num = 0;
foreach ($curent_day->getTasksList() as $task){
echo
'<div class="task">' .
'<span class="task_id">' . ($task_num+1) . '.'.'</span>' .
'<div class="task_time">' .
'<span class="task_time_start">' . $task->getStartTime() . '</span>' .
'<span class="task_time_finish">' . $task->getFinishTime() . '</span>' .
'</div>' .
''.$task->name.'' .
'Start' .
'Finish' .
'<div class="status_round '.$task->status.'"></div>' .
'</div>';
$task_num++;
}
You have to echo to output you data to the browser.
You need not use string concatenation or multiple echo statements.
Alternative
$task_num = 0;
foreach ($curent_day->getTasksList() as $task){
$task_num++;
echo
"<div class='task'>
<span class='task_id'>{$task_num}</span>
<div class='task_time'>
<span class='task_time_start'>{$task->getStartTime()}</span>
<span class='task_time_finish'>{$task->getFinishTime()}</span>
</div>
<a href='/' class='task_name'>{$task->name}</a>
<a href='/' class='btn task_start btn_disabled'>Start</a>
<a href='/' class='btn task_finish btn_disabled'>Finish</a>
<div class='status_round {$task->status}'></div>
</div>";
}
You can concatenate your html in one variable and then you can use it with one time echo statement
<?php
$task_num = 0;
$html = '';
foreach ($curent_day->getTasksList() as $task){
$html .= '<div class="task">';
$html .= '<span class="task_id">'.($task_num+1).'.'.'</span>';
$html .= '<div class="task_time">';
$html .= '<span class="task_time_start">'.$task->getStartTime().'</span>';
$html .= '<span class="task_time_finish">'.$task->getFinishTime().'</span>';
$html .= '</div>';
$html .= ''.$task->name.'';
$html .= 'Start';
$html .= 'Finish';
$html .= '<div class="status_round '.$task->status.'"></div>';
$html .= '</div>';
$task_num++;
}?>

div is not on the code

I'm generating html code with a string this way
foreach ($busquedas as $busqueda) {
$checked = $busqueda->porDefecto ? "checked" : ' ';
$radio_html = "<input type='radio' radio-id='".$busqueda->id."' name='default' class='radio-default' value='Por defecto' checked =".$checked." > Por defecto";
$html .= "<div class='col-md-12 search-div'>";
$html .= "<div class='col-md-12'>";
$html .= "<div class='col-md-6'>".$busqueda->nom."</div>";
$html .= "<div class='col-md-6'>".$_SESSION['user_rol'] == 0?$radio_html:''."</div>";//if $radio_html is shwon the paren div col-md-6 is not shown
$html .="</div>";
$html .= "<div class='col-md-12'>";
$html .= "<div class='col-md-6'><button class='btn btn-default load_search_btn' search_id='".$busqueda->id."'>Cargar</button></div>";
$html .= "<div class='col-md-6'><button class='btn btn-default delete_search_btn' search_id='".$busqueda->id."'>Eliminar</button></div>";
$html .="</div>";
$html .="</div>";
}
When $radio_tml is shown the parent div with class col-md-6 is not on the code but if $radio_html is shown the div is shown too,I thought some tag is not closed but I can't see it
For me your code implies that when you have
$_SESSION['user_rol'] == 0
the div section after that is not closed
You should add brackets as IncredibleHat was saying or add the </div> in the if statement (and not only in the else)

How to rendering different markup for each record from mysql select?

I have a set of items to select from mysql. And then I want to display these items on my page with different 'markup`.
This is how my HTML look like for each item.
<ul class='unstyled main-facilities row'>
<li class='info-facility-item '>
<span class='fa-stack'>
<i class='fa fa-square fa-stack-2x'></i>
<i class='fa fa fa-cutlery fa-stack-1x fa-inverse'></i>
</span> Item-01
</li>
<li class='info-facility-item '>
<span class='fa-stack'>
<i class='fa fa-square fa-stack-2x'></i>
<i class='fa fa fa-rss fa-stack-1x fa-inverse'></i>
</span> Item-02
</li>
<li class='info-facility-item '>
<span class='fa-stack'>
<i class='fa fa-square fa-stack-2x'></i>
<i class='fa fa-refresh fa-stack-1x fa-inverse'></i>
</span> Item-03
</li>
...
...
...
</ul>
If I have same markup for each item, then I can do it like this:
// Fetch all the records:
while ($stmt->fetch()) {
$result = "<li class='info-facility-item '>\n";
$result .= " <span class='fa-stack'>\n";
$result .= " <i class='fa fa-square fa-stack-2x'></i>\n";
$result .= " <i class='fa fa fa-rss fa-stack-1x fa-inverse'></i>\n";
$result .= " </span>{$item}\n";
$result .= "</li>\n";
$items[] = $result;
}
}
But I am not sure how to modify my while loop to render different markup for each item.
Can anybody tell me is there a way to do this in PHP?
Thank you.
code below generates 4 different css classes that are added to the sequential LI. If you want - you can add more similar classes elsewhere. The name of the new classes will be new_class0, new_class1, ...
Is that what you need?
<?php
//Fetch all the records:
$xi = 0;
while ($stmt->fetch()) {
if ( $xi > 3 ) {
$xi = 0;
}
$result = "<li class=\"info-facility-item new_class{$xi}\">\n";
$result .= " <span class='fa-stack'>\n";
$result .= " <i class='fa fa-square fa-stack-2x'></i>\n";
if ( $xi == 0 ) {
$result .= " <i class='fa fa fa-rss fa-stack-1x fa-inverse'></i>\n";
else if ( $xi == 1 ) {
$result .= " <i class='fa fa fa-cutlery fa-stack-1x fa-inverse'></i>\n";
}
$result .= " </span>{$item}\n";
$result .= "</li>\n";
$items[] = $result;
$xi += 1;
}
If you know the values you need to set to each position you could solve this like this:
Look at the array $val
$val= array("value0", "value1", "value2");
$i = 0;
// Fetch all the records:
while ($stmt->fetch()) {
$result = "<li class='info-facility-item '>\n";
$result .= " <span class='fa-stack'>\n";
$result .= " <i class='fa fa-square fa-stack-2x'></i>\n";
$result .= " <i class='fa fa {$val[$i]} fa-stack-1x fa-inverse'></i>\n";
$result .= " </span>{$item}\n";
$result .= "</li>\n";
$items[] = $result;
$i++;
}

mail to in an existing php code

i have this code:
if($this->helix3->getParam('contact_email')) $output .= '<li class="sp-contact-email"><i class="fa fa-envelope"></i> ' . $this->helix3->getParam('contact_email') . '</li>';
$output .= '<ul>';
return $output;
I would like that click in the frontpage will send an email to the result of the getParam('contact_email')
If I understand you correctly you need to use an anchor tag with mailto: in the href:
if($this->helix3->getParam('contact_email')){
$email=$this->helix3->getParam('contact_email');
$output .= '<li class="sp-contact-email"><i class="fa fa-envelope"></i> ' . $email . '</li>';
}
$output .= '<ul>';
return $output;

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

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

Categories