Frederik Posted September 15, 2011 Share Posted September 15, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/247206-problem-with-array/ Share on other sites More sharing options...
WebStyles Posted September 15, 2011 Share Posted September 15, 2011 This line $navn_arr = $mail_arr = array(); is resetting both arrays inside the loop, so basically what you're doing is: 1. reset arrrays 2. add data to arrays 3 reset arrays 4 add data to arrays... etc Quote Link to comment https://forums.phpfreaks.com/topic/247206-problem-with-array/#findComment-1269665 Share on other sites More sharing options...
Frederik Posted September 15, 2011 Author Share Posted September 15, 2011 So what should I do to fix it?? Delete that line? Quote Link to comment https://forums.phpfreaks.com/topic/247206-problem-with-array/#findComment-1269667 Share on other sites More sharing options...
WebStyles Posted September 15, 2011 Share Posted September 15, 2011 initialize your arrays before the while loop, not inside it. Quote Link to comment https://forums.phpfreaks.com/topic/247206-problem-with-array/#findComment-1269669 Share on other sites More sharing options...
Frederik Posted September 15, 2011 Author Share Posted September 15, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/247206-problem-with-array/#findComment-1269676 Share on other sites More sharing options...
jcbones Posted September 15, 2011 Share Posted September 15, 2011 print it out, and see what it looks like: echo '<pre>' . print_r($mail_arr,true) . '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/247206-problem-with-array/#findComment-1269685 Share on other sites More sharing options...
WebStyles Posted September 15, 2011 Share Posted September 15, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/247206-problem-with-array/#findComment-1269689 Share on other sites More sharing options...
Frederik Posted September 16, 2011 Author Share Posted September 16, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/247206-problem-with-array/#findComment-1269832 Share on other sites More sharing options...
Frederik Posted September 17, 2011 Author Share Posted September 17, 2011 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?? Quote Link to comment https://forums.phpfreaks.com/topic/247206-problem-with-array/#findComment-1270236 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.