Need PHP Mysql Suggested Categories name from Title posted - php

<?php
$colname_subselect = "-1";
if (isset($_POST['title'])) {
$colname_subselect = $_POST['title'];
}
mysql_select_db($database_donedeal, $donedeal);
$query_subselect = sprintf("SELECT * FROM subcategories WHERE subcatename LIKE %s");
?>
I'm trying to show suggested categories from the title which is posted from previous page to categories.php page, example, In previous Page Field Name Title Contain Text :
## I Want My Motor Car To Sell##
Once submit it will go to categories.php which query i written above. Using LIKE function i able to get suggested categories when i write only one keyword like i written car in title field of previous page so in categories.php page suggested categories will show which contain car keyword.
No what i want is that when i posted complete ad post title like
## I Want My Motor Car To Sell##
how can i show the suggested categories like above title contain motor and car keyword, so it should show in categories.php categories contain car and motor keyword ? Got ME ?
I m using innodb so MATCH method will not work, any other prefer method would be much appreciated please...

$colname_subselect = "-1";
if (isset($_POST['title'])) {
$colname_subselect = $_POST['title'];
}
mysql_select_db($database_donedeal, $donedeal);
$queryString = trim(implode('or subcatename like ',explode(' ',$colname_subselect )), 'or');
$query_subselect = sprintf("SELECT * FROM subcategories WHERE " . $queryString );
Please Note that you might have to escape string for stopping sql injection.

Related

Prestashop, Get other products with same ean13 in product page

