Why is the "Learn More" link not linking to the page? - php

I'm trying to understand how this code (from another developer) is written. It has a bug but I can't seem to fix it. The learn more link doesn't link to the post in the custom field.
I've tried to remove the learn more lines but it then it changes the slide link to link to the image itself and not what's present in the custom link field.
$slides = ONS_Slide_Custom_Post_Type::find_all('DESC');
if (isset($slides) && count($slides > 0)) {
$items = array();
foreach ($slides as $slide) {
//echo '<tt><pre>' . var_export($slide, true) . '</pre></tt>';
$item = new stdClass();
if (isset($slide->custom_data) && count($slide->custom_data) > 0) {
if (isset($slide->custom_data['ons_slide_image'])) {
$item->src = $slide->custom_data['ons_slide_image'];
}
if (isset($slide->custom_data['ons_slide_heading'])) {
$item->heading = $slide->custom_data['ons_slide_heading'];
$item->heading .= '<span class="punctuation">.</span><span class="learn_more"> »</span>';
}
if (isset($slide->custom_data['ons_slide_caption'])) {
$item->caption = $slide->custom_data['ons_slide_caption'];
$item->caption .= ' Learn more »';
}
if (isset($slide->custom_data['ons_slide_href'])) {
$item->href = $slide->custom_data['ons_slide_href'];
} else {
$item->href = "#";
}
}
$items[] = $item;
}
$carousel = new ONS_Bootstrap_Carousel($items);
echo $carousel;
}

You are already doing something with $slide->custom_data['ons_slide_href']; but AFTER the lines of code that are outputting your anchor tag.
So try switching the processing about a bit like this
$slides = ONS_Slide_Custom_Post_Type::find_all('DESC');
if (isset($slides) && count($slides > 0)) {
$items = array();
foreach ($slides as $slide) {
//echo '<tt><pre>' . var_export($slide, true) . '</pre></tt>';
$item = new stdClass();
if (isset($slide->custom_data) && count($slide->custom_data) > 0) {
if (isset($slide->custom_data['ons_slide_image'])) {
$item->src = $slide->custom_data['ons_slide_image'];
}
if (isset($slide->custom_data['ons_slide_heading'])) {
$item->heading = $slide->custom_data['ons_slide_heading'];
$item->heading .= '<span class="punctuation">.</span><span class="learn_more"> »</span>';
}
// moved this code above the anchor tag line
if (isset($slide->custom_data['ons_slide_href'])) {
$item->href = $slide->custom_data['ons_slide_href'];
} else {
$item->href = "#";
}
// Now concatenate $item->href in the anchor tag line
if (isset($slide->custom_data['ons_slide_caption'])) {
$item->caption = $slide->custom_data['ons_slide_caption'];
$item->caption .= ' Learn more »';
}
}
$items[] = $item;
}
$carousel = new ONS_Bootstrap_Carousel($items);
echo $carousel;
}

Related

Auto Description From Tags

I'm trying to generate auto description from tags.
The code was working but after updating my site to Laravel 6 in stop working. I need to get it back working.
if( !empty( $request->description ) )
{
$description = Helper::checkTextDb($request->description);
}
else
{
$a_key = explode(",", strtolower($request->tags));
if(count($a_key) == 0)
$description = 'This is a great thing';
else
{
$description_get_keys = '';
foreach ($a_key as &$value)
{
if($value == end($a_key) && count($a_key) != 1)
$description_get_keys = $description_get_keys.' and '.$value.'.';
else if(count($a_key) == 1)
$description_get_keys = $value.'.';
else if (count($a_key) > 1 && $a_key[0] == $value)
$description_get_keys = $value;
else
$description_get_keys = $description_get_keys.', '.$value;
}
$description = 'This is a great thing about '.$description_get_keys;
}
}
I see a couple things that could possibly be an issue, not knowing what came before this code.
I will assume that the $request variable is an instance of Illuminate\Http\Request and that it is available in the function, right?
Try this updated code:
if($request->has('description'))
{
$description = Helper::checkTextDb($request->description);
}
else if ($request->has('tags'))
{
if (strpos($request->tags, ',') === false)
{
$description = 'This is a great thing';
}
else {
$a_key = explode(",", strtolower($request->tags));
$a_count = count($a_key);
$description_get_keys = '';
for ($i = 0; $i < $a_count; $i++)
{
if ($a_count == 1) {
$description_get_keys = "{$a_key[$i]}.";
}
else {
// first
if ($i === 0) {
$description_get_keys = $a_key[0];
}
// last
else if ($i === $a_count - 1) {
$description_get_keys .= " and {$a_key[$i]}.";
}
// middle
else {
$description_get_keys .= ", {$a_key[$i]}";
}
}
}
$description = "This is a great thing about {$description_get_keys}";
}
}
I wrote that quick so hopefully there are no errors.

