Link to home
Start Free TrialLog in
Avatar of doctorbill
doctorbillFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Help with php echo

 <?php
            
            $query = $conn->prepare("SELECT sup_id, sup_ticketnumber, sup_company, sup_contact, sup_email, sup_phone, sup_rep, sup_comments, sup_addcom1, sup_addcom2, sup_addcom3, sup_editcom1, sup_editcom2, sup_editcom3, sup_todo, sup_timefrom,  sup_timeto,  sup_date,  radnew, radinprogress, radclosed, radnocategory, chkemail, chkphone, chksms, chkproject FROM support ORDER BY sup_date asc");
            
            $query->execute();
            $results = $query->get_result();
            ?>
I need to echo out the sum of the sup_id column
Help please

Open in new window

Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Something like this?

...
$results = $query->get_result();

$sum_of_sup_id = 0;
while ($row = $results->fetch_array(MYSQLI_BOTH))
{
  $sum_of_sup_id += $row["sup_id"];
}
echo $sum_of_sup_id;

Open in new window

Avatar of doctorbill

ASKER

<?php
            // Let's run the query
            $query = $conn->prepare("SELECT sup_id, sup_ticketnumber, sup_company, sup_contact, sup_email, sup_phone, sup_rep, sup_comments, sup_addcom1, sup_addcom2, sup_addcom3, sup_editcom1, sup_editcom2, sup_editcom3, sup_todo, sup_timefrom,  sup_timeto,  sup_date,  radnew, radinprogress, radclosed, radnocategory, chkemail, chkphone, chksms, chkproject FROM support ORDER BY sup_date asc");
            
            $query->execute();
            $results = $query->get_result();
        
 <?php while ($row = $results->fetch_assoc()):$style = "";
?>

 <table id="customers">
                 <tr>
                  <th>ID:</th>
                     <th>Ticket Number:</th>
                  <th>Company:</th>
                  <th>Contact:</th>
                  <th>Ticket Status:</th>
                     <th>Ticket Status:</th>
                     <th>Ticket Status:</th>
                     <th>Ticket Status:</th>
                 </tr>
  
                
       
                    <tr>
                        <td><?= $row['sup_id'] ?>&nbsp;<?php echo "<a href=\"FileEdits/viewformSupportTickets.php?id=" . $row['sup_id'] ."\" target=\"_blank\">RecordLink... </a>"; ?></td>
                        <td><?= $row['sup_ticketnumber'] ?></td>
                        <td><?= $row['sup_company'] ?></td>
                        <td><?= $row['sup_contact'] ?></td>
                        <td <?php echo $style2 ?>></td>
                        <td <?php echo $style3 ?>></td>
                        <td <?php echo $style4 ?>></td>                       
                        <td <?php echo $style5 ?>><?= $row['radnew'] ?></td>
                
                    </tr>
        </table>
                
      
            <?php endwhile; ?>
        



        
            ?>

Open in new window

Question:
Where do I put your script so that the echo is at the end of the table and does not disrupt the current structure?
Try:
<?php
            // Let's run the query
            $query = $conn->prepare("SELECT sup_id, sup_ticketnumber, sup_company, sup_contact, sup_email, sup_phone, sup_rep, sup_comments, sup_addcom1, sup_addcom2, sup_addcom3, sup_editcom1, sup_editcom2, sup_editcom3, sup_todo, sup_timefrom,  sup_timeto,  sup_date,  radnew, radinprogress, radclosed, radnocategory, chkemail, chkphone, chksms, chkproject FROM support ORDER BY sup_date asc");
            
            $query->execute();
            $results = $query->get_result();
$sum = 0;        
 while ($row = $results->fetch_assoc()):
    $style = "";
    $sum += $row['sup_id'];
?>

 <table id="customers">
                 <tr>
                  <th>ID:</th>
                     <th>Ticket Number:</th>
                  <th>Company:</th>
                  <th>Contact:</th>
                  <th>Ticket Status:</th>
                     <th>Ticket Status:</th>
                     <th>Ticket Status:</th>
                     <th>Ticket Status:</th>
                 </tr>
  
                
       
                    <tr>
                        <td><?= $row['sup_id'] ?>&nbsp;<?php echo "<a href=\"FileEdits/viewformSupportTickets.php?id=" . $row['sup_id'] ."\" target=\"_blank\">RecordLink... </a>"; ?></td>
                        <td><?= $row['sup_ticketnumber'] ?></td>
                        <td><?= $row['sup_company'] ?></td>
                        <td><?= $row['sup_contact'] ?></td>
                        <td <?php echo $style2 ?>></td>
                        <td <?php echo $style3 ?>></td>
                        <td <?php echo $style4 ?>></td>                       
                        <td <?php echo $style5 ?>><?= $row['radnew'] ?></td>
                
                    </tr>
        </table>
                
      
            <?php endwhile; 

        echo '<p>Total: ', $sum,'</p>';        
            ?>

Open in new window

If I use this I get page not displayed error
Sorry - working now. Just one change - how do I sum the number of records rather than sum the values
ie If there are 20 records in the recordset how do I show this as an echo value
You should be able to use the num_rows propery of the result:

echo $results->num_rows;
I get this:
Notice: Undefined variable: result in C:\xampp\htdocs\Development\Inventas\Sites\TicketStatus_AllStatusSearchMetrics.php on line 83

Notice: Trying to get property of non-object in C:\xampp\htdocs\Development\Inventas\Sites\TicketStatus_AllStatusSearchMetrics.php on line 83
This is the code giving the error:
 $query->execute();
            $results = $query->get_result();
        
      [b]  $row_cnt = $result->num_rows;[/b]
        $sum = 0; 
        $sum2 = 0;  
 while ($row = $results->fetch_assoc()):
    $style = "";
    $sum += $row['sup_id'];
      $sum2 += $row['radinprogress'];  
       
            ?>
   
      
            <?php endwhile;
        
 echo '<p>Total: ', $sum,'</p>';
        echo '<p>Total2: ', $sum2,'</p>';
        [b]printf("Result set has %d rows.\n", $row_cnt);[/b]
       
        ?>

Open in new window

Sorry - The and tags are not in the code
I mean the
[b] and [/b] tags

Open in new window

This is the undefined variable:
$row_cnt = $result->num_rows;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I cannot believe I missed that. Thanks - perfect
Closed
Thanks so much for your help
You're welcome!