I'm newbie to smarty and php.
My situation is that my product_reference is unique but product_ean13 is similar in some products which are like each other. What I need is to have product_id of those products in one of them product page. I mean when visitors open a product page I want to show Image of those products which have the same ean13. Showing the images and HTML, CSS is ok for me, my problem is in PHP,SMARTY which should pass the value from PHP file to TPL file.
I guess I should write a function in Product.php file and pass array values to product.tpl file. But I couldn't.
Would you please help me?
Edited: As you may know a weakness in Prestashop 1.6 is that if your products nature is to have colors and size, like clothes and smartphones! you have two approach to create them. first approach is to create them as a combination of one product and second approach is to create them as separated and not related products. first approach has a good point that all of them will be showed up in product page when customer visits each of them and also have a weakness of exposure in category page which all those attributes will be seen as one product.(imagine between all colors of one shirt only one is seen and your costumer may like blue more but always the red is being showed in category page. or its not easy to know from category page that you also have the gold color of the smartphone) this solution will helps you what to create your products as separated products but you what to show them in product page of each of them. this way we will use benefit of both approach and we wont have the weakness of any of them. we use ean13 (or any other unused field you have, to use as a code that is a same value in same products)
I you have created the function in product.php, you should use it in ProductContronller.php and here you can assign a value that contains the list of your products wich has the same ean13.
we should add a class in product.php to get cover images of products with the same ean13:
public static function getImageByEan13Product($ean13_colors)
{
$rows = Db::getInstance()->executeS('
SELECT `id_product`, `id_image`
FROM `'._DB_PREFIX_.'image`
WHERE `id_product` IN ('.implode(',', $ean13_colors).') AND `cover` = 1'
);
$images = array();
foreach ($rows as $row){
$images[] = array(
'idProduct' => $row['id_product'],
'idImage' => $row['id_image']
);
}
return $images;
}
and the following function which gets the color attributes of the products which contain same ean13 code:
public static function getColorByEan13Product($ean13)
{
$rows = Db::getInstance()->executeS('
SELECT `id_product`
FROM `'._DB_PREFIX_.'product`
WHERE `ean13` = '.$ean13.' AND `active` = 1 AND `id_product` IN (SELECT DISTINCT `id_product` FROM `'._DB_PREFIX_.'stock_available` WHERE `quantity` > 0)');
$colors = array();
foreach ($rows as $row){
$colors[] = $row['id_product'];
}
return $colors;
}
and also there is another class in the ProductController.php named assignAttributesGroups() which provide attributes of the product. in this class we should add following code:
$ean13_colors = array();
$ean13_colors = Product::getColorByEan13Product($this->product->ean13);
$this->context->smarty->assign('ean13colors', $ean13_colors);
$ean13_images = array();
$ean13_images = Product::getImageByEan13Product($ean13_colors);
$this->context->smarty->assign('ean13images', $ean13_images);
then these two values ean13_images and ean13_coloes can be used in TPL file product.tpl and showed up instead of color attributes. like the code bellow:
{foreach from=$ean13images key=k item=v}
{if $product->id != $v.idProduct}
<li>
<a href="{$link->getProductLink($v.idProduct)|escape:'html':'UTF-8'}" class="pro_column_left selected">
<img src="{$link->getImageLink($product->link_rewrite, $v.idImage, 'small_default')}" height="{$smallSize.height}" width="{$smallSize.width}" class="replace-2x img-responsive" {if !isset($from_product_secondary_column) || !$from_product_secondary_column} itemprop="image"{/if} />
</a>
</li>
{/if}
{/foreach}
and the value ean13colors can be used where we want to show a balloon tooltip on mouseover the image to say whats the color attribute of the other product that is being showed by same ean13.

PHP Group arrays by parameters on filtered query

I got three classes :
class libro {
var $id;
var $name;
var $editorial;
var $materia = array();
[... getters and setters ]
class editorial {
var $name;
var $id;
[...]
class materia {
var $name;
var $id;
[...]
This is working on a website catalogue, where I got an amount of books I get from an xml file. That works well, I checked it; i receive xml values good.
On my website's catalogue, I got a aside bar. By default, are shown all editorials and materias (categories), as well as all books.
So, when I click on a checkbox of one editorial/materia, page reloads with a new query from xml, where one of them are filtered. Books in catalogue are shown, but on the aside, I need to show only the element I checked in, and the other are grouped researching in the new xml query data.
For example, If I select editorial Great Books, page will reload showing only that one editorial on the checkbox, and on aside's section about Materias I will see all that are contained on the array(materia) inside every book, grouped by to not show repeated categories.
The other way of the example :
If I search by a Materia first, editorials on new query will be grouped by too. If I got Editorial selected and I click a category, only category and editorial selected will be shown, and upside down If I selected first the category.
I'm working with $_GET['editorial'] and $_GET['materia'] to work on new query data.
This is my code about grouping...
$bookList = array();
// Here are methods where I set xml data to classes, and I get an array (link below)
$groupedBookList = array();
foreach ($bookList as $book){
// ERROR IN LINE BELOW (Undefined offset: 0 in ...)
$groupBL_Element = &$groupedBookList[$book->get_editorial()->get_id() . "_" .$libro->get_materia(0)->get_id() ];
$groupBL_Element['editorial'] = $book->get_editorial()->get_id();
$groupBL_Element['materia'] = $book->get_materia(0)->get_id();
}
return array_values($groupedBookList);
goo.gl/ 397Wuz
Someone knows some way to group this? Thanks
The error I get is :
Undefined offset: 0 in (line selected by mine up)
I'm not sure if this is which you are looking for, but if you add to your foreach:
foreach ($bookList as $book){
$groupBL_Element = &$groupedBookList[$book->get_editorial()->get_id() . "_" .$libro->get_materia(0)->get_id() ];
$groupBL_Element['editorial'] = $book->get_editorial()->get_id();
$groupBL_Element['materia'] = $book->get_materia(0)->get_id();
$groupedBookList[] = $groupBL_Element;
}
Then you can return $groupedBookList with our your elements grouped.

Joomla get content by ajax

How to get joomla content using ajax? (I want to show content of specyfic page in popup), this is my code: (called by ajax)
$option = JRequest::getCmd('option');
$view = JRequest::getCmd('view');
if ($option=="com_content" && $view=="article") {
$ids = explode(':',JRequest::getString('id'));
$article_id = $ids[0];
$article =& JTable::getInstance("content");
$article->load($article_id);
echo '<h2>'.$article->get("title").'</h2>';
echo $article->get("introtext"); // and/or fulltext
}
This works fine only for artilces, but the problem is when for example I want to show category, or component
Please see your if condition it checks if option is equal to com_content & view is equal to article only. If view contains category it won't work. So add the conditions in if statement so that your code gets executed.
for category you need to add view=category & like for other components as well.

Make SEO sensitive URL (avoid id) Zend framework

i have url like this :
http://quickstart.local/public/category1/product2
and in url (category1/product2) numbers are id , categorys and products fetched from database attention to the id
id is unique
i need to the sensitive url like zend framework url. for example :http://stackoverflow.com/questions/621380/seo-url-structure
how i can convert that url to the new url like this
is there any way?!!
You'll need to store a unique value in your database with a field name such as 'url' or something similar. Every time you generate a new product you will have to create this unique url and store it with the product information. A common way to do this is to take the name of the product and make it url friendly:
public function generateUrl($name)
{
$alias = str_replace(' ', '-', strtolower(trim($name)));
return preg_replace('/[^A-Za-z0-9-]/', '', $alias);
}
Calling this method:
$url = $this->generateUrl("My amazing product!");
echo $url;
will output:
my-amazing-product
You'll need to check that the output from this function does not already exist in the database as you will use this value to query on instead of the id.
If you apply this logic to the categories as well, you can have easily readable and descriptive urls like the one below. You may need to tweak your routing before this works correctly though.
http://quickstart.local/public/awesome-stuff/my-amazing-product
You could use ZF's Zend_Controller_Router_Route. For example, to make similar url to those used by SO, one could define a custom route in an application.ini as follows (assuming you have controller and action called questions and show respectively):
resources.router.routes.questions.route = '/questions/:id/:title'
resources.router.routes.questions.type = "Zend_Controller_Router_Route"
resources.router.routes.questions.defaults.module = default
resources.router.routes.questions.defaults.controller = questions
resources.router.routes.questions.defaults.action = show
resources.router.routes.questions.defaults.id =
resources.router.routes.questions.defaults.title =
resources.router.routes.questions.reqs.id = "\d+"
Having such a route, in your views you could generate an url as follows:
<?php echo $this->url(array('id'=>621380,'title' => 'seo url structure'),'questions');
// results in: /myapp/public/questions/621380/seo+url+structure
//OR if you really want to have dashes in your title:
<?php echo $this->url(array('id'=>621380,'title' => preg_replace('/\s+/','-','seo url structure'),'questions');
// results in: /myapp/public/questions/621380/seo-url-structure
Note that /myapp/public/ is in the url generated because I don't have virtual hosts setup on my localhost nor any modifications of .htaccess made. Also note that you don't need to have unique :title, because your real id is in :id variable.
As a side note, if you wanted to make it slightly more user friendly, it would be better to have your url as /question/621380/see-url-structure rather than /questions/621380/see-url-structure. This is because under this url you would have only one question, not many questions. This could be simply done by changing the route to the following resources.router.routes.questions.route = '/question/:id/:title'.
EDIT:
And what to do with categories and products that you have in your question? So, I would define a custom route, but this time using Zend_Controller_Router_Route_Regex:
resources.router.routes.questions.route = '/questions/(\d+)-(d+)/(\w*)'
resources.router.routes.questions.type = "Zend_Controller_Router_Route_Regex"
resources.router.routes.questions.defaults.module = default
resources.router.routes.questions.defaults.controller = questions
resources.router.routes.questions.defaults.action = show
resources.router.routes.questions.map.1 = category
resources.router.routes.questions.map.2 = product
resources.router.routes.questions.map.3 = title
resources.router.routes.questions.reverse = "questions/%d-%d/%s"
The url for this route would be then generated:
<?php echo $this->url(array('category' => 6213,'product' => 80,'title' => preg_replace('/\s+/', '-', 'seo url structure')),'questions' ); ?>
// results in: /myapp/public/questions/6213-80/seo-url-structure
Hope this will help or at least point you in the right direction.

Joomla : how to get the url of a specific Menu itemID?

Friends a newbie question.........I need help in getting the URL of a specific Menu itemID. The situation is like this:
I am running Joomla and asking for a user to input for a menu ID and choose a layout for that menu ID.
I want to do something else with this URL of the Menu itemID.
How can I get the URL of this Menu itemID provided by the user?
For Example if the user input is liek $this->get ('menulayoutid'>; and he inputs and ID of 54 then how do I get the URL for Menu ID 54.
Please note: I want to get this URL from within my PHP file and not in the browser so that I can use the value of that URL for some other purpose.
Kindly help.
$itemid = JRequest::getVar('Itemid');
$application = JFactory::getApplication();
$menu = $application->getMenu();
$item = $menu->getItem($itemid);
$link = new JURI($item->link);
$link->setVar('ItemId', $itemid);
Source: http://forum.joomla.org/viewtopic.php?p=1836005
However, we get the Itemid from anywhere (user input, from our own developed module using the "menu item" field type in the xml file as described in the Joomla Docs - Standard form field types)
// get the menuItemId from wherever...
// as described above or as in other posts here and do whatever with that!
$menuItemId = 'fromWherever'; // as an example "107";
// build the link to the menuItemId is just easy and simple
$url = JRoute::_('index.php?Itemid=' . $menuItemId);
i think if we need only a link to a specific menu id, this is the best solution, because we have absolutely less requests and a clean code
this works also in Joomla 3.0, 3.1
I just want to add that if you need to target a specific menu you pass the menu name as an argument to getMenu().
$itemid = JRequest::getVar('Itemid');
$application = JFactory::getApplication();
$menu = $application->getMenu( 'menu-name' );
$item = $menu->getItem($itemid);
$link = new JURI($item->link);
$link->setVar('ItemId', $itemid);
I'm not sure if Joomla changed the way this works since 2.5 or even 1.7 but I spent the worse half of 2 hours looking for this.
Hopefully it helps someone.
$menuID = $params->get('menuItem'); // from module field menu ex. '105'
$js = new JSite;
$menu = $js->getMenu();
$link = $menu->getItem($menuID)->route;
//Returns URL Friendly Link -> menu/article
//Then format it ->
$link = 'http://www.yoursite.com/index.php/'.$link;
echo 'Borrowed Menu Link Path";
When you need to get your active menu item ID in Joomla to display some specific content for only that menu item or just to show the ID of the menu item, insert the following code where you wish to display the active menu item ID:
<?php
$currentMenuId = JSite::getMenu()->getActive()->id;
echo $currentMenuId;
?>

Categories