X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Changing design (https://forum.x-cart.com/forumdisplay.php?f=51)
-   -   Extra Field Search From product.tpl (https://forum.x-cart.com/showthread.php?t=48320)

mrerotic 06-26-2009 09:23 PM

Extra Field Search From product.tpl
 
Ok I have an extra field called 'Stars' the value of this field example is: "mark gregory, shanna more, debra smorton". I'm wondering how can I use smarty or whatever to give each name a seperate link and their value to perform a search for each one? I know how to do this in php, just not in smarty. Any help would be great.

gb2world 06-28-2009 12:19 PM

Re: Extra Field Search From product.tpl
 
Does this help as far as the extra fields code?

To create search links - try this

mrerotic 06-28-2009 12:40 PM

Re: Extra Field Search From product.tpl
 
Thanks it does cover alot of info that I already read, but does'nt touch how to make each result in an extra field that is comma seperated have its own search link. Any ideas?

gb2world 06-28-2009 12:49 PM

Re: Extra Field Search From product.tpl
 
I would probably want to do that in the php file with "explode". Is there a reason why you want to do it in smarty in the template? I've not done this - but here is what I found from google that may help.

mrerotic 06-30-2009 08:10 PM

Re: Extra Field Search From product.tpl
 
I hate to act like I need a babysitter, I know php. I would prefer to do it in php, but I don't know how to make a smarty template pull the php file. Or where to even include that php file for this. If you dont mind or have time to explain I would appreciate it. I do appreciate that link as well.

Sorry it took me soo long to reply. I never received an email that you responded.

gb2world 07-01-2009 12:24 PM

Re: Extra Field Search From product.tpl
 
Assuming you want to do this on the product page, try something like this:

The smarty of extra fields are assigned in this file: /modules/Extra_Fields/extra_fields.php

There is already a loop at the bottom of that file that assigns the extra_fields array to the smarty variables. You can add your explode inside that loop, or you can create another one - depending on the conditions you want to do this, or you can add a new loop if you want it always to read the extra field.

There are some print/echo statements in the code to help you debug

Code:

if (!empty($extra_fields)) {
        foreach ($extra_fields as $ef_k=>$ef_v) {
          if ($ef_v['field'] == "stars") {
  echo "explode stars: ";
  $explode_stars = explode(',', $ef_v['field_value']);
  print_r($explode_stars);

}
          $smarty->assign("explode_stars", $explode_stars);
 }



Now - the array should be available to your product.tpl. If you use this tip - you should see the $explode_stars array, and the values in the array as you expect.

Now - in your product template - you need to cycle through the extra fields array and then the $explode_stars array, and set the links that you want based on the values. If you are already looping through extra fields, you can put this inside that loop instead of creating another.

Code:

{section name=field loop=$extra_fields}
{if $extra_fields[field].field eq "stars" && $extra_fields[field].field_value ne ""}
  <p>{$extra_fields[field].field_value}</p>
<p>exploded: <br />
{section name=star_name loop=$explode_stars}
 {$smarty.section.star_name.index} : {$explode_stars[star_name]} <br />
{/section}
</p>
{/if}
{/section}


Use the smarty variable console while you are debugging to see the variables and what their values (In case I have a code error - you should be able to fix it)

mrerotic 07-02-2009 10:30 AM

Re: Extra Field Search From product.tpl
 
Awesome thank you. One last thing though. How can I keep each comma for each star so each star is still on one line rather than seperated by a "<br>" so basically I want each star broken down seperately so I can add a <a href> to them, but keep them all on one line seperated by a comma?

Thanks a million for your help. I'm almost there.

gb2world 07-02-2009 02:20 PM

Re: Extra Field Search From product.tpl
 
You can do what ever you want with html/css around the smarty variables. The slight complication is that you don't want a comma after your last star, so you need an if to only put the comma before the star if it is not the first star. Something like:

{if $smarty.section.star_name.index ne 0}, {/if}<a href="search.php?mode=search&by_title=on&by_shortd escr=on&by_fulldescr=on&by_sku=on&category_main=on &category_extra=on&search_in_subcategories=on&subs tring={$explode_stars[star_name]}">{$explode_stars[star_name]}</a>

mrerotic 07-02-2009 05:52 PM

Re: Extra Field Search From product.tpl
 
Ok this works, but it does show a , for the last one. Any ideas? Also you might know so I'm gonna ask. If I'm using clean urls how can I know what category id I'm in for each category? I have a search by price mod I modified to work by using (if ($_GET['cat']) { show this } else { do that }) but now that clean urls are on im not able to use the $_GET in php for the catid. THANKS MAN :)

gb2world 07-02-2009 09:53 PM

Re: Extra Field Search From product.tpl
 
Are you sure you used the code as I wrote it? I'll have to make a test case and try it when I have time, because looking at it, I can't see how you would have a comma after the last array element. If you want to debug it, just try printing out the variable I am using in the if statement: {$smarty.section.star_name.index} - it should only be equal to 0 the first time through the loop, then increment. And, I intend for it to print every time through the loop except the first time.

If the list does not begin with a comma, then it should have worked. The only reason I can think of where a comma would end the list is if there was a blank element at the end of the array. When you entered the extra field, did you end the list with a comma? If you want to test for stuff like that - I would do it in the php and pass the array to smarty after it is cleaned up.

You can always see the category id in the database. An easier way might be to bring up the category in the administration. There are no clean urls in the admin, so you can look at the URL there and see the cat id.

One other issue I just thought of - your star names have a blank space in between the first and last name - so the link I gave you won't work as the search URL. You need to replace the blank with a "+" - this should do it:

Code:

{if $smarty.section.star_name.index ne 0}, {/if}<a href="search.php?mode=search&by_title=on&by_shortdescr=on&by_fulldescr=on&by_sku=on&category_main=on &category_extra=on&search_in_subcategories=on&substring={$explode_stars[star_name]}">{$explode_stars[star_name]|replace:' ':'+'}</a>

mrerotic 07-03-2009 04:47 PM

Re: Extra Field Search From product.tpl
 
Thanks man the problem is that there is a blank one being created after the last one. So if there are actually 3 stars its showing a 4th that is blank.

mrerotic 07-03-2009 05:32 PM

Re: Extra Field Search From product.tpl
 
Also is there anyway to make this actually just search for the exact stars in the extra fields for stars?

gb2world 07-03-2009 05:54 PM

Re: Extra Field Search From product.tpl
 
That is odd. I'm not sure why there would be an extra array element like that. You should see it using the print_r and echo statements in the original php code I gave you. You can always fall back on your php knowledge and add a test for that in the php when creating the $explode_stars array. I would do it in the php if you expect you might have to do more programming for other cases later. You can look at example 2 here to see how to filter the array in php for blanks.

Or. just add another condition in the smarty that won't let anything print if the value is empty - maybe this will work:

Code:

{if $explode_stars[star_name] ne ""}
{if $smarty.section.star_name.index ne 0 }, {/if}<a href="search.php?mode=search&by_title=on&by_shortdescr=on&by_fulldescr=on&by_sku=on&category_main=on &category_extra=on&search_in_subcategories=on&substring={$explode_stars[star_name]}">{$explode_stars[star_name]|replace:' ':'+'}</a>
{/if}


If you can do the search you want on your advanced search page - just use the tip I gave you earlier to get the URL for the link. In advanced search, you can set up the criteria over which you want to search, and it will provide the URL.

mrerotic 07-03-2009 07:31 PM

Re: Extra Field Search From product.tpl
 
awesome thanks man, but in regards to the search now, im trying to search only the extra field "Stars". And I can't select that in my advanced search. Any ideas?

mrerotic 07-03-2009 07:46 PM

Re: Extra Field Search From product.tpl
 
Nevermind got it.. I didnt realize you can turn on extra field searches from the admin area. THX for all your help man.. AWESOME SUPPORT. THX A MILLION

gb2world 07-03-2009 08:05 PM

Re: Extra Field Search From product.tpl
 
you are welcome

mrerotic 07-04-2009 12:08 AM

Re: Extra Field Search From product.tpl
 
gb2world - one last questions totally different. If I want to change how each page title is displayed, I know you can choose from two options in the admin area, but what if I want to make my own custom one for each page? Is there a simple fix for this? Or any ideas?

gb2world 07-04-2009 04:19 AM

Re: Extra Field Search From product.tpl
 
You can search the forum to find answers.
First, make sure you use this tip
Next, select the advanced search under the search in the top menu
next enter:
page AND title AND modify
in the search box
narrow your search by selecting
Storefront Design & Template Editing in 4.2.x
and select Search Now

mrerotic 07-21-2009 11:05 AM

Re: Extra Field Search From product.tpl
 
Thanks gb2world any ideas on how to show all categories that a product is listed in? Same as we did here for the stars, but now I want to show each category.

Thanks in advance :)

gb2world 07-21-2009 01:36 PM

Re: Extra Field Search From product.tpl
 
I doubt that the category information is available to the product page in the default installation. You can use the tip I told you about in post #6 to see the smarty variables that are available to you. If it is there, you can update the template to display the categories.

If the categories are not there - you are going to have to write your own query to get the information and add that to the product.php file. I think you need to query the _products_categories table and find all categories for the productid.

If you understand the code in this thread (the sql query and the php), the concept is similar to what you want to do. You should be able to add an array with the categories to the $products array.

mrerotic 07-21-2009 03:33 PM

Re: Extra Field Search From product.tpl
 
ok the following is what I added to product.php
PHP Code:

// TEST ADDITION //
$product_cat func_query("SELECT category FROM $sql_tbl[categories] WHERE categoryid IN (SELECT categoryid FROM $sql_tbl[products_categories] WHERE productid ='$productid')"); 
if (!empty(
$product_cat)) {
foreach (
$product_cat as $c => $d) {
$explode_cats[] = $d['category'];
}
 
$smarty->assign("explode_cats"$explode_cats);
}
// END TEST ADDITION // 


And in product.tpl I added:
------------------------------
<div>
{foreach from=$explode_cats item=c}
<a href="search.php?mode=search&including=any&categor yid=1" title="{$c}">{$c}</a> |
{/foreach}
</div>

How can I pass the category id's to product.tpl in above so each href is correct for each categoryid? I have the categories passing correctly, I just can't figure out how to get the id's.

Thanks :)

mrerotic 07-21-2009 04:54 PM

Re: Extra Field Search From product.tpl
 
OK I got this to work:

In product.php I have:
PHP Code:

# Start show categories for each product (ADD-ON)
$product_cat func_query("SELECT category, categoryid FROM $sql_tbl[categories] WHERE categoryid IN (SELECT categoryid FROM $sql_tbl[products_categories] WHERE productid ='$productid')");  
if (!empty(
$product_cat)) {
 foreach (
$product_cat as $c => $d) {
  
$explode_cats[] = $d;
 }
 
 
$smarty->assign("explode_cats"$explode_cats);
}
# end add to show categories for each product (ADD-ON) 


Then in product.tpl I have the following:
---------------------------------------
<div>
{foreach from=$explode_cats item=c}
{assign var=Pcatid value=$c.categoryid}
{assign var=Pcatname value=$c.category}
<a href="search.php?mode=search&including=any&categor yid={$Pcatid}" title="{$Pcatname}">{$Pcatname}</a> |
{/foreach}
</div>

I HOPE THIS HELPS SOMEONE...

THANKS FOR ALL THE HELP :)

flamers 04-21-2010 02:44 AM

Re: Extra Field Search From product.tpl
 
Hi mrerotic, I also want to explode extra field items (a performer list, exactly the same as your initial query on this thread) but I am having a little difficulty following the code changes needed. Could you possibly post the coding changes that you carried out to make a hyperlink for each performer name?

Thanks in advance.


All times are GMT -8. The time now is 04:55 AM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.