Im trying to delete an item from a session shopping cart. I used unset(), but somehow it didn't work
Link
<td width="100"><a href="?id=<?php echo $ids;?>&action=delete">
<?php echo $ids;?></a></td>
Unset
if(isset($_GET['action'])&&($_GET['action']=="delete"))
{
$new_id=$_GET['id'];
unset($_SESSION['items'][$new_id]);
}
unset session ok look this code result array(1) { ["id"]=> int(10) }
<?php
$_SESSION['items']=
array(
"id"=>10,
"new_id"=>6
);
unset($_SESSION['items']["new_id"]);
var_dump($_SESSION['items']);
?>
Always make sure the id you are passing as a get parameter is set properly, and analyse the structure of your session variable with a var_dump($_SESSION['items']), you should make sure it matches and comment your code as well.
Related
How to unset specific session in my code below:
for($i=0;$i<count($_SESSION['src']);$i++)
{
?>
<tr>
<td><?php echo $_SESSION['name'][$i]; ?></td>
<td><?php echo $_SESSION['price'][$i]; ?></td>
<td>Hapus</td>
</tr>
<?php
}
if($_GET['delPrd']);
{
unset($_SESSION['name']);
unset($_SESSION['price']);
}
Example I have 3 data with each session id. Now I want to unset session by click each delete link.
How can I do that? So I click delete then it will hide the row that I clicked before in <tr>
I came across the same issue when using the unset(), I tried to debug it to see if it would ever unset using a simple while loop.
while(isset($_SESSION['...'])):
unset($_SESSION['...']);
endwhile;
This seems to work for me and actually unset the cache, it's like it needs to know its unset before continuing which makes no logical sense but that's code for you - you spend 50% of your time trying to fix code, the only 50% wondering how the hell it worked.
Hope this helped.
EDIT: You could make this into a reusable object, something like:
class SessionRemovale
{
public function Remove($param)
{
while(isset($_SESSION[$param])) {
unset($_SESSION[$param]);
}
}
}
Then use it like:
$session_controller = new SessionRemovale();
$session_controller->Remove("....");
$session_controller->Remove("....");
I am looking to set a session variable based on a search conducted by the user. The idea is that the search is populated with their last search wherever they go on the site.
I have the following code that I thought would set the variable if the variable geo-box was present and use the saved variable if it isn't, but this doesn't work...
session_start();
if(isset($_GET['geo-box'])){
echo $_SESSION['town'] = $_GET['geo-box'];
} else {
echo $_SESSION['town'];
}
session_start();
if(isset($_GET['geo-box']))
$_SESSION['town'] = $_GET['geo-box'];
echo $_SESSION['town'];
You can't echo a variable while defining it.
Best of Luck!
You are trying to echo a variable and set it in the same line.
Try this:
session_start();
if( isset($_GET['geo-box']) ) {
$_SESSION['town'] = $_GET['geo-box'];
}
echo $_SESSION['town'];
You can not echo a value and assign it at the same time. Give this a try!
Hope this helps.
I have a session that stores an array.
Let's say, my sessions are cars and music that each stores the following:
<?php
$_Session['cars'] =array("Volvo","BMW","Toyota");
$_Session['music'] = array("Beatles", "Carpenters", "Sting");
?>
Now, how can I echo the following based on my sessions:
BMW Sting
Thanks in advance. :)
Add session_start() at the top of your code, then
echo $_SESSION['cars'][1];
echo $_SESSION['music'][2];
Note the case of $_SESSION - your snippet uses the wrong case and won't store data in the $_SESSION array
You try with array index value. i.e session array index value
<?php
session_start(); // To enable session to current page
$_SESSION['cars'] =array("Volvo","BMW","Toyota");
$_SESSION['music'] = array("Beatles", "Carpenters", "Sting");
echo $_SESSION['cars'][1]; // will print "BMW"
echo $_SESSION['music'][2]; // will print "Sting"
?>
I know I this is simple, but i just can;t find the resource that tells me how to do it.
SO my code is as follows:
session_start();
$wquery=
"select week_num,week_name
from stats_week
where season=$this_season
order by week_num";
$wresult=mysql_query($wquery);
print'<form action="changeplayer_2.php" method="post">';
print"<select name='Week_select'> <br>";
while ($wrow=mysql_fetch_array($wresult))
{
print '<option value="'.$wrow['week_num'].'">'.'week '.$wrow['week_num'].' '.$wrow['week_name'].'</option><br>\n';
}
print "</select><br><br>";#
print'<button type="submit" >Next</button>';
print"</form>";
So I am making a selection:
I want that selection to end up in: $_SESSION['week']
$_SESSION['week']=$_POST['Week_select'];
you can do this either sending data by form on submit or if you want to do without refreshing page you can do this by ajax
and than set like this
$_SESSION['week']=isset($_POST['Week_select'])?$_POST['Week_select']:someDefault;
and to do this by ajax check this answer
Inside file "changeplayer_2.php" you'll want to load the POSTed value to the session:
session_start();
$_SESSION['week'] = $_POST['Week_select'];
Whenever you want to get the value of the session at key 'week', simply do:
$weekValue = isset($_SESSION['week']) ? $_SESSION['week'] : false;
But if you do use the session variable on any script, make sure you start the session using session_start().
To destroy the session variable, you can do:
session_start();
unset($_SESSION['week']);
How would I for example, take a url with some $_GET data, for example http://www.website.com/something?food=steak
How would I then output steak? My current situation is that I'm trying to use the Header function to redirect to a page where I have it so that if $_GET["duplicate"] is equal to 1, then echo this, else, echo nothing. But its not taking the $_GET data I can tell I did a var_dump($_GET);
<?PHP if ($_GET["duplicate"] == 1 )
{
echo "<h1>Username Taken!</h1>";
}
else
{
echo "";
}
?>
The above is using the url http://something.com/register?duplicate=1
It's just a variable, treat it like one:
echo $_GET['food'];
Everything after question mark is available in form of global array $_GET.
$a=$_GET["food"];
echo $a;
also
if url has ?food=steak&color=red;
$a=$_GET["food"];
$b=$_GET["color"];
more than one is possible. Also search for $_POST.
Alright, so I figured my issue out. I have a $_GET variable that gets the end of the page and declares it as "p" for page. I need to do the following to get it to work.
?p=createuser&duplicate=1