0

I'm working on a way to add recipients from a contact list to an array so that that the contacts of the array can be used as recipients in message. I understand arrays well enough for basic use but have a vexing issue. My list of contacts are dynamically generated in a list with this;

{
$contact = $row['contact'];
echo "<tr>";
echo "<td><font color=#808080><a href='mypagepost.php?contact=$contact' STYLE='TEXT-  DECORATION: NONE'><font color=#808080>" . $row['contact'] . "</a></font></td>";
echo "</tr>";
}

So, each contact is variable $contact. When you click on the contact, it takes you to mypagepost.php where this happens;

$messagerecipient = array();
$contact = $_GET['contact'];
$messagerecipient[] = "$contact";

I can print out the contents of the array, no problem. However, every time I add a new contact, it overwrite the contact in [0]. If I manually add new contacts with subsequest $messagerecipient entries, like;

 $messagerecipient[] = "Confused";
 $messagerecipient[] = "Oh, man";

it increments them just fine; Array ( [0] => csmith [1] => Confused [2] => Oh, man )

But [0] will always be overwritten when I select a new contact. I thought that maybe I needed to make the array into a session, but it does the same thing. Can someone recommend a way to do this? I need each contact added to the array to increment (and, ultimately, I'm going to be setting something up that will allow me to remove entries from the array). Any help is appreciated.

1
  • Well with session you're actually on the right track, because it does not overwrite, it just adds. Before that the array is just empty because the script is invoked a new time so all variables are empty (better: not set) by default.
    – hakre
    Commented Aug 25, 2013 at 21:10

2 Answers 2

2

Make a Session variable, say $_SESSION['messagerecipient']; and then push the values to this variable, using array_push

Eg: array session variables

8
  • It does the same thing. Like this? $messagerecipient = array(); $contact = $_GET['contact']; $_SESSION['recipientlist'] = $messagerecipient; array_push($_SESSION['recipientlist'], $contact);
    – Kimomaru
    Commented Aug 25, 2013 at 21:19
  • 1
    Declare the session variable at the start of your application, not again and again. that's the reason the values are not appended. Can you post your try with the session variables Commented Aug 25, 2013 at 21:21
  • $_SESSION['recipientlist'] = $messagerecipient; is at the top of my application on my second form. Are you saying it should be on my first form?
    – Kimomaru
    Commented Aug 25, 2013 at 21:28
  • 1
    Tou can use the array_unique just after the addition in order to remove any duplicates. Syntax: array_unique($myarray); Commented Aug 25, 2013 at 21:54
  • 1
    Its certainly not possible. Read about array_unique here: php.net/manual/en/function.array-unique.php Commented Aug 25, 2013 at 22:02
2

This line: $messagerecipient = array(); is setting the variable to an empty array each time before adding the contact. $_SESSION is the right idea here, but I'm guessing you replaced that line with one creating an empty array in $_SESSION, which would cause the same problem. You'll want to check if your session variable is set first - if not, then create it. This would occur the first time you select a recipient; subsequent times should merely add to the existing via $_SESSION['recipient'][] = $contact (or array_push, as mentioned by Shadowfax - these are equivalent calls). Otherwise every time you'll just be resetting it to empty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.