Setting an If Statement with AJAX in PHP - php

I am nearing completion of this code / plugin and I just have one bit to finish up before we make it all pretty and presentable; I am attempting to get my PHP file to re-load/re-fresh with the POST variables sent from the AJAX file, all from the response of the AJAX send:
$("#CategoryTree").load("poster.php #CategoryTree");
Right now on clicking a list item the CSS fires, the ajax fires and responds, variables are sent (checked them in the action registry as well all good there) but the area I call to re-load with the poster.php #CategoryTree does not react, error 500 is thrown.
Here is my full Javascript code:
jQuery(document).ready(function($) {
$('#CategoryTree').on('click', 'input[type=checkbox]', function() {
var $this = $(this);
var data = '';
if ($this.is(":checked")) {
$this.addClass('selectCheckbox');
//$this.val("1");
data = {
action: 'catID_Callback',
catID: $this.attr('id'),
catState: 1
};
$.post(the_ajax_script.ajaxurl, data, function(response) {
//alert('Got this from the server: ' + response);
$("#CategoryTree").load("poster.php #CategoryTree");
//alert( "Load was performed." );});
console.log(response);
});
} else {
$this.removeClass('selectCheckbox');
//$this.val("0");
data = {
action: 'catID_Callback',
catID: $this.attr('id'),
catState: 0
};
$.post(the_ajax_script.ajaxurl, data, function(response) {
// alert('Got this from the server: ' + response);
console.log(response);
});
}
});
});
And here is the PHP code (Line 55 is where the check starts):
<?php
//$message = "Started";
//echo "<script type='text/javascript'>alert('$message');</script>";
$thearray = [];
$terms = get_terms("pa_mymelp");
foreach ( $terms as $term ) {
$categories = $term->name;
array_push($thearray, $categories);
}
$categoryLines = $thearray;
function buildCategoryTree($categoryLines, $separator) {
$catTree = array();
foreach ($categoryLines as $catLine) {
$path = explode($separator, $catLine);
$node = & $catTree;
foreach ($path as $cat) {
$cat = trim($cat);
if (!isset($node[$cat])) {
$node[$cat] = array();
}
$node = & $node[$cat];
}
}
return $catTree;
}
function displayCategoryTree($categoryTree, $indent = '') {
foreach ($categoryTree as $node => $children) {
echo $indent . $node . "\n";
displayCategoryTree($children, $indent . '|- ');
}
}
$categoryTree = buildCategoryTree($categoryLines, '/');
function displayHtmlCategoryTree($categoryTree, $id = null, $pathSeparator = '/', $parents = '') {
if (empty($categoryTree)) return '';
$str = '<ul' . (!empty($id) ? ' id="'.$id.'"' : '') . '>';
foreach ($categoryTree as $node => $children) {
$currentPath = $parents . (empty($parents) ? '' : $pathSeparator) . $node;
$thelink = '';
$opener = 0;
if (substr_count($currentPath, '/')==5){
$patterns = array(" ", "/", ".");
$thelink = 'http://caap.co.nz/?pa_mymelp=' . strtolower(str_replace($patterns, '-', $currentPath));
$str .= '<li title="' . $currentPath . '">' . '<input value ="0" class="first" type="checkbox" id="' . $currentPath . '">' . '<label for="' . $currentPath . '">' . '' . $node . '</label>' .
/*displayHtmlCategoryTree($children, null, $pathSeparator, $currentPath) .
*/'</li>';}
else
{
$cat = 0;
$catState = 0;
if (isset($_POST['catID'])){
$cat = $_POST['catID'];
}
if (isset($_POST['catState'])){
$catState = $_POST['catState'];
}
$str .= '<li title="' . $currentPath . '">' . '<input value="0" class="first" type="checkbox" id="' . $currentPath . '">' . '<label for="' . $currentPath . '">' . $node . '</label>';
if ($cat == $currentPath && $catState == 1 ){$str.=displayHtmlCategoryTree($children, null, $pathSeparator, $currentPath);}
}
}
$str .= '</li></ul>';
return $str;
}
echo displayHtmlCategoryTree($categoryTree, "CategoryTree", '/');
/*
function add_query_vars_filter( $vars ){
foreach (array_keys(Contracts::$query_params) as $name)
$vars[] = $name;
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
*/
?>
Here is the pertinent code from the PHP file
$cat = 0;
$catState = 0;
if (isset($_POST['catID'])){
$cat = $_POST['catID'];
}
if (isset($_POST['catState'])){
$catState = $_POST['catState'];
}
$str .= '<li title="' . $currentPath . '">' . '<input value="0" class="first" type="checkbox" id="' . $currentPath . '">' . '<label for="' . $currentPath . '">' . $node . '</label>';
if ($cat == $currentPath && $catState == 1 ){$str.=displayHtmlCategoryTree($children, null, $pathSeparator, $currentPath);}

