If this is your first visit, check our documentation. To post, register or login.
Forums / Code Matters / Bug Reports / defining product listing default sort order in default_filter.php, faulty iteration

defining product listing default sort order in default_filter.php, faulty iteration

Started by torvista on 15 May 2013, 11:49 AM · Views: 3,072

Results 1 to 15 of 15
15 May 2013, 11:49 AM
#1
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,880
Plugin Contributions:
7

Post #1 by torvista

Here I go sticking my neck out again...:unsure:

Regarding the product listing default sort order code in
/includes/index_filters/default_filter.php…

  1. Question
if (isset($_GET['sort']) && strlen($_GET['sort']) > 3) {
  $_GET['sort'] = substr($_GET['sort'], 0, 3);
}

Why is this limited to three when as far as I can see, the code deals only with two characters eg 2a?

  1. Bug (?)

On initial page load $_GET['sort'] is not set.

If there is a sort order defined in the admin, this gets put into $_GET['sort']:

if (!isset($_GET['sort']) and PRODUCT_LISTING_DEFAULT_SORT_ORDER != '') {
  $_GET['sort'] = PRODUCT_LISTING_DEFAULT_SORT_ORDER;
}

If
there is no default sort order defined in the admin
OR $_GET['sort'] contains characters not 1-8 or a or d ( Question: Why are column numbers 1-8 allowed when there are only 7 options?)
OR the number character is more than the number of display-enabled columns:
then this loop runs:
```php
for ($i=0, $n=sizeof($column_list); $i<$n; $i++) {
if (isset($column_list[$i]) && $column_list[$i] == 'PRODUCT_LIST_NAME') {
$_GET['sort'] = $i+1 . 'a';
$listing_sql .= " order by p.products_sort_order, pd.products_name";
break;
} else {
// sort by products_sort_order when PRODUCT_LISTING_DEFAULT_SORT_ORDER is left blank
// for reverse, descending order use:
// $listing_sql .= " order by p.products_sort_order desc, pd.products_name";
$listing_sql .= " order by p.products_sort_order, pd.products_name";
break;
}
}

As far as I can see this loop is supposed to identify which column is the Product Name and use that for sorting, ascending.
Bug? : However the else clause has a break too, so this loop will only ever execute once and never identify the Product Name column unless it is the first column.

I made this change and it works, i.e. it iterates through all the columns and assigns the correct column id to the $_GET['sort'] for all positions of the Product Name
                ```php
for ($i=0, $n=sizeof($column_list); $i<$n; $i++) {
if (isset($column_list[$i]) && $column_list[$i] == 'PRODUCT_LIST_NAME') {//get the column with the PRODUCT_LIST_NAME in it and set the $_GET['sort'] to match its position
          //$listing_sql .= " order by p.products_sort_order, pd.products_name";
          break;//break out of the for loop when PRODUCT_LIST_NAME is found
                }
                                }
// sort by products_sort_order when PRODUCT_LISTING_DEFAULT_SORT_ORDER is left blank
// for reverse, descending order use:
//       $listing_sql .= " order by p.products_sort_order desc, pd.products_name";
          $listing_sql .= " order by p.products_sort_order, pd.products_name";
  1. Question

