Zen Cart Logo
Forums / General Questions / including jscript*.php file in category description

including jscript*.php file in category description

Locked

Views: 2,417

Results 1 to 15 of 15
This thread is locked. New replies are disabled.
8 Mar 2008, 2:18 PM
#1
shocker avatar

shocker

Zen Follower

Join Date:
Jul 2007
Location:
Jakarta
Posts:
346
Plugin Contributions:
0

including jscript*.php file in category description

I'm trying to optimize my site. :blush:

One way of doing this is to move *.js scripts that don't need to be global to specific pages.

I have a jscript_wimpy.js (a video player) that I want to convert to jscript_wimpay.php.

I used the js file and copied it adding <script> at the beginning and </script> at the end.

I then put

<?php include "includes/templates/{my_template_directory}/jscript/jscript_wimpy.php"; ?>

in my html of the category description.

This must not be the correct technique because it doesn't work.:unsure:

9 Mar 2008, 1:26 AM
#3
shocker avatar

shocker

Zen Follower

Join Date:
Jul 2007
Location:
Jakarta
Posts:
346
Plugin Contributions:
0

Re: including jscript*.php file in category description

DrByte:

You're making it more complicated than it needs to be.

http://www.zen-cart.com/wiki/index.php/Customisation_-_Templates_-_Javascript
Thanks Dr. Byte, for trying to help.

But what I did was gleemed from the same intructions gaven in the link.

Where am I going wrong? I only want this particular js to load for the particular category.

9 Mar 2008, 1:31 AM
#4
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
177

Re: including jscript*.php file in category description

shocker:

I have a jscript_wimpy.js (a video player) that I want to convert to jscript_wimpay.php.

I used the js file and copied it adding <script> at the beginning and </script> at the end.

I then put

<?php include "includes/templates/{my_template_directory}/jscript/jscript_wimpy.php"; ?>
> 
> in my html of the category description.

1. If you've put a jscript_xxxx.php or jscript_xxxx.js file in the /includes/templates/{your_template}/jscript folder, then it's going to load on every page automatically.
So, adding it to your category description serves no useful purpose.

2. Adding PHP code (ie: <?php include ...... ?> ) to the html of a category description will do nothing, since Zen Cart doesn't process embedded PHP instructions contained in category descriptions.

3. If you only want the script to run when looking at category listings, stick the jscript_xxxxx.js file in your /includes/modules/pages/index/ folder.  It'll load for all categories, but not for other pages such as checkout or shopping-cart, etc.

4. An alternative to #3 above would be to use a jscript_xxxx.php file (instead of .js) and wrap some PHP code (perhaps an IF statement) around the usual contents, which checks to see whether a certain cPath has been specified, and only send the script to the browser if the certain category (cPath) is selected.
9 Mar 2008, 2:03 AM
#5
shocker avatar

shocker

Zen Follower

Join Date:
Jul 2007
Location:
Jakarta
Posts:
346
Plugin Contributions:
0

Re: including jscript*.php file in category description

DrByte:

  1. An alternative to #3 above would be to use a jscript_xxxx.php file (instead of .js) and wrap some PHP code around the usual contents which check to see whether a certain cPath has been specified, and only send the script to the browser if the certain category (cPath) is selected.
    For now #4 is what I want to do.
    I guess I can conditionally add the include statement if the cPath is the one I want. I currently looking at tpl_main_page.php. Where to do suggest I implement this condition check?

Eventually what I want is an EZ-page that has certain instructional videos on it. I only want the js code loaded for this page. Can I put the jscript_xxxx.php include in the text html of the EZ-page? this just seems easier.

9 Mar 2008, 2:22 AM
#6
shocker avatar

shocker

Zen Follower

Join Date:
Jul 2007
Location:
Jakarta
Posts:
346
Plugin Contributions:
0

Re: including jscript*.php file in category description

I'm planning on adding the code below to tpl_main_page.php. Will SEO rewrites prevent this from working? do I have to add code for this to. I'm new to SEO so I'm not very clear on how it works and what it does in relation to what ZenCart standard does.

if ($current_page_base == 'index' and $cPath == '2') {
    echo "include/..../jscript_XXXX.php";
    }

Where should the jscript_XXXX.php be placed in order to not interfere with anything and not load other than where I want?

9 Mar 2008, 2:25 AM
#7
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
177

Re: including jscript*.php file in category description

If you go back and read the wiki article, you'll see that if you put the jscript_xxxx.php or .js file in the correct folder, it will automatically load for you.
You DO NOT need to "include" it anywhere at all.

That's why I said to put it into the /includes/modules/pages/index/ folder.

9 Mar 2008, 2:28 AM
#8
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
177

Re: including jscript*.php file in category description

As to customizing it to load just for a certain category, here's what I meant in my #4 suggestion earlier:

/includes/modules/pages/index/jscript_mywimpyplayerscript.php

