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

PHPMySQL Server

Avatar of undefined
Last Comment
gr8gonzo

8/22/2022 - Mon
gr8gonzo

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

ASKER
doctorbill

<?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?
hielo

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

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER
doctorbill

If I use this I get page not displayed error
ASKER
doctorbill

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
gr8gonzo

You should be able to use the num_rows propery of the result:

echo $results->num_rows;
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
doctorbill

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
ASKER
doctorbill

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

ASKER
doctorbill

Sorry - The and tags are not in the code
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER
doctorbill

I mean the
[b] and [/b] tags

Open in new window

ASKER
doctorbill

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

Open in new window

ASKER CERTIFIED SOLUTION
gr8gonzo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
doctorbill

I cannot believe I missed that. Thanks - perfect
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
doctorbill

Closed
ASKER
doctorbill

Thanks so much for your help
gr8gonzo

You're welcome!
Your help has saved me hundreds of hours of internet surfing.
fblack61