Forums / Templates, Stylesheets, Page Layout / attribute PHP modding question

attribute PHP modding question

Views: 1,013

Results 1 to 12 of 12
23 Mar 2012, 7:28 PM
#1
skylab001 avatar

skylab001

New Zenner

Join Date:
Jan 2008
Posts:
67
Plugin Contributions:
0

attribute PHP modding question

I would like to add and IF/ELSE php statement in the tpl_module_attributes.php page of my template that will check if an attribute is a radio field or select field and change the "<div class='back'>" to a 'new class' on line 32 depending on what input type it is. I could probably do this in javascript after the page loads, but it would be more elegant to do it server side. Any help is welcome.

23 Mar 2012, 9:14 PM
#2
niccol avatar

niccol

Totally Zenned

Join Date:
Apr 2009
Posts:
2,138
Plugin Contributions:
1

Re: attribute PHP modding question

Well to do that you are going to need to have a look at modules/attributes.php.

Two ways of approaching it:

  1. When the menu is created set a flag variable like $input_type that you later use in a conditional statement.
  2. Move the containing div into attributes.php from tpl_modules_attributes. Then you can create it all in one go without the need for the flag variable.

attributes.php is kind of long and involved but the bit you are interested in is down near the bottom where $options_menu is being created.

Nick
iszent.com

23 Mar 2012, 10:24 PM
#3
skylab001 avatar

skylab001

New Zenner

Join Date:
Jan 2008
Posts:
67
Plugin Contributions:
0

Re: attribute PHP modding question

my original thought was#1, only because that seemed easiest. I've been poking about in modules/attributes.php with no luck. I can see for the most part where the menu is being built but any idea how to set a flag variable, and where I should put it.

24 Mar 2012, 4:32 AM
#4
skylab001 avatar

skylab001

New Zenner

Join Date:
Jan 2008
Posts:
67
Plugin Contributions:
0

Re: attribute PHP modding question

So after attempting to get something to work after poking about most of the day, I've thought it might be better to change directions.

A hidden attribute field would be a cleaner way of doing what I was attempting to do as a hack. So I created a new HIDDEN option type, I added it to my db and then I added the PRODUCTS_OPTIONS_TYPE_HIDDEN to the option_name.php file. Everything seems good on the Admin side.

But the catalog side...I've tried to hack together a new Hidden attribute field in modules/attributes.php using zen_draw_hidden_field. My attributes seem to be passing as radio buttons, but that may be the default for single item options. So what I thought I would be capable of doing, I just can't quite get my head around. How to output my PRODUCTS_OPTIONS_TYPE_HIDDEN as a zen_draw_hidden_field in attributes.php. Any thoughts other than thinking a novice could tackle this? :frusty:

24 Mar 2012, 7:36 AM
#5
niccol avatar

niccol

Totally Zenned

Join Date:
Apr 2009
Posts:
2,138
Plugin Contributions:
1

Re: attribute PHP modding question

