1

I have created a little php file that will display a list of products that "Customers have also bought" that I want to display on each of my products pages. However, I want to exclude the same product showing up as an "also bought" on the product page.

Here is the php file I have created that loads a random product on page load:

THIS FILE IS CALLED "**products.php**"
<?php

// Customers also bought

$product1 = '<a href="../images/image1.jpg" class="zoom">
                <img src="../images/image1.jpg" alt="gallery-image" title="gallery-image" class="img-responsive" /></a>
                <div class="item-hover"><a href="../product1.php">Product 1</a> </div>';

$product2 = '<a href="../images/image2.jpg" class="zoom">
                <img src="../images/image2.jpg" alt="gallery-image" title="gallery-image" class="img-responsive" /></a>
                <div class="item-hover"><a href="../product2.php">Product 2</a> </div>';

$product3 = '<a href="../images/image3.jpg" class="zoom">
                <img src="../images/image3.jpg" alt="gallery-image" title="gallery-image" class="img-responsive" /></a>
                <div class="item-hover"><a href="../product3.php">Product 3</a> </div>';

$product4 = '<a href="../images/image4.jpg" class="zoom">
                <img src="../images/image4.jpg" alt="gallery-image" title="gallery-image" class="img-responsive" /></a>
                <div class="item-hover"><a href="../product4.php">Product 4</a> </div>';

$product5 = '<a href="../images/image1.jpg" class="zoom">
                <img src="../images/image5.jpg" alt="gallery-image" title="gallery-image" class="img-responsive" /></a>
                <div class="item-hover"><a href="../product5.php">Product 5</a> </div>';

   $products = array($product1, $product2, $product3, $product4, $product5);

shuffle($products);

?>

Here is the code I am using in my main product pages:

This file is called **product1.php**
<?php 
    include("../includes/**products.php**");
?>

<h4>Customers Also Bought</h4>
<div class="col-lg-6 col-md-6 col-xs-6 gallery-item gallery-popup all themes">
<figure> <?php print $products[0] ?> </figure>
</div>
6
  • It would be helpful if we knew the structure of the data of the current product. Is it stored in another array? A variable? Commented Aug 19, 2015 at 3:23
  • No, it is stored just like it written above and then I am just echoing/printing in a separate php file. Commented Aug 19, 2015 at 3:34
  • So on each product page you would like to output both the main product and then an array of additional products, but right now I only see you outputting $products[0] to the page. Is there code which you aren't including? Commented Aug 19, 2015 at 3:42
  • The code above shuffles each of the products so that one of them appears on page load ... in a random order. I want to exclude from the random order the product that is already being shown Commented Aug 19, 2015 at 3:45
  • Where is the code for the product that is already being shown? Commented Aug 19, 2015 at 3:47

5 Answers 5

1

Use unset() to remove an item in a array. But you have to provide a key and the value to the array.

$p["p1"]="abc";
$p["p2"]="efg";

then,

unset($p["p1"]);
Sign up to request clarification or add additional context in comments.

2 Comments

Hello and welcome to Stack Overflow! You might want to take a quick look at the site's markdown guide. It will help you post easier to read answers.
Hi,Thanks for the tip.
1

Try in_array() or array_key_exists();

$products= [1,2,3,4,5]; 
$array=[1,5];
foreach($products as $key => $product){
    if (array_key_exists($key,$array)){
        unset($products[$key]);
    }
}

this should do the trick, // loop through the new array to create your html

$html = '';
foreach($products as $key => $product){
   $html .= '<a href="'.$product['id'].'">$product['name']  </a>';
}
echo $html;

2 Comments

Do I still use "shuffle($products);" and "<?php print $products[0] ?>" ?
Nope, just loop through the new products array and create your html.
1
$array = array("a","b","c","d","e");
$array2 = array("d","b");
shuffle($array);
for($i=0;$i<count($array2);$i++) {
    if(stristr($array[0],$array2[$i])) {
        unset($array[0]);
    }
}

echo $array[0];

Comments

0

You should have the current product stored somewhere, so you just need to compare that value with the values in your $products array. You can use in_array to find the $current_product in the $products array. Something like the below:

$current_product = '1';
$products = array('1', '2', '3', '4');

if (in_array($current_product, $products)) {
  unset($products['key'])
}

You can use unset() to remove the value from the array, but be sure to supply the array key or index value when you do so.

UPDATE:

After a conversation with the OP:

I think you need to restructure your code. If you can restructure the HTML in products.php, then store all of your products, including the main product, in php variables. Then, print out the main product and additional products to the page with php variables. Something like:

<!-- Main Product Code -->
<?php print $main_product ?>

<!-- Customers Also Bought -->
<?php print $products[0] ?>

That way all of your data is stored in PHP variables. If it is all stored in variables then you can use PHP to compare the two products with the method above.

HOWEVER, if you can't change products.php then you are going to need to parse through the HTML and find some identifying information that will indicate which product is being shown. After you identify the product, you will then need to compare it with the HTML which you stored in $products to find a match. Basically, you need to compare one string against another, and you really don't want to have to go down that route.

Comments

0

Ok, after reading and trying to implement all of the offered suggestions without success I came up with a solution that will work for me. I realize that this is a more crude way of doing it but it is the only way I could achieve what I needed.

I modified the php file that contained all of the product names and html to be echoed on the main product pages.

$all_products = array($product1, $product2, $product3, $product4, $product5); // for all non-promoted products pages
$for_product5 = array($product1, $product2, $product3, $product4); // for Product 5 page
$for_product4 = array($product1, $product2, $product3, $product5); // for Product 4 page
$for_product3 = array($product1, $product2, $product4, $product5); // for Product 3 page
$for_product2 = array($product1, $product3, $product4, $product5); // for Product 2 page
$for_product1 = array($product2, $product3, $product4, $product5); // for Product 1 page

shuffle($all_products);
shuffle($for_product1);
shuffle($for_product2);
shuffle($for_product3);
shuffle($for_product4);
shuffle($for_product5);

This way I can echo on each page only the items I want to be randomly shown on that page.

Yes, I know that there is probably a much better way of doing it but this gets me going for now.

Thanks for all of the help though.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.