Home > database >  How to code a row count and where to put it in my current code
How to code a row count and where to put it in my current code

Time:03-06

I have created a successful query and have all my row headers set up and ready for a row count under the "Q#" column. I can't figure out how to actually create the code to assign the num_rows=0 and then count up by one and insert it into my table.

Form example (without the numbered rows): https://fireytech.com/FireytechDatabase/sandbox.php

I have tried to ask this question before but people keep closing it as a duplicate and sendng me to an existing article which I have read and tried to apply to my code but I haven't been able to figure it out. Please help me understand how to code the variable "$rn= $num_rows=0; " to insert into my table.

Current code (I only included the table section):

$result = $conn->query($sql);

echo"<table border='1'>";
echo "<tr>
<th>Q#</th>
<th>Ticket Type</th>
<th>Ticket Number</th>
<th>Date Received</th>
<th>Last Edited</th>
<th>Last Name</th>
<th>Followup By</th>
<th>Sub Status</th>
<th>Repair Type</th>
<th>Edit</th>
<tr>";

if ($result->num_rows > 0){
while($row = $result->fetch_assoc() ){
    $rn= $num_rows=0;
    $dr= date("m/d/Y", strtotime($row['DateReceived']));
    $dl= date("m/d/Y", strtotime($row['DateLastEdited']));
    echo "<tr>
    <td>{$row['$rn']}</td>
    <td>{$row['TaskSubType']}</td>
    <td>{$row['TicketNumber']}</td>
    <td>{$dr}</td>
    <td>{$dl}</td>
    <td>{$row['ContactLast']}</td>
    <td>{$row['FollowupBy']}</td>
    <td>{$row['TicketSubStatus']}</td>
    <td>{$row['ItemType']}</td>
    <td>{$row['']}</td>
    </tr>";
    
}
} else {
    echo "0 records";

CodePudding user response:

$result = $conn->query($sql);

echo"<table border='1'>";
echo "<tr>
<th>Q#</th>
<th>Ticket Type</th>
<th>Ticket Number</th>
<th>Date Received</th>
<th>Last Edited</th>
<th>Last Name</th>
<th>Followup By</th>
<th>Sub Status</th>
<th>Repair Type</th>
<th>Edit</th>
<tr>";

if ($result->num_rows > 0){
    $rn = 0;
while($row = $result->fetch_assoc() ){
    $rn  ;
    $dr= date("m/d/Y", strtotime($row['DateReceived']));
    $dl= date("m/d/Y", strtotime($row['DateLastEdited']));
    echo "<tr>
    <td>{$rn}</td>
    <td>{$row['TaskSubType']}</td>
    <td>{$row['TicketNumber']}</td>
    <td>{$dr}</td>
    <td>{$dl}</td>
    <td>{$row['ContactLast']}</td>
    <td>{$row['FollowupBy']}</td>
    <td>{$row['TicketSubStatus']}</td>
    <td>{$row['ItemType']}</td>
    <td>{$row['']}</td>
    </tr>";
    
}
} else {
    echo "0 records";

This will solve your problem. Here is a similar working example

  •  Tags:  
  • php
  • Related