-1

I have a class that retrieves a result set from a database. The result is wrapped in arrays which are then stored as session variables as shown in the code

 $days = array();
  $amount = array();
  $commission = array();

foreach ($result as $row) {
         array_push($days,$row['sales_day']);
         array_push($amount,$row['sales_total']);
         array_push($commission,$row['sales_comm']);
     $_SESSION['days'] = $days;
     $_SESSION['amount'] = $amount;
     $_SESSION['commission'] = $commission;

I then attempt to display the data in HTML table as in this code:

<tr>
    <td><label><?php print_r($_SESSION['days']); ?></label></td>
    <td><label><?php print_r ($_SESSION['amount']); ?></label></td>
    <td><label><?php print_r($_SESSION['commission']); ?></label></td>
</tr>

The problem is that the output is something like: array[0] => 1. But I want only the values retrieved from the database to display and not the array indices.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Zeekstem
  • 718
  • 1
  • 7
  • 15
  • If `print_r` shows that, it means that's what `$row['sales_day']` (and others) contained to begin with. You're gonna have to show more code than that. – Jeto Mar 03 '19 at 07:13
  • 2
    `print_r` is intended to output the structure of data, not as a user friendly table output. You'll need to iterate the data to output the individual values, probably each in their own ``, like done [here](https://stackoverflow.com/q/8393034/5459839). NB: the use of session variables seems irrelevant to your question? – trincot Mar 03 '19 at 07:30

1 Answers1

0

I've considered that you need to save data in a part of your code and print them somewhere else.

if at the first step you want to save all items into $_SESSION and then show them in next steps, use this code:

the first step:

$merged_data = array();
foreach ($result as $row) {
    $merged_data[] = array(
        'sales_day' => $row['sales_day'],
        'sales_total' => $row['sales_total'],
        'sales_comm' => $row['sales_comm']
    );
}
$_SESSION['merged_data'] = $merged_data;

the second step: (somewhere else in your code)

<?php foreach ($_SESSION['merged_data'] as $data) { ?>
    <td><label><?= $data['days']; ?></label></td>
    <td><label><?= $data['amount']; ?></label></td>
    <td><label><?= $data['commission']; ?></label></td>
<?php } ?>
SoheilYou
  • 907
  • 5
  • 23
  • 43
  • Thanks a lot @sohiel. I used your code as is and it worked perfectly. But note that the variables in $data are: sales_day, sales_amount, sales_comm. Thanks again. I am very grateful – Zeekstem Mar 03 '19 at 08:49
  • @Zeekstem your welcome, you can select the answer as the accepted answer if it solved your problem – SoheilYou Mar 04 '19 at 14:51