1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Only Show Categories That Have Listings In Them?

Discussion in 'PHP' started by phplduser, Mar 27, 2020.

  1. #1
    Hi all,

    I have a fair few categories and i want to only show categories that have listings in them, This is the code i have at the moment that shows all categories regardless -

        $i_substr_cat=substr_count($category, ".");
       
        foreach($categories as $key=>$value)
        {
            $i_substr_key=substr_count($key, ".");
           
            if($i_substr_key != ($i_substr_cat+1))
            {
                continue;
            }
           
            if(strpos($key, $category.".", 0) === 0)
            {
           
                if($website->GetParam("SEO_URLS")==1)
                {
                    $strLink = "https://".$DOMAIN_NAME."/".($MULTI_LANGUAGE_SITE?$M_SEO_CATEGORY:"category")."-".$website->format_str($value)."-".str_replace(".","-",$key).".html";
                }
                else
                {
                    $strLink = "index.php?mod=search&category=".str_replace(".","-",$key).($MULTI_LANGUAGE_SITE?"&lang=".$website->lang:"");
                }
               
                echo "\n<div class=\"col-sm-4 no-left-padding margin-bottom-10\">\n";
               
                echo "\n<a class=\"sub-cat-result\" href=\"".$strLink."\" title=\"".trim($value)."\" >".trim($value)."</a>";
           
                if($website->GetParam("SHOW_LISTINGS_NUMBER")==1)
                {
                    echo " (".(isset($arr_listings_count[$key])?$arr_listings_count[$key]:"0").")";
                }
           
                echo "</div>";
            }
        }
        ?>
    PHP:
    Any help would be very much appreciated.
    Justin
     
    phplduser, Mar 27, 2020 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #2
    What's the back-end system? What data are you feeding this? What's the structure of $categories?

    Without that information there's really no way to answer your question. We're not psychic.

    That said, STOP using double quotes making your life harder, STOP using \n manually to do a normal enter and whitespace's job, stop avoiding whitespace in your string additions, stop making variables for nothing, stop using multiple echo to do one echo's job, etc, etc, etc...

    Cleaning up what you have so far:

    
    $i_substr_cat = substr_count($category, '.');
    
    foreach($categories as $key=>$value) {
    
    	if (substr_count($key, '.') != ($i_substr_cat + 1)) continue;
    
    	if (strpos($key, $category . '.') === 0) {
    
    		if ($website->GetParam("SEO_URLS") == 1) $strLink =
    		) . '-' . $website->format_str($value) . '-' . str_replace('.', '-', $key) . '.html';
    
    		else $strLink = 'index.php?mod=search&category=' . str_replace('.', '-', $key ) . (
    			$MULTI_LANGUAGE_SITE ? '&lang=' . $website->lang : ''
    		);
    
    		echo '
    			<div class="col-sm-4 no-left-padding margin-bottom-10">
    				<a class="sub-cat-result" href="', (
    					$website->GetParam("SEO_URLS") == 1 ?
    					'https://' . $DOMAIN_NAME . '/' . (
    						$MULTI_LANGUAGE_SITE ? $M_SEO_CATEGORY : 'category'
    					) :
    					'index.php?mod=search&category=' . str_replace('.', '-', $key ) . (
    						$MULTI_LANGUAGE_SITE ? '&lang=' . $website->lang : ''
    					)
    				), '">', trim($value), '</a>';
    
    
    		if ($website->GetParam("SHOW_LISTINGS_NUMBER") == 1) echo '(', (
    			array_key_exists($key, $arr_listings_count) ?
    			$arr_listings_count[$key] :
    			'0'
    		), ')';
    
    		echo '
    			</div>';
    
    	}
    
    }
    
    Code (markup):
    Though that's untested (drive-by posting here) might have a typo or two, but gives you a better idea how the PHP should be written.

    ... although I see the mental midgetry that is bootcrap pissing on the markup and a complete lack of semantics -- this looks like a list of categories, we have tags for lists making just anchors with only a DIV around them non-semantic gibberish. There's a reason the folks who CREATED bootcrap are unqualified to write a single blasted line of HTML, much less tell others how to do so.
     
    deathshadow, Mar 28, 2020 IP