php - Selecting data from table using another table's data -


i'm building app stores data different types of products. here schema of products table:-

------------------------------------------------------------------------------------ | id  | product_name  | price | details  | category_id | phone_ram  | bike_mileage | ------------------------------------------------------------------------------------ 

here, id, product_name, price , details column applicable products. 'phone_ram' column applicable items under 'phone' category , 'bike_mileage' column applicable items under 'bike' category.

here 'category' table:-

---------------------- | id | category_name | ---------------------- |  1 | phone         | ---------------------- |  2 | bike          | ---------------------- 

and here 'category_data' table stores name of data of category.

----------------------------------- | id | category_id | name_of_data | ----------------------------------- |  1 |           1 | phone_ram    | ----------------------------------- |  2 |           2 | bike_mileage | ----------------------------------- 

now, in query. first, select columns 'products' table applicable of kinds of data. 'category_id' of table same table , check on 'category_data' table find additional data products table. again select additional data according categories products table. ie: 'phone_ram' column if category 'phone'.

how achieve it?

using php:

<?php  session_start();  include 'db_connect.php';    $get_category = mysqli_query("select * category category_name = '$categoryin'");   if(mysqli_num_rows($get_category)==0){      //no result when looking category   }   else {       //select table      $category = mysqli_fetch_array($get_category);      $categoryname = $category['category_name'];      $categoryid = $category['id'];        //start selecting other table based on first query       $get_nameofdata = mysqli_query("select * category_data category_id = '$categoryid'");       if(mysqli_num_rows($get_nameofdata)==0){          //no category data result.       }      else{          $cat_data = mysqli_fetch_array($get_nameofdata);           $nameofdata = $cat_data['name_of_data'];         }        //your name_of_data result      echo $nameofdata;  } 

wrote in hurry, hope helps.

also, try use structure in database :)


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -