How do I fix it?

I have three rows but it shows only one row in report table
what’s the mistake

source code

<?php include_once 'header.php'; ?>
<?php include_once 'sidebar.php'; ?>
<?php include_once 'navtop.php'; ?>
                <div class="container-fluid">
                  <?php
                  $student=output(@$_GET['student']);
                  $fix=output(@$_GET['fix']);
                  $type_of_c=output(@$_GET['type_of_c']);
                  $loc=output(@$_GET['loc']);
                  $gen=output(@$_GET['gen']);
                  $fdate=output(@$_GET['fdate']);
                  $tday=output(@$_GET['tday']);
                  $cl=output(@$_GET['cl']);

                  if (isset($_GET['class'])) {
                    ?>
                  <div class="card shadow mb-4">
                    <div class="card-header py-3">
                    </div>
                    <div class="card-body">
                      <form method="get">
                        <div class="form-group">
                          <label for="email">Fixation Type:</label>
                            <select class="form-control" name="fix" id="class_id">
                              <option>choose</option>
                              <option value="Screw">Screw</option>
                              <option value="Cement">Cement</option>
                           </select>
                        </div>
                        <div class="form-group">
                            <label for="email">Type of crown:</label>
                            <select class="form-control" name="type_of_c" id="class_id">
                              <option>choose</option>
                              <option value="ceramic">ceramic</option>
                              <option value="zirconium">zirconium</option>
                              <option value="Acrylic">Acrylic</option>
                              <option value="Emax">Emax</option>
                              <option value="Veneer">Veneer</option>
                              <option value="other">other</option>
                           </select>
                        </div>

                        <div class="form-group">
                            <label for="name">name:</label>
                            <input type="text" name="sn" class="form-control" value="" id="student_name" placeholder="N-Mobile">
                            <div data-lastpass-icon-root="true" style="position: relative !important; height: 0px !important; width: 0px !important; float: left !important;"></div>
                        </div>
                        <hr>
                        <div class="form-group">
                              <label>From Date</label>
                              <input type="text" name="fdate" class="form-control" value="" id="fdate">
                         </div>

                         <div class="form-group">
                              <label>To Date</label>
                             <input type="text" name="tday" class="form-control" value="" id="tday">
                         </div>

                         <div class="form-group" align="center">
                            <input type="submit" name="class" value="Searching" class="btn btn-primary btn-sm btn-block">
                         </div>
                        </form>
                            <?php
                            $stm = $db->prepare("SELECT name as n, birth as b, Cost as c, count(id) as total FROM patient WHERE fixation_t=:fix or type_of_c=:type_of_c");
                            //$stm = $db->prepare("SELECT * FROM sub WHERE cl=:class or gr=:gr or sn=:sn or (datee between :fdate and :tday AND cl=:class) ");
                            $stm->bindParam(":fix",$fix, PDO::PARAM_STR);
                            $stm->bindParam(":type_of_c",$type_of_c, PDO::PARAM_STR);

                            //$stm->bindParam(":sn",$sn, PDO::PARAM_STR);


                            //$stm->bindParam(":fdate",$fdate, PDO::PARAM_STR);
                            //$stm->bindParam(":tday",$tday, PDO::PARAM_STR);
                            $stm->execute();
                            $rowCount = $stm->rowCount();
                            if($rowCount > 0){
                            ?>




                              <div class="table-responsive">
                              <h5 align="center">patient Report from <?php echo $fdate?> to <?php echo $tdate?></h5>
                              <table class="table table-bordered" id="dataTables-example">
                                  <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>Name</th>
                                        <th>birth</th>
                                        <th>Fixation Type</th>
                                        <th>Doctor</th>
                                        <th>No:Impalnt</th>
                                        <th>Implant System</th>
                                        <th>Cost</th>
                                        <th>Action</th>
                                    </tr>
                                  </thead>
                                  <tbody>
                                    <?php
                                    $results=$stm->fetchAll(PDO::FETCH_ASSOC);
                                    $cnt=1;
                                      foreach ($results as $row) {
                                      ?>
                                      <tr>
                                        <td><?php echo $row['id']; ?></td>
                                        <td><?php echo $row['n']; ?></td>
                                        <td><?php echo $row['b']; ?></td>
                                        <td><?php echo $row['fixation_t']; ?></td>
                                        <td><?php echo $row['doctor']; ?></td>
                                        <td><?php echo '<span class="badge badge-pill badge-success">Qty '.$row['Impalnt_n'].'</span>'; ?></td>
                                        <td><?php echo $row['type']; ?></td>
                                        <td>$<?php echo $totalmarks=$row['c']; ?></td>
                                          <td>
                                            <a href="edit_pateint.php?id=<?php echo htmlentities($row['id']);?>" class="btn btn-warning btn-sm"><i class="fa fa-edit"></i></a>
                                            <a href="patietlist.php?del=<?php echo $row['id']; ?>" class="btn btn-danger btn-sm"><i class="fa fa-trash"></i></a>
                                          </td>
                                      </tr>
                                      <?php $totlcount+=$totalmarks;}?>
                                    <tr>
                                    <th scope="row" colspan="7" style="text-align: center">Total Cost</th>
                                    <td colspan="3" style="text-align: left"><b><?php echo htmlentities($totlcount);?></td>
                                    </tr>
                                    <tr>
                                      <th scope="row" colspan="7" style="text-align: center">count</th>
                                      <td colspan="3" style="text-align: left"><b> <?php echo $row['total'];?></td>
                                    </tr>
                                  </tbody>
                              </table>
                          </div>

                          <?php
                        } else {
                            echo 'هیچ ئەنجامێك نەدۆزراوە!';
                          }
                       ?>
                   </div>
               </div>

             <?php } ?>
              </div>
              <script type="text/javascript">
              $('#fromdate').daterangepicker({
                singleDatePicker: true,
                locale: { format: "YYYY-MM-DD" }
                //startDate: moment().subtract(6, 'days')
              });
              </script>
              <script type="text/javascript">
              $('#todate').daterangepicker({
                singleDatePicker: true,
                locale: { format: "YYYY-MM-DD" }
                //startDate: moment().subtract(6, 'days')
              });
              </script>
<?php include_once 'footer.php'; ?>

It’s because you are using an aggregate function COUNT(). This will consolidate the result set in to a single row.

What exactly are you trying to count in that query?

Rows The number of rows as shown below

So… let’s take a moment to look at what you’ve got.

You are selecting 4 things: n, b, c, and total.

Here, you expect $row to contain id, n, b, fixation_t, doctor, Implant_n, type, and c.

There’s… quite a bit missing from your query, don’t you think?

How about -

If there is no data in the table, show msg there is not any data

It still didn’t get fix after you noticed it

Show us your updated code.

updated

<?php include_once 'header.php'; ?>
<?php include_once 'sidebar.php'; ?>
<?php include_once 'navtop.php'; ?>
                <div class="container-fluid">
                  <?php

                  $fix=output(@$_GET['fix']);
                  $type_of_c=output(@$_GET['type_of_c']);
                  $s=output(@$_GET['s']);
                  //$fdate=output(@$_GET['fdate']);
                  //$tday=output(@$_GET['tday']);

                  if (isset($_GET['class'])) {
                    ?>
                  <div class="card shadow mb-4">
                    <div class="card-header py-3">
                    </div>
                    <div class="card-body">
                      <form method="get">
                        <div class="form-group">
                          <label for="email">Fixation Type:</label>
                            <select class="form-control" name="fix" id="class_id">
                              <option>choose</option>
                              <option value="Screw">Screw</option>
                              <option value="Cement">Cement</option>
                           </select>
                        </div>
                        <div class="form-group">
                            <label for="email">Type of crown:</label>
                            <select class="form-control" name="type_of_c" id="class_id">
                              <option>choose</option>
                              <option value="ceramic">ceramic</option>
                              <option value="zirconium">zirconium</option>
                              <option value="Acrylic">Acrylic</option>
                              <option value="Emax">Emax</option>
                              <option value="Veneer">Veneer</option>
                              <option value="other">other</option>
                           </select>
                        </div>

                        <div class="form-group">
                          <label for="name">Size:</label>
                          <input type="text" name="s" value="" class="form-control" id="student_name" placeholder="Diameter">
                        </div>
                        <hr>
                        <div class="form-group">
                              <label>From Date</label>
                              <input type="text" name="fdate" class="form-control" value="" id="fdate">
                         </div>

                         <div class="form-group">
                              <label>To Date</label>
                             <input type="text" name="tday" class="form-control" value="" id="tday">
                         </div>


                         <div class="form-group" align="center">
                            <input type="submit" name="class" class="btn btn-primary btn-sm btn-block">
                         </div>
                        </form>
                            <?php
                            $stm = $db->prepare("SELECT * FROM patient WHERE fixation_t=:fix or type_of_c=:type_of_c or Size=:s ");
                            //$stm = $db->prepare("SELECT * FROM sub WHERE cl=:class or gr=:gr or sn=:sn or (datee between :fdate and :tday AND cl=:class) ");
                            $stm->bindParam(":fix",$fix, PDO::PARAM_STR);
                            $stm->bindParam(":type_of_c",$type_of_c, PDO::PARAM_STR);
                            $stm->bindParam(":s",$s, PDO::PARAM_STR);

                            //$stm->bindParam(":sn",$sn, PDO::PARAM_STR);


                            //$stm->bindParam(":fdate",$fdate, PDO::PARAM_STR);
                            //$stm->bindParam(":tday",$tday, PDO::PARAM_STR);
                            $stm->execute();
                            $rowCount = $stm->rowCount();
                            if($rowCount > 0){
                            ?>




                              <div class="table-responsive">
                              <h5 align="center">patient Report from <?php echo $fdate?> to <?php echo $tdate?></h5>
                              <table class="table table-bordered" id="dataTables-example">
                                  <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>Name</th>
                                        <th>birth</th>
                                        <th>Fixation Type</th>
                                        <th>Doctor</th>
                                        <th>No:Impalnt</th>
                                        <th>Implant System</th>
                                        <th>Cost</th>
                                        <th>Action</th>
                                    </tr>
                                  </thead>
                                  <tbody>
                                    <?php
                                    $results=$stm->fetchAll(PDO::FETCH_ASSOC);
                                    $cnt=1;
                                      foreach ($results as $row) {
                                      ?>
                                      <tr>
                                        <td><?php echo $row['id']; ?></td>
                                        <td><?php echo $row['name']; ?></td>
                                        <td><?php echo $row['birth']; ?></td>
                                        <td><?php echo $row['fixation_t']; ?></td>
                                        <td><?php echo $row['doctor']; ?></td>
                                        <td><?php echo '<span class="badge badge-pill badge-success">Qty '.$row['Impalnt_n'].'</span>'; ?></td>
                                        <td><?php echo $row['type']; ?></td>
                                        <td>$<?php echo $row['cost']; ?></td>
                                          <td>
                                            <a href="edit_pateint.php?id=<?php echo htmlentities($row['id']);?>" class="btn btn-warning btn-sm"><i class="fa fa-edit"></i></a>
                                            <a href="patietlist.php?del=<?php echo $row['id']; ?>" class="btn btn-danger btn-sm"><i class="fa fa-trash"></i></a>
                                          </td>
                                      </tr>
                                      <?php $totlcount+=$totalmarks;}?>
                                    <tr>
                                    <th scope="row" colspan="7" style="text-align: center">Total Cost</th>
                                    <td colspan="3" style="text-align: left"><b><?php echo htmlentities($totlcount);?></td>
                                    </tr>
                                    <tr>
                                      <th scope="row" colspan="7" style="text-align: center">count</th>
                                      <td colspan="3" style="text-align: left"><b> <?php echo $row['total']; ?></td>
                                    </tr>
                                  </tbody>
                              </table>
                          </div>

                          <?php
                        } else {
                            echo 'هیچ ئەنجامێك نەدۆزراوە!';
                          }
                       ?>
                   </div>
               </div>

             <?php } ?>
              </div>
              <script type="text/javascript">
              $('#fromdate').daterangepicker({
                singleDatePicker: true,
                locale: { format: "YYYY-MM-DD" }
                //startDate: moment().subtract(6, 'days')
              });
              </script>
              <script type="text/javascript">
              $('#todate').daterangepicker({
                singleDatePicker: true,
                locale: { format: "YYYY-MM-DD" }
                //startDate: moment().subtract(6, 'days')
              });
              </script>