Remove semicolon after if curly braces. if{}; to if{}
if (isset($_POST['catID'])){$cat = $_POST['catID'];};
replace it to
if (isset($_POST['catID'])){
$cat = $_POST['catID'];
}
Now your code looks like.
if (isset($_POST['catID'])){
$cat = $_POST['catID'];
}
if (isset($_POST['catState'])){
$catState = $_POST['catState'];
}
$str .= '<li title="' . $currentPath . '">' . '<input value="0" class="first" type="checkbox" id="' . $currentPath . '">' . '<label for="' . $currentPath . '">' . $node . '</label>';
if ($cat == $currentPath && $catState == 1){
$str.=displayHtmlCategoryTree($children, null, $pathSeparator, $currentPath);
}

Related

How to add a new line in textarea element in php code?

I want to add a newline in a textarea. I tried with < p> tag but it's not working. Can you help me to insert a newline in a textarea? Please find the code above for a generic contact form. Where should I add tags for line breaks here?
public function cs_form_textarea_render($params = '') {
global $post, $pagenow;
extract($params);
if ( $pagenow == 'post.php' ) {
$cs_value = get_post_meta($post->ID, 'cs_' . $id, true);
} else {
$cs_value = $std;
}
if ( isset($cs_value) && $cs_value != '' ) {
$value = $cs_value;
} else {
$value = $std;
}
$cs_rand_id = time();
if ( isset($force_std) && $force_std == true ) {
$value = $std;
}
$html_id = ' id="cs_' . sanitize_html_class($id) . '"';
$html_name = ' name="cs_' . sanitize_html_class($id) . '"';
if ( isset($array) && $array == true ) {
$html_id = ' id="cs_' . sanitize_html_class($id) . $cs_rand_id . '"';
$html_name = ' name="cs_' . sanitize_html_class($id) . '_array[]"';
}
$cs_required = '';
if ( isset($required) && $required == 'yes' ) {
$cs_required = ' required="required"';
}
$cs_output = '<div class="' . $classes . '">';
$cs_output .= ' <textarea' . $cs_required . ' rows="5" cols="30"' . $html_id . $html_name . ' placeholder="' . $name . '">' . sanitize_text_field($value) . '</textarea>';
$cs_output .= $this->cs_form_description($description);
$cs_output .= '</div>';
if ( isset($return) && $return == true ) {
return force_balance_tags($cs_output);
} else {
echo force_balance_tags($cs_output);
}
}
Just add a "\n" char somewhere between your text area tags
$cs_output .= ' <textarea' . $cs_required . ' rows="5" cols="30"' . $html_id . $html_name . ' placeholder="' . $name . '">' . sanitize_text_field($value) . "\n" . "this text is on a new line". '</textarea>';
You will try to add "\n" before your end position of the tag. You need to concatenate Like ."\n".

I have some problem with send and write array in database (Codeingniter 3)

