Welcome to Discussion Forum, where you can ask questions and receive answers from other members of the community.

syntax error, unexpected 'if' (T_IF), expecting ')' in D:\xampp\htdocs\ramesh\wmw\catalog\controller\product\sub_category.php on line 29

1 like 0 dislike
I received this error while trying to run the if statement for different href
 
Parse error: syntax error, unexpected 'if' (T_IF), expecting ')' in D:\xampp\htdocs\ramesh\wmw\catalog\controller\product\sub_category.php on line 29
sub_category.php
 
foreach ($category_info as $result) {
        $data['categories'][] = array(
        'name' => $result['name'],
        'parent_id' => $result['parent_id'],
        'thumb' => $this->model_tool_image->resize($result['image'], $this->config->get('config_image_category_width'), this->config->get('config_image_category_height')),
        'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get('config_product_description_length')) . '..',
        /*'href' => $this->url->link('product/filter', '&category_id=' . $result['category_id'])
        'href' => $this->url->link('product/sub_category')*/
        if($result['category_id']==24)
        {
            'href' => $this->url->link('product/transmission', 'sub_category_id='.$result['parent_id'].'&category_id=' . $result['category_id'])
    }
    elseif
    {
            /*some code for href*/
        }
 
      );
}
asked Mar 22, 2017 in Opencart by harsh (8,710 points)

2 Answers

0 like 0 dislike

There are many mistakes in your code and also you have added if, else condition inside array function which is not allowed.

If you want to assign different href for different-2 condition, then you can use if condition outside of the array and simply assign.

Corrected code is below -

foreach ($category_info as $result) {
        $data['categories'][] = array(
            'name' => $result['name'],
            'parent_id' => $result['parent_id'],
            'thumb' => $this->model_tool_image->resize(
                $result['image'],
                $this->config->get('config_image_category_width'),
                $this->config->get('config_image_category_height')
            ),
            'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get('config_product_description_length')) . '..',
        );
        if($result['category_id']==24)
        {
            $data['categories']['href'] = $this->url->link('product/transmission', 'sub_category_id='.$result['parent_id'].'&category_id=' . $result['category_id']);
        }
        else
        {
            // Code for different href
        }
    }

 

answered Mar 22, 2017 by Mayank Singh (940 points)
0 like 0 dislike

Your code syntax is wrong. If else conditions are not allowed with in an array.

Please replace your code with the following and error will be resolved - 

foreach ($category_info as $result) {
        $href = '';
        
        if ($result['category_id']==24) {
            $href = $this->url->link('product/transmission', 'sub_category_id='.$result['parent_id'].'&category_id=' . $result['category_id']);
        } else {
            /*some code for href*/
        }
        
        $data['categories'][] = array(
        'name' => $result['name'],
        'parent_id' => $result['parent_id'],
        'thumb' => $this->model_tool_image->resize($result['image'], $this->config->get('config_image_category_width'), $this->config->get('config_image_category_height')),
        'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get('config_product_description_length')) . '..',
        /*'href' => $this->url->link('product/filter', '&category_id=' . $result['category_id'])
        'href' => $this->url->link('product/sub_category')*/        
        'href' => $href    
 
      );
}
answered Mar 22, 2017 by rsharma (360 points)

Related questions

1 like 0 dislike
3 answers 1,103 views
0 like 0 dislike
0 answers 292 views
0 like 0 dislike
1 answer 2,485 views
1 like 0 dislike
0 answers 23 views
23 views asked Jun 13, 2017 in Opencart by harsh (8,710 points)
...