<?php
if ($cPath== '3_10') {
?>
<script type="text/javascript" language="javascript">

....copy and paste wimpy.js code here


</script>
<?php } ?>
```Change '3_10' to whatever the right cPath is for the one category you're limiting it to.

That's it. Put the file in the place specified.  Leave your descriptions alone. No other coding needed.
9 Mar 2008, 2:29 AM
#9
shocker avatar

shocker

Zen Follower

Join Date:
Jul 2007
Location:
Jakarta
Posts:
346
Plugin Contributions:
0

Re: including jscript*.php file in category description

DrByte:

If you go back and read the wiki article, you'll see that if you put the jscript_xxxx.php or .js file in the correct folder, it will automatically load for you.
You DO NOT need to "include" it anywhere at all.

That's why I said to put it into the /includes/modules/pages/index/ folder.
Does this apply to #4 in your previous post #4? If so, I dont' know how to "send the script to the browser" other that my attempt in post #6

9 Mar 2008, 2:44 AM
#10
shocker avatar

shocker

Zen Follower

Join Date:
Jul 2007
Location:
Jakarta
Posts:
346
Plugin Contributions:
0

Re: including jscript*.php file in category description

Please forgive me for belaboring this point. :blush:

I got it. Leave description alone.

Everything is clear except...
my question is if I only want the code loaded for one page (category description) why do I need to put the file in the index/ folder also?

If I'm pasting js code directly into the tpl_main_page.php, do I still need the file at all?

Thanks for your time

9 Mar 2008, 2:46 AM
#11
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
177

Re: including jscript*.php file in category description

shocker:

If I'm pasting js code directly into the tpl_main_page.php, do I still need the file at all?If you don't want to use the built-in abilities to load the code for just a certain page, that's fine. Go ahead and paste it into your tpl_main_page.php file.

What I gave you is how the built-in infrastructure can be used to auto-handle things.

You only need one approach or the other. Not both.

9 Mar 2008, 3:17 AM
#12
shocker avatar

shocker

Zen Follower

Join Date:
Jul 2007
Location:
Jakarta
Posts:
346
Plugin Contributions:
0

Re: including jscript*.php file in category description

Got it! Beautiful! :clap:

Thanks for your time a patience.

24 Sep 2009, 7:32 AM
#13
city_directory avatar

city_directory

New Zenner

Join Date:
Sep 2009
Posts:
4
Plugin Contributions:
0

Re: including jscript*.php file in category description

I've successfully implemented the .php page with the .js inserted. But, I want to go further than have the code work on one page identified in the .php. I want to have all the pages in one category use one script, another category with all of its pages use another script, etc.
I'm using:

<?php if ($cPath== '66') { ?>

...and then the remaining .js to complete the script.

I want to add the additional pages in the '66' cPath, but I can't figure out the correct php code to allow additional pages. Is this possible? Or will I have to add the "else" statement for each page that will use the same code?
Isn't there a way to include a statement for all of the pages in a specific category?
http://www.peninsulaoutfitters.com/store/index.php?main_page=index&cPath=66 will take you to the main category page. I want to have all of the pages in that category use the same script. Other categories will use a different script in order to display different images in the header.

Am I making sense?
Any thoughts? Suggestions?

24 Sep 2009, 4:00 PM
#14
city_directory avatar

city_directory

New Zenner

Join Date:
Sep 2009
Posts:
4
Plugin Contributions:
0

Re: including jscript*.php file in category description

I've had limited success using:
if ($cPath >= 66 and $cPath < 75)
That allows all of the categories and sub-categories in the above scenario to work, but still doesn't address individual product_id.
I've attempted using $product_id >= 124 (or whatever id #), but that doesn't seem to work.
I've tried duplicating the code so that the below the category named ($cPath....) is ($product_id...) to include the product_id's for those contained in the category, but that didn't work either.

The method works great for categories. Now, how can I incorporate each desired product_id to use the same script?
In case you visit the site, bear in mind that each page has a call to a .js that was used site-wide so that all pages use the same images. So whatever images are viewed are not necessarily those desired for the specific page.
In the general instructions to include "per page .js", there is no "page name" - only a product_id. So that method won't work either...at least not as I understand the method.

24 Sep 2009, 6:36 PM
#15
city_directory avatar

city_directory

New Zenner

Join Date:
Sep 2009
Posts:
4
Plugin Contributions:
0

Re: including jscript*.php file in category description

I've succeeded in doing exactly what I needed. I had to read tons of threads in order to, eventually, figure out that what I needed was a combination of 2 threads. Following is the result of this thread and (finally finding) the one other I needed.

Each category in the site has different images displayed at the top of the page (as per the client's desire). Each product_id for the specific category displays the same images as the parent category. The JavaScript displays a random order of unlimited images each time the page is refreshed or when moving to another page.

To make it work, all js is in 2 php files. The 2 php files making it work are:
/includes/modules/pages/index/jscript_index.php (optional name jscript_whatever.php)
/includes/modules/pages/product_info/jscript_main.php (it "must" be this php page)

Both files must have "matching" code:

<?php if ($cPath >= 66 and $cPath <= 126) { ?> <script type="text/javascript" language="javascript"> ...your javascript goes here.... </script> <?php } ?>

The $cPath (in this example) points to one specific category that has multiple sub-categories and products. The statement says if the category is equal to or greater than 66, and is equal to or less than 126, then display the following code.
The exact same/matching script is placed in the jscript_main.php page.
jscript_whatever.php controls categories and sub-categories.
jscript_main.php controls all the product_id pages.
Each of the "calls" or declarations (snippet above) contain the specific category number (i.e. ($cPath== 75) for a single category).

This particular site/project has 7 top categories. Each category must have the (above) code addressing that category number. So, both php files have 7 "calls", or copies of the code (above) with the category number changed and the JavaScript inside the code as you need.
If you place the code in the files as described and only include some of your categories, "all" of the categories will be effected. Declarations must be included to address each category and/or product_id. It's a "once you start you have to finish" operation.

This may have been simple for a php programmer, but for those of us who are doing something similar and don't code php very well (or at all), this thread should shed some light on current or future projects.