How to Update Categories that Already Exists in Magento Database Using Custom Script

I have Already Created The Categories in Magento by using the custom script,
but issue is when I again run the same URL my script creates another set of same category. as i want to update the existing category and insert the new categories if exist in the csv.
My csv structure is:
Category 1,Description,Category 2,Description,Category 3,Description,Category 4,Description
1,COSTUMES,1,ADULTS,1,MENS
1,COSTUMES,1,ADULTS,1,MENS,2,ASIAN
1,COSTUMES,1,ADULTS,1,MENS,3,BIKER
1,COSTUMES,1,ADULTS,1,MENS,1,CAPES & ROBES
1,COSTUMES,1,ADULTS,1,MENS,5,CAVE PEOPLE
Thanking All of You in Advance For Your Answer
Below is my code to create multi level categories:
define('MAGENTO', realpath(dirname(__FILE__)));
require_once MAGENTO . '/app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$count = 0;
$file = fopen('export/web categories.csv', 'r');
function odd($var)
{
$odd = array();
foreach ($var as $k => $v) {
if ($k % 2 !== 0) {
$odd[$k] = $v;
}
}
return $odd;
}
function even($var)
{
$even = array();
foreach ($var as $k => $v) {
if ($k % 2 == 0) {
$even[$k] = $v;
}
}
return $even;
}
function strcount($str,$sy){
$ch= explode($sy, $str);
return count($ch);
}
$pos=0;
$row_config= array();
$test=array();
$catgoryID=array();
$parID = 1;
while (($line = fgetcsv($file)) !== FALSE) {
if(!in_array($valtest,$row_config)){
if($count == 0){
$count++;
continue;
}
$count++;
$filterd_data=array_filter($line);
$odd_cell_data=odd($filterd_data);
$even_cell_data=even($filterd_data);
$config=array();
$string='';
$intialID=$even_cell_data[0];
foreach($odd_cell_data as $key=>$val){
if(!in_array($val,$config)){
$config[] = $val;
$data['general']['name'] =$val;
$data['general']['meta_title'] = "";
$data['general']['meta_description'] = "";
$data['general']['is_active'] = "";
$data['general']['url_key'] = "";
$data['general']['is_anchor'] = 0;
$data['general']['position'] =1 ;
$storeId = 0;
$string .=$val.'~';
if(!array_key_exists($string, $row_config)){
$catID=createCategory($data, $storeId);
$catgoryID[$string]=$catID;
$row_config[$string]=$parID;
} else {
$parID =$row_config[$string] ;
$row_config[$string]=$row_config[$string];
}
if( strcount($string,'~')==2){
$parID=1;
}
$int[$string]=$parID;
assignCat($catgoryID[$string],$parID);
$parID = $catgoryID[$string];
sleep(0.5);
unset($data);
}
}
}
}
//This Function is used for create each category
function createCategory($data, $storeId) {
$category = Mage::getModel('catalog/category');
$category->setStoreId($storeId);
if (is_array($data)) {
$category->addData($data['general']);
if (!$category->getId()) {
$parentId = $data['category']['parent'];
if (!$parentId) {
if ($storeId) {
$parentId = Mage::app()->getStore($storeId)->getRootCategoryId();
} else {
$parentId = Mage_Catalog_Model_Category::TREE_ROOT_ID;
}
}
$parentCategory = Mage::getModel('catalog/category')->load($parentId);
$category->setPath($parentCategory->getPath());
} if ($useDefaults = $data['use_default']) {
foreach ($useDefaults as $attributeCode) {
$category->setData($attributeCode, null);
}
}
$category->setAttributeSetId($category->getDefaultAttributeSetId());
if (isset($data['category_products']) && !$category->getProductsReadonly()) {
$products = array();
parse_str($data['category_products'], $products);
$category->setPostedProducts($products);
} try {
$category->save();
$category = Mage::getModel('catalog/category')->load($category->getId());
$category->setPosition($data['general']['position']);
$category->addData($data['general']);
$category->save();
echo "Suceeded <br /> ";
} catch (Exception $e) {
echo "Failed <br />";
}
}
return $category->getId();
}
//This Function is used for moving category under respective parent
function assignCat($id, $parent){
$category = Mage::getModel( 'catalog/category' )->load($id);
Mage::unregister('category');
Mage::unregister('current_category');
Mage::register('category', $category);
Mage::register('current_category', $category);
$category->move($parent);
return;
}

