Jason Gugg
Jason Gugg

Reputation: 35

Comparing 2 strings

I have a php script that's not working correctly.

when i test it using strcmp it returns 0 at the right time but the if statement still returns false.

Contents of keywords.php

<?php $cat_array = array("All Deals","Arts & Crafts","Automotive","Dental","Education & Training","Entertainment","Fashion","Fitness & Health","Floral","Fun & Adventure","Home & Garden","Pets","Photography","Restaurants","Spa & Beauty","Sports","Subscriptions","Tours & Attractions","Trade Shows","Travel","Workshops");
$cat_nav_array = array("alldeals","arts_crafts","automotive","dental","education_training","entertainment","fashion","fitness_health","floral","fun_adventure","home_garden","pets","photography","restaurants","spa_beauty","sports","subscriptions","tours_attractions","tradeshows","travel","workshops");

?>

php code for main page

 <?php 
        require_once("keywords/keywords.php");
        $count = 0;
        $category = $_GET['category'];
        foreach ($cat_array as $link)
        {
            $count++;
        }
        for ($i = 0; $i <= $count; $i++)
        {
            $link = $cat_nav_array{$i};
            $text = $cat_array{$i};

            if ($category === $link) 
            {
            ?>
                <li><a class="ch_selected" href="<?php echo $_SERVER['HTTP_HOST']?>/chunky/others/?category=<?php echo $link?>" title="View all Deals from <?php echo $text ?>"><?php echo $text?></a></li>
            <?php 
            } 
            else
            {?>
                <li><a href="<?php echo $_SERVER['HTTP_HOST']?>/chunky/others/?category=<?php echo $link ?>" title="View all Deals from <?php echo $text ?>"><?php echo $text?></a></li>
            <?php
            }
        } ?>

Working code:

    <?php 
        require_once("keywords/keywords.php");
        $count = 0;
        $category = strtolower($_GET['category']);
        foreach ($cat_array as $link)
        {
            $count++;
        }
        for ($i = 0; $i <= $count; $i++)
        {
            $link = $cat_nav_array{$i};
            $text = $cat_array{$i};

            $same = strcmp($link, $category);

            if ($same != 0) 
            {
            ?>
                <li><a href="<?php echo $_SERVER['HTTP_HOST']?>/chunky/others/?category=<?php echo $link?>" title="View all Deals from <?php echo $text ?>"><?php echo $text?></a></li>
            <?php 
            } 
            else
            {?>
                <li class="ch_selected"><a href="<?php echo $_SERVER['HTTP_HOST']?>/chunky/others/?category=<?php echo $link ?>" title="View all Deals from <?php echo $text ?>"><?php echo $text?></a></li>
            <?php
            }
        } ?>

Upvotes: 2

Views: 2062

Answers (3)

Johannes Klau&#223;
Johannes Klau&#223;

Reputation: 11040

Compare with if ($catagory === $link).

Upvotes: 2

seth
seth

Reputation: 1389

I may be way off the answer but if i recall correctly that is not the way to compare strings. Please refer to this: http://www.phpcatalyst.com/php-compare-strings.php

Upvotes: 0

Fabrizio
Fabrizio

Reputation: 3776

try to do a var_dump($link, $catagory) right before the if line and see if the two vars are really the same.

Upvotes: 0

Related Questions