Page 1 of 1

Handeling Multiple select

Posted: Tue Apr 10, 2007 7:34 am
by bsvyas
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


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,

Code: Select all

<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>
I pass this array values as comma saperated values.

Code: Select all

$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.")";
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

Code: Select all

select category from CatMast where categorystatus = 'A' and category in (1,2,3,)
It should be like this

Code: Select all

select category from CatMast where categorystatus = 'A' and category in (1,2,3)
Please suggest me the solution for removing last comma


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Apr 10, 2007 8:06 am
by CoderGoblin
Simplest solution is trim passing a comma as the extra value.

The way I normally handle things like this however is building the list as an array and useimplode to produce the string.

Posted: Tue Apr 10, 2007 9:07 am
by bsvyas
I used implode function and it worked,

Thanks a lot for help

Posted: Tue Apr 10, 2007 9:41 am
by CoderGoblin
You're welcome

Posted: Tue Apr 10, 2007 11:17 am
by RobertGonzalez
Just a suggestion, you might want to scrap the short PHP tags. There have been a number of users posting recently about problems that were eventually directly tied to short opening PHP tags.

Hanedling categories in query

Posted: Sun Apr 22, 2007 2:57 pm
by bsvyas
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

Code: Select all

select * from emptable where EmpID = 1 and EmpCategory in (1,2)
This fails when i search by below given query

Code: Select all

select * from emptable where EmpID = 1 and EmpCategory in (2,4,6)
Please suggest to find proper result for above situation

Re: Hanedling categories in query

Posted: Sun Apr 22, 2007 3:59 pm
by timvw
bsvyas wrote: I am able to store comma saperated categories in table - the values are stored in this way "2,4,6"
It's not good practice to store multiple values in a single column...

Normalize your schema and notice that queries become a lot easier...


(eg)

EmpID | EmpName
----------------------
1 | Joe
2 | Sue

EmpID |Emp_EmpCategory
-------------------------------------
1|1
1|2
1|4
1|6
2|5
2|3