Wednesday, 20 November 2019

Join Function used to match value of field in one table with other table

I am trying to display field values, when values of field in the table matches with the values in field of another table.

Its like I am creating a drop down fields. Here basis the car Model dropdown. Like Ferrari as Model chosen in dropdown by user as prod_name under table newcar_products then its variants like Portnifo, superfast,488 should be shown under Variant field.

But whats happening as of now - irrespective of model selected whether Ferrari or Honda or Volkswagen - the variants displays the entire list of variants like polo, jazz,portnifo,accord,passat as stored in v_name in table newcar_variants. I want that variants v_name should be displayed only for v_prod_id in table A matches with id in table B

Like, i have created these 2 functions where first function of drop down are working perfectly

      <?php    
        function model_drop_down(){
            $database = JFactory::getDBO();
            $sql = "SELECT * from #__newcar_products where state='1'  order by prod_name Asc";
            $database->setQuery($sql);
            $rows = $database->loadObjectList();
            $list="";
            foreach($rows as $row){
                $list.="<option value='".$row->id."' style='padding-left:10px;'>".$row->prod_name."</option>";
            }
            return $list;
        }


       function variant_drop_down(){
    $database = JFactory::getDBO();
    $sql = "SELECT a.id, a.v_name FROM #__newcar_variants a JOIN #__newcar_products b ON a.v_prod_id = b.id
    and a.state='1' where  1 order by a.v_prod_id asc";
    $database->setQuery($sql);
    $rows = $database->loadObjectList();
    $list="";
    foreach($rows as $row){
        $list.="<option value='".$row->id."' style='padding-left:10px;'>".$row->v_name."</option>";
    }
    return $list;
    } ?>

     <div class="common-box">
        <div class="common-box-left">Car Model*</div>
        <div class="common-box-right">
          <div id="txtHint">
          <select name="model"  class="list-box-big"  id="model"  lang="MUST" title="Model" style="width:245px; height:25px;">
              <option value="" selected="selected" style="padding-left:10px;"> Select Model</option>
<?php echo model_drop_down();?>
            </select>
          </div>
        </div>
      </div>
      <!--box end-->
      <!--box start-->
      <div class="common-box">
        <div class="common-box-left">Sub Model*</div>
        <div class="common-box-right">
          <div id="txtHintVariant">
          <select name="variant" class="list-box" id="brand"  lang="MUST" title="Brand">
                <option value="" selected="selected" style='padding-left:10px;'>Select Variant</option>
                <?php echo variant_drop_down();?>
            </select>
          </div>

Heres schema of both tables enter image description here

As of now the fields displaying all models and name of all variants. Whats happening now in second field of variant its displaying all value stored in v_name rather than only those under v_name where v_prod_id in newcar_variants matches with id in newcar_products

I think a way needed which Step 1 When a user select Model from drop down in the first field, the varle of field id needs to be stored in newcar_products

Step 2 Then in second field of select variant, when the user select drop down of model in the first field. the value of 'id' as stored in newcar_products should match with the v_prod_id in the second table newcar_variants and thus the list of variants to be displayed in matching 'v_name'

How to create the function to achieve desired. Can anyone help, been novice this would be a great learning. thanks



from Join Function used to match value of field in one table with other table

No comments:

Post a Comment