skip to Main Content

I would like to print the values per array in pdf form since this program hold a lot of array and will never be specific in terms of data.

This is my code: $this->submissionPSDirectCost function in model which call all the data in database.

$ps_data = array();
foreach ($this->submissionPSDirectCost as $ps) {
    $ps_data[] = ($ps->ps_type == 0) ? "Salary" : "Honoraria";
    $ps_data[] = "<br>";
    $ps_data[] = $ps->ps_quantity;
    $ps_data[] = $ps->title;
    $ps_data[] = "at";
    $ps_data[] = $ps->salary;
    $ps_data[] = "/Month x";
    $ps_data[] = $ps->ps_num_duration;
    $ps_data[] = $ps->mode_payment;
    $ps_data[] = " ";
    $ps_data[] = " ";
    $ps_data[] = $ps->amount;
    $ps_data[] = "<br><br>";
} $ps_data_string = implode(" ", $ps_data);

and then this is my html inline code to call the $ps_data_string

<tr>
   <td colspan="3">'.$ps_data_string.'</td>
</tr>

However, the process keeps calling all the data inside the $ps_data_string.
I know I should put foreach so that the output of data should be per row.
But Idk how to do it or where to put the said function.

Expecting the stored data of $ps_string_data will be print per <tr> in PDF
Example there is array[0,1,2,3,4]
this should be print as

<tr> "0" </tr>
<tr "1" </tr> ... 

this is just an example… hope u guys help me thanks

2

Answers


  1. It seems you want each item of $ps_data to appear in its own table row in the PDF. Instead of building a string, construct the rows directly in your loop.

    $ps_rows = "";
    foreach ($this->submissionPSDirectCost as $ps) {
        $type = ($ps->ps_type == 0) ? "Salary" : "Honoraria";
        $ps_rows .= "<tr><td colspan="3">$type</td></tr>";
        // ... (continue for other properties of $ps)
        $ps_rows .= "<tr><td colspan="3"></td></tr>";  // for spacing
    }
    

    Then, in your HTML:

    <?= $ps_rows ?>
    

    This way, each data point will be in its own table row in the PDF.

    Login or Signup to reply.
  2. As suggested in another answer, creating rows directly inside the loop should serve the purpose. However, if I understood the requirement correctly, you need to (shift $ps_data, $ps_data_string inside it and) create the row only after concatenating all 13 elements of $ps_data

    foreach ($this->submissionPSDirectCost as $ps) {
      $ps_data = null;
      $ps_data[] = ($ps->ps_type == 0) ? "Salary" : "Honoraria";
      //remaining 12 elements of $ps_data to follow
      $ps_row = implode(" ", $ps_data);
      $ps_data_string .= "<tr><td colspan="3">$ps_row</td></tr>";
    }
    

    Then you use <table><?php echo $ps_data_string;?></table> in your html.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search