Forum

Thread tagged as: Question, Problem, Configuration

Two URL types behaving differently

I've got two types of URL's on a list and detail page

One is generated from a select/submit form... .../category.php?type=type/office/

And the other from the category tag/s at the bottom of a detail page... .../category/type/office/

Both URL's extract the correct list and detail information but a tiny "icing on the cake" thing is pre-populating the select menus the same as .../category.php?type=type/office/ does. The latter URL fails because catPath does not match {current_type_category}. I can't spot why that should be, seeing that everything else works

PHP...

// Get cat into current_category
PerchSystem::set_var('current_type_category', perch_get('type'));

    // This create the catergory tags set
    perch_categories(array(
        'template'   => 'category-type-select.html',
        'set' => 'type',
        )); 

Template

<perch:before>
    <p class="smallprompt">Property search:</p> 
    <form method="get" action="category.php">
    <select id="cattype" class="typeselect" name="type">
        <option value=""/>Any type</option>
</perch:before>
        <option value="<perch:category id="catPath" />" <perch:if id="catPath" match="eq" value="{current_type_category}">selected</perch:if>><perch:category id="catTitle" /></option> 
<perch:after>
    </select>
</perch:after>
David Owen

David Owen 0 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

What does perch_get('type') return and what does your route look like?

I'm using Perch 3. No routing?

Using ../category.php?type=type/retail/ and perch_get('type') returns {current_type_category} in all three categories correctly as type/office/ , type/retail etc. Which then matches catPath

However, using the URL as .../category/type/retail/ or .../category/type/office/ this first two selection options and current_type_category returns correctly, However on these catPath returns type/industrial which is the last in the category set regardless. So it's only using the last selection where it works.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Before you get anywhere near a template, what's the value of perch_get('type') ?

From the detail template, the value of type is <perch:category id="catPath" />

Sent from the detail template category link tags

<perch:before>
    <h2 class="propCatTitle">Property Categories</h2>
    <ul class="prodcats">
</perch:before>

<perch:categories id="type" label="Type" set="type" display-as="checkboxes" required="true" help="Select a Property Type. More than one can be used for a multi-purpose property" divider-before="Categories">
    <li class="ProdCatTag"><a class="CatTag" href="/category/<perch:category id="catPath" />"><perch:category id="catTitle" /></a></li>
</perch:categories>

<perch:categories id="area" label="Area" set="area" display-as="checkboxes" required="true" help="Select the location of this property" divider-before="Categories">
      <li class="ProdCatTag"><a class="CatTag" href="/category/<perch:category id="catPath" />"><perch:category id="catTitle" /></a></li>
</perch:categories>

<perch:after>
    </ul>
</perch:after>
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hi David,

When the URL is .../category.php?type=type/office/, perch_get('type') gets you the catPath type/office.

When the URL is .../category/type/office/, what do you get if you output the perch_get('type') ? Do you get type/office? Or just office?

echo perch_get('type');

Your perch:if tag is testing against a full catPath. So you need a catPath like type/office.

office by itself is a catSlug.

Hi Hussein,

Both URL's .../category/type/office/ and .../category.php?type=type/office/ gets the correct value type/office using...

echo perch_get('type');

Hence why I'm puzzled.

Here's the full php of the select form...

echo perch_get('type');
echo perch_get('area');

// Get type cat into current_category
PerchSystem::set_var('current_type_category', perch_get('type'));

    // This create the catergory tags set
    perch_categories(array(
        'template'   => 'category-type-select.html',
        'set' => 'type',
        )); 

// Get area cat into current_category
PerchSystem::set_var('current_area_category', perch_get('area'));

perch_categories([
    'set'=>'area',
    'sort'=>'catTitle',
    'template' => 'category-area-select.html',
    'sort-order'=>'ASC',
    'each'=>function($item) 
        {
         if (PerchUtil::count(perch_content_custom('Properties', ['skip-template'=>true, 'page' => '/properties.php', 'category'=>$item['catPath']])) > 0) return $item; 
         }
]);

echo perch_get('type');
echo perch_get('area');

Plus checking the value of type and area are correct before and after.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Umm I think catPath includes a trailing slash. So a catPath would be type/office/ not type/office.

I'm sure .../category.php?type=type/office/ gets you type/office/, but perhaps you're not be getting the trailing slash when using .../category/type/office/?

<!-- works -->
<perch:if id="catPath" match="eq" value="type/office/">selected</perch:if>

<!-- doesn't work -->
<perch:if id="catPath" match="eq" value="type/office">selected</perch:if>

Well spotted. Didn't see that. .../category/type/office/ is returning type/office without a slash

Hussein Al Hammad said:

Umm I think catPath includes a trailing slash. So a catPath would be type/office/ not type/office.

Is there a way to fix this?

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

You could add the trailing slash if it doesn't exist:

$type = perch_get('type');

if(substr($type, -1) !== '/') {
    $type .= '/';
}

PerchSystem::set_var('current_type_category', $type);

Thanks, I was thinking of using `substr'. Just thinking if there's a Perch way.