Jump to content

Easier Way to Output Table from SQL Server


ncurran217

Recommended Posts

I have this code here and it works, just wondering if there is an easier and cleaner way to do this or not. Here is the code I have:

 

<?php
include 'includes/db_connectdate.php';
$Start_Date = $_GET['start_date'];
$End_Date = $_GET['end_date'];
$Forte_ID = $_GET['ForteID'];


$query = ' SELECT Reps.Rep, Logs.RefNumber, Logs.Disposition, Logs.Cancel_Disposition, Logs.Date, Logs.appNumber, 
  Logs.Phone_Num, Logs.Con_Number, Logs.Finance_Num, Logs.Num_Payments, Logs.ACH_CC, Logs.Post_Date, 
  Logs.Callback, Logs.Disc_Amount, Logs.Total_Cost, Logs.Total_MP, Logs.New_MP_Amt, Logs.New_DP_Amt, Logs.Notes
  FROM Logs INNER JOIN Reps ON Logs.ForteID = Reps.ForteID
  WHERE Logs.Date BETWEEN \''.$Start_Date.'\' AND \''.$End_Date.'\' AND Logs.ForteID = \''.$Forte_ID.'\' ORDER BY Logs.RefNumber';
$result = sqlsrv_query($connection,$query);
if (!$result)
{
$message = 'ERROR: ' . sqlsrv_errors();
return $message;
}
else
{ 
echo '<table border="1" cellpadding="5" style="border:3px solid black; text-align: center;">
 <tr>
  <th style="border:3px solid black;">Rep</th>
  <th style="border:3px solid black;">Reference Number</th>
  <th style="border:3px solid black;">Call Disposition</th>
  <th style="border:3px solid black;">Cancel Disposition</th>
  <th style="border:3px solid black;">Date</th>
  <th style="border:3px solid black;">AppNumber</th>
  <th style="border:3px solid black;">Phone Number</th>
  <th style="border:3px solid black;">Contract Number</th>
  <th style="border:3px solid black;">Finance Number</th>
  <th style="border:3px solid black;"># of Payments</th>
  <th style="border:3px solid black;">ACH/CC</th>
  <th style="border:3px solid black;">Post Date</th>
  <th style="border:3px solid black;">Callback</th>
  <th style="border:3px solid black;">Discount Amount</th>
  <th style="border:3px solid black;">New Total Cost</th>
  <th style="border:3px solid black;">Total Monthly Payments</th>
  <th style="border:3px solid black;">New MP Amount</th>
  <th style="border:3px solid black;">New DP Amount</th>
  <th style="border:3px solid black;">Notes</th>
 </tr>';


while ( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_ASSOC ))
{
 echo '<tr style="border:3px solid black; text-align: center;">';
 echo '<td nowrap>'.$row['Rep'].'</td>';
 echo '<td nowrap>'.$row['RefNumber'].'</td>';
 echo '<td nowrap>'.$row['Disposition'].'</td>';
 echo '<td nowrap>'.$row['Cancel_Disposition'].'</td>';
 echo '<td nowrap>'.$row['Date'].'</td>';
 echo '<td nowrap>'.$row['appNumber'].'</td>';
 echo '<td nowrap>'.$row['Phone_Num'].'</td>';
 echo '<td nowrap>'.$row['Con_Number'].'</td>';
 echo '<td nowrap>'.$row['Finance_Num'].'</td>';
 echo '<td nowrap>'.$row['Num_Payments'].'</td>';
 echo '<td nowrap>'.$row['ACH_CC'].'</td>';
 echo '<td nowrap>'.$row['Post_Date'].'</td>';
 echo '<td nowrap>'.$row['Callback'].'</td>';
 echo '<td nowrap>'.$row['Disc_Amount'].'</td>';
 echo '<td nowrap>'.$row['Total_Cost'].'</td>';
 echo '<td nowrap>'.$row['Total_MP'].'</td>';
 echo '<td nowrap>'.$row['New_MP_Amt'].'</td>';
 echo '<td nowrap>'.$row['New_DP_Amt'].'</td>';
 echo '<td nowrap>'.$row['Notes'].'</td>';
 echo '</tr>';
 }
echo '</table>';
}
sqlsrv_free_stmt ($result);
sqlsrv_close( $connection);
?>

 

Thanks for the help in advance!

Link to comment
Share on other sites

You would use CSS to eliminate all the repetitive in-line style information in the tags -

 

<style>
table,th {border:3px solid black;}
td {border:1px solid black;}
th,td {text-align: center; padding: 5px;}
td {white-space: nowrap;}
</style>

 

And you could let the computer produce the table based on a defining array -

 


   // each array key is the database column name, the corresponding value is the legend/heading to display in the HTML table
   // the order of the items in this array are the order they will be output in the HTML table
$fields = array('Rep'=>'Rep','RefNumber'=>'Reference Number','Disposition'=>'Call Disposition',
   'Cancel_Disposition'=>'Cancel Disposition','Date'=>'Date','appNumber'=>'AppNumber',
   'Phone_Num'=>'Phone Number','Con_Number'=>'Contact Number','Finance_Num'=>'Finance Number',
   'Num_Payments'=>'# of Payments','ACH_CC'=>'ACH/CC','Post_Date'=>'Post Date','Callback'=>'Callback',
   'Disc_Amount'=>'Discount Amount','Total_Cost'=>'New Total Cost','Total_MP'=>'Total Monthly Payments',
   'New_MP_Amt'=>'New MP Amount','New_DP_Amt'=>'New DP Amount','Notes'=>'Notes');

// start table and produce table heading
echo "<table>\n<tr>";
foreach($fields as $legend){
   echo "<th>$legend</th>";
}
echo "</tr>\n";

// output table data
while($row = sqlsrv_fetch_array( $result,SQLSRV_FETCH_ASSOC)){
   echo "<tr>";
   foreach($fields as $key=>$not_used){
       echo "<td>$row[$key]</td>";
   }
   echo "</tr>\n";
}
echo "</table>\n";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.