array question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

ekosoftco
Forum Contributor
Posts: 108
Joined: Fri Aug 04, 2006 8:21 pm

array question

Post 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

Code: Select all

$row['Email']
only pulls up one, dont know why its doing that either.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
ekosoftco
Forum Contributor
Posts: 108
Joined: Fri Aug 04, 2006 8:21 pm

Post 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>
&nbsp;&nbsp;&nbsp;<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

Code: Select all

$_email = array();
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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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()
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

var_dump($lineArray);
ekosoftco
Forum Contributor
Posts: 108
Joined: Fri Aug 04, 2006 8:21 pm

Post 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> 
&nbsp;&nbsp;&nbsp;<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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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
}
ekosoftco
Forum Contributor
Posts: 108
Joined: Fri Aug 04, 2006 8:21 pm

Post 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> 
&nbsp;&nbsp;&nbsp;<input type=submit value=Continue></form>"; 
while ($row = mysql_fetch_array($result))
{ 
$lineArray = explode('$_email', $_email); 
$lineArray = array_map('trim', $lineArray); 
var_dump($lineArray);
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You fetch a record and promptly throw most of it away.
ekosoftco
Forum Contributor
Posts: 108
Joined: Fri Aug 04, 2006 8:21 pm

Post 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;
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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> 
&nbsp;&nbsp;&nbsp;<input type=submit value=Continue></form>"; 
while ($row = mysql_fetch_array($result))
{ 
$lineArray = explode('$_email', $_email); 
$lineArray = array_map('trim', $lineArray); 
var_dump($lineArray);
}
ekosoftco
Forum Contributor
Posts: 108
Joined: Fri Aug 04, 2006 8:21 pm

Post by ekosoftco »

oopppss, need to use, var_dump();
works now xD
ekosoftco
Forum Contributor
Posts: 108
Joined: Fri Aug 04, 2006 8:21 pm

Post 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
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
Post Reply