<?php include_once 'footer.php'; ?>

I’m not sure what the actual problem is now, but you can’t use $row['total'] in your table footer - $row doesn’t exist outside of your foreach() loop. Also you add $totalmarks to a running total, but that variable doesn’t seem to get created anywhere.

Is the problem still the same, only displaying a single row? Does the query return all the rows when you test it in phpmyadmin?

Variable Undefined Error: $totlcount
Variable Undefined Error: $totalmarks

I’m going to assume from the previous query that the database table does not contain a column total, and even if it did, the foreach has closed, so

Variable Undefined Error: $row[‘total’];

Logical Error:

Your query makes no limitation based on these date values.

<?php include_once 'header.php'; ?>
<?php include_once 'sidebar.php'; ?>
<?php include_once 'navtop.php'; ?>
                <div class="container-fluid">
                  <?php

                  $ff=output(@$_GET['ff']);
                  $tyy=output(@$_GET['tyy']);
                  $ss=output(@$_GET['ss']);
                  $fdate=output(@$_GET['fdate']);
                  $tday=output(@$_GET['tday']);

                  if (isset($_GET['class'])) {
                    ?>
                  <div class="card shadow mb-4">
                    <div class="card-header py-3">
                    </div>
                    <div class="card-body">
                      <form method="get">

                        <div class="form-group">
                          <label for="email">Fixation Type:</label>
                            <select class="form-control" name="ff" id="class_id">
                              <option>choose</option>
                              <option value="Screw">Screw</option>
                              <option value="Cement">Cement</option>
                           </select>
                        </div>

                        <div class="form-group">
                            <label for="email">Type of crown:</label>
                            <select class="form-control" name="tyy" id="class_id">
                              <option>choose</option>
                              <option value="ceramic">ceramic</option>
                              <option value="zirconium">zirconium</option>
                              <option value="Acrylic">Acrylic</option>
                              <option value="Emax">Emax</option>
                              <option value="Veneer">Veneer</option>
                              <option value="other">other</option>
                           </select>
                        </div>

                        <div class="form-group">
                          <label for="name">Size:</label>
                          <input type="text" name="ss" value="" class="form-control" id="student_name" placeholder="Diameter">
                        </div>
                        <hr>
                        <div class="form-group">
                              <label>From Date</label>
                              <input type="text" name="fdate" class="form-control" value="" id="fdate">
                         </div>

                         <div class="form-group">
                              <label>To Date</label>
                             <input type="text" name="tday" class="form-control" value="" id="tday">
                         </div>
                         <div class="form-group" align="center">
                            <input type="submit" name="class" class="btn btn-primary btn-sm btn-block">
                         </div>
                        </form>
                            <?php
                            $stm = $db->prepare("SELECT id as idi, name as n, registration_d as r, doctor as d, Cost as c, count(id) as total FROM patient WHERE fixation_t=:ff || type_of_c=:tyy || Size=:ss");
                            $stm->bindParam(":ff",$ff, PDO::PARAM_STR);
                            $stm->bindParam(":tyy",$tyy, PDO::PARAM_STR);
                            $stm->bindParam(":ss",$ss, PDO::PARAM_STR);

                            //$stm->bindParam(":fdate",$fdate, PDO::PARAM_STR);
                            //$stm->bindParam(":tday",$tday, PDO::PARAM_STR);
                            //$stm->bindParam(":sn",$sn, PDO::PARAM_STR);
                            $stm->execute();
                            $rowCount = $stm->rowCount();
                            if($rowCount > 0){
                            ?>
                              <div class="table-responsive">
                              <h5 align="center">patient Report from <?php echo $fdate?> to <?php echo $tdate?></h5>
                              <table class="table table-bordered" id="dataTables-example">
                                  <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>Name</th>
                                        <th>re</th>
                                        <th>Doctor</th>

                                        <th>Cost</th>
                                        <th>Action</th>
                                    </tr>
                                  </thead>
                                  <tbody>
                                    <?php
                                    $results=$stm->fetchAll(PDO::FETCH_ASSOC);
                                    $cnt=1;
                                      foreach ($results as $row) {
                                      ?>
                                      <tr>
                                        <td><?php echo $row['idi']; ?></td>
                                        <td><?php echo $row['n']; ?></td>
                                        <td><?php echo $row['r']; ?></td>
                                        <td><?php echo $row['d']; ?></td>

                                        <td>$<?php echo $totalmarks=$row['c']; ?></td>
                                          <td>
                                            <a href="edit_pateint.php?id=<?php echo htmlentities($row['id']);?>" class="btn btn-warning btn-sm"><i class="fa fa-edit"></i></a>
                                            <a href="patietlist.php?del=<?php echo $row['id']; ?>" class="btn btn-danger btn-sm"><i class="fa fa-trash"></i></a>
                                          </td>
                                      </tr>

                                      <?php $totlcount+=$totalmarks;}?>
                                    <tr>
                                    <th scope="row" colspan="4" style="text-align: center">Total Cost</th>
                                    <td colspan="2" style="text-align: left"><b><?php echo htmlentities($totlcount);?></td>
                                    </tr>
                                    <tr>
                                      <th scope="row" colspan="4" style="text-align: center">count</th>
                                      <td colspan="2" style="text-align: left"><b> <?php echo $row['total']; ?></td>
                                    </tr>
                                  </tbody>
                              </table>
                          </div>

                          <?php
                        } else {
                            echo 'هیچ ئەنجامێك نەدۆزراوە!';
                          }
                       ?>
                   </div>
               </div>

             <?php } ?>
              </div>
              <script type="text/javascript">
              $('#fromdate').daterangepicker({
                singleDatePicker: true,
                locale: { format: "YYYY-MM-DD" }
                //startDate: moment().subtract(6, 'days')
              });
              </script>
              <script type="text/javascript">
              $('#todate').daterangepicker({
                singleDatePicker: true,
                locale: { format: "YYYY-MM-DD" }
                //startDate: moment().subtract(6, 'days')
              });
              </script>
<?php include_once 'footer.php'; ?>

yes it is onley show one row

You’ve put the count() back in to the query.

how can fix it?

Dont use your query to count or total things if you want to show the item-line information.

Pull the item-line information with your query, and use PHP to do the totalling and counting.

1 Like

for example how?

You’ve already got an example in your code.

First of all, define your count to start at 0 before the foreach.
Then, on each loop of the foreach, add that item’s cost to the total.
At the end of the loop, the total will have the total value of all of the items.

I did that and it wasn’t fix

You already have a count of the number of rows in the result set. It’s in $rowCount. Just echo it instead of $row[‘total’] (which does exist after the end of a foreach(){} loop if having count() as total in the query happened to be the solution, which it isn’t.)

1 Like