Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

rhp2424

macrumors regular
Original poster
Jul 23, 2008
122
18
I have a selection drop-down that is a single selection drop-down for a persons name and id. This is used to connect that person to an articles database. From the selection made in the form, I write the data into a relational database connecting the article to the user and I send the selected person an email.

What I need to do is change the drop-down to perform multi-select but also write multiple additions to the database and send out multiple emails as well. So for each name selected in the drop down, I need a new entry and a new email.


Example:

You choose from the following in a drop-down select field with the user id not displayed but posted in the background:
ID | Name
1 | Larry
2 | Tom
3 | Jane
4 | Chris

Currently if you choose Tom, the database grabs the article id (posted from the initial url {save.php?id=$row[0]} and writes into the connection database the article id and user id. From there it also sends an email to Tom to say, in part, "Article 5 has been saved into your account"

What I need to do is function where you if choose Tom, Jane and Chris, it performs a query to write a new record for all names to be connected with the same article and sends all of them their own email. To be clear, the email body can be the same, I just need the "to" portion of the name and email to obviously be the fluid portions of this.

There are some details kept out to save time, but that is the essential facts about what is done and what I need to do. I am certain I know how to do a multi-select, but I could be wrong. Where I need help is the multi-selected data being written in a database and sending out the multiple emails.

I should also note that not more than 5 or 6 names would be choosen. This is all behind a user accessed website only open to registered users.

If I've left anything out that needs clarification, etc, please let me know. I tried to only include relevant code and relevant facts.

Any and all help is greatly appreciated! Thank you!

Relevant Code for Form:
Code:
print "<select size=\"3\" multiple=\"multiple\" name=\"users_id[]\">";
print "<option value='-' >Select from list</option>";
while ($row = mysql_fetch_assoc($result)){
$uid = $row['id'];
$fname = $row['fname'];
$lname = $row['lname'];
print "<option value=$users_id>$fname $lname</option>";
}
print "</select>";

Relevant Code to Write to DB:

Code:
$aid=$_POST['id'];
$uid=$_POST['uid'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];

mysql_query("INSERT INTO table (aid, uid) VALUES ('$aid', '$uid')") ;
 
Last edited:

bpaluzzi

macrumors 6502a
Sep 2, 2010
918
1
London
You'll need to iterate through the posted email addresses.

Something like this should get you started:

Code:
if (is_array($_POST['user_ids']) {
    for ($i = 0, $user_ids_count = count($_POST['user_ids']); $i < $user_ids_count; $i++) {
        //put your database insertion logic here
    }
}
 

frocco

macrumors 6502
Jan 27, 2009
494
43
Just use a foreach
Code:
foreach ($_POST['user_id'] as $selectedOption)
    echo $selectedOption."\n";
 

bpaluzzi

macrumors 6502a
Sep 2, 2010
918
1
London
Just use a foreach
Code:
foreach ($_POST['user_id'] as $selectedOption)
    echo $selectedOption."\n";

Yup, another way to do it. My preference is always to use for on indexed arrays / foreach on associative arrays, but in PHP either will work.
 
Last edited:

rhp2424

macrumors regular
Original poster
Jul 23, 2008
122
18
I'll give these a try tomorrow when I can get back at the script.

Thanks for the help! I'll post back when I either get it working or need clarification within the next couple of days.
 

rhp2424

macrumors regular
Original poster
Jul 23, 2008
122
18
Thank you everyone who contributed to assist me. Because of your help, I was able to mostly achieve success with the foreach. Specifically I can successfully write to the database and email the multiple names selected.

The trouble I am now having is probably an easy one to solve for somebody who understands this stuff better than I do.

I need to list the names with a comma and when the last name is listed, naturally I do not want there to be a comma. For instance, if five name are chosen, the resulting list should read: Tom, Dick, Harry, Jane, Susan

Here is what I used when I wanted a delimiter "|" after all but the last. This came after my query

Code:
$number = mysql_num_rows($subdata);
            $i=1;
            while ($subrow = mysql_fetch_array($subdata,MYSQL_NUM))
            {
                if($i < $number)
                {
                    print "$subrow[4]|";
                }    
                else
                {
                    print "$subrow[4]<br />";
                }
                $i++;        
            }

When I thought I could easily just adapt this to fit my current needs, I literally just adapted it to my current query. I tried both methods of having number count the $uid and having it count the original field of $users_id

Counting the $uid results in nothing being posted, when I count the original $users_id, it counts the total but treats each name being posted as the original and first one being posted. I know why it is doing it, but everything I try to use for the count including moving around and/or excluding the foreach, never gives me my result.

Here is what I have that properly counts the total number but does reset the count with each name. It does not print the first one with a comma and the second one without, it just prints them both with a comma.

Thanks again!

Code:
foreach ($_POST['users_id'] as $uid){
    
    $userresult = mysql_query("SELECT id, first FROM udata WHERE id='$uid'");                   
    
            
            $number = count($users_id); 
print "$number";     
$i=1;
            while ($userrow = mysql_fetch_array($userresult,MYSQL_NUM))
            {            
            $first = $userrow[1];
    
                if($i < $number)
                {
                    print "$i $first,;";
                }    
                else
                {
                    print "&$i $first";
                }
                $i++;        
            }
            
     }
 

rhp2424

macrumors regular
Original poster
Jul 23, 2008
122
18
I figured it out. I ended up finding another post out there that suggested the following:

Code:
$last_key = end(array_keys($array));
foreach ($array as $key => $value) {
    if ($key == $last_key) {
        // last element
    } else {
        // not last element
    }
}

I tweaked and adapted it to fit my needs including displaying with and without commas and the ampersand. Thanks again to everyone who contributed and helped me out! It all very much helped!
 

bpaluzzi

macrumors 6502a
Sep 2, 2010
918
1
London
I figured it out. I ended up finding another post out there that suggested the following:

Code:
$last_key = end(array_keys($array));
foreach ($array as $key => $value) {
    if ($key == $last_key) {
        // last element
    } else {
        // not last element
    }
}

I tweaked and adapted it to fit my needs including displaying with and without commas and the ampersand. Thanks again to everyone who contributed and helped me out! It all very much helped!


Cool -- one other thing that you might want to look at is the "implode" command.

Save your strings to an array, then run "implode" with a comma as the delimiter:
Code:
$example = array(
    'foo',
    'bar',
    'baz'
);

$delimiter = ',';
echo implode($delimiter, $example);

Outputs:
foo, bar, baz

FYI, the opposite function is (as you'd expect), "explode" -- it separates a delimited string into an array.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.