James_62 0 Newbie Poster

Guys i'm trying to get data joined from two tables. I have a select options dropdowns with Fabrics names the other has sizes. I am wondering how i will make my dropdown changes the values for each fabric in each size and multiply for the amount of fabric selected.

this is my fabric table, tecido is Fabrics(this table name is almofadas):
Table picture

This is my price table, only a part of it (this table name is valores_almofadas):
Table picture

This is my checkout.php UI:
My UI

Lemme explane it better...

First thing, is the user has to select a fabric name from the dropdown, and then he has to select the size, but it will depend in which state that the user lives to get the right value in the sub.total.

All the values will change based on what the user selects.

The user session it contains the state where the user is from.

icms_7 is for users that lives in my state, **icms_12** is for outsiders.

So if the user select the fabric velvet in the size of 45cm and he lives in the same state that i live, the value of the fabric will be $ 39,60.

The products rows in the UI are cushions, where the users selects from the gallery, theses rows are stored in session, and these sessions are in a foreach loop.

this is my cart.php (in here i have the product sessions)...

 //this part gets the cushion image from gallery.
  if(isset($_GET['add'])){
   $select = "SELECT * FROM gallery2 WHERE id=" .escape_string($_GET['add'])." ";
   $run_selection = mysqli_query($conn,$select);
   while($rows = mysqli_fetch_assoc($run_selection)){
   if($rows['id'] != $_SESSION['product_'.$_GET['add']]){
   $_SESSION['product_' . $_GET['add']]+=1;
        header('Location: ' . $_SERVER['HTTP_REFERER']);
        }else{
        header('Location: checkout.php');
        }
   }
   }

this is my function inside cart.php..

function cart(){
    global $conn;

    $fabric_options = '';
    $query2 = "SELECT almofadas.A_id, almofadas.tecido, almofadas.id_price, valores_almofadas.id_price, valores_almofadas.icms_7, valores_almofadas.icms_12
    FROM almofadas 
    INNER JOIN valores_almofadas
    ON almofadas.id_price=valores_almofadas.id_price";
    $result = mysqli_query($conn,$query2);
    while($rows = mysqli_fetch_assoc($result)){
      $tecido=$rows['tecido'];
      $id_price=$rows['id_price'];
      $price=$rows['icms_7'];
      $fabric_options .= '<option value="'.$id_price.'">'.$tecido.'</option>';

    }

foreach ($_SESSION as $name => $value) {
     if($value > 0){
     if(substr($name, 0, 8 ) == "product_"){
     $length = strlen($name) -8;
     $item_id = substr($name,8 , $length);

     $query = "SELECT * 
               FROM gallery2 
               WHERE gallery2.id =".escape_string($item_id). "";
               $run_item = mysqli_query($conn,$query);
               while($rows = mysqli_fetch_assoc($run_item)){ 
                 $vari   = $rows['variante'];
                 $num    = $rows['title'];
                 $id     = $rows['id'];

  // these are my buttons                  
$btn_add ='<button type="button" class="btn btn-success actions plus" data-action="plus" product_id="'.$id.'"><i class="fa fa-plus fa-lg" aria-hidden="true" add_btn></i></button>';

$btn_remove ='<button type="button" class="btn btn-warning actions less" data-action="remove" product_id="'.$id.'"><i class="fa fa-minus fa-lg" aria-hidden="true" remove_btn></i></button>';

$btn_delete ='<button type="button" class="btn btn-default actions" data-action="delete" product_id="'.$id.'" onclick="deleteRow(this)"><i class="fa fa-times fa-lg" aria-hidden="true"></i></button>';

     if($rows['variante'] < 1){
        $vari="";
        }else{
        $vari = "-".$rows['variante'];
        }

  $product = '
   <tr>
   <td style="width:100px; "><img src="../'.$rows['image'].'" style="width:90%;border: 1px solid black;"></td>
   <td>'.$num.''.$vari.'</td>
   <td style="width:15%;">
     <select id="" class=" fabric select form-control selectpicker" required=""  >
    '. $fabric_options . '  
     </select>
    </td>

    <td>
     <select  id="size" class="select form-control selectpicker" required style="width:80%;" onchange="saveChoice()" >
       <option value="50">50x50</option>
       <option value="40">45x45</option>
     </select>
    </td>

    <td class="product'.$id.'">'.$value.'</td> // this is the amount of items for each row
    <td class="'.$id.'">R$:'.$price.'</td>
    <td>$ ' .$value * $price.'</td>
    <td> 
    '.$btn_add.' '.$btn_remove.' '.$btn_delete.'
    </td>
    </tr>';
           echo $product;  

             } 
          } 
       }
    }   
 }

This is the script that i'm using to make my amout update on btn click in checkout.php...

$('.actions').click(function(e){
  e.preventDefault();
  var action = $(this).attr('data-action'); //gets the button action
  var id = $(this).attr('product_id'); //id of the products
  console.log("triggered action " + action + " for product " + id); //debugging
  $.ajax({
    url: 'cart_functions.php',
    type : 'GET',
    data: {action:action,prod_id:id},
    dataType: 'json',
    success: function(data)
    {
      console.log("ajax call returned the following: " + JSON.stringify(data)); //debugging
      if (data.result == "success") {
        if (action == "plus" || action == "remove")
        {
          console.log("identified product td for " + id + ": " + $(".product" + id).length); //debugging
          $(".product" + id).text(data.val);//update the UI with the new total of each item
         //rest of the script....

This got so complex now, i had help here in stackoverflow with the ajax script. So how i will make the values change based on what the user selects?

To this better look here http://stackoverflow.com/questions/39879899/joining-two-tables-and-change-total-cost-based-on-user-dropdown-selection-php

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.