Jump to content

Recommended Posts

Hi,

 

I am totaly new to PHP.

 

I do need to test the availability of a product in a basket via an array of the product ids.

This code works :

$match_products = array(9,10,11);

$cartClass = hikashop_get('class.cart');
$cart = $cartClass->loadFullCart();
foreach($cart->products as $product){
  if(in_array($product->product_id, $match_products))
   {
      return true;
   }
return false;
}

but I need to test 2 differents array and have to be sure that both of them are true in order to get a global true value, for all other situations I need a false global value.

 

Second array would be array(3,4,5,6,7,12).

 

the logical shema would be

 

test1 = product in array1 : true/false

test2 = product in array 2 : true/false

Global test : test1=true AND test2=true than global=true else global=false

 

How can I do this ? Maybe my question is a bit silly for some of you, sorry about that, but as said, my PHP knowledge is very limited.

 

Thanks in advance for your help and advise,

Best regards

Steph

I tought about this code to combine the two tests :

//test 1
$match_products = array(9,10,11);

$cartClass = hikashop_get('class.cart');
$cart = $cartClass->loadFullCart();
foreach($cart->products as $product){
  if(in_array($product->product_id, $match_products)) 
      return $result = false;
   }
return $result = true;

//test2
$match_product1 = array(3,4,5,6,7,12);

$cartClass = hikashop_get('class.cart');
$cart = $cartClass->loadFullCart();
foreach($cart->products as $product){
  if(in_array($product->product_id, $match_product1)) 
      return $result1 = false;
   }
return $result1 = true;

//test1 AND 2
if ($result = true && $result1 = true);
	return true;
 return false;

but I am really not sure about it

 

As said the aim of it is to test if products (by their id - 9,10,11) are within an array.

But the customer has to add a product in step one into the basket that's tested by the //test1 and working.

Now on step 2 the customer has to add a second product from within the second array (ids 3,4,5,6,7,12)

 

In order to be sure the customer hasn't removed a product from step one I have to check again both arrays and both have to be true.

 

I use this PHP with the Joomla! extention ReReplacer from RegularLabs.

For this extension I need a global true or false state

 

I don't know if it is more clear now.

Thanks for your help and advise,

Best regards

Steph

Edited by steph3838

You seem to have missed the whole XY Problem thing. I don't care about your attempted solution. What is the overall thing that is to be accomplished. What is the REAL problem you are trying to solve, not how you are trying to solve it. Your code and arrays are completely meaningless to my question.

Edited by benanamen

You seem to have missed the whole XY Problem thing. I don't care about your attempted solution. What is the overall thing that is to be accomplished. What is the REAL problem you are trying to solve, not how you are trying to solve it. Your code and arrays are completely meaningless to my question.

Dear Benanamen,

 

What I am trying to accomplish, and believe me, the logic of it is the right one, is to combine the result of two tests having as result true or false in one global true/false result where (test1=true AND test2=true) leads to a global=true else global=false.

 

But where I really need help is in the PHP syntax in which I am really not comfident and where I lak of experience and knowledge. Setting up the right test took about a week with Hikashop and Regularlabs people.

So please consider the logical point of my question solved.

 

The global test should be better this way :

Missing in the syntax are also closing } for each test1 and test2...

//test1 AND 2
if ($result = true && $result1 = true) {
	return true;
} else {
 return false;
}

thanks for your help and advise

best regards

Steph

Edited by steph3838

I give up. You cannot even answer a simple question.

 

and believe me, the logic of it is the right one

 

And no, I don't believe you. My 20+ years as a Software Engineer tell me that whatever you're trying to accomplish, your doing it wrong. Good luck!

Edited by benanamen

Global code would be :

$match_products = array(9,10,11);

$cartClass = hikashop_get('class.cart');
$cart = $cartClass->loadFullCart();
foreach($cart->products as $product){
  if(in_array($product->product_id, $match_products)) 
      return $result = false;
   }
return $result = true;
}

//test2
$match_product1 = array(3,4,5,6,7,12);

