Bravat Posted February 15, 2011 Share Posted February 15, 2011 How to add more data into $_SESSION? I have following code: if(isset($_GET['poredi']) && $_GET['poredi'] == "upo"){ $idp = intval($_POST['idp']); $sql = "SELECT * FROM product WHERE product_id={$idp}"; $query=mysql_query($sql); if(mysql_num_rows($query)!=0){ $row=mysql_fetch_array($query); $_SESSION['poredi'] = $row['product_id']; } } On click it need to store more product_id, and after that i have to be able to do query with data (find all data with product_id)? Quote Link to comment Share on other sites More sharing options...
lastkarrde Posted February 16, 2011 Share Posted February 16, 2011 Do get the session to store an array of product_id's you would do: [code=php:0] if(isset($_GET['poredi']) && $_GET['poredi'] == "upo"){ $idp = intval($_POST['idp']); $sql = "SELECT * FROM product WHERE product_id={$idp}"; $query=mysql_query($sql); $ids = array(); if(mysql_num_rows($query)!=0){ $row=mysql_fetch_array($query); $ids[] = $row['product_id']; } $_SESSION['poredi'] = $ids; } Quote Link to comment Share on other sites More sharing options...
Bravat Posted February 16, 2011 Author Share Posted February 16, 2011 That does the job, but now i am stumble upon the query. Here is code (not working): <?php if(isset($_SESSION['poredi'])){ $idp = $_SESSION['poredi']; $sql="SELECT * FROM product WHERE product_id IN ("; foreach($_SESSION['poredi'] as $id) { $sql.=$id.","; } $sql=substr($sql, 0, -1).") ORDER BY model ASC"; $query=mysql_query($sql); while($row=mysql_fetch_array($query)){ ?> <?php echo "<span class=\"korpa\">" . $row['model'] ?> <?php } ?> Quote Link to comment Share on other sites More sharing options...
lastkarrde Posted February 16, 2011 Share Posted February 16, 2011 What do you mean "not working"? What error is shown? What query is executed? (echo out $sql) Quote Link to comment Share on other sites More sharing options...
Bravat Posted February 16, 2011 Author Share Posted February 16, 2011 My bad, didn't delete the old session. There is no error, but when i add new product_id it just replace old one, instead of adding new one to the list. Quote Link to comment Share on other sites More sharing options...
Bravat Posted February 16, 2011 Author Share Posted February 16, 2011 I think the problem is that it only get one product_id, and when new one is set it erase the old one. How to keep adding new product_id data into $_SESSION['poredi']? Quote Link to comment Share on other sites More sharing options...
lastkarrde Posted February 16, 2011 Share Posted February 16, 2011 To append something to the $_SESSION['poredi'] array you can do: $_SESSION['poredi'][] = 'some value'; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.