I'm trying to modify a wordpress plugin to keep it from displaying $0.00 amounts.
original code
$gTotal = $foodmenu->getPriceWithLabel();
$html .= '<span class="price">' . $gTotal . '</span>';
$html .= "</div>";
This is what i tried so far off reading and searching.
$gTotal = $foodmenu->getPriceWithLabel();
$html .= '<span class="price">' . $gTotal === '0.00' ? '-' : $gTotal . '</span>';
$html .= "</div>";
Add parenthesis:
$html .= '<span class="price">' . ($gTotal === '0.00' ? '-' : $gTotal) . '</span>';
you are missing () on your ternary operator
$html .= '' . ($gTotal === '0.00' ? '-' : $gTotal) . '';
Related
I am trying to build a simple calendar using Carbon in Laravel 9. Also, I need to show some content from the database on specific dates. The calendar is well made without the other contents but with content, some dates which don't have external data are duplicated.
The controller code
$date = empty($date) ? Carbon::now() : Carbon::createFromDate($date);
$startOfCalendar = $date->copy()->firstOfMonth()->startOfWeek(Carbon::SUNDAY);
$endOfCalendar = $date->copy()->lastOfMonth()->endOfWeek(Carbon::SATURDAY);
$html = '<div class="calendar">';
$html .= '<div class="month-year">';
$html .= '<span class="month">' . $date->format('M') . '</span>';
$html .= '<span class="year">' . $date->format('Y') . '</span>';
$html .= '</div>';
$html .= '<div class="days">';
$dayLabels = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
foreach ($dayLabels as $dayLabel)
{
$html .= '<span class="day-label">' . $dayLabel . '</span>';
}
$get_events = CalendarEvents::where("event_date",">=",date("Y-m-d", strtotime($startOfCalendar)))->get();
while($startOfCalendar <= $endOfCalendar)
{
$extraClass = $startOfCalendar->format('m') != $date->format('m') ? 'dull' : '';
$extraClass .= $startOfCalendar->isToday() ? ' today' : '';
foreach ($get_events as $events) {
if($events->event_date == date('Y-m-d',strtotime($startOfCalendar))) {
$html .= '<span class="day flip' . $extraClass . '"><span class="content">' . $startOfCalendar->format('j').'|'.$events->event_name. '</span></span>';
}
else {
$html .= '<span class="day' . $extraClass . '"><span class="content">' . $startOfCalendar->format('j') . '</span></span>';
break;
}
}
$startOfCalendar->addDay();
}
$html .= '</div></div>';
Here the events' dates are printed once but dates without the events are printed multiple times. How can I correct this? Thanks in advance
your loop seems a bit off. Assuming you have multiple events on some days and no events on others, you probably want something like the following.
$date = empty($date) ?Carbon::now() : Carbon::createFromDate($date);
$startOfCalendar = $date->copy()->firstOfMonth()->startOfWeek(Carbon::SUNDAY);
$endOfCalendar = $date->copy()->lastOfMonth()->endOfWeek(Carbon::SATURDAY);
$html = '<div class="calendar">';
$html .= '<div class="month-year">';
$html .= '<span class="month">' . $date->format('M') . '</span>';
$html .= '<span class="year">' . $date->format('Y') . '</span>';
$html .= '</div>';
$html .= '<div class="days">';
$dayLabels = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
foreach ($dayLabels as $dayLabel)
{
$html .= '<span class="day-label">' . $dayLabel . '</span>';
}
$get_events = CalendarEvents::where("event_date", ">=", date("Y-m-d", strtotime($startOfCalendar)))->get();
while ($startOfCalendar <= $endOfCalendar)
{
$extraClass = $startOfCalendar->format('m') != $date->format('m') ? 'dull' : '';
$extraClass .= $startOfCalendar->isToday() ? ' today' : '';
$startOfCalendarFormated = $startOfCalendar->format('j');
$startOfCalendarSqlFormat = $startOfCalendar->format('Y-m-d');
$html .= '<span class="day flip' . $extraClass . '"><span class="content">';
$filled = false;
foreach ($get_events as $events) {
if ($events->event_date == $startOfCalendarSqlFormat) {
$html .= $startOfCalendarFormated . '|' . $events->event_name . "<br>";
$filled = true;
}
}
if(!$filled){
$html .= "No events | {$startOfCalendarFormated}";
}
$html .= '</span></span>';
$startOfCalendar->addDay();
}
$html .= '</div></div>';
I'm making assumptions on how you want your HTML formatted, but hopefully you get the picture.
$date = empty($date) ?Carbon::now() : Carbon::createFromDate($date);
$startOfCalendar = $date->copy()->firstOfMonth()->startOfWeek(Carbon::SUNDAY);
$endOfCalendar = $date->copy()->lastOfMonth()->endOfWeek(Carbon::SATURDAY);
$html = '<div class="calendar">';
$html .= '<div class="month-year">';
$html .= '<span class="month">' . $date->format('M') . '</span>';
$html .= '<span class="year">' . $date->format('Y') . '</span>';
$html .= '</div>';
$html .= '<div class="days">';
$dayLabels = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
foreach ($dayLabels as $dayLabel)
{
$html .= '<span class="day-label">' . $dayLabel . '</span>';
}
$get_events = CalendarEvents::where("event_date", ">=", date("Y-m-d", strtotime($startOfCalendar)))->get();
while ($startOfCalendar <= $endOfCalendar)
{
$extraClass = $startOfCalendar->format('m') != $date->format('m') ? 'dull' : '';
$extraClass .= $startOfCalendar->isToday() ? ' today' : '';
$startOfCalendarFormated = $startOfCalendar->format('j');
$startOfCalendarSqlFormat = $startOfCalendar->format('Y-m-d');
$html .= '<span class="day flip' . $extraClass . '"><span class="content">';
$filled = false;
foreach ($get_events as $events) {
if ($events->event_date == $startOfCalendarSqlFormat) {
$html .= $startOfCalendarFormated . '|' . $events->event_name . "<br>";
$filled = true;
}
}
if(!$filled){
$html .= "No events | {$startOfCalendarFormated}";
}
$html .= '</span></span>';
$startOfCalendar->addDay();
}
$html .= '</div></div>';
for the blade what do you enter to see this?
it would be just what i'm looking for, i need to see some data in a calendar and i haven't been able to view yet, this could be what i need but i didn't understand what to put in the blade to see the result
On my blog homepage, Author and date meta (Author name and date) are in link form. I want to show them only in text. When I tried to remove the link the text is also removed. Kindly help me to remove the link. Codes of display.php are
if (!function_exists('swift_meta_generator')):
/**
* Generate post meta.
*
* Prints the post meta information based on the options set in theme options page.
* Called around post titles on home page and single pages.
*
* #param array $meta post meta order set in options page.
* #param string $classes html classes for the post meta wrapper.
*
*/
function swift_meta_generator($meta, $classes)
{
$data = '<div class="entry-meta ' . $classes . '">';
$size = count($meta);
for ($i = 0; $i < $size; $i++) {
switch ($meta[$i]) {
case 'text' :
if ((current_time('timestamp', 1) - get_the_date('U')) < 86400)
$meta[$i + 1] = preg_replace('(on)', '', $meta[$i + 1]);
$data .= $meta[$i + 1];
$i++;
break;
case 'author' :
$data .= '<span class="vcard author fa-user"><a class="" href="' . esc_url(get_author_posts_url(get_the_author_meta('ID'))) . '" rel="author"><span class="fn">' . esc_attr(get_the_author()) . '</span></a></span> ';
break;
case 'author_avatar' :
$data .= get_avatar( get_the_author_meta('ID'), 16 );
$data .= ' <span class="vcard author"><a class="" href="' . esc_url(get_author_posts_url(get_the_author_meta('ID'))) . '" rel="author"><span class="fn">' . esc_attr(get_the_author()) . '</span></a></span> ';
break;
case 'date' :
if ((current_time('timestamp', 1) - get_the_date('U')) < 86400)
$date = human_time_diff(get_the_time('U'), current_time('timestamp')) . ' '.__('ago','swift');
else
$date = get_the_date();
$data .= '<span class="date updated fa-clock-o"><a class="" href="' . esc_url(get_permalink()) . '" title="' . esc_attr(get_the_time()) . '" rel="bookmark">';
$data .= '<time class="entry-date" datetime="' . esc_attr(get_the_date('c')) . '">' . esc_html($date) . '</time></a></span> ';
break;
case 'updated_on' :
if ((current_time('timestamp', 1) - get_the_modified_date('U')) < 86400)
$date = human_time_diff(get_post_modified_time('U'), current_time('timestamp')) . ' '.__('ago','swift');
else
$date = get_the_modified_date();
$data .= '<span class="date updated fa-clock-o"><a href="' . esc_url(get_permalink()) . '" title="' . esc_attr(get_post_modified_time()) . '" rel="bookmark">';
$data .= '<time class="entry-date" datetime="' . esc_attr(get_the_modified_date('c')) . '">' . esc_html($date) . '</time></a></span> ';
Replace this code with your code
if (!function_exists('swift_meta_generator')):
/**
* Generate post meta.
*
* Prints the post meta information based on the options set in theme options page.
* Called around post titles on home page and single pages.
*
* #param array $meta post meta order set in options page.
* #param string $classes html classes for the post meta wrapper.
*
*/
function swift_meta_generator($meta, $classes)
{
$data = '<div class="entry-meta ' . $classes . '">';
$size = count($meta);
for ($i = 0; $i < $size; $i++) {
switch ($meta[$i]) {
case 'text' :
if ((current_time('timestamp', 1) - get_the_date('U')) < 86400)
$meta[$i + 1] = preg_replace('(on)', '', $meta[$i + 1]);
$data .= $meta[$i + 1];
$i++;
break;
case 'author' :
$data .= '<span class="vcard author fa-user"><span class="fn">' . esc_attr(get_the_author()) . '</span></span> ';
break;
case 'author_avatar' :
$data .= get_avatar( get_the_author_meta('ID'), 16 );
$data .= ' <span class="vcard author"><a class="" href="' . esc_url(get_author_posts_url(get_the_author_meta('ID'))) . '" rel="author"><span class="fn">' . esc_attr(get_the_author()) . '</span></a></span> ';
break;
case 'date' :
if ((current_time('timestamp', 1) - get_the_date('U')) < 86400)
$date = human_time_diff(get_the_time('U'), current_time('timestamp')) . ' '.__('ago','swift');
else
$date = get_the_date();
$data .= '<span class="date updated fa-clock-o">' . esc_attr(get_the_time()) . '</span>';
$data .= '<time class="entry-date" datetime="' . esc_attr(get_the_date('c')) . '">' . esc_html($date) . '</time></span> ';
break;
case 'updated_on' :
if ((current_time('timestamp', 1) - get_the_modified_date('U')) < 86400)
$date = human_time_diff(get_post_modified_time('U'), current_time('timestamp')) . ' '.__('ago','swift');
else
$date = get_the_modified_date();
$data .= '<span class="date updated fa-clock-o"><a href="' . esc_url(get_permalink()) . '" title="' . esc_attr(get_post_modified_time()) . '" rel="bookmark">';
$data .= '<time class="entry-date" datetime="' . esc_attr(get_the_modified_date('c')) . '">' . esc_html($date) . '</time></a></span> ';
This code removed links from your author and date .... Hope that works fine to you.
I can't figure out how to write a ternary operator for the ending of this line (i.e. "location" and "description"). I've defined the $location and $description variables but I'm getting an error that says unexpected $location variable.
Basically, for both location and description, I'm trying to say: If the field has been filled out, display the field. If not, don't print anything.
$content .= '<div data-order="' . $count . '" data-group="' . $group . '"><div class="img-wrap"><img data-iwidth="'.$isize[0].'" data-iheight="' . $isize[1] . '" class="myslide" data-lazy="' . get_template_directory_uri() . '/img/slides/' . strtolower($group) . '/' . $image_url . '" alt="' . get_sub_field('title') . '"><div class="slide-caption"><strong>' . get_sub_field('title') . '</strong><hr>' . get_sub_field('location') ? '<em>'$location'</em>' : ''; . get_sub_field('description') ? '<hr><span class="details">Details [+]</span><div class="details-display">'$description'</div>' : ''; . '</div></div></div>';
Here is the full context:
if( have_rows('slides', $frontpage_id) ):
while ( have_rows('slides', $frontpage_id) ) :
the_row();
$group = get_sub_field('group');
$count = 1;
$i = 0;
$nav .= '<ul id="nav_'.$group.'" style="display: none;">';
$content .= '<div class="image-data-container image-container-'. $group .'">';
while( have_rows('images') ) : the_row(); $count++;
$image_url = get_sub_field('image');
$location = get_sub_field('location');
$description = get_sub_field('description');
$isize = #getimagesize(get_template_directory_uri() . '/img/slides/' . strtolower($group) . '/' . $image_url);
$content .= '<div data-order="' . $count . '" data-group="' . $group . '"><div class="img-wrap"><img data-iwidth="'.$isize[0].'" data-iheight="' . $isize[1] . '" class="myslide" data-lazy="' . get_template_directory_uri() . '/img/slides/' . strtolower($group) . '/' . $image_url . '" alt="' . get_sub_field('title') . '"><div class="slide-caption"><strong>' . get_sub_field('title') . '</strong><hr>' . get_sub_field('location') ? '<em>'$location'</em>' : ''; . get_sub_field('description') ? '<hr><span class="details">Details [+]</span><div class="details-display">'$description'</div>' : ''; . '</div></div></div>';
$nav .= '<li >' . get_sub_field('title') . '</li>';
$i++;
endwhile;
$content .= '</div>';
$nav .= '</ul>';
endwhile;
endif;
?>
Other the fact that your $content variable is a formatting nightmare. The ternary operator syntax returns result based on the condition and it should be separated from your $content attribute
$someVariable = (get_sub_field('location')) ? '<em>'.$location.'</em>' : '';
Now use that some variable inside your $content variable instead
Here is your code with comments:
while( have_rows('images') ) : the_row(); $count++;
$image_url = get_sub_field('image');
// you have already defined the location variable here
// It also means and I am assuming they your get_sub_field is returning a string
// If you are receiving a boolean or null value than and than only than you can do your ternary condition call of
// get_sub_field('location') ? '<em>'$location'</em>' : ''
// and if you are receiving a string field then you will have to explicitly check the condition
// something like this (get_sub_field('location') === '') ? '<em>'$location'</em>' : ''
// I am not that familiar with wordpress framework or the ACF plugin but it looks like get_sub_field will return something so you actually wont need the ternary condition
$location = get_sub_field('location');
// Similar case with description
$description = get_sub_field('description');
$isize = #getimagesize(get_template_directory_uri() . '/img/slides/' . strtolower($group) . '/' . $image_url);
// After removing the ternary condition your content variable will look like this
$content .= '<div data-order="' . $count . '" data-group="' . $group . '"><div class="img-wrap"><img data-iwidth="'.$isize[0].'" data-iheight="' . $isize[1] . '" class="myslide" data-lazy="' . get_template_directory_uri() . '/img/slides/' . strtolower($group) . '/' . $image_url . '" alt="' . get_sub_field('title') . '"><div class="slide-caption"><strong>' . get_sub_field('title') . '</strong><hr>' . '<em>' . $location . '</em>' . '<hr><span class="details">Details [+]</span><div class="details-display">' . $description . '</div>' . '</div></div></div>';
$nav .= '<li >' . get_sub_field('title') . '</li>';
$i++;
endwhile;
You know you can always make it more readable
// You know instead of all the concatenation you can do this instead
$templateDirURI = get_template_directory_uri();
$groupLower = strtolower($group);
$title = get_sub_field('title');
$content .= "<div data-order='{$count}' data-group='{$group}'><div class='img-wrap'><img data-iwidth='{$isize[0]}' data-iheight='{$isize[1]}' class='myslide' data-lazy='{$templateDirURI}/img/slides/{$groupLower}/{$image_url}' alt='{$title}'><div class='slide-caption'><strong>{$title}</strong><hr><em>{$location}</em><hr><span class='details'>Details [+]</span><div class='details-display'>{$description}</div></div></div></div>";
Updated Code
$content .= "<div data-order='{$count}' data-group='{$group}'><div class='img-wrap'><img data-iwidth='{$isize[0]}' data-iheight='{$isize[1]}' class='myslide' data-lazy='{$templateDirURI}/img/slides/{$groupLower}/{$image_url}' alt='{$title}'><div class='slide-caption'><strong>{$title}</strong>";
// Assuming the user did not submit location information
if($location !== "")
$content .= "<hr><em>{$location}</em><hr>";
if($description !== "")
$content .= "<span class='details'>Details [+]</span><div class='details-display'>{$description}</div>";
$content .= "</div></div></div>";
I am trying to add for a Bootstrap Theme at OSCommerce an active class for manufacturors. I am a noob with PHP and cant finish it.. Anybody who can help me out?
The Code
$manufacturers_list = '<ul class="nav nav-pills nav-stacked">';
while ($manufacturers = tep_db_fetch_array($manufacturers_query))
{
$manufacturers_name = ((strlen($manufacturers['manufacturers_name']) > MAX_DISPLAY_MANUFACTURER_NAME_LEN) ? substr($manufacturers['manufacturers_name'], 0, MAX_DISPLAY_MANUFACTURER_NAME_LEN) . '..' : $manufacturers['manufacturers_name']);
if (isset($HTTP_GET_VARS['manufacturers_id']) && ($HTTP_GET_VARS['manufacturers_id'] == $manufacturers['manufacturers_id']))
$manufacturers_name = '<strong>' . $manufacturers_name .'</strong>';
$manufacturers_list .= '<li>' . $manufacturers_name . '</li>';
}
$manufacturers_list .= '</ul>';
I thought I could do it code it like here
$manufacturers_list .= '<li><a' if (isset($manufacturor_name)) {echo "class="'active'} ' href="' . tep_href_link(FILENAME_DEFAULT, 'manufacturers_id=' . $manufacturers['manufacturers_id']) . '">' . $manufacturers_name . '</a></li>';
Thanks for advices.
You can't have an if statement in between strings, concatenate using . and the ternary operator ?::
$manufacturers_list .= '<li><a '.(isset($manufacturor_name) ? 'class="active"' : '').'href="' . tep_href_link(FILENAME_DEFAULT, 'manufacturers_id=' . $manufacturers['manufacturers_id']) . '">' . $manufacturers_name . '</a></li>';
Any help is much appreciated. I have been trying to move the product Description to below the price in zencart, but the description fails to show. The question is where the dickens am I going wrong. Thanks for looking Jon
The line I have moved is:
<div class="listingDescription">' . zen_trunc_string(zen_clean_html(stripslashes(zen_get_products_description($listing->fields['products_id'], $_SESSION['languages_id']))), PRODUCT_LIST_DESCRIPTION) . '</div>
The original code is:
if (isset($_GET['manufacturers_id'])) {
$lc_text = 'fields['products_id']), 'cPath=' . (($_GET['manufacturers_id'] > 0 and $_GET['filter_id']) > 0 ? zen_get_generated_category_path_rev($_GET['filter_id']) : ($_GET['cPath'] > 0 ? zen_get_generated_category_path_rev($_GET['cPath']) : zen_get_generated_category_path_rev($listing->fields['master_categories_id']))) . '&products_id=' . $listing->fields['products_id']) . '">' . $listing->fields['products_name'] . '' . zen_trunc_string(zen_clean_html(stripslashes(zen_get_products_description($listing->fields['products_id'], $_SESSION['languages_id']))), PRODUCT_LIST_DESCRIPTION) . '' ;
} else {
$lc_text = 'fields['products_id']), 'cPath=' . (($_GET['manufacturers_id'] > 0 and $_GET['filter_id']) > 0 ? zen_get_generated_category_path_rev($_GET['filter_id']) : ($_GET['cPath'] > 0 ? zen_get_generated_category_path_rev($_GET['cPath']) : zen_get_generated_category_path_rev($listing->fields['master_categories_id']))) . '&products_id=' . $listing->fields['products_id']) . '">' . $listing->fields['products_name'] . '' . zen_trunc_string(zen_clean_html(stripslashes(zen_get_products_description($listing->fields['products_id'], $_SESSION['languages_id']))), PRODUCT_LIST_DESCRIPTION) . '';
}
break;
case 'PRODUCT_LIST_MANUFACTURER':
$lc_align = '';
$lc_text = 'fields['manufacturers_id']) . '">' . $listing->fields['manufacturers_name'] . '';
break;
case 'PRODUCT_LIST_PRICE':
$lc_price = zen_get_products_display_price($listing->fields['products_id']) . '';
$lc_align = 'right';
$lc_text = $lc_price;
The code I have tried (The page / price shows, but not the product description):
if (isset($_GET['manufacturers_id'])) {
$lc_text = '<h3 class="itemTitle">' . $listing->fields['products_name'] . '</h3>' ;
} else {
$lc_text = '<h3 class="itemTitle">' . $listing->fields['products_name'] . '</h3>';
}
break;
case 'PRODUCT_LIST_MANUFACTURER':
$lc_align = '';
$lc_text = '' . $listing->fields['manufacturers_name'] . '';
break;
case 'PRODUCT_LIST_PRICE':
$lc_price = zen_get_products_display_price($listing->fields['products_id']) . '';
$lc_align = 'right';
$lc_text = "<div style='font-size: 22px;'>".$lc_price. "</div>";
break;
case 'PRODUCT_LIST_DESCRIPTION':
$lc_align = '';
$lc_text = '<div class="listingDescription">' . zen_trunc_string(zen_clean_html(stripslashes(zen_get_products_description($listing->fields['products_id'], $_SESSION['languages_id']))), PRODUCT_LIST_DESCRIPTION) . '<br></div>';
No need to change any code.
Instead, simply change the desired sort order in Admin->Configuration->Product Listing.