Displaying an order status form Below is code i used to call Api and display the "Status" results in web page.



my senior told me that code is completely mess and need to improve so that it should be easy to understand for others. but i am not getting how i can refactor below code ? is mixing php and html code is bad ? i really need to mix those html and php to get the results that i needed .please help me for this....

&lt;p&gt;&lt;button type= "button" class="call"&gt;Show Status&lt;/button&gt;&lt;/p&gt;

    &lt;table class="tbl-qa" border="1"&gt;
        &lt;thead&gt;
            &lt;tr&gt;    
               &lt;th class="table-header"&gt;&lt;/th&gt;     
               &lt;th class="table-header"&gt;ORDERID&lt;/th&gt;                                                      
               &lt;th class="table-header"&gt;Status&lt;/th&gt;                    
            &lt;/tr&gt;
        &lt;/thead&gt;

       &lt;tbody id="table-body"&gt;
&lt;?php
$tabindex = 1;

if (!empty($orderrecords))
    {
    foreach($orderrecords as $k =&gt; $v)
        { ?&gt;

        &lt;?php
        $hide = '';
        $data['username'] = 'admin';
        $data['password'] = 'admin123';     

        $url = 'https://plapi.ecomexpress.in/track_me/api/mawbd/?awb=awbnumber&amp;order=' . $orderrecords[$k]["order_id"] . '&amp;username=admin&amp;password=admin123';
        $ch = curl_init();
        // some curl code
        $res = explode("\n", $output);
        if (!isset($res[13]))
            {
            $res[13] = null;
            }

        $status = $res[13];
?&gt;

&lt;tr class="table-row" id="table-row-&lt;?php echo $orderrecords[$k]["id"]; ?&gt;" tabindex="&lt;?php echo $tabindex; ?&gt;"&gt;

&lt;td&gt;&lt;input onclick="assignorderids('&lt;?php echo $orderrecords[$k]["order_id"]; ?&gt;')" type="checkbox" name="assigneeid" id="assigneeid-&lt;?php echo $orderrecords[$k]["order_id"]; ?&gt;" value="&lt;?php echo $orderrecords[$k]["order_id"]; ?&gt;"&gt;&lt;/td&gt;

&lt;td&gt;&lt;?php echo $orderrecords[$k]["order_id"]; ?&gt;&lt;/td&gt;             
&lt;td&gt;&lt;?php echo $status; ?&gt;&lt;/td&gt;

&lt;/tr&gt;


 &lt;?php
 $tabindex++;
 }
    } ?&gt;

    &lt;/tbody&gt;
    &lt;/table&gt;
I can totally understand the frustration of your colleague that will need to maintain this code.

But would have been more constructive to tell that this code is considered a "mess" mainly because:


Logic of fetching order status is mixed in the middle of table rendering;
"Dirty" formatting: inconsistent indentation and trailing spaces;


The exact same thing could be better expressed like that:

&lt;?php
function getOrderTrackingStatus($orderId) {
    $url = 'https://plapi.ecomexpress.in/track_me/api/mawbd/';
    $url.= "?awb=awbnumber&amp;order=$orderId&amp;username=admin&amp;password=admin123";

    $ch = curl_init();
    // some curl code
    $res = explode("\n", $output);

    $status = isset($res[13]) ? $res[13] : null;
    return $status;
}
?&gt;

&lt;p&gt;
    &lt;button type="button" class="call"&gt;Show Status&lt;/button&gt;
&lt;/p&gt;

&lt;table class="tbl-qa" border="1"&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th class="table-header"&gt;&lt;/th&gt;
            &lt;th class="table-header"&gt;ORDERID&lt;/th&gt;
            &lt;th class="table-header"&gt;Status&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;

    &lt;tbody id="table-body"&gt;
    &lt;?php $tabindex = 1; ?&gt;
    &lt;?php foreach($orderrecords as $record): ?&gt;

        &lt;tr class="table-row"
            id="table-row-&lt;?php echo $record['id']; ?&gt;"
            tabindex="&lt;?php echo $tabindex++; ?&gt;"&gt;
            &lt;td&gt;
                &lt;input type="checkbox"
                       name="assigneeid"
                       id="assigneeid-&lt;?php echo $record['order_id']; ?&gt;"
                       value="&lt;?php echo $record['order_id']; ?&gt;"
                       onclick="assignorderids('&lt;?php echo $record['order_id']; ?&gt;')"&gt;
            &lt;/td&gt;
            &lt;td&gt;&lt;?php echo $orderId; ?&gt;&lt;/td&gt;
            &lt;td&gt;&lt;?php echo getOrderTrackingStatus($orderId); ?&gt;&lt;/td&gt;
        &lt;/tr&gt;

    &lt;?php endforeach; ?&gt;
    &lt;/tbody&gt;
&lt;/table&gt;


Ideally this getOrderTrackingStatus function would be moved somewhere else, instead of living inside the same file that handles data visualisation.

Hope it helps you, and depending on your project you should consider using a templating engine as a way to force yourself to move logic code away from the views.