OK. In attributes.php you are looking for a 'switch'. It is worth working out what switch actually does (http://www.tizag.com/phpT/switch.php). Basically it is an easier way of writing lots of 'if' statements. The switch you are interested in is

                // Option Name Type Display
                switch (true) {

It is down near the bottom of the file.

Now, each 'case' in that switch applies to one kind of attribute. They are commented so you should be able to work them out. For every 'case' ( and doing every one is important even if you do not use them ) you need to add a line like this:

                  // radio buttons
                  case ($products_options_names->fields['products_options_type'] == PRODUCTS_OPTIONS_TYPE_RADIO):
                    $options_type[] = 'radio';

Then in tpl_modules_attributes you can add this line:

<div class="wrapperAttribsOptions">
<?php echo $options_type[$i];?>

Which will just output the option type next to the menu so that you can test that it is all working the way you would expect.

After that you should have all the stuff in place to write some 'if' statements to get it to work the way you want. You might even just try this:

<div class="wrapperAttribsOptions">
<h4 class="optionName back"><?php echo $options_name[$i]; ?></h4>
<div class="<?php echo $options_type[$i];?>"><?php echo "\n" . $options_menu[$i]; ?></div>
<br class="clearBoth" />
</div>

Nick
iszent.com

24 Mar 2012, 9:40 PM
#6
skylab001 avatar

skylab001

New Zenner

Join Date:
Jan 2008
Posts:
67
Plugin Contributions:
0

Re: attribute PHP modding question

I understand the 'switch', I actually read up on it yesterday and did quite a bit of trying it out. Now I placed the variable flag like mentioned, I even put one in every case, here's a sample:

// radio buttons
                  case ($products_options_names->fields['products_options_type'] == PRODUCTS_OPTIONS_TYPE_RADIO):
                  if ($show_attributes_qty_prices_icon == 'true') {
                    $options_name[] = ATTRIBUTES_QTY_PRICE_SYMBOL . $products_options_names->fields['products_options_name'];
                  } else {
                    $options_name[] = $products_options_names->fields['products_options_name'];
                  }
                  $options_menu[] = $tmp_radio . "\n";
                  $options_comment[] = $products_options_names->fields['products_options_comment'];
                  $options_comment_position[] = ($products_options_names->fields['products_options_comment_position'] == '1' ? '1' : '0');
                  $options_type[] = "radio";
				  break;

and then I used the code in tpl_module_attributes to see what the output was:

<div class="wrapperAttribsOptions">
<?php echo $options_type[$i];?>

that worked well and it was good to see what was going on, like seeing that my attempt at a hidden type actually output a dropdown converted to a radio button(default case I think), probably because my code was not right. I was actually trying to output the type of option yesterday but was trying to pull the variable from elsewhere and it wasn't working. Anyway I reverted my modules/attributes.php to original, added the flags, reverted to radio button options in the admin, and things started to output as they should without modifications.

After checking everything I placed this conditional in the tpl_module_attributes:

<?php if ($options_menu == 'radio'){
	$class = 'hideme'; 
} else {
	$class = 'styled-select';
}
?>

<div class="<?php echo $class; ?>" ><?php echo "\n" . $options_menu[$i]; ?></div>
<br class="clearBoth" />

but alas, it doesn't work, it outputs every type of option in the same $class rather than just switching the radio options. The whole point is to hide the radio buttons but nothing else. I'm still thinking that figuring out how to have a <input type="hidden" field output as an option would probably be best.

24 Mar 2012, 9:50 PM
#7
niccol avatar

niccol

Totally Zenned

Join Date:
Apr 2009
Posts:
2,138
Plugin Contributions:
1

Re: attribute PHP modding question

Well there are lots of ways to skin a cat....

I suspect that this in your code:

<?php if ($options_menu == 'radio'){

should actually say $option_type

Nick
iszent.com

24 Mar 2012, 10:34 PM
#8
skylab001 avatar

skylab001

New Zenner

Join Date:
Jan 2008
Posts:
67
Plugin Contributions:
0

Re: attribute PHP modding question

Hmm... good catch, unfortunately still not working, I get the 'styled-select' class all the time, it doesn't change whether or not $option_type == 'radio'.

25 Mar 2012, 8:29 AM
#9
niccol avatar

niccol

Totally Zenned

Join Date:
Apr 2009
Posts:
2,138
Plugin Contributions:
1

Re: attribute PHP modding question

Well I am not sure exactly what you have done but in my version you would need to have

$option_type[$i]

exactly the same vbariable as

<?php echo $options_type[$i];?>

that you said that you had working previously.

Nick
iszent.com

25 Mar 2012, 8:41 AM
#10
niccol avatar

niccol

Totally Zenned

Join Date:
Apr 2009
Posts:
2,138
Plugin Contributions:
1

Re: attribute PHP modding question

so probably your if statement is going to look like

<?php if ($options_type[$i] == 'radio'){
	$class = 'hideme'; 
} else {
	$class = 'styled-select';
}
?>

If this kind of statement is not working the way that you think it should it then the first step is to include some kind of error reporting or diagnostic code. In this case it can be a s simple as:

<?php 
        echo $options_type[$i];
if ($options_type[$i] == 'radio'){
	$class = 'hideme'; 
} else {
	$class = 'styled-select';
}
?>

that will just print the value of the variable just before the if statement.

Nick
iszent.com

26 Mar 2012, 12:00 AM
#11
skylab001 avatar

skylab001

New Zenner

Join Date:
Jan 2008
Posts:
67
Plugin Contributions:
0

Re: attribute PHP modding question

I had actually done it like this $options_type[$i] at one point, with the array variable, but it still wasn't working so I tried it other ways. I did eventually find the problem though. It was at the very bottom of attributes.php around line 600, where the dropdown menu is created.

I had caught the first part of the default case, the dropdown switch to radio button and didn't realize that there was an if/else below it that built the regular dropdown. So I hadn't put a flag in there and it was the cause of the problem. Once I put in a flag it worked flawlessy. Too many hours spent going over and over that page looking for some error. :wacko:

I find it funny how it's always the little things I miss, a typo or a semicolon, that can destroy perfectly good code.
Thanks so much for your assistance!!!

26 Mar 2012, 6:58 AM
#12
niccol avatar

niccol

Totally Zenned

Join Date:
Apr 2009
Posts:
2,138
Plugin Contributions:
1

Re: attribute PHP modding question

Welcome to the world of programming :-) Little details can turn the whole apple cart over. And, php is actually pretty tolerant.

I find, like most programmers I guess, that I end up putting echo statements everywhere when things start going a bit wrong. There are more scientific ways of tracking things down but lots of echo statements through the code works and is quick to do.

Glad that you got it working.

Nick
iszent.com