Jump to content

Using the good old in_array() function. But...


theITvideos

Recommended Posts

Hi there, I am currently using the in_array() function to check the user's country with the country in the ShippingProfile table.

 

I have a function that returns the shipment profile info from the table.

 

$myshipProfile = filter_shippingprofile(array(......)); 

 

And in the $myshipProfile I get the profile info with the countryID. Now I can check the user's country in the array:

 

 if(in_array($_SESSION['userCountryId'], $myshipProfile))

      {

        print "yes country is found";

      } 

 

I works fine so far. But there is a slight problem to it.

 

I did a quick:

 

print_r($myshipProfile); 

 

And the output was:

 

Array ( [shippingprofileID] => 14 [supplierId] => 66 [shippingprofilename] => ProfName1 [shippingto] => CustomCountries [shipRegionCountries] => 66 [shippingcost] => 10

 

Notice the supplierId and the ShipRegionCountries has the same value. And now when I try to perform in_array() it will return true every time even if the ShipRegionCountries is not 66 because as supplierID is 66.

 

You see the conflict.

 

Therefore, can we write like this:

 

 if(in_array($_SESSION['userCountryId'], $myshipProfile['ShipRegionCountries']))

                    {

                        print "yes country is found";

                    } 

 

 

I get no ouput for this. Whats the correct way to check only the 'ShipRegionCountries' in the array?

 

Thank you :)

Link to comment
Share on other sites

Thanks for the reply.

 

Thanks exactly my point. using

 

if($_SESSION['userCountryId'] == $myshipProfile['ShipRegionCountries']))
{
    echo 'Yes, Country is found';
}

 

would be an easy solution but not exactly what we want.

 

Because one Product can have multiple Shipping Profiles attached to it and we need to loop through those and see which destination country in one of those profiles matches with the customer's country.

 

Remember:

One product may have many Shipping Profiles.

A Shipping profile can have many destination countries in it.

 

I have the user's country in the session variable i.e

$_SESSION['userCountryId']

 

And Shipping Profile info is stored in:

$myshipProfile array.

 

What looping do we use here, or how to get the matching destination country from the list of Shipping profiles.

Link to comment
Share on other sites

I'm confused.

 

have a function that returns the shipment profile info from the table.

 

$myshipProfile = filter_shippingprofile(array(......));

What does the function filter_shippingprofile() do and what is array(.....)

 

Where is this code

 if(in_array($_SESSION['userCountryId'], $myshipProfile))

      {

        print "yes country is found";

      } 

Being executed? Is it within a loop?

 

You may need to post more code.

 

 

Link to comment
Share on other sites

I'm confused.

 

have a function that returns the shipment profile info from the table.

 

$myshipProfile = filter_shippingprofile(array(......));

What does the function filter_shippingprofile() do and what is array(.....)

 

Where is this code

 if(in_array($_SESSION['userCountryId'], $myshipProfile))

      {

        print "yes country is found";

      } 

Being executed? Is it within a loop?

 

You may need to post more code.

 

Ok the code for getting profileinfo is:

 

$myshipProfile   = filter_shippingprofile(array('shippingprofileID' => $product['shippingprofileID'],'enabled' => 1));

 

Where shippingprofileID is the field in the ShippingProfile Table and enabled field indicates if the profile is active or not.

 

Now when we do print_r() on the $myshipProfile variable we get:

 

print_r($myshipProfile); 

 

We get all this:

 

Array ( [shippingprofileID] => 14 [supplierId] => 45 [shippingprofilename] => MyProfileName1 [shippingto] => CustomCountries [shipRegionCountries] => |24|37|45| [handlingtime] => 10 business day [flatrateoptions] => 0 [shipmentweight] => 1 [shippingcost] => 10.00 [enabled] => 1 )

 

the field 'ShipRegionCountries' in this array has the country id. Now we need to loop or compare user's country ID (which is stored in the session variable) with the 'ShipRegionCountries'. But we might need to split the |24|37|45| before we can start comparing.

 

So how do we split and then compare.

 

How do we structure the loop.

 

Thank you. Please read it one more time you will understand my question. Thank you :)

 

 

 

Link to comment
Share on other sites

Array ( [shippingprofileID] => 14 [supplierId] => 45 [shippingprofilename] => MyProfileName1 [shippingto] => CustomCountries [shipRegionCountries] => |24|37|45| [handlingtime] => 10 business day [flatrateoptions] => 0 [shipmentweight] => 1 [shippingcost] => 10.00 [enabled] => 1 )

 

