-1

I want my site to search for any product that belonged to particular size when click any size option in the table cell for desktop users but I am having two challenges

  1. My loop for horizontal cells suppose to be incrementing from left to right like this 28D, 28DD, 28E, 28F, 28FF, 28G ...28K and then move to second line 30D --- 30k but instead it was reapeating each value 12 times horizontally like this 28D, 28D, 28D 12times and move to second line 28DD repeating it 12 times again. Please what could be the problem in my four loop?

  2. I don't know how to put anchor tag around the table cell and how to put name = size attribute in the table cell for it to link sizeresult.php. where it will be processed by the select query here is my code:

                    <div class="table-responsive"><!-- table-responsive begin -->
                         <table class="table table-striped table-hover"  border="0" width="100%" cellpadding="5" cellspacing="5">
                             <thead>
                                 <tr>
                                     <th class="success">
                                     <h4 class="text-center white-text">D</h4>
                                     </th>
                                     <th class="info">
                                     <h4 class="text-center white-text">DD</h4>
                                     </th>
                                     <th class="danger">
                                     <h4 class="text-center white-text">E</h4>
                                     </th>
                                     <th class="success">
                                     <h4 class="text-center white-text">F</h4>
                                     </th>
                                     <th class="info">
                                     <h4 class="text-center white-text">FF</h4>
                                     </th>
                                     <th class="danger">
                                     <h4 class="text-center white-text">G</h4>
                                     </th>
                                     <th class="success">
                                     <h4 class="text-center white-text">GG</h4>
                                     </th>
                                     <th class="info">
                                     <h4 class="text-center white-text">H</h4>
                                     </th>
                                     <th class="danger">
                                     <h4 class="text-center white-text">HH</h4>
                                     </th>
                                     <th class="success">
                                     <h4 class="text-center white-text">J</h4>
                                     </th>
                                     <th class="info">
                                     <h4 class="text-center white-text">JJ</h4>
                                     </th>
                                     <th class="danger">
                                     <h4 class="text-center white-text">K</h4>
                                     </th>
                                 </tr>
                             </thead>
    
                             <tbody>
    
                             <?php 
    
    
                                 $count = 12; // Number of possible cells to add at once:
    
                                 $i=0;
    
                                 $get_sizes = "select * from sizes";
                                 $run_sizes = mysqli_query($dbc,$get_sizes);
    
                                 while ($row_sizes=mysqli_fetch_array($run_sizes)){
    
                                     $size_id = $row_sizes['size_id'];
                                     $size_name = $row_sizes['size'];
    
                                     $i++;
    
                             ?>
    
                             <tr>
                                 <?php for ($i = 1; $i <= $count; $i++) { 
                                         echo "
                                         <td  value='$size_name' align='center'>
    
                                         $size_name
                                         </td>";
                                     } // End of FOR loop.
    
                                 ?>
    
                             </tr>
    
                             <?php } ?>
                             </tbody>
    
                         </table> 
                   </div>         <!-- table-responsive end -->
                 </form>
    

sizeresult.php code is this :

$size_name=$_POST['size'];

$run_products = mysqli_query($dbc,"SELECT * FROM products INNER JOIN SIZES USING (size_id) WHERE sizes.size ='%$size_name%'");

Sunday Olaoye
  • 67
  • 1
  • 7
  • **Warning:** You are wide open to [SQL Injections](https://stackoverflow.com/a/60496/1839439) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Aug 07 '20 at 14:58
  • I know and I will use prepared statement I just want to clarify these issues in simple term – Sunday Olaoye Aug 07 '20 at 15:03
  • @SundayOlaoye you sending the size value only or there are other data's that needs to be sent alongside? – waanofii.tech Aug 10 '20 at 12:04

1 Answers1

0

I made the following changes

  1. $count variable and for loop completely

  2. created block of code to format your list of items in the way you have explained in your question grouping them by 12 in each row.

  3. based on your request of using anchor tag i added anchor tag so that when the user clicks on it it will be taken to the sizeresult.php page and the query will be processed.

  4. I made sure that the post and get request doesn't conflict by putting the following code right above the query in sizeresult.php

    if(isset($_GET['size'])){ $size_name=$_GET['size'] } else if(isset($_POST['size'])){ $size_name=$_POST['size']; }

the following in your main page

<div class="table-responsive"><!-- table-responsive begin -->
    <table class="table table-striped table-hover"  border="0" width="100%" cellpadding="5" cellspacing="5">
        <thead>
            <tr>
                <th class="success">
                <h4 class="text-center white-text">D</h4>
                </th>
                <th class="info">
                <h4 class="text-center white-text">DD</h4>
                </th>
                <th class="danger">
                <h4 class="text-center white-text">E</h4>
                </th>
                <th class="success">
                <h4 class="text-center white-text">F</h4>
                </th>
                <th class="info">
                <h4 class="text-center white-text">FF</h4>
                </th>
                <th class="danger">
                <h4 class="text-center white-text">G</h4>
                </th>
                <th class="success">
                <h4 class="text-center white-text">GG</h4>
                </th>
                <th class="info">
                <h4 class="text-center white-text">H</h4>
                </th>
                <th class="danger">
                <h4 class="text-center white-text">HH</h4>
                </th>
                <th class="success">
                <h4 class="text-center white-text">J</h4>
                </th>
                <th class="info">
                <h4 class="text-center white-text">JJ</h4>
                </th>
                <th class="danger">
                <h4 class="text-center white-text">K</h4>
                </th>
            </tr>
        </thead>
        <tbody>   
        <tr>
        <?php
            //$count = 12; // Number of possible cells to add at once://you don't need this too.
            $i=1;
            $get_sizes = "select * from sizes";
            $run_sizes = mysqli_query($dbc,$get_sizes);
            while ($row_sizes=mysqli_fetch_array($run_sizes)){
                $size_id = $row_sizes['size_id'];
                $size_name = $row_sizes['size'];
                if($i==12){
                    echo "<td align='center'><a href='product-card.php?size=$size_name' type='button' style='text-decoration:none; color:black;' class='btn btn-block'>$size_name</a></td>";
                    $i=1;
                    echo "</tr><tr>";
                }
                else {
                    echo "<td align='center'><a href='product-card.php?size=$size_name' type='button' style='text-decoration:none; color:black;' class='btn btn-block'>$size_name</a></td>";
                    $i++;
                } 
            } // End of while loop.
            ?>
            </tr>
        </tbody>
    </table> 
</div>         <!-- table-responsive end -->
 <!-- </form> you can remove this FORM tag if you want -->
waanofii.tech
  • 311
  • 1
  • 12