Basic PHP if statement error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
arunkar
Forum Commoner
Posts: 50
Joined: Mon Feb 25, 2008 10:37 pm

Basic PHP if statement error

Post by arunkar »

Hi experts,

I've been breaking my head with a basic php if statement logic error for hours... :banghead: :crazy: I seem not to get the result.

What I need to achieve is simple. I have 2 dropdown boxs, the first box 'value Range' filters the data from DB, default value is none. The second box 'sort Value' sorts the data asc or desc, default value is 0.

If sort value is selected. I need my second if statement to be executed, which fails!...

Code: Select all

 
<tr>
    <td class="tblSTD tblTHBL tblTHBT">USD Value Range </td>
    <td class="tblSTD tblTHBR tblTHBT"><select id="valueRange" name="valueRange">
        <option value="Select">ALL</option>
        <option value="0.01" <?php if($valueRange=="0.01")  echo "selected='selected'";?>> < $1 </option>
        <option value="1" <?php if($valueRange=="1")    echo "selected='selected'";?>>$1 to $3</option>
        <option value="3" <?php if($valueRange=="3")    echo "selected='selected'";?>>$3 to $5</option>
        <option value="5" <?php if($valueRange=="5")    echo "selected='selected'";?>>$5 to $10</option>
        <option value="11" <?php if($valueRange=="11")  echo "selected='selected'";?>> >$10</option>
    </select></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
  <tr>
    <td height="24">&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td class="tblSTD tblTHBL tblTHBB">Sort (Value Range) </td>
    <td class="tblSTD tblTHBR tblTHBB"><select id="sortValue" name="sortValue">
        <option value="0" >None</option>
        <option value="asc" <?php if($sortValue=="asc") echo "selected='selected'";?>>Ascending</option>
        <option value="desc" <?php if($sortValue=="desc") echo "selected='selected'";?>>Descending</option>
    </select></td>
    <td>&nbsp;</td>
    <td><input type="submit" name="GO" value="GO" style=" font-weight: bold; color: #ffffff; background: #ff6600;"/></td>
    </tr>
 
 

Code: Select all

 
        echo "valueRange ".$valueRange."<br>";
        echo "sortValue ".$sortValue."<br>";
if($sortValue == "" || $sortValue == 0){
        echo "sortValue=' ' r 0 ".$sortValue."<br>";
        $Orderby = "Region, Country, CategoryName, BeneficiaryName";
}elseif($sortValue == "asc" || $sortValue == "desc"){
        echo "sortValue_Inside ".$sortValue."<br>";
        $Orderby = "CostUSD $sortValue, Region, Country, CategoryName, BeneficiaryName";
}
        echo "Orderby ".$Orderby."<br>";
 
        $query = "SELECT Title, ID, s.OrgJosUserID, OrgName, r.Region, Country, Story, PicURL, CostUSD, b.BeneficiaryName, c.CategoryName   
    FROM tbl s   
        INNER JOIN jos_users js ON js.id = s.OrgJosUserID   
        INNER JOIN tbl_regions r        ON r.RegionID = s.RegionID  
        INNER JOIN tbl_categories c ON c.CategoryID = s.CatID 
        INNER JOIN tbl_beneficiary b    ON b.BeneficiaryID = s.BeneficiaryID 
        WHERE  r.Region LIKE '$regionList' AND c.CategoryName LIKE '$categoryList' 
        AND b.BeneficiaryName LIKE '$beneficiaryList'  
        AND CostUSD BETWEEN '$valueRange1' AND '$valueRange2' 
        ORDER BY $Orderby "; 
 

The output I get for echo are:

valueRange: 1
sortValue: asc
sortValue='' r 0: asc
Orderby: Region, Country, CategoryName, BeneficiaryName


Any idea were I'm going wrong? :banghead:


Thanks
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Basic PHP if statement error

Post by califdon »

What are you asking? What result do you expect, compared with what you are getting?
arunkar
Forum Commoner
Posts: 50
Joined: Mon Feb 25, 2008 10:37 pm

Re: Basic PHP if statement error

Post by arunkar »

I just swaped the if statement and it worked. Dose any one know why... I've no Idea... :crazy:

THis is what I did

Code: Select all

 
        if($sortValue == "asc" || $sortValue == "desc"){
            $Orderby = "CostUSD $sortValue, Region, Country, CategoryName, BeneficiaryName";
        }elseif($sortValue == 0){
            $Orderby = "Region, Country, CategoryName, BeneficiaryName";
        }
 
 //  ($sortValue == 0 || $sortValue == "" ) did not work
 
 
Thank you
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Basic PHP if statement error

Post by RobertGonzalez »

Your sort element will always pass either a string of "0", a string of "asc" or a string of "desc". It would never be an empty string.
Post Reply