0

I know this question might be very basic for someone experienced. But I'm still in the learning process so I need some help.

I want to populate a session array with data that's coming from the database. There can be multiple items in the session array. I'm trying to do it like this -

$sql = mysqli_query($con, "SELECT * FROM `purchase_info_details` WHERE  `purchase_details_id` = '$pur_det_id'");

while ($row = mysqli_fetch_array($sql)) {

   $data[$i]['purchase_details_id'] = $row['purchase_details_id'];
   $data[$i]['cid'] = $row['id'];
   $data[$i]['item_id'] = $row['item_id'];
   $data[$i]['unit_id'] = $row['unit_id'];
   $data[$i]['quantity'] = $row['quantity'];
   $data[$i]['price'] = $row['price'];
   $data[$i]['conv_rate'] = $row['conv_rate'];

   $_SESSION['list1_data']['purchase_details_id'] = $data[$i];
 }

FYI: purchase_details_id is the primary key. This code works partially. What I mean is I only get one row from the table in my session array but I need to get all the rows from the table that matches my SQL query.

I've been searching for a relevant example on the internet but yet to find any. I'm really stuck with it and couldn't find any solution. Please help!

Thanks!!

2
  • 2
    use another level in session array, like $_SESSION['list1_data'][] = $data[$i]; Commented Mar 30, 2016 at 6:52
  • I believe you need to do session_start(); at the top of your file. Commented Mar 30, 2016 at 6:58

5 Answers 5

1

The problem is that your are always overwriting the same data in your session array. When your loop is finished, you will have the last row from your dataset in your session array.

To add a new row in your session array everytime you can proceed with either:

$_SESSION['list1_data'][] = $data[$i];

or if you want a specific key from your data (assuming it is unique):

$_SESSION['list1_data'][$row['purchase_details_id']] = $data[$i];
Sign up to request clarification or add additional context in comments.

2 Comments

finally, an answer that explains the solution and not just "do this" =)
Thanks!! @st2rw2od
0

try something like this

    while ($row = mysqli_fetch_array($sql)) {

   $data[$i]['purchase_details_id'] = $row['purchase_details_id'];
   $data[$i]['cid'] = $row['id'];
   $data[$i]['item_id'] = $row['item_id'];
   $data[$i]['unit_id'] = $row['unit_id'];
   $data[$i]['quantity'] = $row['quantity'];
   $data[$i]['price'] = $row['price'];
   $data[$i]['conv_rate'] = $row['conv_rate'];

   $_SESSION["db_data"][$i] = $data[$i];// or just $_SESSION[$i] = $data[$i]
 }

Comments

0
$_SESSION['list1_data']['purchase_details_id'] = $data[$i]; always overwrite and you get only last value . use $_SESSION['list1_data'][$row['purchase_details_id']] = $data[$i];    

just assign array to session when you finish your loop . like below

 $_SESSION['list1_data'] = $data;

 or as you say product_id is unique then why you use $i ? (use $data[$row['purchase_details_id']]['purchase_details_id'] = value , use $data[$row['purchase_details_id']]['cid'] = value etc ...)

Comments

0

Do this:

$_SESSION['list1_data'][$row['purchase_details_id']] = $data[$i];

This way you can directly access specific details id from the session. For example:

$row = $_SESSION['list1_data'][1]; //row for the id = 1

Pro tip: Always check if the key is set or else you will get warnings =)

Comments

0

make sure that you are initialize the session() method, and you can make a 2 or 3 dimensional array as you need to keep the database value as @B-and-P mention in his comments.

thanks

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.