Handeling Multiple select

Discussion in 'PHP' started by bsvyas, Apr 10, 2007.

  1. #1
    Hello,

    While developing i am finding a problem for handeling select box array.

    Below given code is used to fetch the categories from database and this is selectbox array,
    ==================================================
    <select name="Catsearch[]" multiple>
    <?
    while ($row = mysql_fetch_assoc($Catresult)) {
    if($_REQUEST['Catsearch'] == $row["catid"]){
    $selVal = "selected";
    }
    else {
    $selVal = "";
    }
    ?>
    <option value="<?=$row["catid"]?>" <?=$selVal?>><?=$row["CategoryName"]?></option>
    <? }
    ?>
    </select>
    HTML:
    ==================================================

    I pass this array values as comma saperated values.

    $TotalCat = sizeof($_REQUEST['Catsearch']);
    
    for($i = 0; $i < $TotalCat ; $i++){
    
    if($TotalCat == 1){
    $appendVar=$_REQUEST['Catsearch'][0];
    }
    else{
    $appendVar=$_REQUEST['Catsearch'][$i].",".$appendVar;
    }
    }
    $sqlc .= " And category in (".$appendVar.")";
    PHP:
    When the value is passed it goes with extra comma. I need to remove last comma. Please suggest me how to handel this.

    at present my query looks like this

    select category from CatMast where categorystatus = 'A' and category in (1,2,3,)

    It should be like this

    select category from CatMast where categorystatus = 'A' and category in (1,2,3)

    Please suggest me the solution for removing last comma
     
    bsvyas, Apr 10, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    if($TotalCat == 1){
    $appendVar=$_REQUEST['Catsearch'][0];
    }
    else{
    $appendVar= implode(', ', $_REQUEST['Catsearch']);
    }
    
    PHP:
    And take out the loop.
     
    nico_swd, Apr 10, 2007 IP
  3. bsvyas

    bsvyas Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I did that and its working now,

    Thanks a lot for your help
     
    bsvyas, Apr 10, 2007 IP
  4. bsvyas

    bsvyas Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hello ,

    I am able to store comma saperated categories in table - the values are stored in this way "2,4,6"

    Now the catch is when i search by this query it works

    ---------------------------------------------
    EmpID | EmpName | EmpCategory
    ---------------------------------------------
    1 | Joe | 1,2,4,6
    2 | Sue | 5,3
    ---------------------------------------------

    Note: when i search by this query it works fine for emp table
    This fails when i search by below given query

    select * from emptable where EmpID = 1 and EmpCategory in (2,4,6) 
    Code (markup):
    Please suggest to find proper result for above situation
     
    bsvyas, Apr 22, 2007 IP