Page 1 of 2
array question
Posted: Thu May 31, 2007 11:14 pm
by ekosoftco
i have a code where im trying to pull each user that says yes to a newsletter field, then pull up to echo each user, so, if jane, jim, and john say yes to newsletter in the database and jerry and sue say no, i use this after pulling up everything,
Code: Select all
$lineArray = explode('$_email', $_email);
$sendto = trim($lineArray[0]);
echo "$sendto";
so that it will display
Jane jim john
now, how do i get it to trim the array, but display them all and not just one?
there are 2 people with yes in the Email field on the database, but
only pulls up one, dont know why its doing that either.
Posted: Thu May 31, 2007 11:22 pm
by feyd
Posted: Thu May 31, 2007 11:23 pm
by John Cartwright
Code: Select all
$lineArray = explode('$_email', $_email);
$lineArray = array_map('trim', $lineArray);
You can use array_map to apply a callback function to an array
Posted: Thu May 31, 2007 11:47 pm
by ekosoftco
ok, this is what i have.
Code: Select all
<?php
echo "<span class=style20>";
//include the header
require("../configlog.php");
$result = mysql_query("SELECT * FROM users") or die(mysql_error());
$row = mysql_fetch_array( $result );
echo "<br><center>";
echo "Forgot your password? Just enter your email into the field below, and we will email you the password.<br><br>";
echo "<form method=post action=newsletter.php?action=email>
Send to:<input type=text name=Email value=" . $row['Email'] . "><br>
<input type=text name=subject>
<input type=text name=message>
<input type=submit value=Continue></form>";
$_email = $row['Email'];
$lineArray = explode('$_email', $_email);
$lineArray = array_map('trim', $lineArray);
echo $lineArray;
?>
and the echo shows "Array"
what am i missing i dont get this.

maybe this isnt the best way to do this, it looks like my array is empty, i tried this too
but it didnt change anything.
if im trying to pull every email from every user who's field value for news =yes, is there a better way to do this?
Posted: Fri Jun 01, 2007 12:15 am
by John Cartwright
You can't echo an array, you either have to loop, using a forech(), for(), while(), etc or convert it to a string using implode()
Posted: Fri Jun 01, 2007 12:19 am
by djot
var_dump($lineArray);
Posted: Fri Jun 01, 2007 12:24 am
by ekosoftco
ok, now with this, it shows one result, and there's more than one...
Code: Select all
<?php
echo "<span class=style20>";
//include the header
require("../configlog.php");
$result = mysql_query("SELECT * FROM users WHERE news='yes'") or die(mysql_error());
$row = mysql_fetch_array( $result );
echo "<br><center>";
echo "Forgot your password? Just enter your email into the field below, and we will email you the password.<br><br>";
echo "<form method=post action=newsletter.php?action=email>
Send to:<input type=text name=Email value=" . $row['Email'] . "><br>
<input type=text name=subject>
<input type=text name=message>
<input type=submit value=Continue></form>";
$_email = $row['Email'];
$lineArray = explode('$_email', $_email);
$lineArray = array_map('trim', $lineArray);
var_dump($lineArray);
anyone see why? i sure dont :/
if there's a better way to approach getting results from a field called news, from each user, and showing all users email that have news field to equal yes, please let me know, please

What im trying to do is make a code for a newsletter, so all users who have the news field to equal yes show up their email, so i know who and where to send the newsletters to.
Posted: Fri Jun 01, 2007 6:53 am
by superdezign
$row is only one row. You'd have to loop in order to get more than one.
Code: Select all
while($row = mysql_fetch_array($result)
{
// Do something with this $row
}
Posted: Fri Jun 01, 2007 9:05 am
by ekosoftco
I only get 1 result, and i should get 2.
Code: Select all
<?php
echo "<span class=style20>";
//include the header
require("../configlog.php");
$result = mysql_query("SELECT * FROM users WHERE news='yes'") or die(mysql_error());
$row = mysql_fetch_array( $result );
$_email = $row['Email'];
echo "<br><center>";
echo "Forgot your password? Just enter your email into the field below, and we will email you the password.<br><br>";
echo "<form method=post action=newsletter.php?action=email>
Send to:<input type=text name=Email value=" . $row['Email'] . "><br>
<input type=text name=subject>
<input type=text name=message>
<input type=submit value=Continue></form>";
while ($row = mysql_fetch_array($result))
{
$lineArray = explode('$_email', $_email);
$lineArray = array_map('trim', $lineArray);
var_dump($lineArray);
}
Posted: Fri Jun 01, 2007 9:06 am
by feyd
You fetch a record and promptly throw most of it away.
Posted: Fri Jun 01, 2007 9:16 am
by ekosoftco
im still learning php, as i go, so i dont see/know what youre saying...
ive researced this alot on google, and got nothing to work,
i tried this too, all i get for echo is "Array"
Code: Select all
$queryresult = mysql_query("SELECT * FROM users WHERE news='yes'");
$total_rows = mysql_num_rows($queryresult);
$i = 0;
while($i < $total_rows){
$data = mysql_fetch_array($queryresult);
$rows[$i] = $data['Email'].$data['aname'];
$i++;
}
echo $rows;
Posted: Fri Jun 01, 2007 9:24 am
by superdezign
Code: Select all
<?php
echo "<span class=style20>";
//include the header
require("../configlog.php");
$result = mysql_query("SELECT * FROM users WHERE news='yes'") or die(mysql_error());
$row = mysql_fetch_array( $result ); // <--- This is pretty much wasted
$_email = $row['Email'];
echo "<br><center>";
echo "Forgot your password? Just enter your email into the field below, and we will email you the password.<br><br>";
echo "<form method=post action=newsletter.php?action=email>
Send to:<input type=text name=Email value=" . $row['Email'] . "><br>
<input type=text name=subject>
<input type=text name=message>
<input type=submit value=Continue></form>";
while ($row = mysql_fetch_array($result))
{
$lineArray = explode('$_email', $_email);
$lineArray = array_map('trim', $lineArray);
var_dump($lineArray);
}
Posted: Fri Jun 01, 2007 9:25 am
by ekosoftco
oopppss, need to use, var_dump();
works now xD
Posted: Fri Jun 01, 2007 9:38 am
by ekosoftco
ok, another question, now i get a result like this
Code: Select all
array(2) { [0]=> string(24) "jason@jasonmcconnell.com" [1]=> string(20) "bratchild_85@msn.com" }
those are 2 emails that are needed to be pulled up. now, is there a way i can pull up just the values, seperated by a comma? i dont think str_replace will work here but i could be wrong
Posted: Fri Jun 01, 2007 9:45 am
by superdezign
var_dump gives you a visual representation of the array. If you don't use var_dump, you can just use the array as usual.