Array only returning one value

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

Post Reply
MikeSpider
Forum Commoner
Posts: 25
Joined: Sat Oct 22, 2011 6:45 pm

Array only returning one value

Post by MikeSpider »

hi guys I need some help with something that might appear really basic:
I have a table in db for registered users with all sorts of info including emails.
I wrote a function to return all emails but I'm having a few problems because it's only returning 1 email.

Code: Select all

 //THIS IS THE FUNCTION INSIDE MY CLASS

public function SelectEmailUser()

{
    try{
        $sql="SELECT  email AS all_emails FROM registration Order BY id ASC";
        $result = $this->db->mysqli->query($sql);

        if(!$result)
        {
                 throw new Exception("Query failed: " . $sql . " - " . $this->db->mysqli->error);

        }else{

        while($mymails = $result->fetch_assoc()){

            $mails = $mymails['all_emails'];
            $this->user_emails = $mails;
            // echo $this->user_emails = $mails;

        }
     }
        } catch(Exception $e){
           echo("Message: " . $e->getMessage());
            }
}


Code: Select all

//THIS IS THE PAGE WHERE i CALL IT
if("Send" == $_POST['submit'])

{
    
     $get_emails_user = new Registration();
    $get_emails_user->SelectEmailUser();
    $userEmails = $get_emails_user->user_emails;
    $allEmails = $adminEmails;
     echo $userEmails;

If I uncomment the echo in the function it gives- me all emails but when I call it on this page
only gives-me 1 email.

Any help I'll be very greatful
Thanks in advance,
Mike
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Array only returning one value

Post by Celauran »

Code: Select all

while($mymails = $result->fetch_assoc()){

            // $mails = $mymails['all_emails']; // unnecessary
            $this->user_emails[] = $mymails['all_emails'];
            // echo $this->user_emails = $mails;
        }
MikeSpider
Forum Commoner
Posts: 25
Joined: Sat Oct 22, 2011 6:45 pm

Re: Array only returning one value

Post by MikeSpider »

Many thanks for your quick reply, Yes! it worked, :oops: I really forgot the [] :lol:
But i had to put a for loop in the page where i want to get the values:

Code: Select all


//THIS IS THE PAGE WHERE i CALL IT
if("Send" == $_POST['submit'])

{
   
     $get_emails_user = new Registration();
    $get_emails_user->SelectEmailUser();
    $userEmails = $get_emails_user->user_emails;

  for($loop = 0; $loop<count($get_emails_user->user_emails);  $loop++){
     echo $get_emails_user->user_emails[$loop];
  }
  
Thanks :D ,
Mike
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Array only returning one value

Post by Celauran »

Code: Select all

$userEmails = $get_emails_user->user_emails;
foreach ($userEmails as $email)
{
    echo $email;
}
MikeSpider
Forum Commoner
Posts: 25
Joined: Sat Oct 22, 2011 6:45 pm

Re: Array only returning one value

Post by MikeSpider »

yes you're right, foreach is quicker ;)

cheers
Post Reply