The next item in this code is:
```php
// if set to nothing use products_sort_order and PRODUCTS_LIST_NAME is off
if (PRODUCT_LISTING_DEFAULT_SORT_ORDER == '') {
$_GET['sort'] = '20a';


I cannot find where the second digit “0” is taken any account of…is this a typo and should be “2a”?

Steve
github.com/torvista: BackupMySQL, Structured Data, Multiple Copy-Move-Delete, Google reCaptcha, Image Checker, Spanish Language Pack and more...

17 May 2013, 2:30 PM
#2
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,880
Plugin Contributions:
7

Post #2 by torvista

No comment?

Steve
github.com/torvista: BackupMySQL, Structured Data, Multiple Copy-Move-Delete, Google reCaptcha, Image Checker, Spanish Language Pack and more...

17 May 2013, 2:45 PM
#3
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Post #3 by ajeh

You will notice in the default code the use of: 20a

that is 3 characters ...

Then you could customize this code in some wild manner and need more than 9 choices which would go to 3 vs 2 characters ...

There are a number of possibilities as to why you might need more than 2 ...

Linda McGrath
If you have to think ... you haven't been zenned ...

**Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!**

Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
Officially PayPal-Certified! Just click here

Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!

17 May 2013, 5:05 PM
#4
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,880
Plugin Contributions:
7

Post #4 by torvista

Maybe I'm being dense here as I although I see "20a" is in there, I can only see code that examines the "2" and the "a", not the "0".

and the "bug" with the two breaks, is that a bug?

Steve
github.com/torvista: BackupMySQL, Structured Data, Multiple Copy-Move-Delete, Google reCaptcha, Image Checker, Spanish Language Pack and more...

17 May 2013, 5:10 PM
#5
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Post #5 by ajeh

Is this the code that you think is a bug:

// if set to nothing use products_sort_order and PRODUCTS_LIST_NAME is off
if (PRODUCT_LISTING_DEFAULT_SORT_ORDER == '') {
        $_GET['sort'] = '20a';  

Linda McGrath
If you have to think ... you haven't been zenned ...

**Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!**

Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
Officially PayPal-Certified! Just click here

Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!

17 May 2013, 5:16 PM
#6
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,880
Plugin Contributions:
7

Post #6 by torvista

No, this with two breaks in it. Maybe I didn't explain myself properly above.

for ($i=0, $n=sizeof($column_list); $i<$n; $i++) {
        if (isset($column_list[$i]) && $column_list[$i] == 'PRODUCT_LIST_NAME') {
          $_GET['sort'] = $i+1 . 'a';
          $listing_sql .= " order by p.products_sort_order, pd.products_name";
          break;
        } else {
// sort by products_sort_order when PRODUCT_LISTING_DEFAULT_SORT_ORDER is left blank
// for reverse, descending order use:
//       $listing_sql .= " order by p.products_sort_order desc, pd.products_name";
          $listing_sql .= " order by p.products_sort_order, pd.products_name";
          break;
        }
      }

Steve
github.com/torvista: BackupMySQL, Structured Data, Multiple Copy-Move-Delete, Google reCaptcha, Image Checker, Spanish Language Pack and more...

17 May 2013, 5:37 PM
#7
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Post #7 by ajeh

The way it works is based on how many columns are showing ...

If you have 3 columns, then your choices are:
1a
1d
2a
2d
3a
3d
or Blank, which will be changed in the code to:
20a

I am not understanding your change in the code as in why change that?

I am not able to see what is breaking as changing that one line I am not reproducing any breaks in the code ...

More or less, I am not following your question or able to break anything ... :blink:

Can you try typing really, really slow for me with a step by step click for click to break things?

I am not trying to be difficult but I just am not getting the issue or problem ...

Linda McGrath
If you have to think ... you haven't been zenned ...

**Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!**

Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
Officially PayPal-Certified! Just click here

Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!

17 May 2013, 7:44 PM
#8
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,880
Plugin Contributions:
7

Post #8 by torvista

What I understand is that this bit is trying to identify if and which of the defined columns is PRODUCT_LIST_NAME by looking at each of the columns that have been enabled, then when it has found PRODUCT_LIST_NAME, assign the column id to the get and then break out of the loop.
If it doesn't find PRODUCT_LIST_NAME, oh well never mind carry on and assign 20a anyway.

What I see is that if PRODUCT_LIST_NAME is not the first defined column, the flow falls through to the ELSE where it breaks out of the loop anyway.
So this code only checks the first column defined, not all of them.

Steve
github.com/torvista: BackupMySQL, Structured Data, Multiple Copy-Move-Delete, Google reCaptcha, Image Checker, Spanish Language Pack and more...

21 May 2013, 4:00 AM
#9
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,880
Plugin Contributions:
7

Post #9 by torvista

Do I understand correctly what this code is doing?

Steve
github.com/torvista: BackupMySQL, Structured Data, Multiple Copy-Move-Delete, Google reCaptcha, Image Checker, Spanish Language Pack and more...

21 May 2013, 7:06 AM
#10
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,874
Plugin Contributions:
8

Post #10 by gjh42

I think the "break" statements break out of the if (isset(... sequence, not the for(... loop. Thus $i will continue to be incremented and tests made. At least that would seem to be the case since the stock code does in fact assign sort orders (non-20a) when the product name is not the first element. The demo product setup has the name second, and it has sort order 2a, not 1a as it would be if the name was being tested first (count starts at position 0; 0+1=1).

21 May 2013, 1:58 PM
#11
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,874
Plugin Contributions:
8

Post #11 by gjh42

More study about PHP "break" shows that only operates in for/while/switch type structures, not if() structures, but it is not unequivocally clear whether a break inside an if will operate on the for outside it.
But yet, it moves...

21 May 2013, 2:50 PM
#12
rodg avatar

rodg

Deceased

Join Date:
Jan 2007
Location:
Australia
Posts:
6,263
Plugin Contributions:
4

Post #12 by rodg

gjh42:

More study about PHP "break" shows that only operates in for/while/switch type structures, not if() structures, but it is not unequivocally clear whether a break inside an if will operate on the for outside it.
But yet, it moves...

How would you place a 'break' inside an 'if' anyway?

if ($a == 1) {
dosomething;
break ;
} else {
dosomethingelse;
break ;
}

Clearly, there is nothing to break out of here.

if ($a == 1) {
dosomething;
break ;
dosometyhingelse ;
}

That may work, sort of, but even if it did, it is clearly a pointless thing to do, the 'dosometyhingelse' will never be invoked.

What other possible scenarios are there?

for ($i=0, $n=sizeofCOLOR=#007700; $i<$n; $i++) {
if (isset(
$column_list[$i]) && $column_list[$i] == 'PRODUCT_LIST_NAME') {
$_GET['sort'] = $i+1 . 'a';
$listing_sql .= " order by p.products_sort_order, pd.products_name";
break;
} else {
$listing_sql .= " order by p.products_sort_order, pd.products_name";
break;
}
}

[/COLOR]Just for grins, I've stepped through this code and torvista is correct, this code only checks the first column defined, not all of them.

Cheers
Rod

NOTE: We are sorry that Rod is no longer with us.
We are grateful for all his contributions to the Zen Cart community.

Ozpost - The Ultimate Shipping module for Australian Merchants. Click these links for its Homepage, or Download from Zen-Cart.com.

21 May 2013, 3:16 PM
#13
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Post #13 by ajeh

That code only runs if the setting is:

1 Blank

2 Invalid choice

to set up a valid order by for:

          $listing_sql .= " order by p.products_sort_order, pd.products_name";

The IF allows for an invalid setting to be used on the ELSE portion that may be wanted that goes beyond the normal settings such as if you wanted a method for products_date_added and set to 5a (an invalid choice if only 4 columns are defined) by using:

        } else {
echo 'ELSE ' . '<br>';        	
// sort by products_sort_order when PRODUCT_LISTING_DEFAULT_SORT_ORDER is left blank
// for reverse, descending order use:
//       $listing_sql .= " order by p.products_sort_order desc, pd.products_name";
[B]//[/B]          $listing_sql .= " order by p.products_sort_order, pd.products_name";
[B]          $listing_sql .= " order by p.products_date_added, pd.products_name";
[/B]          break;
        }

While not eloquent ... it makes the change easier to change on the default display order for the listing ...

Linda McGrath
If you have to think ... you haven't been zenned ...

**Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!**

Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
Officially PayPal-Certified! Just click here

Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!

21 May 2013, 9:30 PM
#14
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,512
Plugin Contributions:
176

Post #14 by drbyte

To summarize ...

torvista:

  1. Question
    Why is this limited to three when as far as I can see, the code deals only with two characters eg 2a?Correct. At the present time the code only deals with two characters.
    But, harmless.
    torvista:

2a)
$_GET['sort'] contains characters not 1-8 or a or d ( Question: Why are column numbers 1-8 allowed when there are only 7 options?)Actually, it should probably be expanded to allow for 0-thru-9.

torvista:

2b) Bug? : However the else clause has a break too, so this loop will only ever execute once and never identify the Product Name column unless it is the first column.Ya, probably a bug.
Yet, it appears Ajeh has posted about a curious beneficial side-effect which can utilize this for positive results.

torvista:

  1. Question about "20a"
    I cannot find where the second digit “0” is taken any account of…is this a typo and should be “2a”?
    Indeed, in the case of "20a", the "0a" has no effect, and is essentially ignored, since it is neither "a" nor "d", and thus the current code uses other defaults.
    Changing it to "2a" doesn't necessarily accomplish the desired end result either, depending what your desired end result is.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

22 May 2013, 3:00 AM
#15
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,880
Plugin Contributions:
7

Post #15 by torvista

ok, thanks for the confirmations.
This all came about while trying to understand the default sorting mechanisms...:wacko:

Steve
github.com/torvista: BackupMySQL, Structured Data, Multiple Copy-Move-Delete, Google reCaptcha, Image Checker, Spanish Language Pack and more...