theITvideos Posted August 30, 2010 Share Posted August 30, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/212067-using-the-good-old-in_array-function-but/ Share on other sites More sharing options...
wildteen88 Posted August 30, 2010 Share Posted August 30, 2010 You wouldn't want to use in_array. You'll just want to do a simple variable comparison if($_SESSION['userCountryId'] == $myshipProfile['ShipRegionCountries'])) { echo 'Yes, Country is found'; } Quote Link to comment https://forums.phpfreaks.com/topic/212067-using-the-good-old-in_array-function-but/#findComment-1105195 Share on other sites More sharing options...
theITvideos Posted August 30, 2010 Author Share Posted August 30, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/212067-using-the-good-old-in_array-function-but/#findComment-1105252 Share on other sites More sharing options...
AbraCadaver Posted August 30, 2010 Share Posted August 30, 2010 Show us an example array of the multiple shipping profiles. Quote Link to comment https://forums.phpfreaks.com/topic/212067-using-the-good-old-in_array-function-but/#findComment-1105255 Share on other sites More sharing options...
wildteen88 Posted August 30, 2010 Share Posted August 30, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/212067-using-the-good-old-in_array-function-but/#findComment-1105257 Share on other sites More sharing options...
theITvideos Posted August 30, 2010 Author Share Posted August 30, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/212067-using-the-good-old-in_array-function-but/#findComment-1105267 Share on other sites More sharing options...
wildteen88 Posted August 30, 2010 Share Posted August 30, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/212067-using-the-good-old-in_array-function-but/#findComment-1105268 Share on other sites More sharing options...
theITvideos Posted August 31, 2010 Author Share Posted August 31, 2010 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: Quote Link to comment https://forums.phpfreaks.com/topic/212067-using-the-good-old-in_array-function-but/#findComment-1105503 Share on other sites More sharing options...
theITvideos Posted August 31, 2010 Author Share Posted August 31, 2010 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] Quote Link to comment https://forums.phpfreaks.com/topic/212067-using-the-good-old-in_array-function-but/#findComment-1105509 Share on other sites More sharing options...
JasonLewis Posted August 31, 2010 Share Posted August 31, 2010 Surely there is a better way to design your tables so you don't need to do all this mucking around. Quote Link to comment https://forums.phpfreaks.com/topic/212067-using-the-good-old-in_array-function-but/#findComment-1105514 Share on other sites More sharing options...
theITvideos Posted August 31, 2010 Author Share Posted August 31, 2010 Yes but now I am in a situation. Please see the 2 screenshots and what missing ingredients do we need to add in the foreach loop. Kindly see it. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/212067-using-the-good-old-in_array-function-but/#findComment-1105518 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.