Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls
 

Passing Smarty Variables to PHP?

 
Reply
   X-Cart forums > X-Cart 4 > Dev Questions > Changing design
 
Thread Tools Search this Thread
  #11  
Old 11-24-2002, 09:53 AM
 
AJ AJ is offline
 

Member
  
Join Date: Oct 2002
Location: Marietta, Ohio
Posts: 25
 

Default

You're right. I don't think I've adequately explained it. I have added a column to the xcart_products table for "Unit of Issue" (unit). I needed to be able to tell the customer at a glance whether the item was a set of 4, set of 12, a dozen in a bag, etc.

But the unit column itself only contains unit codes (ST4, ST12, BG, etc). The full descriptive unit names (ie, Set of 4) are stored in a separate table that I created.

The ultimate goal is to add a line underneath the price of each product that says "Unit of issue: " and then insert the descriptive unit name, so that it reads something like "Unit of issue: Set of 4".

So I created a php file that would take the productid (which products.tpl already is using), then query the xcart_products table to determine the unitcode, then query the unit table to determine the full unit name and display it as "Unit of issue: Set of 4". Seemed pretty easy to me.

I figured I could just include that PHP file in the template right where I wanted it to appear, and that would be that. But since the PHP file can't use any Smarty variables, I'm stuck. Hope that clears things up a bit. I got confused just writing it
Reply With Quote
  #12  
Old 11-24-2002, 10:12 AM
 
tmx tmx is offline
 

Member
  
Join Date: Nov 2002
Location: Arizona (USA)
Posts: 28
 

Default ...

Ok correct me if I'm wrong but could you add that into the array that products.php already creates, that is if the array doesn't already have it, and then in the tpl file use it by something like { $products[product].unit } ?
__________________
-tmx-
Reply With Quote
  #13  
Old 11-24-2002, 10:17 AM
 
AJ AJ is offline
 

Member
  
Join Date: Oct 2002
Location: Marietta, Ohio
Posts: 25
 

Default Re: ...

Quote:
Originally Posted by tmx
Ok correct me if I'm wrong but could you add that into the array that products.php already creates, that is if the array doesn't already have it, and then in the tpl file use it by something like { $products[product].unit } ?
That would be easy to do, if the $products[product].unit variable was actually what I wanted to display. In that form, it would just be a code (ie, ST4, ST12). I need to use the $products[product].unit variable to query another table to translate the unit code to the unit name.

I guess what this may come down to is just finding a different way of storing the unit information in the db, but I'm once again having to do things differently just to cater to Smarty. Ugh.
Reply With Quote
  #14  
Old 11-24-2002, 10:44 AM
 
tmx tmx is offline
 

Member
  
Join Date: Nov 2002
Location: Arizona (USA)
Posts: 28
 

Default ...

Ok so couldn't you set that other table up into an array in either another php file with just an include or just in products.php and use what $products[product].unit comes up to get the full unit name using the same basic method?
__________________
-tmx-
Reply With Quote
  #15  
Old 11-24-2002, 01:29 PM
 
derrick92130 derrick92130 is offline
 

Advanced Member
  
Join Date: Nov 2002
Location: San Diego, California USA
Posts: 68
 

Default variable passing

-tmx- described the exact process that I did. I had the same scenario where I added a new "leadtime" field to the products table. All that went into that field was a CODE that was translated from a new table ("leadtimes"). In the product.php code, I pulled the contents of the leadtimes table into an array. In the product.tpl, I then just referenced the matching entry in the array to the code in the product. That way I could change the "leadtimes" table at any time to change custom leadtimes based upon product characteristics without having to do a mass change in to all entries in the products table.

Let me know if you want an example of either the php or tpl files.
__________________
-Derrick
FreeRangeMinds, LLC
Reply With Quote
  #16  
Old 11-24-2002, 01:35 PM
 
AJ AJ is offline
 

Member
  
Join Date: Oct 2002
Location: Marietta, Ohio
Posts: 25
 

Default Re: variable passing

Quote:
Originally Posted by derrick92130
Let me know if you want an example of either the php or tpl files.
I think I know where you're going with this, and it does sound identical to my problem. Creating a query that loads all the unit names into an array is no problem. But I would be curious to see what code you used in product.tpl. Thanks much.
Reply With Quote
  #17  
Old 11-24-2002, 04:13 PM
 
derrick92130 derrick92130 is offline
 

Advanced Member
  
Join Date: Nov 2002
Location: San Diego, California USA
Posts: 68
 

Default passing variables/alternative approach