PHP Simple HTML DOM parser

I am working with simple web crawler. Below is simple html code i used to learn.
input.php
<ul id="nav">
<li>
Google
<ul>
<li>
Gmail
</li>
</ul>
</li>
<li>
Yahoo
<ul>
<li>
Yahoo Mail
</li>
</ul>
</li>
</ul>
I need to crawl the first anchor tag in ul[id=nav]->li. The code i used to crawl input.php is
<?php
include 'simple_html_dom.php';
$html = file_get_html('input.php');
foreach ($html->find('ul[id=nav]') as $navUL){
foreach ($navUL->find('li') as $navUL_LI){
echo $navUL_LI->find('a',0)->outertext."<br>";
}
}
?>
It Displays all the anchor tag in my input.php. I need to display only google and yahoo. How can i achieve this?
In this case you can directly point it out with children() method. Example:
foreach($html->find('ul#nav') as $ul) {
foreach($ul->children() as $li) {
echo $li->children(0)->outertext . '<br/>';
}
}
Alternatively, you can use DOMDocument + DOMXpath for this too:
$dom = new DOMDocument();
$dom->loadHTML($str);
$xpath = new DOMXpath($dom);
// directly target those links
$links = $xpath->query('//ul[#id="nav"]/li/a');
foreach($links as $a) {
echo $a->nodeValue . '<br/>';
}
<?php
include 'simple_html_dom.php';
$html = file_get_html('input.php');
foreach ($html->find('ul[id=nav]') as $navUL){
foreach ($navUL->find('li') as $navUL_LI){
if(strpos($navUL_LI,'google')||strpos($navUL_LI,'google')){
echo $navUL_LI->find('a',0)->outertext."<br>";
}
}
}
?>
i have done the same work in Objective-c.
You can use the XML or HTML api's to serialize your html object.
If you want to do this form cold hand... find open tag and the close tag.
After this get first child, then the second and so on...
Try this:
// get the children of the element #nav, i.e. the top level lis
$lis = $html->getElementById("#nav")->childNodes();
// for each child, find the first 'a' element
foreach ($lis as $li) {
$a = $li->find('a',0);
// retrieve the link text itself.
echo "link text: " . $a->innertext() . "\n";
}
See the simple-html-dom manual for details of all these methods.
you can simply achieve that by:
<?php
foreach ($html->find('ul[id=nav]') as $navUL){
foreach ($navUL->find('li') as $navUL_LI){
echo $navUL_LI->find('a',-2)->outertext."<br>";
}
}
?>
<?php
$in = '<style> .catalog-product-view .product.attribute.overview ul { margin-top: 10px; } </style><img src="/media/wysiwyg/img/misc/made-in-the-usa-doh-blue4.png"><ul><li>Ships as (12) 40 fl oz bottles</li></ul>';
function parseTags($input, $callback) {
$len = strlen($input);
$stack = [];
$tag = "";
$data = "";
$isTag = false;
$isString = false;
for ($i=0; $i<$len; $i++) {
$char = $input[$i];
if ($char == '<') {
$isTag = true;
$tag .= $char;
} else if ($char == '>') {
$tag .= $char;
if (substr($tag, 0, 2) == '</') {
$close = str_replace('>', '', str_replace('</', '', explode(' ', $tag, 1)[0]));
$open = str_replace('>', '', str_replace('<', '', explode(' ', end($stack), 1)[0]));
if ($open == $close) {
$callback($tag, $data, $stack, $i, false);
array_pop($stack);
}
} else if (substr($tag, -2) == '/>') {
$callback($tag, $data, $stack, $i, false);
} else {
$callback($tag, $data, $stack, $i, true);
$stack[] = $tag;
}
$tag = "";
$data = "";
$isTag = false;
} else if ($char == '"' || $char == "'") {
if ($isString == false) {
$isString = $char;
} else if ($isString == $char && $input[$i-1] != '\\') {
$isString = false;
}
} else if ($isTag) {
$tag .= $char;
} else {
$data .= $char;
}
}
}
parseTags($in, function($tag, $data, $stack, $position, $isOpen) use (&$out) {
print_r(func_get_args());
});

