简体   繁体   中英

SQL, PHP - How to select Heading and all Subheadings of current Heading?

I need a select query for search in my e-shop. I have the following mysql table

id  |  product_id  |   param   |  value
 1  |      123     |   Head 1  |   v1
 2  |      456     |   Head 1  |   v2
 3  |      789     |   Head 1  |   v3
 4  |      abc     |   Head 2  |   v4
 5  |      def     |   Head 2  |   v5

I need to select distinct "param" and the values corresponding to the param.

Example:

Head 1 - v1, v2, v3
Head 2 - v4, v5

I need add it to HTML :

<h3>Head 1</h3>
<div class="vyber">
 <select>
  <option>v1</option>
  <option>v2</option>
  <option>v3</option>
 </select>
</div>

<h3>Head 2</h3>
<div class="vyber">
 <select>
  <option>v4</option>
  <option>v5</option>
 </select>
</div>

My SQL :

SELECT p.*
FROM Parametre p
JOIN produkty pr ON p.ProduktID = pr.ProId      
WHERE pr.KATEGORIA_URL = '$Kategoria'
GROUP BY p.Hodnota
ORDER BY p.Param, p.Hodnota

And this is PHP code

$pamet = '';
$konec = '';
while($ParamVyhladavanieResult = mysqli_fetch_object($ParamVyhladavanie)){

  $Param = htmlspecialchars($ParamVyhladavanieResult->param);
  $Hodnota = htmlspecialchars($ParamVyhladavanieResult->value);
  $CisloHodnoty = $ParamVyhladavanieResult->id;

  if (($ParamVyhladavanieResult->param)!=$pamet)
  {
      $pamet = $ParamVyhladavanieResult->param;

      echo "
      <h3>".$Param."</h3>
      <div class=\"vyber\">
      <select class=\"bielePozadie\" style=\"height:35px;color:#000;border:1px solid grey;outline:none;width:100%\">
      <option value=\"0\">--nevybraté--</option>
      ";
      $konec = "</select>
      </div>";
  }

  echo "   <option value=\"$CisloHodnoty\">".$Hodnota."</option>
  ";
}
echo $konec;

If you're using MySQL you can use GROUP_CONCAT()

SELECT param, GROUP_CONCAT(value) value
  FROM table1
 GROUP BY param

Output:

|  PARAM |    VALUE |
|--------|----------|
| Head 1 | v1,v2,v3 |
| Head 2 |    v4,v5 |

Here is SQLFiddle demo

Then you can easily explode() values of value column while iterating over the resultset and build your html.

You can eg. make 2 nested loops:

  1. Select dinstict values of param
  2. Select values which have param = result from first loop

Another option is to select all - ordered by param - and ask for the difference of param in PHP for creating the view.

//First select DISTINCT params
$result = mysql_query('SELECT DISTINCT param FROM YOUR_TABLE');
while($row = mysql_fetch_array($result))
{
    // now fetch its values
    $sub_result = mysql_query('SELECT * FROM YOUR_TABLE WHERE param = "'.$row['param'].'"');
    while($sub_row = mysql_fetch_array($sub_result))
    {
         // generate your HTML here
    }
}

Alternate approach

$last_param = '';
$result = mysql_query('SELECT * FROM YOUR_TABLE ORDER BY param');
while($row = mysql_fetch_array($result))
{
    if($row['param'] != $last_param)
    {
       if($last_param != '')
       {
         ?>
            </select>
     </div>
         <?php 
       }
       $last_param = $row['param'];
       ?>
        <h3><?php echo $last_param; ?></h3>
        <div class="vyber">
          <select>
       <?php
    }
    else
    {
       ?>
       <option><?php echo $row['value'];?></option>
       <?php 
    }
}

Simple Query in SQL as

select param, value from table;

In HTML, According to your requirment, we can pass result with json to Javascript and handle from there. Using Jquery for code shor

<div id="SelectHolder">
</div>

<script type="text/javascript">
var ResultArray = <?php echo json_encode(mysqlresult, JSON_UNESCAPED_SLASHES); ?>;
for(var i=0; i<ResultArray.length;i++)
{
   if(document.getElementbyId(ResultArray[i][0])!=undefined && document.getElementbyId(ResultArray[i][0])!=null)
   {
      $('#'+ResultArray[i][0]).append('<option>'+ResultArray[i][1]+'</option>');
   }
   else
   {
     $('#SelectHolder').append('<select id="'+ResultArray[i][0]+'">'+ResultArray[i][0]+'</select>');
     $('#'+ResultArray[i][0]).append('<option>'+ResultArray[i][1]+'</option>');
   }
}
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM