Jump to content

Problem with array.


Frederik

Recommended Posts

Hello

 

 

I have a problem with an array. I get this result:

Array ( [0] => name1 ) Array ( [0] => mail1@mail.com ) Array ( [0] => name2 ) Array ( [0] => mail2@mail.com ) Array ( [0] => name3 ) Array ( [0] => mail3@mail.com ) Sent 1 messages

 

 

where it should be something like this:

Array ( [0] => name1 ) Array ( [0] => mail1@mail.com ) Array ( [1] => name2 ) Array ( [1] => mail2@mail.com ) Array ( [2] => name3 ) Array ( [2] => mail3@mail.com ) 

 

And then it will hopefully send 3 messages.

 

 

My code is:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<?php
if (isset($_SESSION['login'])){
if($_SESSION['login'] == true){
}
?>

<html lang="da">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>title</title>
</head>
<body>



<?php
require("config.php");

if(!isset($_POST['theid'])){
echo "Intet ID er valgt. Prøv venligst igen.";

} else {

$ids = join(',',$_POST['theid']);

$emne = $_POST['emne'];
$besked = $_POST['besked'];
$besked = nl2br($besked);

mysql_connect($mysql_host, $mysql_user, $mysql_pw);
mysql_select_db($mysql_db);
$sql = "SELECT * FROM database WHERE id IN (" . $ids . ")";
$query = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($query) == 0){
    echo ("Ingen elementer er valgt.");
exit;

  } else {

while($row = mysql_fetch_assoc($query)) {

    $to_str = "";
    $navn_arr = $mail_arr = array();

  
      $navn = $row['fornavn'] . " " . $row['efternavn'];
      $mail = $row['email'];

      $to_str .= ", $navn <$mail>";
      

      $navn_arr [] = $navn;
      $mail_arr [] = $mail;
print_r ($navn_arr);
print_r ($mail_arr);

}
}

require_once 'lib/swift_required.php';

//Create the Transport
$transport = Swift_SmtpTransport::newInstance('domain', 25);

//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

//Create a message
$message = Swift_Message::newInstance($emne)
  ->setFrom(array('email...test' => 'Test'))
  ->setBody(
'<html>' .
'<head></head>' .
'<body>' .
$besked .
'<br>' .
'<img src="http://www.test.test/mail.jpg" />' .
'</body>' .
'</html>',
'text/html');

//Send the message
$failedRecipients = array();
$numSent = 0;

$to = array_combine($mail_arr, $navn_arr);

foreach ($to as $address => $name)
{
  $message->setTo(array($address => $name));
  $numSent += $mailer->send($message, $failedRecipients);
}

printf("Sent %d messages\n", $numSent);


exit;
}
?>





<?
} else {

echo("Denne side kræver adminstrator rettigheder.");

}
?>
</body>
</html>

 

 

 

Thanks in advance!

 

 

- Frederik

Link to comment
Share on other sites

I'm not that familiar with PHP, so it would be great if you could give an example.

 

Should it be something like:

 

  } else {
$to_str = "";
$navn_arr = $mail_arr = array(); 
while($row = mysql_fetch_assoc($query)) {


      $navn = $row['fornavn'] . " " . $row['efternavn'];
      $mail = $row['email'];
      $navn_arr [] = $navn;
      $mail_arr [] = $mail;
      $to_str .= ", $navn <$mail>";

 

Or is that wrong?

Link to comment
Share on other sites

I'm not that familiar with PHP, so it would be great if you could give an example.

I'd rather explain again than give you the code.

 

This is your original code: (just the while loop)

 

while($row = mysql_fetch_assoc($query)) {
    $to_str = "";
    $navn_arr = $mail_arr = array();
    $navn = $row['fornavn'] . " " . $row['efternavn'];
    $mail = $row['email'];
    $to_str .= ", $navn <$mail>";
    $navn_arr [] = $navn;
    $mail_arr [] = $mail;
print_r ($navn_arr);
print_r ($mail_arr);
}

 

this line:

$navn_arr = $mail_arr = array();

is the same as doing this

$navn_arr = array();
$mail_arr = array();

and it's just saying "make these two variables be empty arrays"

 

at the bottom, when you do this:

$navn_arr[] = $navn;
$mail_arr[] = $mail;

you're adding stuff to each of those arrays.

 

As I said before, since everything is inside a while loop, you're starting with an empty array, then adding stuff, then emptying it again (because the while loop repeats all the code inside the curly braces {} as many times as necessary), so you en up just having the last line from the database stored in those arrays. Since you also have both print_r() statements inside the loop, it will print every line from the database for you (but will only store the last)

what you want is to initialize the arrays before the loop.

 

Hope this helps

Link to comment
Share on other sites

I have tried these combinations:

 

else {
$to_str = "";
$navn_arr = $mail_arr = array(); 
while($row = mysql_fetch_assoc($query)) {

      $navn = $row['fornavn'] . " " . $row['efternavn'];
      $mail = $row['email'];

     $to_str .= ", $navn <$mail>";
      


print_r ($navn_arr);
print_r ($mail_arr);

}

 

This gives the error:

Array ( ) Array ( ) Array ( ) Array ( ) Warning: array_combine(): Both parameters should have at least 1 element in ........

 

 

else {
$to_str = "";
$navn_arr [] = $navn;
$mail_arr [] = $mail;
print_r ($navn_arr);
print_r ($mail_arr);
while($row = mysql_fetch_assoc($query)) {

      $navn = $row['fornavn'] . " " . $row['efternavn'];
      $mail = $row['email'];

     $to_str .= ", $navn <$mail>";
}

This gives the error:

Array ( [0] => ) Array ( [0] => ) Fatal error:........

 

 

 

I'm not sure how to fix this?

 

Link to comment
Share on other sites

I have moved the Print_r outside the while loop.. The code is now:

 

else {
$to_str = "";
$navn_arr = $mail_arr = array(); 
while($row = mysql_fetch_assoc($query)) {

$navn = $row['fornavn'] . " " . $row['efternavn'];
$mail = $row['email'];
$navn_arr [] = $navn;
$mail_arr [] = $mail;
      $to_str .= ", $navn <$mail>";
}
print_r ($navn_arr);
print_r ($mail_arr);

 

and I get the result:

Array ( [0] => Name1 [1] => name2 [2] => name3 ) Array ( [0] => mail1@mail.com [1] => mail2@mail.com [2] =>mail3@mail.com )

 

 

As I see it, the code returns the correct code, but am I wrong about that??

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.