the field 'ShipRegionCountries' in this array has the country id. Now we need to loop or compare user's country ID (which is stored in the session variable) with the 'ShipRegionCountries'. But we might need to split the |24|37|45| before we can start comparing.

We never knew you where separating the country ids with a delimiter. So you'd use

if(in_array($_SESSION['userCountryId'], explode('|', $myshipProfile['ShipRegionCountries'])))
{
    print "yes country is found";
} 

explode will convert the list of ShipRegionCountries ids into an array. Now we can use in_array to see if the customers country id is within that list.

Link to comment
Share on other sites

Array ( [shippingprofileID] => 14 [supplierId] => 45 [shippingprofilename] => MyProfileName1 [shippingto] => CustomCountries [shipRegionCountries] => |24|37|45| [handlingtime] => 10 business day [flatrateoptions] => 0 [shipmentweight] => 1 [shippingcost] => 10.00 [enabled] => 1 )

 

the field 'ShipRegionCountries' in this array has the country id. Now we need to loop or compare user's country ID (which is stored in the session variable) with the 'ShipRegionCountries'. But we might need to split the |24|37|45| before we can start comparing.

We never knew you where separating the country ids with a delimiter. So you'd use

if(in_array($_SESSION['userCountryId'], explode('|', $myshipProfile['ShipRegionCountries'])))
{
    print "yes country is found";
} 

 

 

Thanks the code

 

if(in_array($_SESSION['userCountryId'], explode('|', $myshipProfile['ShipRegionCountries'])))
{
    print "yes country is found";
} 

 

is working. But before we do this we first have a list of profile attached to one product. And we might need to loop through each of the 'shippingprofileID' (not the ShipRegionCountries) .

 

Please see the table structure for clear understanding.

 


//This function gives me the product info an an array.
$product = filter_products(array('productId' => $productId, 'enabled' => 1)); 

//checking if there is a shippingprofileID value in products table 
if(!empty($product['shippingprofileID']))

// $product['shippingprofileID'] has values |14|15|16|
                    {   
//Now looping through these values to see if these shippingprofileIDs have a country matching with user's country                 
                        foreach($product['shippingprofileID'] as $sProfile)
                        {             
                            if(in_array($_SESSION['userCountryId'], explode('|', $sProfile['shippingprofileID'])))
                            {
                                print "yes country is found";
                            }                          
                        }
                    }

 

I think the coding is not correct. Kindly see the 2 screenshots with this email and it will be very clear as to what I am trying to accomplish.

 

enclosures:

 

Link to comment
Share on other sites

Array ( [shippingprofileID] => 14 [supplierId] => 45 [shippingprofilename] => MyProfileName1 [shippingto] => CustomCountries [shipRegionCountries] => |24|37|45| [handlingtime] => 10 business day [flatrateoptions] => 0 [shipmentweight] => 1 [shippingcost] => 10.00 [enabled] => 1 )

 

the field 'ShipRegionCountries' in this array has the country id. Now we need to loop or compare user's country ID (which is stored in the session variable) with the 'ShipRegionCountries'. But we might need to split the |24|37|45| before we can start comparing.

 

Sorry Here is the proper Re-post with proper attachment :)

 

Thanks for the reply. This does work and gives me the ShippingProfile country matching with the user's country.

 

But before we do that, we FIRST need to go through each ShippingProfile ID in Product table: (See the attachment ProductTable Fig1.png)

 

 $product = filter_products(array('productId' => $productId, 'enabled' => 1));

 

this returns an array and when we write

 

print_r($product['shippingprofileID']);  

 

it gives us the Shipping ProfileIDs |14|15|16| which is good.

 

Now, the main part, we need to go through (or loop) these IDs to see which which ID has the corresponding country matching with user's country.

 

I wrote:

 

 
//first checking if there is a value of ShippingProfile ID in the products table
if(!empty($product['shippingprofileID']))
                    {  
                       //$product['shippingprofileID'] has values: |14|15|16| 
                        foreach($product['shippingprofileID'] as $sProfile)
                        {
             //now we are looping and checking each ID
                            if(in_array($_SESSION['userCountryId'], explode('|', $sProfile['shippingprofileID'])))
                            {
                                print "yes country is found";
                            }                          
                        }
                    } 

 

I am sure there is some missing code here. Please see the 2 screenshot and it will be very clear as to what I am trying to accomplish here.

 

Enclosures:

ProductTable Fig1.png

ShipingTable Fig2.png

 

[attachment deleted by admin]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.