1

Still banging my head, but I think an answer may help me see how things really work. I always sucked at languages, but I figure with your help I can brute force this.

So I am just going to post the whole code so as not to cause confusion.

<html>
<head>
<title>Change a player</title>
</head>
<BODY >
<?php
 include "inc.php";
 session_start();

 print 'Select week for season: ';
   print $this_season;
##the week drop down query#
$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">';
    session_start();
        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>';
$varWeek=$_POST['Week_select'];
    print"</form>";
$_SESSION['week'] = $varWeek;
?>  
</body>
</html>

So, I don't know how to get 'Week_select' to be assigned to $_SESSION['week']. Above code was my last ditch effort. Secondly, once I assign something to the Session, how do I clear it out later so it doesn't keep popping up? Bonus question

print 'Select week for season: ';
   print $this_season;

Is the only way I can make the both the statement and the variable to display. When I do

print 'Select week for season: '$this_season;

I don't get the variable displayed, also when I do

print 'Select week for season: $this_season';

I get Select week for season: $this_season displayed

Bill Flippen
  • 453
  • 1
  • 4
  • 19

5 Answers5

2

When displaying (print/echo) a variable with a specific string, you must concatenate the two with " . " (dot)

print 'Select week for season: ' . $this_season;
1

Session unset and destroy: here Or you could just unregister the one variable: session_unregister($week)

And yes you should only have once session_start per page ideally before anything is output. And the correct way to print is to concatenate:

print "Select week for season: " . $this_season;
Community
  • 1
  • 1
Ben Dubuisson
  • 727
  • 13
  • 38
  • Thank you, I didn't realize I had the second session_start(), I was moving it to different parts of the code forgot I moved it without deleting – Bill Flippen Apr 12 '13 at 14:12
1

Place your session_start() at the top of the html. Remove the second session_start() in the page.

Try printing/echoing your $_SESSION['week'] to check if it has a value.

print $_SESSION['week'];    
  • thank you for the time, my 2nd page does have that but fails to print (it does print other $_session['x'] that I was testing with (thus my question about unregistering them) – Bill Flippen Apr 12 '13 at 04:25
  • Come to find out, my pftp session ended, I reopened and didn't get the cd directory path quite right. I was uploading my changes to the wrong directory. Thanks for your patience! – Bill Flippen Apr 12 '13 at 14:14
1

Your first question:

Part 1

To set a session the code should be inserted before the HTML tags. Read code below:

<?php
session_start();
$_SESSION['week'] = $this_season;
?>

<html>
<head>
//your code here
</head>
<body>
//your code here
</body>
</html>

Part 2

If you want to remove a session completely you can use the following code, which will destroy the session:

<?php session_destroy(); ?>

Or if you only want to clear a session you can use the following code:

<?php unset($_SESSION['week']); ?>

It will clear the session value.

Your second question:

To merge both print syntax together you need to use a dot "." . In some other language "+" is used. The process is called concatenation

Your new code should be:

print 'Select week for season: ' . $this_season;

This means that you want to print the string 'Select week for season: ' followed by the variable $this_season.

  • Thank you, after a bit more clarification [here](http://stackoverflow.com/a/15963224/2227549) your answe makes perfect sense – Bill Flippen Apr 12 '13 at 14:11
0

session_start() should be placed before output or any $_SESSION interaction. And only once.

<?php
session_start();
?>
<html>
<head>
<title>Change a player</title>
</head>
<BODY >
<?php
 include "inc.php";
// etc...
?>
</body>
</html>

UPDv1:

if you need to remove something from session, then you can perform:

<?php unset($_SESSION['something']); ?>

UPDv2:

If you need tu put entire select into $_SESSION, you may do:

ob_start();

print "<select name='Week_select'>";

while ($wrow=mysql_fetch_array($wresult))
    { print '<option value="'.$wrow['week_num'].'">week '.$wrow['week_num'].' '.$wrow['week_name'].'</option>'.PHP_EOL; }

print "</select>";

$_SESSION['something'] = ob_get_clean();

// var_dump($_SESSION);
BlitZ
  • 12,038
  • 3
  • 49
  • 68
  • Removed second Session(). So how do I get the print" – Bill Flippen Apr 12 '13 at 03:48
  • Read below. If you save something to $_SESSION, then output should show it via var_dump($_SESSION). Useful while debugging. – BlitZ Apr 12 '13 at 03:55
  • I understand (kinda of) how the $_SESSION works, just not how to get a $variable assigned from my ? – Bill Flippen Apr 12 '13 at 04:10
  • I guess I don;t know how to ask the question, let me try again and thank you for your patience. From the drop down list, I want to select 1 item, then assign it to a variable to get it to $_SESSION – Bill Flippen Apr 12 '13 at 04:20
  • I will open this as another question thanks so much for all your help – Bill Flippen Apr 12 '13 at 04:27
  • Could you tell me what `var_dump($_POST['Week_select']);` shows to you? It should be working already. Do you destroy (`session_destroy()`) your session anywhere? If not, then it should be working. – BlitZ Apr 12 '13 at 04:28
  • I actually got it answered [HERE](http://stackoverflow.com/a/15963224/2227549). It was a combination not knowing the SUBMIT/POST operation and uploading to the wrong directory – Bill Flippen Apr 12 '13 at 14:08