Skip to content
Advertisement

creating a check for a single column of fetched data PHP sql

So I’m working a module of a Hotel Website which is Wine List and data is fetched dynamically and there are multiple categories of wines.

I am showing them all according to their category and I also want to have a title above the wines of a single category.. e.g Wine A is a category and i want all those wines to be shown under one title of Wine A and then a title of Wine B and all those of B type to be shown under that title.

I’m trying to do that but every single time when a wine data is shown on the page the name of category is also showing and i want it to show once for all the wines of single category. below is the code, and sorry for the messy code, and let me know if you want know anything else about it

   <div class="row">
                        <?php 


                    

            if ((isset($_GET["category"]))) {
                                     $getCategory=$_GET["category"];
                                    
                                    $WineCardSql= "SELECT
        tbl_wine_list.wine_title, tbl_wine_list.description,
        tbl_wine_list.wine_region, tbl_wine_list.wine_style,
        tbl_wine_list.recommended_for, tbl_wine_list.unit_price,
        tbl_wine_list.bottle_price, tbl_wine_list.img_url,
        tbl_currency.symbol, tbl_wine_category.wine_category
    
                                     FROM tbl_wine_list 
                                      INNER JOIN tbl_wine_category 
                                      ON
                                      tbl_wine_list.winc_id = tbl_wine_category.winc_id
                                      INNER JOIN tbl_currency 
                                      ON
                                      tbl_wine_list.curr_id = tbl_currency.curr_id
                                    WHERE tbl_wine_category.winc_id=$getCategory";
                                    //  ORDER BY tbl_wine_category.winc_id ASC";
                                }
    
                                   else {
                                           $WineCardSql="SELECT
        tbl_wine_list.wine_title, tbl_wine_list.description,
        tbl_wine_list.wine_region, tbl_wine_list.wine_style,
        tbl_wine_list.recommended_for, tbl_wine_list.unit_price,
        tbl_wine_list.bottle_price, tbl_wine_list.img_url,
        tbl_currency.symbol, tbl_wine_category.wine_category
                                         FROM tbl_wine_list 
                                      INNER JOIN tbl_wine_category 
                                      ON
                                      tbl_wine_list.winc_id = tbl_wine_category.winc_id
                                      INNER JOIN tbl_currency 
                                      ON
                                      tbl_wine_list.curr_id = tbl_currency.curr_id
                                    ORDER BY tbl_wine_category.winc_id ASC";
                                   }
                                     $WineCardQuery=$DbObject->query($WineCardSql);
                                     if($WineCardQuery)
                                    //  if ($WineCardQuery && $WineCardQuery->num_rows >0) {
                                        {
                                            while ($DataTable=$WineCardQuery->fetch()) {
                                                
                                                    $Title             =$DataTable["wine_title"];
                                                    $Description       =$DataTable["description"];
                                                    $Region            =$DataTable["wine_region"];
                                                    $Style             =$DataTable["wine_style"];
                                                    $Recommendation    =$DataTable["recommended_for"];
                                                    $GlassPrice        =$DataTable["unit_price"];
                                                    $BottlePrice       =$DataTable["bottle_price"];
                                                    $Currency          =$DataTable["symbol"];
                                                    $ImageLink         =$DataTable["img_url"];
                                                    $WineCategoryName  =$DataTable["wine_category"]; ?>
                                      
                <!-- <div class="row ">  -->
                        <div class="col-xl-12">
                            <h2 style="font-size:30px; font-weight:lighter; 
                       margin-left:5px; color:#f5f5f5;"><?php echo $WineCategoryName; ?></h2> 
                        </div>
                <!-- </div> -->
    
                                <div class="col-xl-6 col-md-6 col-sm-6 mt-3  ">
                                    <div class=" mr-3 ml-3 hover-affect-div bg-white">
                                        <div class="  px-2 ">
                                            <div class="row">
                                                <div class="col-xl-7 col-md-7 col-sm-12 col-xs-12"> <!-- inner column starts -->
                                                 <div class="pt-2"style="width:250px;">
                                                        <?php echo $Title; ?> 
                                                    <div class="underline"></div>   <!-- Underline -->
                                                    <span> <i><?php echo $Description; ?></i></span> 
                                                    </div><br>
                                                    <!--<h6>Region:</h6> <i> <?php// echo $Region; ?>   </i> <br>-->
                                 
                                                    <div class="textSizeMedium" style="width:250px;">
                                                                <strong>Region:</strong><?php echo $Region; ?> <br>
                                                                <strong>Style:</strong>  <?php echo $Style; ?> <br>
                                                                <strong>Recommended for:</strong><i><?php echo $Recommendation; ?> </i><br>
                                                            <div style="border-top:2px solid gray; float:right;">
                                                                <?php echo $GlassPrice." ".$Currency?> <strong>/ Glass</strong> <br>
                                                                <?php echo $BottlePrice." ".$Currency ?><strong>/ Bottle</strong>
                                                            </div>
                                                   </div> 
                                              </div>
                                                <div class="col-xl-5 col-md-5 col-sm-12 col-xs-12 pl-4 pt-4">
                                                    <img src="wine.png" alt="" >
                                                </div>
                                        </div><br>
    
                                                 </div> <!--  row Class ends -->
                                    </div>
                                 </div>
    <?php
                                                }
                                            }     
    
                                    //  }
    ?>
                               </div><!-- ROW Ends -->

Advertisement

Answer

So basically I took $i=0 before while loop. And while echoing $WineCategoryName I am checking if($i==0).If true print it else print empty string.Before closing of while loop I am incrementing $i.

$i=0;
while ($DataTable=$WineCardQuery->fetch()) {
                                            
    $Title=$DataTable["wine_title"];
    $Description       =$DataTable["description"];
    $Region            =$DataTable["wine_region"];
    $Style             =$DataTable["wine_style"];
    $Recommendation    =$DataTable["recommended_for"];
    $GlassPrice        =$DataTable["unit_price"];
    $BottlePrice       =$DataTable["bottle_price"];
    $Currency          =$DataTable["symbol"];
    $ImageLink         =$DataTable["img_url"];
    $WineCategoryName  =$DataTable["wine_category"]; 
?>
                                  
    <!-- <div class="row ">  -->
            <div class="col-xl-12">
                <h2 style="font-size:30px; font-weight:lighter; margin-left:5px; color:#f5f5f5;"><?php echo ($i==0) ? $WineCategoryName : ""; ?></h2> 
            </div>
            ....
<?php
                $i++;
            }
         }     
    }
?>
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement