2

I'm trying to output a table using php and ajax. Now my function gets data from 1 table and then iterate on the row of that table to get rows from different table and then form a table. To achieve this I have put while loop inside foreach loop, the data from the first table is in array, so Im iterating on array using forean and then putting while loop. This results in only 1 row. Need help

public function GetReportData()
{
   $status_list = $this->GetStatusList();

    foreach ($status_list as $status)
    {
        $staus_orignal = $status->orignal_status;
        $status_site =  $status->site_status;
        try
        {
        $db = $this->GetDBHandle();
        $start_date = '05/01/2015';
        $end_date = '05/02/2015';
        $affiliate_id = 0;

        $output_string = '';
        $output_string .=  '<table class="tg">
          <tr>
            <th class="tg-031e"><span style="color:#fff;">Disposition Type</span></th>
            <th class="tg-yw4l"><span style="color:#fff;">Lead Count</span></th>
            <th class="tg-yw4l"><span style="color:#fff;">Revenue</span></th>
          </tr>';

        $query = "exec affiliate_portal_report_data_select $staus_orignal, $affiliate_id,'". $start_date."','". $end_date."'";
        $result = odbc_exec ( $db, $query );

        if ( !$result )
        {
             throw new Exception ( 'Error from  ' . $query . ': ' . odbc_errormsg() );
        }

        else
        {
            while ( odbc_fetch_row ( $result ) )
            {

               $lead_count = odbc_result( $result, 'leadcount' );
               $revenue = odbc_result( $result, 'revenue' );
               $output_string .= '<tr>
                <td class="tg-yw4l">'.$status_site.'</td>
                <td class="tg-yw4l">'.$lead_count.'</td>
                <td class="tg-yw4l dollar">'.$revenue.'</td>
              </tr>';
            }

        }
        }

    catch ( Exception $e )        
        {
        $error_status = $e->getMessage();
        print $error_status;
        }

    }
    $output_string .=  '</table>';
    return $output_string;
}
2
  • If you put a "var_dump(odbc_num_rows ( $result ));" above the while loop, how many rows does it say you have in the $result object? Commented Feb 11, 2016 at 22:28
  • 1
    Looks like you need to initialize your $output_string outside the foreach loop. Commented Feb 11, 2016 at 22:31

2 Answers 2

1

There is an $output_string = ''; on line 16. You are re-initializing your output on each iteration of the foreach loop, so you will only ever get the last row.

I am not entirely sure from your question, but if this code is supposed to produce one table, then you can get rid of $output_string = ''; altogether, put this:

$output_string =  '<table class="tg">
<tr>
    <th class="tg-031e"><span style="color:#fff;">Disposition Type</span></th>
    <th class="tg-yw4l"><span style="color:#fff;">Lead Count</span></th>
    <th class="tg-yw4l"><span style="color:#fff;">Revenue</span></th>
</tr>';

before the foreach loop, and leave this:

$output_string .=  '</table>';

after the foreach loop (like it already is).

But if your code is supposed to produce multiple tables, then you still need to get rid of $output_string = '';, and you can leave the <th> section where it is, but you'll need to move $output_string .= '</table>'; inside the foreach loop, or you will end up with a bunch of unclosed table tags.

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

Comments

0

As said by @Don'tPanic in comments, you have to initialise your $output_string variable outside of the loop.

Like actual, you are recreating an empty string for each row.
If you build an array or a string by looping another, keep in mind to make the variable declaration outside, and the incrementation inside the loop.

Change your code to :

$output_string = '';

foreach ($status_list as $status) {
    // ...
    $output_string .= 'yourstring';
    // ...
}

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.