I have a form that sends customer data to the database, I added a choice of another category of customers through the input, and instead of sending the data in text, a number is sent.
db
In View
<?php echo render_select('sellers',get_sellers(),array('articleid','subject'),''); ?>
echo render_select helper:
function render_select($name, $options, $option_attrs = [], $label = '', $selected = '', $select_attrs = [], $form_group_attr = [], $form_group_class = '', $select_class = '', $include_blank = true)
{
$callback_translate = '';
if (isset($options['callback_translate'])) {
$callback_translate = $options['callback_translate'];
unset($options['callback_translate']);
}
$select = '';
$_form_group_attr = '';
$_select_attrs = '';
if (!isset($select_attrs['data-width'])) {
$select_attrs['data-width'] = '100%';
}
if (!isset($select_attrs['data-none-selected-text'])) {
$select_attrs['data-none-selected-text'] = _l('dropdown_non_selected_tex');
}
foreach ($select_attrs as $key => $val) {
// tooltips
if ($key == 'title') {
$val = _l($val);
}
$_select_attrs .= $key . '=' . '"' . $val . '" ';
}
$_select_attrs = rtrim($_select_attrs);
$form_group_attr['app-field-wrapper'] = $name;
foreach ($form_group_attr as $key => $val) {
// tooltips
if ($key == 'title') {
$val = _l($val);
}
$_form_group_attr .= $key . '=' . '"' . $val . '" ';
}
$_form_group_attr = rtrim($_form_group_attr);
if (!empty($select_class)) {
$select_class = ' ' . $select_class;
}
if (!empty($form_group_class)) {
$form_group_class = ' ' . $form_group_class;
}
$select .= '<div class="select-placeholder form-group' . $form_group_class . '" ' . $_form_group_attr . '>';
if ($label != '') {
$select .= '<label for="' . $name . '" class="control-label">' . _l($label, '', false) . '</label>';
}
$select .= '<select id="' . $name . '" name="' . $name . '" class="selectpicker' . $select_class . '" ' . $_select_attrs . ' data-live-search="true">';
if ($include_blank == true) {
$select .= '<option value=""></option>';
}
foreach ($options as $option) {
$val = '';
$_selected = '';
$key = '';
if (isset($option[$option_attrs[0]]) && !empty($option[$option_attrs[0]])) {
$key = $option[$option_attrs[0]];
}
if (!is_array($option_attrs[1])) {
$val = $option[$option_attrs[1]];
} else {
foreach ($option_attrs[1] as $_val) {
$val .= $option[$_val] . ' ';
}
}
$val = trim($val);
if ($callback_translate != '') {
if (function_exists($callback_translate) && is_callable($callback_translate)) {
$val = call_user_func($callback_translate, $key);
}
}
$data_sub_text = '';
if (!is_array($selected)) {
if ($selected != '') {
if ($selected == $key) {
$_selected = ' selected';
}
}
} else {
foreach ($selected as $id) {
if ($key == $id) {
$_selected = ' selected';
}
}
}
if (isset($option_attrs[2])) {
if (strpos($option_attrs[2], ',') !== false) {
$sub_text = '';
$_temp = explode(',', $option_attrs[2]);
foreach ($_temp as $t) {
if (isset($option[$t])) {
$sub_text .= $option[$t] . ' ';
}
}
} else {
if (isset($option[$option_attrs[2]])) {
$sub_text = $option[$option_attrs[2]];
} else {
$sub_text = $option_attrs[2];
}
}
$data_sub_text = ' data-subtext=' . '"' . $sub_text . '"';
}
$data_content = '';
if (isset($option['option_attributes'])) {
foreach ($option['option_attributes'] as $_opt_attr_key => $_opt_attr_val) {
$data_content .= $_opt_attr_key . '=' . '"' . $_opt_attr_val . '"';
}
if ($data_content != '') {
$data_content = ' ' . $data_content;
}
}
$select .= '<option value="' . $key . '"' . $_selected . $data_content . $data_sub_text . '>' . $val . '</option>';
}
$select .= '</select>';
$select .= '</div>';
return $select;
}
get sellers arrays in db functions
function get_sellers()
{
$CI = & get_instance();
return $CI->db->get(db_prefix() . 'sellers')->result_array();
}
I have created a column "sellers" in my table but it sends numbers and not text.
I need it so that when the form is saved it will submit the title and display it in the selection.
Thanks you
View form:
click

