09jesses 0 Newbie Poster

I have what I call a buildList, or better yet a Wishlist or cart. Inside this "buildList.php" is roughly 10 links, +ADD, that basically act like a button and categorized "part-1, part-2, part-3...etc. When a user chooses one button, lets say "part-1 +ADD", it links to the "contents.php" page to show all the products from the database for that specific part. When user finds a part they like they click the "+ADD" button on contents.php which on another page creates session with an array of the parts info, then I have it go back to the buildList.php and replaces the "+ADD" link button with the selected product into "part-1" category. The way I kind of have it working is :

<?php
  if (!isset($_SESSION['buildList']) || empty($_SESSION['buildList']) {
?>

//This normally shows if no session exists or if session exists but empty
// +ADD link button
<td class="add-component">
  <a href="content.php?PartType=Part1" class="add-btn"><i class="fas fa-plus"></i>ADD</a>
</td>

//If part has been added into session['buildList'] then replace +ADD link button with this
<?php
   }else{
     foreach ($_SESSION['buildList'] as $product) :
       if ($product['PartType'] == 'Part1') {
?>
<a href="pageContent.php?id=<?php echo $product['id'];?>"><img class="p-img" src="<?php echo $product['Image_thumb'];?>"/></a>
  </td>
  <td class="td-manufacturer">
    <h6>MANUFACTURER</h6>
    <p>
      <?php
        echo $product["Manufacturer"];
       ?>
     </p>
   </td>
   <td class="td-addComponent">
     <p>
       <?php 
         echo $product["Price"];
       ?>
     </p>
   </td>
   <td class="td-material">
     <h6>MATERIAL</h6>
       <p>
         <?php
           echo $product["Material"];
         ?>
       </p>
     </td>
   </tr>           
 <?php
         }
      endforeach;
    } 
  ?>

So far I can add "part-1", it does replace the "part-1" category +ADD link button with the product info like I want. But when "part-1" is added, "part-2" +ADD link button goes away and shouldn't because now I cannot get the "part-2" page to add those products. I need a way to show ALL +ADD buttons when no session exists or does exist and other categories have a product. The code above basically repeats for the other category parts. I hope this makes sense, I have tried different ways with the if statements on session variable, also searched around and didn't find this type of issue that would help. Thanks

Dani AI

Generated

— the issue is that your page decides “show +ADD” based on whether the whole session array is empty. Once any product is in $_SESSION['buildList'] you always take the else branch and never render a per‑slot +ADD for slots that don’t have a matching product. The fix is simple: determine presence per PartType, not globally.

Create a small lookup keyed by PartType (one pass over the session) and for each slot check that lookup. This is faster and makes the logic obvious: if there’s an item for Part1 show it, otherwise show the +ADD link.

<?php
session_start();

$byPart = [];
if (!empty($_SESSION['buildList']) && is_array($_SESSION['buildList'])) {
    // build a map so checks are O(1)
    foreach ($_SESSION['buildList'] as $item) {
        $key = trim($item['PartType']);
        $byPart[$key] = $item; // or push into an array if you expect multiple items per part
    }
}

// render the slot
$slot = 'Part1';
if (isset($byPart[$slot])) {
    $p = $byPart[$slot];
    echo '<a href="pageContent.php?id='.htmlspecialchars($p['id']).'"><img src="'.htmlspecialchars($p['Image_thumb']).'"></a>';
} else {
    echo '<a href="content.php?PartType='.urlencode($slot).'" class="add-btn">+ADD</a>';
}

Troubleshooting tips: always call session_start() before output; var_export($_SESSION['buildList'], true) helps debug; normalize PartType strings to avoid whitespace/case mismatches; consider storing only product IDs in session (then query DB when rendering) to keep session size small; and after adding an item redirect back with header('Location: buildList.php'); exit; to avoid duplicate adds on refresh.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.