How to shuffle an array and then slice it

I have a problem with shuffling and slicing array.
I have this code:
$maxDisplayItem = $this->_getFlexformConfig('max_item_to_display', 'product_setting');
$selectedProducts = array_slice($selectedProducts, 0, $maxDisplayItem);
foreach ($selectedProducts as $_id) {
shuffle($products);
foreach ($products as $_product) {
....
}
}
My code limiting the number of displayed item but didn't shuffle it at all.
When I change the order of actions:
shuffle($selectedProducts);
foreach ($selectedProducts as $_id) {
$maxDisplayItem = $this->_getFlexformConfig('max_item_to_display', 'product_setting');
$products = array_slice($products, 0, $maxDisplayItem);
foreach ($products as $_product) {
....
}
}
the code shuffling and slicing results but only first (e.g. 3 results) from whole array who has 50 items.
Could anyone help me with this?
here is the whole function:
function displayProductList()
{
// Store View
$store = $this->getStoreViewCode();
$selectedProducts = $this->getSelectedProducts();
$products = $this->_products->getProductsFromDb($selectedProducts, $store, $this->getProductsStoragePid());
// Load Template File
$templateHtml = $this->cObj->fileResource( $this->_getConfig('templateProductList') );
$productListHtml = $this->cObj->getSubpart($templateHtml, '###PRODUCT_LIST###');
$productHtml = $this->cObj->getSubpart($productListHtml, '###PRODUCT_ITEM###');
$subPartContent = ''; $item = 0; $items = count($products); $even = true; $line = '';
shuffle($selectedProducts);
foreach ($selectedProducts as $_id) {
$maxDisplayItem = $this->_getFlexformConfig('max_item_to_display', 'product_setting');
$products = array_slice($products, 0, $maxDisplayItem);
foreach ($products as $_product) {
if ($_id === $_product['product_id']) {
$markers = $this->_products->getProductMarkers($_product);
// Even/Odd CSS Class Determination
if ($even === true) {
$line = 'even';
$even = false;
} else {
$line = 'odd';
$even = true;
}
// Class Determination First/Last
if ($item == 0) {
$markers['###EVENODD###'] = $line . ' ' . 'first';
} else if ($item == $items-1) {
$markers['###EVENODD###'] = $line . ' ' . 'last';
} else {
$markers['###EVENODD###'] = $line;
}
// Check if the product has an image
$imageHtml = '<p>'.$this->pi_getLL('template_label_no_image_available').'</p>';
if ($markers['###DETAIL_IMAGE###'] != 'no_selection') {
$imageHtml = $this->cObj->getSubpart($productHtml, '###PRODUCT_IMAGE###');
}
$p = $this->cObj->substituteSubpart($productHtml, '###PRODUCT_IMAGE###', $imageHtml);
$subPartContent .= $this->cObj->substituteMarkerArray($p, $markers);
$item++;
}
}
}
return $this->cObj->substituteSubpart($productListHtml, '###PRODUCT_ITEM###', $subPartContent);
}
function displayProductList(){
// Store View
$store = $this->getStoreViewCode();
$selectedProducts = $this->getSelectedProducts();
$products = $this->_products->getProductsFromDb($selectedProducts, $store, $this->getProductsStoragePid());
// Load Template File
$templateHtml = $this->cObj->fileResource( $this->_getConfig('templateProductList') );
$productListHtml = $this->cObj->getSubpart($templateHtml, '###PRODUCT_LIST###');
$productHtml = $this->cObj->getSubpart($productListHtml, '###PRODUCT_ITEM###');
$subPartContent = ''; $item = 0; $items = count($products); $even = true; $line = '';
$maxDisplayItem = $this->_getFlexformConfig('max_item_to_display', 'product_setting');
$products = array_slice($products, 0, $maxDisplayItem);
shuffle($products);
foreach ($products as $_product) {
$markers = $this->_products->getProductMarkers($_product);
// Even/Odd CSS Class Determination
if ($even === true) {
$line = 'even';
$even = false;
} else {
$line = 'odd';
$even = true;
}
// Class Determination First/Last
if ($item == 0) {
$markers['###EVENODD###'] = $line . ' ' . 'first';
} else if ($item == $items-1) {
$markers['###EVENODD###'] = $line . ' ' . 'last';
} else {
$markers['###EVENODD###'] = $line;
}
// Check if the product has an image
$imageHtml = '<p>'.$this->pi_getLL('template_label_no_image_available').'</p>';
if ($markers['###DETAIL_IMAGE###'] != 'no_selection') {
$imageHtml = $this->cObj->getSubpart($productHtml, '###PRODUCT_IMAGE###');
}
$p = $this->cObj->substituteSubpart($productHtml, '###PRODUCT_IMAGE###', $imageHtml);
$subPartContent .= $this->cObj->substituteMarkerArray($p, $markers);
$item++;
}
return $this->cObj->substituteSubpart($productListHtml, '###PRODUCT_ITEM###', $subPartContent);
}