How can I have this custom shortcode display within the page content (Divi Builder)?

I am using the Divi Theme.
The shortcode is working, however it is displaying directly under the header rather than where I place the shortcode within the Divi Builder.
I read more about WP shortcodes and it looked like we should be using return rather than echo, but when I change it to return it does not display at all on the page.
Thanks!
function breadcrumb() {
$delimiter = '»'; // delimiter between crumbs
$home = 'Home'; // text for the 'Home' link
$showCurrent = 1; // 1 - show current post/page title in breadcrumbs, 0 - don't show
$before = '<span class="current">'; // tag before the current crumb
$after = '</span>'; // tag after the current crumb
global $post;
$homeLink = get_bloginfo('url');
echo '<div id="crumbs">' . $home . ' ' . $delimiter . ' ';
if (is_single() && !is_attachment()) {
if (get_post_type() != 'post') {
$post_type = get_post_type_object(get_post_type());
$slug = $post_type->rewrite;
return '' . $post_type->labels->singular_name . '';
if ($showCurrent == 1) {
return ' ' . $delimiter . ' ' . $before . get_the_title() . $after;
}
} else {
$cat = get_the_category();
$cat = $cat[0];
$cats = get_category_parents($cat, true, ' ' . $delimiter . ' ');
if ($showCurrent == 0) {
$cats = preg_replace("#^(.+)\s$delimiter\s$#", "$1", $cats);
}
echo $cats;
if ($showCurrent == 1) {
echo $before . get_the_title() . $after;
}
}
} elseif (!is_single() && !is_page() && get_post_type() != 'post' && !is_404()) {
$post_type = get_post_type_object(get_post_type());
return $before . $post_type->labels->singular_name . $after;
} elseif (is_attachment()) {
$parent = get_post($post->post_parent);
$cat = get_the_category($parent->ID);
$cat = $cat[0];
return get_category_parents($cat, true, ' ' . $delimiter . ' ');
echo '' . $parent->post_title . '';
if ($showCurrent == 1) {
echo ' ' . $delimiter . ' ' . $before . get_the_title() . $after;
}
} elseif (is_page() && !$post->post_parent) {
if ($showCurrent == 1) {
return $before . get_the_title() . $after;
}
} elseif (is_page() && $post->post_parent) {
$parent_id = $post->post_parent;
$breadcrumbs = array();
while ($parent_id) {
$page = get_page($parent_id);
$breadcrumbs[] = '' . get_the_title($page->ID) . '';
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse($breadcrumbs);
for ($i = 0; $i < count($breadcrumbs); $i++) {
return $breadcrumbs[$i];
if ($i != count($breadcrumbs)-1) {
return ' ' . $delimiter . ' ';
}
}
if ($showCurrent == 1) {
return ' ' . $delimiter . ' ' . $before . get_the_title() . $after;
}
}
return '</div>';
}
function breadcrumb() {
$delimiter = '»'; // delimiter between crumbs
$home = 'Home'; // text for the 'Home' link
$showCurrent = 1; // 1 - show current post/page title in breadcrumbs, 0 - don't show
$before = '<span class="current">'; // tag before the current crumb
$after = '</span>'; // tag after the current crumb
global $post;
$homeLink = get_bloginfo('url');
$bredcrumb = '<div id="crumbs">' . $home . ' ' . $delimiter . ' ';
if (is_single() && !is_attachment()) {
if (get_post_type() != 'post') {
$post_type = get_post_type_object(get_post_type());
$slug = $post_type->rewrite;
$bredcrumb .='' . $post_type->labels->singular_name . '';
if ($showCurrent == 1) {
$bredcrumb .=' ' . $delimiter . ' ' . $before . get_the_title() . $after;
}
} else {
$cat = get_the_category();
$cat = $cat[0];
$cats = get_category_parents($cat, true, ' ' . $delimiter . ' ');
if ($showCurrent == 0) {
$cats = preg_replace("#^(.+)\s$delimiter\s$#", "$1", $cats);
}
$bredcrumb .= $cats;
if ($showCurrent == 1) {
$bredcrumb .= $before . get_the_title() . $after;
}
}
} elseif (!is_single() && !is_page() && get_post_type() != 'post' && !is_404()) {
$post_type = get_post_type_object(get_post_type());
$bredcrumb .= $before . $post_type->labels->singular_name . $after;
} elseif (is_attachment()) {
$parent = get_post($post->post_parent);
$cat = get_the_category($parent->ID);
$cat = $cat[0];
$bredcrumb .= get_category_parents($cat, true, ' ' . $delimiter . ' ');
$bredcrumb .= '' . $parent->post_title . '';
if ($showCurrent == 1) {
$bredcrumb .= ' ' . $delimiter . ' ' . $before . get_the_title() . $after;
}
} elseif (is_page() && !$post->post_parent) {
if ($showCurrent == 1) {
$bredcrumb .= $before . get_the_title() . $after;
}
} elseif (is_page() && $post->post_parent) {
$parent_id = $post->post_parent;
$breadcrumbs = array();
while ($parent_id) {
$page = get_page($parent_id);
$breadcrumbs[] = '' . get_the_title($page->ID) . '';
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse($breadcrumbs);
for ($i = 0; $i < count($breadcrumbs); $i++) {
$bredcrumb .= $breadcrumbs[$i];
if ($i != count($breadcrumbs)-1) {
$bredcrumb .= ' ' . $delimiter . ' ';
}
}
if ($showCurrent == 1) {
$bredcrumb .= ' ' . $delimiter . ' ' . $before . get_the_title() . $after;
}
}
$bredcrumb .= '</div>';
echo $bredcrumb;
return $bredcrumb;

Magento 2 : Allow customer to edit custom options on Cart

I am trying to add functionality to allow a customer to edit options.
I have created a Module Vendor/COptions.
This is loading custom options and select, however is not getting user selected options.
I would like to know how to received selected options and check if the option is selected.
At the moment this line in Select.php is not receiving anything, and the variable $checked is null.
$configValue = $this->getProduct()->getPreconfiguredValues()->getData('options/' . $_option->getId());
in Vendor/COptions\view\frontend\templates\cart\item\default.phtml :
<?php $selectedOptions = $block->getSelectedOptionList(); ?>
<?php //$_options = Mage::helper('core')->decorateArray($block>getOptions())
$_options = $block->getOptions() ?>
<?php if ($_options AND count($_options)):?>
<dl>
<?php foreach($_options as $_option): ?>
<?php echo $this->getOptionHtml($_option) ?>
<?php endforeach; ?>
</dl>
<?php endif; ?>
in Vendor\COptions\Helper\Rewrite\Product:
public function getCustomOptions(\Magento\Catalog\Model\Product\Configuration\Item\ItemInterface $item, $simple="")
{
$product = $item->getProduct();
$coptions = [];
$coptions = $product->getOptions();
$options = array();
if ($coptions) {
foreach ($coptions as $option) {
$itemOption = $item->getOptionByCode('option_' . $option->getId());
/** #var $group \Magento\Catalog\Model\Product\Option\Type\DefaultType */
$group = $option->groupFactory($option->getType())
->setOption($option)
->setConfigurationItem($item)
->setConfigurationItemOption($itemOption);
if ('file' == $option->getType()) {
$downloadParams = $item->getFileDownloadParams();
if ($downloadParams) {
$url = $downloadParams->getUrl();
if ($url) {
$group->setCustomOptionDownloadUrl($url);
}
$urlParams = $downloadParams->getUrlParams();
if ($urlParams) {
$group->setCustomOptionUrlParams($urlParams);
}
}
}
if($simple == "Y"){
array_push($options, $itemOption['value']);
}else{
$options[] = [
'label' => $option->getTitle(),
'value' => $group->getFormattedOptionValue($itemOption['value']),
'print_value' => $group->getFormattedOptionValue($itemOption['value']),
'option_id' => $option->getId(),
'option_type' => $option->getType(),
'custom_view' => $group->isCustomizedView(),
];
}
}
}
$addOptions = $item->getOptionByCode('additional_options');
if ($addOptions) {
$options = array_merge($options, $this->serializer->unserialize($addOptions->getValue()));
}
return $options;
}
in Vendor/COptions\Block\Rewrite\Catalog\Product\View\Options\Type\Select.php:
public function getValuesHtml()
{
$_option = $this->getOption();
$configValue = $this->getProduct()->getPreconfiguredValues()->getData('options/' . $_option->getId());
$store = $this->getProduct()->getStore();
$this->setSkipJsReloadPrice(1);
// Remove inline prototype onclick and onchange events
if ($_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_DROP_DOWN ||
$_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_MULTIPLE
) {
$require = $_option->getIsRequire() ? ' required' : '';
$extraParams = '';
$select = $this->getLayout()->createBlock(
\Magento\Framework\View\Element\Html\Select::class
)->setData(
[
'id' => 'select_' . $_option->getId(),
'class' => $require . ' product-custom-option admin__control-select'
]
);
if ($_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_DROP_DOWN) {
$select->setName('options[' . $_option->getid() . ']')->addOption('', __('-- Please Select --'));
} else {
$select->setName('options[' . $_option->getid() . '][]');
$select->setClass('multiselect admin__control-multiselect' . $require . ' product-custom-option');
}
foreach ($_option->getValues() as $_value) {
$priceStr = $this->_formatPrice(
[
'is_percent' => $_value->getPriceType() == 'percent',
'pricing_value' => $_value->getPrice($_value->getPriceType() == 'percent'),
],
false
);
$select->addOption(
$_value->getOptionTypeId(),
$_value->getTitle() . ' ' . strip_tags($priceStr) . '',
['price' => $this->pricingHelper->currencyByStore($_value->getPrice(true), $store, false)]
);
}
if ($_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_MULTIPLE) {
$extraParams = ' multiple="multiple"';
}
if (!$this->getSkipJsReloadPrice()) {
$extraParams .= ' onchange="opConfig.reloadPrice()"';
}
$extraParams .= ' data-selector="' . $select->getName() . '"';
$select->setExtraParams($extraParams);
if ($configValue) {
$select->setValue($configValue);
}
return $select->getHtml();
}
if ($_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_RADIO ||
$_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_CHECKBOX
) {
$selectHtml = '<div class="options-list nested" id="options-' . $_option->getId() . '-list">';
$require = $_option->getIsRequire() ? ' required' : '';
$arraySign = '';
switch ($_option->getType()) {
case \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_RADIO:
$type = 'radio';
$class = 'radio admin__control-radio';
if (!$_option->getIsRequire()) {
$selectHtml .= '<div class="field choice admin__field admin__field-option">' .
'<input type="radio" id="options_' .
$_option->getId() .
'" class="' .
$class .
' product-custom-option" name="options[' .
$_option->getId() .
']"' .
' data-selector="options[' . $_option->getId() . ']"' .
($this->getSkipJsReloadPrice() ? '' : ' onclick="opConfig.reloadPrice()"') .
' value="" checked="checked" /><label class="label admin__field-label" for="options_' .
$_option->getId() .
'"><span>' .
__('None') . '</span></label></div>';
}
break;
case \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_CHECKBOX:
$type = 'checkbox';
$class = 'checkbox admin__control-checkbox';
$arraySign = '[]';
break;
}
$count = 1;
foreach ($_option->getValues() as $_value) {
$count++;
$priceStr = $this->_formatPrice(
[
'is_percent' => $_value->getPriceType() == 'percent',
'pricing_value' => $_value->getPrice($_value->getPriceType() == 'percent'),
]
);
$htmlValue = $_value->getOptionTypeId();
if ($arraySign) {
$checked = is_array($configValue) && in_array($htmlValue, $configValue) ? 'checked' : '';
} else {
$checked = $configValue == $htmlValue ? 'checked' : '';
}
$dataSelector = 'options[' . $_option->getId() . ']';
if ($arraySign) {
$dataSelector .= '[' . $htmlValue . ']';
}
$selectHtml .= '<div class="field choice admin__field admin__field-option' .
$require .
'">' .
'<input type="' .
$type .
'" class="' .
$class .
' ' .
$require .
' product-custom-option"' .
($this->getSkipJsReloadPrice() ? '' : ' onclick="opConfig.reloadPrice()"') .
' name="options[' .
$_option->getId() .
']' .
$arraySign .
'" id="options_' .
$_option->getId() .
'_' .
$count .
'" value="' .
$htmlValue .
'" ' .
$checked .
' data-selector="' . $dataSelector . '"' .
' price="' .
$this->pricingHelper->currencyByStore($_value->getPrice(true), $store, false) .
'" />' .
'<label class="label admin__field-label" for="options_' .
$_option->getId() .
'_' .
$count .
'"><span>' .
$_value->getTitle() .
'</span> ' .
$priceStr .
'</label>';
$selectHtml .= '</div>';
}
$selectHtml .= '</div>';
return $selectHtml;
}
}

using an image for a button on php

I have a php script which creates its own button, as I am using an open source framework. What I want is to use an image to create a button instead of a the link the is created. Here is the line for which I need the button:
<td class="main button_marg"><?php echo tep_draw_button(IMAGE_BUTTON_REVIEWS . (($reviews['count'] > 0) ? ' (' . $reviews['count'] . ')' : ''), 'comment', tep_href_link(FILENAME_PRODUCT_REVIEWS, tep_get_all_get_params())); ?></td>
That is the script I am using to create the review button, but I would like to insert an image into it. Any ideas would be most appreciative.
This is the code for tep_draw_button() function:
function tep_draw_button($title = null, $icon = null, $link = null, $priority = null, $params = null) {
static $button_counter = 1;
$types = array('submit', 'button', 'reset');
if ( !isset($params['type']) ) {
$params['type'] = 'submit';
}
if ( !in_array($params['type'], $types) ) {
$params['type'] = 'submit';
}
if ( ($params['type'] == 'submit') && isset($link) ) {
$params['type'] = 'button';
}
if (!isset($priority)) {
$priority = 'secondary';
}
$button = '<span class="tdbLink">';
if ( ($params['type'] == 'button') && isset($link) ) {
$button .= '<a id="tdb' . $button_counter . '" href="' . $link . '"';
if ( isset($params['newwindow']) ) {
$button .= ' target="_blank"';
}
} else {
$button .= '<button id="tdb' . $button_counter . '" type="' . tep_output_string($params['type']) . '"';
}
if ( isset($params['params']) ) {
$button .= ' ' . $params['params'];
}
$button .= '>' . $title;
if ( ($params['type'] == 'button') && isset($link) ) {
$button .= '</a>';
} else {
$button .= '</button>';
}
$button .= '</span><script type="text/javascript">$("#tdb' . $button_counter . '").button(';
$args = array();
if ( isset($icon) ) {
if ( !isset($params['iconpos']) ) {
$params['iconpos'] = 'left';
}
if ( $params['iconpos'] == 'left' ) {
$args[] = 'icons:{primary:"ui-icon-' . $icon . '"}';
} else {
$args[] = 'icons:{secondary:"ui-icon-' . $icon . '"}';
}
}
if (empty($title)) {
$args[] = 'text:false';
}
if (!empty($args)) {
$button .= '{' . implode(',', $args) . '}';
}
$button .= ').addClass("ui-priority-' . $priority . '").parent().removeClass("tdbLink");</script>';
$button_counter++;
return $button;
}
?>
You can probably override the style by using some css:
#your_button_id{ background: url(); height: x; width: x; etc... }
.your_button_class{ background: url(); height: x; width: x; etc... }
Try this:
To call the button, use the function with the parameters as they are below:
$image['src'] = 'http://yoururl.com/image.jpg'; // Image Source
$image['height'] = 600; // Image Height
$image['width'] = 300; // Image Width
tep_draw_button(IMAGE_BUTTON_REVIEWS . (($reviews['count'] > 0) ? ' (' . $reviews['count'] . ')' : ''), 'comment', tep_href_link(FILENAME_PRODUCT_REVIEWS, tep_get_all_get_params()), null, null, $image);
Then modify the tep_draw_button function like so. If you have an image source in the function parameter, the image will be placed in as a link rather than as a button.
<?
function tep_draw_button($title = null, $icon = null, $link = null, $priority = null, $params = null, $image = null) {
static $button_counter = 1;
if($image != null){
$button = '<span class="tdbLink">';
$button .= '<a id="tdb' . $button_counter . '" href="' . $link . '"';
$button .= '><img src="'. $image['src'] .'"';
if(is_numeric($image['height'])) $button .= ' height="'.$image['height'].'"';
if(is_numeric($image['width'])) $button .= ' width="'.$image['width'].'"';
$button .= 'alt="'.$title.'" /></a></span>';
}else{
$types = array('submit', 'button', 'reset');
if ( !isset($params['type']) ) {
$params['type'] = 'submit';
}
if ( !in_array($params['type'], $types) ) {
$params['type'] = 'submit';
}
if ( ($params['type'] == 'submit') && isset($link) ) {
$params['type'] = 'button';
}
if (!isset($priority)) {
$priority = 'secondary';
}
$button = '<span class="tdbLink">';
if ( ($params['type'] == 'button') && isset($link) ) {
$button .= '<a id="tdb' . $button_counter . '" href="' . $link . '"';
if ( isset($params['newwindow']) ) {
$button .= ' target="_blank"';
}
} else {
$button .= '<button id="tdb' . $button_counter . '" type="' . tep_output_string($params['type']) . '"';
}
if ( isset($params['params']) ) {
$button .= ' ' . $params['params'];
}
$button .= '>' . $title;
if ( ($params['type'] == 'button') && isset($link) ) {
$button .= '</a>';
} else {
$button .= '</button>';
}
$button .= '</span><script type="text/javascript">$("#tdb' . $button_counter . '").button(';
$args = array();
if ( isset($icon) ) {
if ( !isset($params['iconpos']) ) {
$params['iconpos'] = 'left';
}
if ( $params['iconpos'] == 'left' ) {
$args[] = 'icons:{primary:"ui-icon-' . $icon . '"}';
} else {
$args[] = 'icons:{secondary:"ui-icon-' . $icon . '"}';
}
}
if (empty($title)) {
$args[] = 'text:false';
}
if (!empty($args)) {
$button .= '{' . implode(',', $args) . '}';
}
$button .= ').addClass("ui-priority-' . $priority . '").parent().removeClass("tdbLink");</script>';
$button_counter++;
}
return $button;
}
?>
Let me know if this works, or if you need it tweaked at all. It's hard for me to test since I don't have an oscommerce instance on my machine to test on.

Categories