0

I'm trying to write data from a query to an HTML table using PHP and MS SQL. I can successfully write the data of text, integer and float fields to the table. However, when I try to format date columns, the whole process seems to break.

    <?php           
echo "<table id='stakeholderTable' class='display nowrap'>
     <thead>
        <tr>";
            foreach ($array as $colmn) {
                echo '<th><strong>' . $colmn . '</strong></th>';
            }
        echo "</tr>
    </thead>";

    echo '<tbody>';
        $sel_result = sqlsrv_query($con,$query);
        while($sel_row = sqlsrv_fetch_array($sel_result)) {
            echo '<tr>';
                foreach ($array as $colmn) {
                    $colmn2 = str_replace("'", "", $colmn);
                    $joe = str_replace("tbl_actor.", "", $colmn2);
                    $joe = str_replace("tbl_commitment.", "", $joe);
                    if($joe == 'com_date' || $joe == 'com_due') {
                        echo '<td>' . $sel_row[$joe]->format('Y-m-d') . '</td>';
                    }else{
                        echo '<td>' . $sel_row[$joe] . '</td>';
                    }
                }
            echo '</tr>';
        }
    echo '</tbody>'; 
echo '</table>';
?>

The code above code works as long as the value is not a date (com_date or com_due). If the value is a date, the first row of the table will print in the correct format, but no subsequent rows will print and the page's Javascript stops working.

The problem appears to be with the line:

echo '<td>' . $sel_row[$joe]->format('Y-m-d') . '</td>';

but I can't figure out what is wrong with it.

Any help would be greatly appreciated

Cheers,

4
  • 1
    $sel_row[$joe] is just array element type of string or integer. but you are calling ->format('Y-m-d') method. Those values are not objects - they don't have any methods to call Commented Dec 18, 2018 at 18:51
  • @Alex The array is just a list of field names. You may be missing the While loop which is cycling through the data. I can call methods in the while loop Commented Dec 18, 2018 at 19:17
  • 1
    you can call methods whenever you want but only against objects but not primitive types which do not support any. Commented Dec 18, 2018 at 19:25
  • It was the null values that were screwing it up. I had to check if the value was null before calling the method. My answer below has the fix. I think the foreach in the while loop made things more confusing. Commented Dec 18, 2018 at 19:28

2 Answers 2

1

The dates are probably already a string object in php, so you'd have to create date objects, and then format them. PS I would have left this as a comment but I don't have enough reputation.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Kevin. They were coming in as date objects if they had dates in the database but, if they were null (no data in the cell), they had to be formatted as a date. Don't know if it's possible to format null as a date, so, I just put in another if statement.
0

Alright. Figured it out. I was calling a method on null cells. I had to check for null values before running the code.

Change:

foreach ($array as $colmn) {
    $colmn2 = str_replace("'", "", $colmn);
    $joe = str_replace("tbl_actor.", "", $colmn2);
    $joe = str_replace("tbl_commitment.", "", $joe);
    if($joe == 'com_date' || $joe == 'com_due') {
        echo '<td>' . $sel_row[$joe]->format('Y-m-d') . '</td>';
    }else{
        echo '<td>' . $sel_row[$joe] . '</td>';
    }
}

To:

foreach ($array as $colmn) {
    $colmn2 = str_replace("'", "", $colmn);
    $joe = str_replace("tbl_actor.", "", $colmn2);
    $joe = str_replace("tbl_commitment.", "", $joe);
    if($joe == 'com_date' || $joe == 'com_due') {
        if(!empty($sel_row[$joe])){
            echo '<td>' . $sel_row[$joe]->format('Y-m-d') . '</td>';
        }else{
            echo '<td>' . $sel_row[$joe] . '</td>';
        }
    }else{
        echo '<td>' . $sel_row[$joe] . '</td>';
    }
}

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.