After I created the mod above, I noticed that my site was running slower and I suspected it was because the code above was looping through the products and doing a separate query for each product options and then lopping through the products again and doing a separate query for each product javascript options.
I rewrote the script so that it only does two queries, one to get all the options, and one to get all the javascript. I then loop through the products and and options and javascripts to associate the proper options and javascript with each product without doing any more MySQL queries.
In the end, I'm not sure if this way is faster since I'm now forced to loop through all the options for each product to associate them, and then loop through all the javascripts for each product to associate the correct javascript with each product. So ultimately, there's a lot more loops happening and I can't tell if I've made it faster or not. At any rate, here's the code...it still works with the modified templates posted above.
Code:
<?
/*****************************************************************************\
+-----------------------------------------------------------------------------+
| X-Cart |
| Copyright (c) 2001-2002 Ruslan R. Fazliev. All rights reserved. |
+-----------------------------------------------------------------------------+
| The Ruslan R. Fazliev forbids, under any circumstances, the unauthorized |
| reproduction of software or use of illegally obtained software. Making |
| illegal copies of software is prohibited. Individuals who violate copyright |
| law and software licensing agreements may be subject to criminal or civil |
| action by the owner of the copyright. |
| |
| 1. It is illegal to copy a software, and install that single program for |
| simultaneous use on multiple machines. |
| |
| 2. Unauthorized copies of software may not be used in any way. This applies |
| even though you yourself may not have made the illegal copy. |
| |
| 3. Purchase of the appropriate number of copies of a software is necessary |
| for maintaining legal status. |
| |
| DISCLAIMER |
| |
| THIS SOFTWARE IS PROVIDED BY Ruslan R. Fazliev ``AS IS'' AND ANY |
| EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| DISCLAIMED. IN NO EVENT SHALL Ruslan R. Fazliev OR ITS |
| CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| |
| The Initial Developer of the Original Code is Ruslan R. Fazliev. |
| Portions created by Ruslan R. Fazliev are Copyright (C) 2001-2002 |
| Ruslan R. Fazliev. All Rights Reserved. |
+-----------------------------------------------------------------------------+
\*****************************************************************************/
#
# $Id: products.php,v 1.30 2002/11/19 12:06:10 alfiya Exp $
#
# Navigation code
#
$objects_per_page = $config["General"]["products_per_page"];
$total_nav_pages = ceil($current_category["product_count"]/$config["General"]["products_per_page"])+1;
require "../include/navigation.php";
if($active_modules["Advanced_Statistics"])
include "../modules/Advanced_Statistics/cat_viewed.php";
//if($active_modules["Product_Options"])
// include "../modules/Product_Options/customer_options_productspage.php";
#
# Get products data for current category and store it into $products array
#
$search_query = "($sql_tbl[products].categoryid='$cat' or $sql_tbl[products].categoryid1='$cat' or $sql_tbl[products].categoryid2='$cat' or $sql_tbl[products].categoryid3='$cat') and $sql_tbl[products].forsale='Y'";
$products = func_search_products($search_query, $user_account['membership'], $first_page,$current_category["product_count"]);
if (count($products) ==0) $products="";
if($active_modules["Subscriptions"]) {
include "../modules/Subscriptions/subscription.php";
}
//--------Brett's Additions
if($active_modules["DigitalSubscriptions"]) {
include "../modules/DigitalSubscriptions/digital_subscription.php";
}
//------------end Brett's Additions
// Product Options Mod by Brett Brewer
if(is_array($products)){
$i=0;
$productcount=count($products);
// first we build a query to get all the product options with a single query
$query="SELECT * from $sql_tbl[product_options] WHERE ";
foreach($products as $key=>$value){
$query.="productid='$value[productid]' ";
if($i < $productcount-1){
$query.="OR ";
}
$i++;
}
//now get the product options
$product_options=func_query($query);
//now loop through the products and match each one with its corresponding options
foreach($products as $key=>$product){
if(is_array($product_options)){
foreach($product_options as $options){
if($product['productid']==$options['productid']){
$products[$key]['product_options'][]=$options;
}
}
}
}
// now we build a query to get all the javascript option code for the products
$i=0;
$query="SELECT * from $sql_tbl[product_options_js] WHERE ";
foreach($products as $key=>$value){
$query.="productid='$value[productid]' ";
if($i < $productcount-1){
$query.="OR ";
}
$i++;
}
//now get the js_opt_code
$js_opt_code=func_query($query);
//now loop through the products and associate the correct javascript code with each option.
//Currently, x-cart is set up to use only one Javascript function for each product, so we will
//actually be creating more javascript code than we need...
foreach($products as $key=>$value){
//Make sure there are options so no errors or warnings are thrown.
if(is_array($value['product_options'])){
//get the count up front so we don't waste cpu cycles on recounting at each iteration
$optionscount=count($value['product_options']);
for($i=0;$i<$optionscount;$i++){
//now make sure we have some js code before we try to manipulate it
if(is_array($js_opt_code)){
//get the count up front so we don't waste cpu cycles on recounting at each iteration
$js_optioncode_count=count($js_opt_code);
//now loop through the js code and associate the correct javascript code with the correct options
for($j=0;$j<$js_optioncode_count;$j++){
if($value['productid']==$js_opt_code[$j]['productid']){
$products[$key]['product_options'][$i]['javascript_code']=$js_opt_code[$j]['javascript_code'];
}
}
}
//we need to parse the options so that the template can understand them...
$products[$key]['product_options'][$i]['options']=func_parse_options($products[$key]['product_options'][$i]['options']);
// now that we have all the options and the javascript code we need to parse the names
//of the javascript functions and number them the same way the forms and form elements
//on the template will be numbered. I'm not sure this is the best solution for this,
//but it was the only one I could think of.
$javascript=$products[$key]['product_options'][$i]['javascript_code'];
$pattern="/product_option/i";
$replacement="product_option_".$key."_".$i;
$new_javascript=preg_replace($pattern,$replacement,$javascript);
$products[$key]['product_options'][$i]['javascript_code']=$new_javascript;
}
}
}
}
// Now, presumably, the data is correctly formatted for the template.
//-----END Product Options Mod by Brett Brewer
$smarty->assign("products",$products);
$smarty->assign("navigation_script","home.php?cat=$cat");
?>
If anyone knows if this is faster than the previous method, let me know please. Thanks...and...uh...you're welcome.
__________________
www.brettbrewer.com
Getting back into x-cart dev after a long hiatus. Modded lots of x-carts from version 3.1.x to 4.1.x. Developer of ImageScaler mod, Pre-login per user coupon mod, Wordpress feed mod, DigitalSubscriptions mod, Phonetic bulk download keys addon for DownloadExpander mod, Serial Number Generator for ESD products, Custom CMS/LMS integrations, external products mod, and more.