Code help, Unlimited php menu

Just got this code working for 2 levels of my menu. But I want it to work with unlimited levels.
Do any of you guys have an idea where to start?
Right now if I type in more levels in the database it says "Undefined index Linnk & Label Line 29", and the new parent that has been entered does not show up.
$sql = "SELECT id, label, link_url, parent_id FROM dyn_menu ORDER BY parent_id, id ASC";
$items = mysql_query($sql);
while ($obj = mysql_fetch_object($items)) {
if ($obj->parent_id == 0) {
$parent_menu[$obj->id]['label'] = $obj->label;
$parent_menu[$obj->id]['link'] = $obj->link_url;
} else {
$sub_menu[$obj->id]['parent'] = $obj->parent_id;
$sub_menu[$obj->id]['label'] = $obj->label;
$sub_menu[$obj->id]['link'] = $obj->link_url;
if (!isset($parent_menu[$obj->parent_id]['count'])) {
$parent_menu[$obj->parent_id]['count'] = 0;
}
$parent_menu[$obj->parent_id]['count']++;
}
}
mysql_free_result($items);
function dyn_menu($parent_array, $sub_array, $qs_val = "menu", $main_id = "nav", $sub_id = "subnav", $extra_style = "foldout") {
$menu = "<ul id=\"".$main_id."\">\n";
foreach ($parent_array as $pkey => $pval) {
if (!empty($pval['count'])) {
$menu .= " <li><a class=\"".$extra_style."\" href=\"".$pval['link']."?".$qs_val."=".$pkey."\">".$pval['label']."</a></li>\n";
} else {
$menu .= " <li>".$pval['label']."</li>\n";
}
if (!empty($_REQUEST[$qs_val])) {
$menu .= "<ul id=\"".$sub_id."\">\n";
foreach ($sub_array as $sval) {
if ($pkey == $_REQUEST[$qs_val] && $pkey == $sval['parent']) {
$menu .= "<li>".$sval['label']."</li>\n";
}
}
$menu .= "</ul>\n";
}
}
$menu .= "</ul>\n";
return $menu;
}
function rebuild_link($link, $parent_var, $parent_val) {
$link_parts = explode("?", $link);
$base_var = "?".$parent_var."=".$parent_val;
if (!empty($link_parts[1])) {
$link_parts[1] = str_replace("&", "##", $link_parts[1]);
$parts = explode("##", $link_parts[1]);
$newParts = array();
foreach ($parts as $val) {
$val_parts = explode("=", $val);
if ($val_parts[0] != $parent_var) {
array_push($newParts, $val);
}
}
if (count($newParts) != 0) {
$qs = "&".implode("&", $newParts);
}
return $link_parts[0].$base_var.$qs;
} else {
return $link_parts[0].$base_var;
}
}
echo dyn_menu($parent_menu, $sub_menu, "menu", "nav", "subnav");
In order to do this, you'll want to use a recursive function to handle your array -> list transformation.
Here's an example:
<?php
function arrToUl($arr) {
$out = '<ul>';
foreach($arr as $key=>$val) {
$out .= '<li>';
$out .= is_array($val) ? arrToUl($val) : $val;
$out .= '</li>';
}
return $out . '</ul>';
}
$arr = array('firstval','secondval',array('firstval2','secondval2',array('firstval3','secondval3',array('firstval4','secondval4',array('firstval5','secondval5')))));
echo arrToUl($arr);
Output in a jsfiddle.

Categories