$cartClass = hikashop_get('class.cart');
$cart = $cartClass->loadFullCart();
foreach($cart->products as $product){
  if(in_array($product->product_id, $match_product1)) 
      return $result1 = false;
   }
return $result1 = true;
}

//test1 AND 2
if ($result = true && $result1 = true);
{
	return true;
} else { 
return false;
}

test 1 like this works :

$match_products = array(9,10,11);

$cartClass = hikashop_get('class.cart');
$cart = $cartClass->loadFullCart();
foreach($cart->products as $product){
  if(in_array($product->product_id, $match_products))
   {
      return true;
   }
return false;
}

test 2 like this works :

$match_product1 = array(3,4,5,6,7,12);

$cartClass = hikashop_get('class.cart');
$cart = $cartClass->loadFullCart();
foreach($cart->products as $product){
  if(in_array($product->product_id, $match_product1))
   {
      return true;
   }
return false;
}

But when combined in the same code I do not know if the syntax is right or not (have I missed a ; , a } etc...  maybe I should not put a return before the $result1 = true etc.) that's my question

Thanks in advance for your help,

Best regards

Steph

I give up. You cannot even answer a simple question.

 

 

And no, I don't believe you. My 20+ years as a Software Engineer tell me that whatever you're trying to accomplish, your doing it wrong. Good luck!

You know dear Benanamen, I am teaching on other subject for about 30+ years, and a good teacher is the one who can place itself at the same level than it's students...

I cannot explain again a whole week of discussion between me and 2 other devloppers just to answer your XY question...

 

Teaching = helping people is fisrt set yourself at their level and not reverse.

 

As said my question is just a syntaxic question not a logical question, you beleve it or not is not the point.

 

But as you said give up if you want, I do not force anybody, but hope you will think about my statments...

 

I am not upset at all, just not surprised by your answer.

 

have a nice evening,

Steph

Is this what you mean?
 

$group1 = [3,4,5,6,7,12];
$group2 = [9,10,11];

$cart_prods = [6,10];
echo checkCart($cart_prods, $group1, $group2) ? 'OK' : 'ERROR';

function checkCart($prods, $g1, $g2)
{
    if (array_intersect($prods, $g1)) {
        return array_intersect($prods, $g2);
    }
    return false;
}



 

Is this what you mean?

 

$group1 = [3,4,5,6,7,12];
$group2 = [9,10,11];

$cart_prods = [6,10];
echo checkCart($cart_prods, $group1, $group2) ? 'OK' : 'ERROR';

function checkCart($prods, $g1, $g2)
{
    if (array_intersect($prods, $g1)) {
        return array_intersect($prods, $g2);
    }
    return false;
}



My PHP knwoledge is, as said, very low. But if I understand right what you are writting

 

You set 2 groups (arrays ?) and comparre if the cart content is in those groups.

but then you are using an unknown function to me array_intersect, but what I could read about it is that it compares the two arrays $prods and $g1 , but then I am lost with the rest...

 

what I need is that the cart has to contain a product from $group1 AND $group2 in order to deliver a true state for anything eslse the false state has to be delivered.

 

Many thanks in advance for your help and advise,

Best regards

Steph

 

this is the pseudocode for the function

if any id is in both cart and group1

    if any id is in both cart and group 2
        return true
    else
        return false
    end if

else
    return false
end if

Many thanks Barand !

 

I like your pseudocode explaination, I understood right away what you're doing !

Great, I will try to convert, it into PHP with my givens and let you know if I have some problems

 

Again thanks for your help !

Best regards

Stéphane

could this be the right code then ?

$match_product1 = array(9,10,11)
$match_product2 = array(3,4,5,6,7,12);

$cartClass = hikashop_get('class.cart');
$cart = $cartClass->loadFullCart();
foreach($cart->products as $product){
  if (array_intersect($product->product_id, $match_product1)) 
   {
      return (array_intersect($product->product_id, $match_product2))
   }
return false;
}

Thanks in advance for your help and advise,

Best regards,

Stéphane

Edited by steph3838