AJ,
I just pulled my code (it has been a little while). It looks like I did the "correlation" in the product.php program, and left the cross-referenced narrative (from the leadtimes table) in a single element array (could have just assigned to a variable) that is referenced in the product.tpl.

Here is a snippet from the product.php (much hacked 3.1.1/3.1.2 version):

Code:
# # Put all product info into $product array # $product_info = func_select_product($productid, $user_account['membership']); $cat = $product_info["categoryid"]; $leadtime_text = func_query("SELECT message FROM leadtimes WHERE code='$product_info[leadtime]'"); #... # body of product.php #... $smarty->assign("leadtime_text",$leadtime_text); $smarty->assign("location",$location); $smarty->assign("product",$product_info); $smarty->assign("main","product"); $smarty->display("customer/home.tpl"); ?>

Here is how I referenced it in product.tpl (once again from hacked 3.1.1 tpl file). Just a simple expression of the array. This was one of the first changes I made when I first started working with X-Cart, so I could have cleaned this up with just a single variable instead of an array, so you see the array index fixed to [0]

Code:
# test is only here to see if I need to display any leadtime message at all {if $product.leadtime ne "NONE"} <tr><td class=Leadtime align=center colspan=2> {$leadtime_text[0].message} </td></tr> <tr><td colspan="2">[img]../{ #ImagesDir# }/spacer.gif[/img]</td></tr> {/if}

Hope this helps. Let me know if I can provide any more details.
__________________
-Derrick
FreeRangeMinds, LLC
Reply With Quote
  #18  
Old 11-26-2002, 05:34 AM
 
AJ AJ is offline
 

Member
  
Join Date: Oct 2002
Location: Marietta, Ohio
Posts: 25
 

Default

No dice. I tried something very similar to your code:

Code:
$product_info = func_select_product($productid, $user_account['membership']); $unit_text = func_query("SELECT unit, code FROM units WHERE code='$product_info[unit]'"); $smarty->assign("unit_text",$unit_text);

...and when I go to any product page, I get the "Access denied !
You are not allowed to access that resource!" message. I also thought about just trying to build my own query, based on the productid, but I can't even figure out how to get that variable. $products['productid'] doesn't work.

I'm sure I'm nearing ten hours on this and I'm about to bug out. Once again, my client is going to have to suffer because Smarty is a pain in the ass. Thanks a bunch for the help so far, and if you've got any other ideas, I'm dying to hear them
Reply With Quote
  #19  
Old 11-26-2002, 08:52 AM
 
derrick92130 derrick92130 is offline
 

Advanced Member
  
Join Date: Nov 2002
Location: San Diego, California USA
Posts: 68
 

Default

Can you send the tpl where you are accessing the variables?

Either in the forum our out, I can look at it later today. Can you also send the version you are working with?
__________________
-Derrick
FreeRangeMinds, LLC
Reply With Quote
  #20  
Old 11-26-2002, 08:24 PM
 
derrick92130 derrick92130 is offline
 

Advanced Member
  
Join Date: Nov 2002
Location: San Diego, California USA
Posts: 68
 

Default more Smarty fun

AJ,

I did look through your posting again (actually read it closely this time) and think I see what the problem is. In my example, I was using the product (not products) table and therefore could do a simple lookup in the php code and pass the variable onto the tpl. In your case, you have an entire array of products. In your sample code (from the Forum), you are trying to do a select to populate the $unit_text array that is trying to match "code" to an entire array, not a single element. That is what is causing the access error.

Just load the entire "units" table into the "$unit_text" array in the products.php program.
Code:
$unit_text = func_query("SELECT unit, code FROM units");
Then in the products.tpl file you can get to the "code" a couple of ways. The brute force way would be to nest a {section} loop around the $unit_text array and then just test for a match:

Code:
{section name=unittext loop=$unit_text} {if $unit_text[unittext].unit == $products[product].unit} Unit of Issue: {$unit_text[unittext].code} {/if} {/section}
A more elegant way would be to use the $products[product].unit to reference the index of the php array. I doubt have the syntax correct, but would be very clean. I'd make the {section} work first to lower your frustration level, then try this:

Code:
{$unit_text['$products[product].unit'].code}


Give it a try! If it still doesn't work, send me an email and I should be able to get my test site back on 3.2.2 tomorrow.

Hope this helps.
__________________
-Derrick
FreeRangeMinds, LLC
Reply With Quote
Reply
   X-Cart forums > X-Cart 4 > Dev Questions > Changing design



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 08:26 PM.

   

 
X-Cart forums © 2001-2020