We help IT Professionals succeed at work.

Help with php echo

doctorbill
doctorbill asked
on
117 Views
Last Modified: 2019-01-19
 <?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

Comment
Watch Question

gr8gonzoConsultant
CERTIFIED EXPERT

Commented:
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

Author

Commented:
<?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?
CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008

Commented:
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

Author

Commented:
If I use this I get page not displayed error

Author

Commented:
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
gr8gonzoConsultant
CERTIFIED EXPERT

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

echo $results->num_rows;

Author

Commented:
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

Author

Commented:
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

Author

Commented:
Sorry - The and tags are not in the code

Author

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

Open in new window

Author

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

Open in new window

Consultant
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
I cannot believe I missed that. Thanks - perfect

Author

Commented:
Closed

Author

Commented:
Thanks so much for your help
gr8gonzoConsultant
CERTIFIED EXPERT

Commented:
You're welcome!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.