Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

php - Order and group query results from LEFT JOIN items

I am working with two MySQL tables:

Table tb_cat_combos:

enter image description here

Table tb_productos_combos:

enter image description here

I would like to get the items from the second table with following output:

Bread

Large Bun

Small Bun(5")

Sides

French Fries

This is my current query:

$queryCombo = "SELECT pro.*, cat.nombre_cat FROM tb_productos_combos pro 
LEFT JOIN tb_cat_combos cat ON pro.cat_combo = cat.id
ORDER BY cat.orden_cat ";

And this is how am I showing the query output:

     if($statementCombo->execute())
    {
      $numero_opciones = 0;
      $result_combo = $statementCombo->fetchAll();
      foreach($result_combo as $row_combo)
      {



        if ($row_combo['origen_combo'] == $row['id']) {
          

          $numero_opciones = $numero_opciones + 1;
          $output .= '
          <div class ="oculto" id="customDiv'.$row["id"].'"  style="display:none;" class="answer_list" > 
          
          <div class="checkbox">
          <label><input type="checkbox" data-nombre="'. $row_combo["nombre"] .'" 
    data-precio="'. $row_combo["precio"] .'"  id="opcion'.$row["id"].'"   class="opcion_cbox" 
    data-rowid="'. $row["id"] .'"  value="">'.$row_combo['nombre'].' (+ $'.$row_combo['precio'].')</label>
          </div>


 </div>

        
          ';

        }
      }
    }

What is the best way to get the needed output?

EDIT:

This is the current output:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Not necessarily the best way - but one approach is to store the category name each time a record is output, then output that name when it changes from the previous record:

    $type = '';
    if ($row_combo['origen_combo'] == $row['id']) {
        
          $numero_opciones = $numero_opciones + 1;
          $output .= '
          <div class ="oculto" id="customDiv'.$row["id"].'"  style="display:none;" class="answer_list" >';
          
          if($type != $row_combo['nombre_cat']) {
           // nombre_cat is different to last row - so output the nombre_cat
           $type = $row_combo['nombre_cat'];
           $output .= '<p>$type</p>';
          }
          
          $output .= '
          <div class="checkbox">
          <label><input type="checkbox" data-nombre="'. $row_combo["nombre"] .'" 
    data-precio="'. $row_combo["precio"] .'"  id="opcion'.$row["id"].'"   class="opcion_cbox" 
    data-rowid="'. $row["id"] .'"  value="">'.$row_combo['nombre'].' (+ $'.$row_combo['precio'].')</label>
          </div>


 </div>';
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...