0

I am trying to get my code to display in an HTML table using the while loop. However, I can't get my table to display. Is there something wrong with the way I am trying to echo the table out?

 <?php


 #-----call includes-----# include('E:/includes/database.php'); 
 include('E:/includes/sendmail.php');

 ini_set('error_reporting', E_ALL ^ E_NOTICE); 
 ini_set('display_errors','on');


 $do = $_REQUEST['do']; 


$queryOrders = "select t2.SlpName As 'Seller', t1.SWW As 'Vendor', t0.DocDate As 'Date',t0.DocTotal As 'Total'
                from twh.dbo.OINV t0
                  inner join twh.dbo.inv1 t1  on t0.DocEntry = t1.DocEntry

         inner join twh.dbo.OSLP t2 on t1.SlpCode = t2.SlpCode
                where t0.DocDate > DATEADD (month, -2, getdate())
                  order by t0.DocTotal desc";

$resultItemData = sqlsrv_query($con, $queryOrders);


    echo "<table border=\"1\" align=\"center\">";
    echo "<tr><th>Seller</th>";
    echo "<th>Vendor</th>";
    echo "<th>Date</th>";
    echo "<th>Total</th></tr>"
    while($rowItemData = sqlsrv_fetch_array($resultItemData)){
        echo "<tr><td>";
        echo "$resultItemData";
        echo "</td></tr>";
    endwhile;

    }
    echo"</table>";
1
  • See answers they pinpoint your syntax issue.. Also would suggest, not doing echo for every line --- that is not needed. Or use a heredoc. Commented Aug 24, 2017 at 14:53

5 Answers 5

1

Modified a little bit. Check this:

while($rowItemData = sqlsrv_fetch_array($resultItemData)) :
    echo "<tr><td>";
    echo $rowItemData[ColumnValue]; //In your code, didn't access the column value earlier 
    echo "</td></tr>";
endwhile;
Sign up to request clarification or add additional context in comments.

Comments

1

There's a combination of problems at play:

  1. First, you open the while with {, but close with endwhile; - while technically not a problem, it's not consistent - if you open with {, it's best practice to close with }.

  2. Second, you're attempting to echo an entire array, which won't work properly.

  3. Third, no need to put the value inside of quotes: echo "$resultItemData"; could simply be echo $resultItemData.

  4. Fourth, you're attempting to echo $resultItemData, which is a resource, not the row data. You want to echo $rowItemData values.

  5. And finally, you'll likely want the results in an associative array, rather than a numerically-indexed array, so you might consider using sqlsrv_fetch_array( $resultItemData, SQLSRV_FETCH_ASSOC).

Below is your code, revised to work, and follow a bit better practices:

// columns: 'Seller',Vendor, 'Date', 'Total'
while( $rowItemData = sqlsrv_fetch_array( $resultItemData, SQLSRV_FETCH_ASSOC ) ) {
    echo "<tr><td>";
    echo "<td>$rowItemData['Seller']</td>";
    echo "<td>$rowItemData['Vendor']</td>";
    echo "<td>$rowItemData['Date']</td>";
    echo "<td>$rowItemData['Total']</td>";
    echo "</tr>";
}

1 Comment

Good explanation @cale_b.
0

You should specify the indexes of the array $rowitemdata.

For example:

echo"<tr><td>".$rowitemdata['Vendor']." 

Comments

0

I haven't gone through your full code, but I can see wrong syntax for while

while($rowItemData = sqlsrv_fetch_array($resultItemData)){
    echo "<tr>";
    echo "<td>".$resultItemData['Seller']."</td>";
    echo "<td>".$resultItemData['Vendor']."</td>";
    echo "<td>".$resultItemData['Date']."</td>";
    echo "<td>".$resultItemData['Total']."</td>";
    echo "</tr>";
}

or

while($rowItemData = sqlsrv_fetch_array($resultItemData)) :
    echo "<tr>";
    echo "<td>".$resultItemData['Seller']."</td>";
    echo "<td>".$resultItemData['Vendor']."</td>";
    echo "<td>".$resultItemData['Date']."</td>";
    echo "<td>".$resultItemData['Total']."</td>";
    echo "</tr>";
endwhile;

Update: still you can optimize the code, I just gave sample working

2 Comments

Yes but you did not point out the issue with trying to echo out a query handle.
echo $resultItemData; is suspicious to me. Which results ..Catchable fatal error: Object of class sqlsrv_result could not be converted to string
0

Let's assume your query actually produces a result, because you don't check that it does:

$resultItemData = sqlsrv_query($con, $queryOrders);

$header = <<<HEREDOC
<table border="1" align="center">
<tr>
  <th>Seller</th>
  <th>Vendor</th>
  <th>Date</th>
  <th>Total</th>
</tr>
HEREDOC;

echo $header;

while($rowItemData = sqlsrv_fetch_array($resultItemData)) {
    echo "<tr>
            <td>{$rowItemData['Seller']}</td>
            <td>{$rowItemData['Vendor']}</td>
            <td>{$rowItemData['Date']}</td>
            <td>{$rowItemData['Total']}</td>
          </tr>";
}
echo '</table>';

To actually check that the query works, you might want to do this instead. This is just to debug/illustrate error checking for the query execution. You wouldn't want to output an error to the screen, but rather log it. You probably want to just output the table header/footer and skip the while/loop entirely.

$resultItemData = sqlsrv_query($con, $queryOrders);
if (resultItemData === false) {
    die(print_r(sqlsrv_errors(), true));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.