A couple of things wrong with that code.

 

  • With array_intersect() there is no need for the foreach loop, is checks them all in one go. - See my function
  • "return" is used to return the result of  a function. Your return is not inside a function.

Thanks Barand !

 

two points :

  1. return : you're right...
  2. How can I set a function in order to select from
    $cart
    the
    product
    and then the
    product_id
    ? I have not understood the "function logic" apparently...

    It is more logical to me with the foreach... the code could then be :
    $match_product2 = array(3,4,5,6,7,12);
    $match_product1 = array(9,10,11)
    
    $cartClass = hikashop_get('class.cart');
    $cart = $cartClass->loadFullCart();
    foreach($cart->products as $product){
      if (in_array($product->product_id, $match_product2)) 
       {
          if (in_array($product->product_id, $match_product1)) 
       }
    return false;
    }
    

    If I keep the foreach... I have problems to understand the true or false statement :
    Normaly a true state is declared after an if and a false after an else (or reverse depending on the logic used)

    If I try to have describe what I have, it would be :

    if (in_array($product->product_id, $match_product2)) 
       {
       (true state of first if   if (in_array($product->product_id, $match_product1)) { (here we create the AND logic for the two tests)
            return true; (true state of the second if)
           }
       } else {
        return false; (false state of first if)
       }
    

    Am I right ? or what do I miss ?

Thanks in advance for your help and advise,

Best regards,

Steph

if you really must do it the long way

$match_product1 = [9,10,11];
$match_product2 = [3,4,5,6,7,12];

$cart_prods = [3,4,9];

$result = 'ERROR';
foreach ($cart_prods as $id) {
    if (in_array($id, $match_product1)) {
        // we found a match in array1 so now check array2
        foreach ($cart_prods as $id) {
            if (in_array($id, $match_product2)) {
                // match found so set result and exit the loops
                $result = 'OK';
                break 2;
            }
        }
    }
}
echo $result;


Many thanks Barand !!

 

I understand my error.

 

But i'd like to learn a bit more... how could we "shorten" it ?

If you don't mind, could you please explain me ?

 

Thanks in advance for the time spended on my queries,

Best regards

Steph

 

if you really must do it the long way

$match_product1 = [9,10,11];
$match_product2 = [3,4,5,6,7,12];

$cart_prods = [3,4,9];

$result = 'ERROR';
foreach ($cart_prods as $id) {
    if (in_array($id, $match_product1)) {
        // we found a match in array1 so now check array2
        foreach ($cart_prods as $id) {
            if (in_array($id, $match_product2)) {
                // match found so set result and exit the loops
                $result = 'OK';
                break 2;
            }
        }
    }
}
echo $result;


Another question :

 

you define a variable $result. this variable can store string elements.

I do need a true or false state. If I declare the $result variable as this :

$result = false;

I should have a boolean variable no ?

 

Thanks in advance for your help and advise,

regards,

Steph

You could shorten it by using array_intersect(), thus removing the need for the nested foreach loops. But I think we've been here before.

Ok understand it... but I will try it later on when I have more time to spend on it.

again thanks for your help !

Regards

Steph

You would also need to substitute "true" for "OK".

 

I just used the string values so the result can be echoed.

 

I have changed the

$result = 'OK';

to

$result = true;

in order to have a boolean variable... but apparently it is not right what I am doing...

 

Am I wrong in declaring this way ?

 

Thanks for your help and advise,

best regards,

steph

I've tried it this way :

$match_product2 = array(3,4,5,6,7,12);
$match_product1 = array(9,10,11)

$cartClass = hikashop_get('class.cart');
$cart = $cartClass->loadFullCart();
foreach($cart->products as $product) {
	if (in_array($product->product_id, $match_product1)) {
		foreach($cart->products as $product) {	
			if (in_array($product->product_id, $match_product2)) {
				return true;
				break 2;
			}
		}	
	}
}

returning a state, but have still an error...

Have to say that at this point I am lost...

 

thanks for your help and advise,

best regards,

Steph

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.