Page 4 of 5

Posted: Mon Jun 25, 2007 10:43 am
by volka
revocause wrote:and how do i pass that? as I assumed that since it was placed into SESSION it would store it on that reload if its using an if(isset).
You have to use _SESSION then.

Code: Select all

// store session data
$_SESSION['data'] = 'value';

// printing session data
echo $_SESSION['data'];

// removing session data
unset($_SESSION['data']);

// test wether session data is present and if print it
if ( isset($_SESSION['data']) ) {
  echo $_SESSION['data'];
}
see how there's always the array _SESSION involved?

What you want to preserved between requests has to be stored in _SESSION.
And everything that has been stored in the session is in _SESSION. If you want to access the data use _SESSION.

Posted: Mon Jun 25, 2007 11:11 am
by revocause
Your code of

Code: Select all

// store session data 
$_SESSION['data'] = 'value';


the value is going to be dynamic. It cant be set as a constant.

and by using

Code: Select all

$_SESSION['quantity'] ="";


wont pass what ever the value becomes. probably because that value is empty.

Also by using if(isset) .
when the page reloads and checks if the select menu is set ..it WILL be. because of the saved user select 'history' function.

so when the page reloads. its 1. checking to see if the menu is set . and it will be.
then it will check for value , but its not doing it , because the value is fetched from the database.

in order to get it to fetch from database, the select menu has to be submitted again to trigger that response.

so i get an Warning that is basically telling me i have NULL values on the 'if(isset) ' menu.
If i click the submit button again, it will then go and fetch the values from database. and the Warning disappears.

the problem is, i dont want that warning on my page at all , ever.
I'm wondering if somebody has done this before and found a way around it.

Because when the page reloads, it will be looking for those 'quantity' values. and they wont exist because where the variables are set in the select menu to the database as

Code: Select all

if(isset($_POST['product_main']))  
{ 
    
$sql = "SELECT quantity, baseprice, weight FROM b1_product WHERE id = " . $_POST['product_main'];

$res = mysql_query($sql); 
$row = mysql_fetch_assoc($res); 
$quantity = $row['quantity']; 
$baseprice = $row['baseprice']; 
$weight = $row['weight']; 
} 
?>


the page reloads and then finds that = hey, there is a NULL value in $row. Because the submit button hasnt triggered to fetch that info from database.

that's why, when the warning pops up, ..you can click the submit button and the warning disappears.

Its as if im neediong constant variables that are determined by dynamic user selection.
thats my question, not the 101 on SESSION : )

Posted: Mon Jun 25, 2007 11:20 am
by volka
revocause wrote:Your code of

Code: Select all

// store session data 
$_SESSION['data'] = 'value';
the value is going to be dynamic. It cant be set as a constant.
It's only an example and it makes no difference.

Code: Select all

$_SESSION['data1'] = 'value1';
$_SESSION['data2'] = rand(1,10);
$_SESSION['data3'] = array();
foreach(range(1,rand(10,100)) as $v] {
  $_SESSION['data3'][] = $k;
}
is that dynamic enough? Still it uses _SESSION and _SESSION only to store the data.
revocause wrote:the problem is, i dont want that warning on my page at all , ever.
I'm wondering if somebody has done this before and found a way around it.
No, of course not. All php pages involving sessions have that warning.
Seriously, first you have to understand the basics of php and how the session mechanism works, then you will know how to code your script without warnings and errors.
revocause wrote:the page reloads and then finds that = hey, there is a NULL value in $row. Because the submit button hasnt triggered to fetch that info from database.
No! It''s a new request and $row hasn't been preserved. The php instance that handles this new request hasn't heard about a varibale $row. There is no variable $row at this point. Not until the script reaches

Code: Select all

$res = mysql_query($sql);
$row = mysql_fetch_assoc($res);
again.

Posted: Mon Jun 25, 2007 11:27 am
by revocause
I'm impressed.
but youre still not seeing it.

Posted: Mon Jun 25, 2007 11:29 am
by volka
trust me, I do.
And I also know you should take some basic tutorials like patrikG suggested.

Posted: Mon Jun 25, 2007 11:33 am
by patrikG
revocause wrote:I'm impressed.
but youre still not seeing it.
Honestly, you have to do some of the work here and not expect everybody on the board to stand at the ready to do everything for you. Volka has repeatedly answered your question, it's time for you to read his answers in detail, think about them and try them out. As I suggested earlier in the this thread, start with the basics.

Posted: Mon Jun 25, 2007 12:12 pm
by revocause
I'm not the type of person that likes things to be done for me. : )

so technically,
I dont want you typing my code. I'm a bit of a control freak programmer , I like to do the programming and do it in a way that makes sense to me, not to someone else.
But there are times when youre missing something, perhaps its a brain fart, and it appears easy to someone else.

I've put 'quantity' into sessions.
but its showing that warning.

I've put 'quantity' into the SESSION 4 different ways :
examples:

Code: Select all

<?php
session_start(); 
if (isset($_POST['product_main'] )) { 
$_SESSION['quantity1'] = (isset($_POST['quantity1'])) ? $_POST['quantity1'] : ""; 
}


I've tried: (using the initial variable from the db fetch)

Code: Select all

<?php
session_start(); 
if (isset($_POST['product_main'] )) { 
$_SESSION['quantity'] = $row['quanity']; 
}
?>
and

Code: Select all

<?php
session_start(); 
if (isset($_POST['product_main'] )) { 
$_SESSION['quantity1'] = $_POST['quantity1'] 
}
?>
and as i stated earlier, some of those work. But the Warning message appears.
how else is there to store the variable thats newly created, so that when the page reloads its not showing that one value is null while the other is in sessions?

