Handeling Multiple select

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
bsvyas
Forum Newbie
Posts: 3
Joined: Tue Apr 10, 2007 7:29 am

Handeling Multiple select

Post 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]
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
bsvyas
Forum Newbie
Posts: 3
Joined: Tue Apr 10, 2007 7:29 am

Post by bsvyas »

I used implode function and it worked,

Thanks a lot for help
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

You're welcome
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
bsvyas
Forum Newbie
Posts: 3
Joined: Tue Apr 10, 2007 7:29 am

Hanedling categories in query

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: Hanedling categories in query

Post 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
Post Reply