SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru mmarif4u's Avatar
    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>
    This part is working fine.
    I am getting the values as:

    PHP Code:
    $drop $_POST['drop1'];
    foreach(
    $drop as $listbox){
    echo 
    $listbox;

    Now lets say, user selected three choices from listbox, and post holds ids like 1,2,3.
    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.

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mmarif4u View Post
    Now lets say, user selected three choices from listbox, and post holds ids like 1,2,3.
    So what i want to do is split the values and use it outside foreach loop.
    I tried exlpode function but in vain.
    $_POST['drop1'] already is an array, so each selected value is already a separate entry in that array. Don't understand what you mean by "split the values"? What do you actually want to achieve?
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  3. #3
    SitePoint Guru mmarif4u's Avatar
    Join Date
    Dec 2006
    Location
    /dev/swat
    Posts
    619
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mark Baker View Post
    $_POST['drop1'] already is an array, so each selected value is already a separate entry in that array. Don't understand what you mean by "split the values"? What do you actually want to achieve?
    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'

    All these values should be outside of the loop to use globally.

    Hope, i am clear.
    If any questions, please ask.

  4. #4
    SitePoint Evangelist
    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]

    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  5. #5
    PHP Developer W1LL's Avatar
    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


  6. #6
    SitePoint Guru mmarif4u's Avatar
    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.

  7. #7
    SitePoint Guru mmarif4u's Avatar
    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{} 
    As you can see, i am using multiple if statements, is because i need all values, but if some one want to use just some value, can use elseif.
    In the if statement can save the value in session or pass through post/get.
    Depends on the requirements.

    Have a good day.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •