0

I have created a function where query displaying database models correctly when chosen, however if no model selected then i want query to display entire list of models

I mean have database as

id
cat_name
cat_sorder for ascending or descending order
state

Here cat_name has different car manufacturers as Honda Tesla Hyundai Suzuki Ford VW etc

Now when in created function and choosing brand as say Tesla then its displaying correctly as tesla. But if i am not choosing any brand then its displaying 'No Category as shown in last line' which i want to change to display 'entire list of car brands.

Surprise Elememt - this function working perfectly fine when php version changed to php 7.4, but in php 8.0 is not working as desired

public function loadMainCat($brand,$Carmodel){
        $mainframe =JFactory::getApplication();
        $db = JFactory::getDBO();
        $query = $db->getQuery(true);
        
        if($brand!=""){
            $query = "Select *  from #__newcar_categories Where id ='".$brand."' and state='1' Order By cat_sorder ";
        }else{
            $query = "Select *  from #__newcar_categories Where 1 and state='1' Order By cat_sorder ";
        }
        
        $db->setQuery($query);
        $rows = $db->loadObjectList();
        $list="";
        $flg=0;
        if($rows){
        $id='';
        $Desc=$this->loadSubCatDescr($id);
        $list.='<div class="listing-box">
                <div class="listing-box-left">
                <h4>Car Make</h4>
                </div>
                <div class="listing-box-middle">
               <h4> Model</h4>
                </div>
                <div class="listing-box-right">
                <h4><a href="#" onMouseover="ddrivetip(\'Ex-Showroom Delhi\')"; onMouseout="hideddrivetip()" style="color:#424141">Base Price Delhi</a></h4>
                </div>';
            foreach($rows as $row){
                $item_id = $this->getItemIdByName($row->cat_name);
                if($row->cat_image!=""){
                    $img = "uploads/categories/".$row->cat_image;
                }
                if($flg==0){
                    $cls="";
                    $flg=1;
                }else{
                    $cls="";
                    $flg=0;
                }
                
                $list.='<div class="listing-box-left"><p>'.$row->cat_name.'</p></div>
                        <div class="listing-box-middle">'.$this->loadSubCat($row->id,$Carmodel).'</div>';
            }
        $list.='</div>';
        }else{
            $list.='<div class="listing-box">No Category.</div>';
        }
        return $list;
    }

Now, If i modify function in this way where value of if($brand!="") changed to if($brand="") then yes then in event of no brand as selected, its displaying all brands, but even if select brand as Tesla then still its displaying all brands

public function loadMainCat($brand,$Carmodel){
        $mainframe =JFactory::getApplication();
        $db = JFactory::getDBO();
        $query = $db->getQuery(true);
        
        if($brand=""){
            $query = "Select *  from #__newcar_categories Where id ='".$brand."' and state='1' Order By cat_sorder ";
        }else{
            $query = "Select *  from #__newcar_categories Where 1 and state='1' Order By cat_sorder ";
        }
        
        $db->setQuery($query);
        $rows = $db->loadObjectList();
        $list="";
        $flg=0;
        if($rows){
        $id='';
        $Desc=$this->loadSubCatDescr($id);
        $list.='<div class="listing-box">
                <div class="listing-box-left">
                <h4>Car Make</h4>
                </div>
                <div class="listing-box-middle">
               <h4> Model</h4>
                </div>
                <div class="listing-box-right">
                <h4><a href="#" onMouseover="ddrivetip(\'Ex-Showroom Delhi\')"; onMouseout="hideddrivetip()" style="color:#424141">Base Price Delhi</a></h4>
                </div>';
            foreach($rows as $row){
                $item_id = $this->getItemIdByName($row->cat_name);
                if($row->cat_image!=""){
                    $img = "uploads/categories/".$row->cat_image;
                }
                if($flg==0){
                    $cls="";
                    $flg=1;
                }else{
                    $cls="";
                    $flg=0;
                }
                
                $list.='<div class="listing-box-left"><p>'.$row->cat_name.'</p></div>
                        <div class="listing-box-middle">'.$this->loadSubCat($row->id,$Carmodel).'</div>';
            }
        $list.='</div>';
        }else{
            $list.='<div class="listing-box">No Category.</div>';
        }
        return $list;
    }

Pls help on how to achieve that

If a brand is selected as showing in original function then correct brand is shown, but if no brand is selected then all brands in database should be shown instead of No Category as result

Thanks

Ruchika
  • 503
  • 1
  • 8
  • 26
  • remove 1 from else statment $query = "Select * from #__newcar_categories Where state='1' Order By cat_sorder "; – Mehrwarz Nov 23 '22 at 05:23
  • do you mean i should mention state as '0' then it will published in discontinued brands too. – Ruchika Nov 23 '22 at 05:26
  • No! here [Where 1 and state=] change to [where state=] – Mehrwarz Nov 23 '22 at 05:27
  • Thanks but no change still showing 'no catergory' – Ruchika Nov 23 '22 at 05:35
  • You may have an issue with $db->loadObjectList(); Can you test the Print_r($db->loadObjectList()); // What is the out put of this line – Mehrwarz Nov 23 '22 at 05:49
  • I'd like to see your Joomla questions posted on [joomla.se] Stack Exchange -- this is why that niche community was created in the first place. – mickmackusa Nov 23 '22 at 06:24
  • `$brand=""` is an assignment. `$brand==""` is a loose comparison. `$brand===""` is a strict comparison. [The 3 different equals](https://stackoverflow.com/q/2063480/2943403) Also, I see `$img` being delcared, but never used. – mickmackusa Nov 23 '22 at 06:25
  • Ok will try with $brand==="" but this function working perfectly fine when php version changed to php 7.4, but in php 8.0 is not working as desired – Ruchika Nov 23 '22 at 06:30
  • If this issue is php-version-specific, then I'd say this is more of a Joomla issue. Perhaps with `JFactory`. I found [Working function in php 7.4 not working in 8.0](https://stackoverflow.com/q/74463638/2943403) while searching -- I guess you are pretty familiar with that page. Perhaps look for some more debugging details. Dump your sql and result as a sanity check. https://joomla.stackexchange.com/a/27909/12352 – mickmackusa Nov 23 '22 at 06:31

0 Answers0