0

I'm a newbie who's been looking for a solution regarding inserting a PHP foreach output in HTML,

there is my query code:

$qr = $this->db->query("select journalDetail.COA_CODE, COA_TITLE, COA_TYPE from t_journal_detail journalDetail left join r_coa coaTitle on journalDetail.coa_code=coaTitle.coa_code where journalDetail.JOURNAL_ID = '".$journalId."'");
$coaTitle = $qr->result_array();

    foreach ($coaTitle as $coa) {
        echo '<td width=55><span style=font-size:10pt; line-height:19px; text-align:left>'.$coaTitle[0]['COA_CODE'].'</span></td><br>';
    }

and there is my html code:

$detailJournal = '<!DOCTYPE html>
                        <html>
                            <table> 
                                <tr>            
                            <table border = 0 cellspacing = 0 cellpadding = 0 align = center>
                                <tr>
                                    <td width=55><span style=font-size:10pt; line-height:19px; text-align:left>'.$coaTitle[0]['COA_CODE'].'</span></td>
                                    <td width=170><span style=font-size:10pt; line-height:19px; text-align:center>'.$coaTitle[0]['COA_TITLE'].'</span></td>
                                    <td width=55><span style=font-size:10pt; line-height:19px; text-align:center>IDR</span></td>
                                    <td width=81><span style=font-size:10pt; line-height:19px; text-align:right>'.$journalDetail[0]['ORIG'].'</span></td>
                                    <td width=89><span style=font-size:10pt; line-height:19px; text-align:right>0</span></td>
                                    <td width=89><span style=font-size:10pt; line-height:19px; text-align:right>'.$journalDetail[0]['SUM'].'</span></td>
                                </tr><br><br>        
                            </table>
                        </html>';

    return $detailJournal;

}

the question is how insert foreach in html?

4
  • give the full html code. You already insert an output inside foreach loop. Commented Nov 12, 2018 at 7:00
  • The easiest way would be by not putting the HTML in a PHP variable but to end the PHP block (?>), write your html (with any PHP you need, like usual), and then start the PHP block again. Otherwise, you need to concatenate your variable. Commented Nov 12, 2018 at 7:00
  • i try insert php block ( ?> ) but it's not working, i try insert function in model it's not working too Commented Nov 12, 2018 at 7:08
  • What have you tried? What are you looking for - what is not working with the given code? Commented Nov 12, 2018 at 7:21

3 Answers 3

1

In your foreach, rather than echoing the value, build it up into a string (adding each part with .=) which you can then insert into your larger HTML...

$qr = $this->db->query("select journalDetail.COA_CODE, COA_TITLE, COA_TYPE from t_journal_detail journalDetail left join r_coa coaTitle on journalDetail.coa_code=coaTitle.coa_code where journalDetail.JOURNAL_ID = '".$journalId."'");
$coaTitle = $qr->result_array();
$htmlTable = "";
foreach ($coaTitle as $coa) {
    $htmlTable .= '<tr>';
    $htmlTable .= '<td width=55><span style=font-size:10pt; line-height:19px; text-align:left>'.$coa['COA_CODE'].'</span></td><br>';

    //  Continue adding fields using `$coa['FIELD NAME']` and $htmlTable .= ...

    $htmlTable .= '</tr>';
}

$detailJournal = '<!DOCTYPE html>
                        <html>
                            <table> 
                                <tr>            
                            <table border = 0 cellspacing = 0 cellpadding = 0 align = center>'
                              .$htmlTable.'<br><br>        
                            </table>
                        </html>';

    return $detailJournal;

}

You just need to use the line

$htmlTable .= '<td width=55><span style=font-size:10pt; line-height:19px; text-align:left>'.$coa['COA_CODE'].'</span></td><br>';

as a template to add in the other columns.

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

Comments

0

try this:

$qr = $this->db->query("select journalDetail.COA_CODE, COA_TITLE, COA_TYPE from t_journal_detail journalDetail left join r_coa coaTitle on journalDetail.coa_code=coaTitle.coa_code where journalDetail.JOURNAL_ID = '".$journalId."'");
$coaTitle = $qr->result_array();

    foreach ($coaTitle as $coa) {
        echo '<td width=55><span style=font-size:10pt; line-height:19px; text-align:left>'.$coa[0]['COA_CODE'].'</span></td><br>';
    }

in foreach($coaTitle as $coa) you must use $coa instead $coaTitle

Comments

0

if you want to add php code in html file then, you can add directly by adding php tag, and save file with .php extension

$detailJournal = '<!DOCTYPE html>
<html>
    <table> 
        <tr>            
            <table border = 0 cellspacing = 0 cellpadding = 0 align = center>
                <?php
                foreach ($coaTitle as $coa) {
                ?>
                    <tr>
                        <td width=55><span style=font-size:10pt; line-height:19px; text-align:left>'.$coa['COA_CODE'].'</span></td>
                        <td width=170><span style=font-size:10pt; line-height:19px; text-align:center>'.$coa[0]['COA_TITLE'].'</span></td>
                        <td width=55><span style=font-size:10pt; line-height:19px; text-align:center>IDR</span></td>
                        <td width=81><span style=font-size:10pt; line-height:19px; text-align:right>'.$journalDetail[0]['ORIG'].'</span></td>
                        <td width=89><span style=font-size:10pt; line-height:19px; text-align:right>0</span></td>
                        <td width=89><span style=font-size:10pt; line-height:19px; text-align:right>'.$journalDetail[0]['SUM'].'</span></td>
                    </tr>
                <?php
                }
                ?>  
            </table>
        </tr>
    </table>
</html>

1 Comment

Please add some explanation to your code. Additionally, are you sure that it does anything good? Why is there an opening <?php within the output?

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.