0

I have an issue where I'm doing a sqlsrv_fetch_array to fetch rows from a SQL server query.

I'm then doing some post processing to echo the rows that fall within the working day criteria using the workingdays function here

the variable $esc is a number E.G. "7 Days" hence we pull the number only. My resulting post processing syntax looks like this.

<?php
Include 'Connectioninfo.php';
Include 'wkgdaysfunction.php';

$sql = "SELECT some SQL"; 

$result = sqlsrv_query($conn,$sql);

echo "<table id=\"table\" class=\"tablesorter\"><thead><tr><th><center>Ticket#</center></th></tr></thead><tbody>";

while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {

// The Priortiy row creates an array showing as "X days" based on the priority and is taken from the DB
$esc = isset($esccom[$row["PRIORITY"]])?$esccom[$row["PRIORITY"]]:"";

$endDate = time();  //strtotime("now"),"\n";

//START_DATE is from the DB
$startDate = strtotime(date('m/d/y', $row["START_DATE"])  );

$workingDays = floor(getWorkingDays($startDate,$endDate,$holidays)) ;

if($workingDays >= (substr($esc,0,1) -1)){  
echo "<tr id=".$rowColour."><td>" . $row["TICKET"] . "</td></tr>";
 }
}
echo "</tbody></table>";

sqlsrv_close( $conn );
?>

As expected this returns the rows where the number of working days bewteen X&Y are greater than or equal to $esc.

What I'm struggling to work out now is how to count the number of rows and echo the number returned from the post processing.

Is there some form of countif I can use?

nixxrite
  • 133
  • 1
  • 1
  • 8
  • Just do the counting inside the `if` block. – Nick Dec 14 '18 at 11:51
  • Isnt this what you need ? sqlsrv_num_rows — Retrieves the number of rows in a result set. Actually I cant figure out which variable you pull from the database. Best way in my opinion is to do your IF condition within SQL query and then you are processing only valid rows and have the number of valid results available via the mentioned function. – John Dec 14 '18 at 12:33
  • I've added the rest of the included code for reference. I originally did a case statement in the SQL as the PRIORITY has a range of 0-3 so the number of working days may differ per row. The problem with this was using the SQL in tandem with the workingdays function hence doing the post processing to get the number of rows within the function. – nixxrite Dec 14 '18 at 13:25
  • Adding `$rowcount=0;` outside the `IF` and `$rowcount++;` inside just returns one result. Putting it outside the `While` returns a count of all rows in the DB. I'm just looking for the count of rows returned in the `IF` statement. – nixxrite Dec 14 '18 at 14:28

0 Answers0