Zend Php Foreach Loop array

626

I have a input field and a array of email from DB table.

I am comparing the similarity of the input field to the array.

But some how, I am stuck with the loop.

Does that loop check compare each email with the input field?

It always bring me to google.com no matter what i input same or not

Here's the code from the controller:

if (isset($_POST['btn_free_download']))
                {
                    // Get current input email from input field
                    $email = $this->getRequest()->getParam('email');
                    // Get all emails from the user
                    $referred_users = $this->_helper->user()->getReferredUsers()->toArray();
                    // Check the similarity which it with a loop
                    foreach ($referred_users as $referred_user)
                    {
                       similar_text($email, $referred_user['email'], $similar);
                    }
                    // If yes, Pop up message or redirect to some page
                    if ($similar < 97)
                    {
                        $this->_redirect('http://google.com');
                    }
                    // If not, redirect user to free download page
                    else 
                    {
                        $this->_redirect('http://yahoo.com');
                    }
 }
377

Answer

Solution:

I think you need to check the manual . Foreach function is same wether you use it on zend or any other framework or raw php only.

$referred_users = $this->_helper->user()->getReferredUsers()->toArray();

$referred_users will probably hold an array of emails from the table user, say:

$referred_users = array("[email protected]", "[email protected]", "[email protected]")

then when you use foreach loop it will iterate through each of the emails in the array

foreach ($referred_users as $referred_user)
{
   // for the first loop $referred_user = [email protected], for second time $referred_user = [email protected] and goes on
    similar_text($email, $referred_user['email'], $similar);
}

Now let us discuss your logic here:

                   // If yes, Pop up message or redirect to some page
                    if ($similar < 97)
                    {
                        $this->_redirect('http://google.com');
                    }
                    // If not, redirect user to free download page
                    else 
                    {
                        $this->_redirect('http://yahoo.com');
                    }

Until and unless the last element in the array $referred_users is exactly equal to your $email

i.e. $email = "[email protected]"

you will always be given result for $similar less than 97% which means you will be redirected to google.

Which I assume you are not trying to do and probably not familiar with foreach function which is why you are not getting the expected result.

Assuming you are trying to do something like, check for the emails in the array if any of the email in the array matches (if array is from table check if the email entered from param is equal to any of the emails in the table) then redirect to somewhere or show some message else carry on. Solution below might be helpful to you.

   $similarText = false;
   foreach ($referred_users as $referred_user)
   {
       // for the first loop $referred_user = [email protected], for second time $referred_user = [email protected] and goes on
        similar_text($email, $referred_user['email'], $similar);
        if ($similar > 97) {
            $similarText = true;
            break;
        }
    }


                   // If yes, Pop up message or redirect to some page
                    if ($similarText)
                    {
                        $this->_redirect('http://google.com');
                    }
                    // If not, redirect user to free download page
                    else 
                    {
                        $this->_redirect('http://yahoo.com');
                    }

Hope you got the idea. But please do check the manual before posting a question in the future.

People are also looking for solutions to the problem: php - PHPGraphLib scientific notation Issue

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.