Otherwise, could you be more clear on WHAT PART of sessions you think I'm doing wrong.

Posted: Mon Jun 25, 2007 12:20 pm
by AKA Panama Jack
volka wrote:trust me, I do.
And I also know you should take some basic tutorials like patrikG suggested.
Now you can understand why I gave up and stopped posting early. :) I could see where this was going.

Posted: Mon Jun 25, 2007 12:47 pm
by volka
AKA Panama Jack wrote:Now you can understand why I gave up and stopped posting early. :)
I know the feeling ;)

revocause wrote:and as i stated earlier, some of those work. But the Warning message appears.
No, they don't. That's why you get the warning.
Store the data when you have the data. Not when you do not have the data.
The last version of the script I've seen is like (an example follows, not real code)

Code: Select all

echo $a;
$a = 'value';
You try to store $row['...'] when there is no $row yet.

I think you haven't understood the lifecycle of a php instance/script.
When $a is set on line 2 (like in the previous example) it will not be set on line 1 when the same script is invoked again because a new instance of php has been started that is not related/connected to the old instance. The new instance does not share variables or resources with the old instance.
There's only one nearly magic item, called _SESSION. When you call session_start() the function looks for a session id sent by the client. If it finds one it loads previously stored session data to _SESSION (otherwise it just creates a new session id and _SESSION is an empty array). When the session mechanism shuts down (e.g. when the script/instance reaches its end) the whole array _SESSION (and only the array _SESSION) is stored as session data.


please try and play a little with

Code: Select all

<?php session_start(); ?>
<html>
	<head><title>session test</title></head>
	<body>
		<pre>_SESSION: <?php print_r($_SESSION); ?></pre>
		<pre>_POST: <?php print_r($_POST); ?></pre>
		<pre>_GET: <?php print_r($_GET); ?></pre>
		<pre>row: <?php echo isset($row) ? 'exists' : ' does not exist'; ?></pre>
		<hr />
		<form method="post" action="?">
			<div>
				<input type="hidden" name="hiddenval" value="<?php echo rand(1,1000); ?>" />
				<input type="text" name="xyz" />
				<input type="submit" value="send parameter via post"/>
			</div>
		</form>
		<hr />
		<a href="?foo=bar">send value parameter via get</a>
	</body>
</html>
<?php
if ( isset($_POST['hiddenval']) ) {
	$_SESSION['posted'] = isset($_SESSION['posted']) ? $_SESSION['posted']+1 : 1;
}

if ( isset($_GET['foo']) ) {
	$_SESSION['foo'] = isset($_SESSION['foo']) ? $_SESSION['foo']+1 : 1;
}

$row = 'tralala';
?>

Posted: Mon Jun 25, 2007 2:07 pm
by revocause
ok, simpler question then is,
if you fetch data from databse .

if you then turn that data into variable you can use on your page.
example:

Code: Select all

<?php 
if(isset($_POST['product_main']))  
{ 
    
$sql = "SELECT quantity, baseprice, weight FROM b1_product WHERE id = " . $_POST['product_main']; 

$res = mysql_query($sql); 
$row = mysql_fetch_assoc($res); 
$quantity = $row['quantity']; 
$baseprice = $row['baseprice']; 
$weight = $row['weight']; 
} 
?>
and then obviously $quantity will work on your page, if you want to echo it out , use it math functions. etc.

but then if you want to include $quantity, $baseprice, and $weight into a SESSION variable. how do you do that.
I've experimented with a few ways. even doing

whats the code to convert that , because you cant use standard

Code: Select all

$quantity = $_POST['quantity'];
thats all i need to know about SESSION.

Posted: Mon Jun 25, 2007 2:13 pm
by revocause
Forget it,
I've figured it out myself.
It was a simple line of code that for some reason I was so caught up in other bigger code, that i was brain farting on it.

The code i was looking for was quite simply.

Code: Select all

$_SESSION['quantity'] = $row['quantity'];



uhh... thanks for your 'help' in remembering that one simple thing.
(looks at watch)

Posted: Mon Jun 25, 2007 2:16 pm
by volka
volka wrote:

Code: Select all

// store session data
$_SESSION['data'] = 'value';

// printing session data
echo $_SESSION['data'];

// removing session data
unset($_SESSION['data']);

// test wether session data is present and if print it
if ( isset($_SESSION['data']) ) {
  echo $_SESSION['data'];
}
see how there's always the array _SESSION involved?
Anything you want to store in the session you have to assgin to _SESSION.

Posted: Mon Jun 25, 2007 2:18 pm
by Luke
revocause, I'm having a hard time watching this... you need to be more polite. just because you do not see the bigger picture and aren't willing to think about what volka is telling you for more than 5 seconds does not mean that he did not help you. You need to help yourself and stop being rude.

Posted: Mon Jun 25, 2007 2:23 pm
by volka
revocause wrote:(looks at watch)
We have all the time of the world....

Posted: Mon Jun 25, 2007 2:30 pm
by revocause
being rude?

asking a question , only to get jumped by as many as can pile on for humiliation frenzy.

L O L

i felt that was pretty rude in my opinion.
but feel free.

Interpretation of my state of mind , i wasnt being rude. and 'looking at watch' comment.
that was a joke.

But it appears some o nthe forum are always on the ultra-serious state of mind themselves, and so interpret all others to
only being 'anal' on their replies.

Sorry for thre misunderstanding.
and have a nice day : )