SitePoint Sponsor |
|
User Tag List
Results 1 to 7 of 7
Thread: Split foreach loop values
-
Oct 7, 2009, 03:20 #1
- Join Date
- Dec 2006
- Location
- /dev/swat
- Posts
- 619
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Split foreach loop values
Hi all,
I have a listbox, where user can select multiple entries, listbox is populated from database.
PHP Code:<select name="drop1[]" multiple="multiple">
<?php
$sql = mysql_query("select node_id,s_name,code from table order by node_id asc");
while($row = mysql_fetch_object($sql)){
echo "<option value='$row->node_id' selected='selected'>$row->node_id $row->s_name</option>";
}
?>
</select>
I am getting the values as:
PHP Code:$drop = $_POST['drop1'];
foreach($drop as $listbox){
echo $listbox;
}
So what i want to do is split the values and use it outside foreach loop.
I tried exlpode function but in vain.
Any ideas.
Thanks.
-
Oct 7, 2009, 03:42 #2
- Join Date
- Jun 2006
- Location
- Wigan, Lancashire. UK
- Posts
- 523
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Oct 7, 2009, 05:50 #3
- Join Date
- Dec 2006
- Location
- /dev/swat
- Posts
- 619
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks for the reply.
Yup, you are right, its already in array.
Let me explain further, what i want to do is:
As i mentioned, user selected three options from the listbox, after that three values are passed to post variable. lets say:1,2,3 (which are ids of items from database).
Now i want these values to use separately outside the loop. like may be i need 1 some time and not 2,3 OR may be i need all of that three.
After this splitting the values, i write a separate query for each item which representing different tables like:
PHP Code:if($id == 1){
select name from table1 where id ='1'
}
elseif($id == 2){
select cat from table2 where id='2'
}
elseif($id == 3){
select cat_name from table3 where id='3'
}
Hope, i am clear.
If any questions, please ask.
-
Oct 7, 2009, 06:20 #4
- Join Date
- Jun 2006
- Location
- Wigan, Lancashire. UK
- Posts
- 523
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You mean something like this?
PHP Code:if(isset($_POST['drop1'][0])){
select name from table1 where id = $_POST['drop1'][0]
}
elseif(isset($_POST['drop1'][1])){
select cat from table2 where id = $_POST['drop1'][1]
}
elseif(isset($_POST['drop1'][2])){
select cat_name from table3 where id = $_POST['drop1'][2]
}
-
Oct 7, 2009, 07:46 #5
- Join Date
- Apr 2001
- Location
- Leicester, UK
- Posts
- 459
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Instead of isset() you should use in_array(), like so:
PHP Code:if (in_array(1, $_POST['drop1'])) {
// SELECT name FROM table1 WHERE id = 1
} elseif (in_array(2, $_POST['drop1'])) {
// SELECT name FROM table1 WHERE id = 2
}
-
Oct 7, 2009, 08:29 #6
- Join Date
- Dec 2006
- Location
- /dev/swat
- Posts
- 619
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks to all for participating in this thread.
I will test it, as i am away from my pc now.
This is what i am looking for.
I will be back with the results.
-
Oct 8, 2009, 02:55 #7
- Join Date
- Dec 2006
- Location
- /dev/swat
- Posts
- 619
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks Mark Baker and W1LL for the support.
I had tested the code and its working fine.
I am going to post the idea, may be later someone will benefit from it.
PHP Code:if (in_array(1, $drop_list)) { //RG (1)
$query1 = "SELECT * FROM table1 where id='1' and datetime between '$datefrom' and '$dateto' order by datetime desc limit 100";
$res1 = mysql_query($query1);
}else{}
if (in_array(2, $drop_list)) { //RG (2)
$query2 = "SELECT * FROM table2 where id='2' and datetime between '$datefrom' and '$dateto' order by datetime desc limit 100";
$res2 = mysql_query($query2);
}else{}
In the if statement can save the value in session or pass through post/get.
Depends on the requirements.
Have a good day.
Bookmarks