The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

2 Pages V < 1 2  
Reply to this topicStart new topic
> sessions trouble
Mindapolis
post Nov 15 2011, 01:11 PM
Post #21


Member
***

Group: Members
Posts: 67
Joined: 2-April 11
Member No.: 14,256



I will look into classes. That is one thing we did not cover in class.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Ephraim F. Moya
post Nov 15 2011, 02:02 PM
Post #22


Advanced Member
****

Group: Members
Posts: 167
Joined: 2-September 07
From: New Mexico
Member No.: 3,702



QUOTE(Mindapolis @ Nov 15 2011, 11:09 AM) *

I understand about crawling before running and that 's what I'm doing. I know how to transfer one variable to multiple pages but I dont understand how to transfer arrays from one page to another. I found this example on http://www.phpriot.com/articles/intro-php-sessions/7 I understand everything except what elements to put in the array since the elements are the user 's input here 's what I have so far.

when I run this I get

Warning: Invalid argument supplied for foreach() in /home/content/a/u/n/auntievics/html/test/checkOut.php on line 18

CODE

<?php
session_start();
$quantity_desired=array($quantity);
$_SESSION['quantity']=$quantity_desired;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$errors=array();
if(isset($_POST['submit']))
{
    if(empty($_POST['name'])){
    $errors['name']="dipshit, put your name";
    }
}
?>    
<form action="checkOut.php" method="post" name="test">  
<?php if(isset($errors['name'])){ echo $errors['name']."<br />"; } ?>
Name:  <input name="name" type="text" size="10" maxlength="15" value="<?php if(isset($_POST['name'])){ echo "$_POST[name]"; } ?>" /><br />  
quantity <input name="quantity" type="text" size="2" /><br />  
quantity <input name="quantity" type="text" size="2" /><br />  
<input name="submit" type="submit" value="Submit" />
</form>
</body>
</html>


CODE

<?php
session_start();

//$_SESSION['name']=$_POST['name']; //gets users input
//$_SESSION['quantity']=$_POST['quantity'];
?>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<table>
<tr>
<td>  
<?php
foreach($_SESSION['quantity'] as $key=>$value)
//  echo $_SESSION['name'];
  echo $_SESSION['quantity'];  
?>
</td>
</tr>
</table>
</body>
</html>



It turns out that you can't point to an array because the chances are that the array will be different next time thru. So you have to, instead, individually save the elements of the array that you want.

That's why I told you to investigate what the rules are for SESSION data.

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Ephraim F. Moya
post Nov 15 2011, 03:23 PM
Post #23


Advanced Member
****

Group: Members
Posts: 167
Joined: 2-September 07
From: New Mexico
Member No.: 3,702




It turns out that you can't point to an array because the chances are that the array will be different next time thru. So you have to, instead, individually save the elements of the array that you want.

That's why I told you to investigate what the rules are for SESSION data.

In your case your foreach statement is completely nonsensical. Foreach applies to an array and you're applying it to a string.

That's why people keep telling you that you need to learn php.

You're like a newborn crying and peeing instead of crawling to the next subject. Basic php includes learning how to handle arrays, not to mention recognizing correct instructions from faulty instructions.

BTW, setting a SESSION variable then printing it in the same program proves NOTHING about transfer of data.

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Ephraim F. Moya
post Nov 16 2011, 03:22 PM
Post #24


Advanced Member
****

Group: Members
Posts: 167
Joined: 2-September 07
From: New Mexico
Member No.: 3,702



What crappy board software. Why won't it format tabs?

You know, wordpress does better and it's free!

I'll make a page of my post and link to that here. Maybe that's allowed.

This post has been edited by Ephraim F. Moya: Nov 16 2011, 03:30 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Darin McGrew
post Nov 16 2011, 05:51 PM
Post #25


WDG Member
********

Group: Root Admin
Posts: 8,365
Joined: 4-August 06
From: Mountain View, CA
Member No.: 3



All the forums I frequent implement some version of BBCode. I don't recall any of them doing anything with tabs. (Although you did get me to look up what WordPress does with tabs...)
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Ephraim F. Moya
post Nov 16 2011, 07:23 PM
Post #26


Advanced Member
****

Group: Members
Posts: 167
Joined: 2-September 07
From: New Mexico
Member No.: 3,702



Here's my first cut at explaining SESSIONS interactions.

http://moya.us/Sessions.php

This is a schematic and isn't point by point.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Nov 17 2011, 03:27 AM
Post #27


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



Ephraim said:

QUOTE
It turns out that you can't point to an array because the chances are that the array will be different next time thru. So you have to, instead, individually save the elements of the array that you want.


I don't understand this. (I haven't actually used sessions, but I assume the $_SESSION superglobal is essentially a persistent variable.)

If you just mean storing an array in $_SESSION, that appears to be OK. What do you mean by "point to"? If you mean a _reference_, then obviously storing pointers would be no good. But you can simply copy the whole array, no?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Ephraim F. Moya
post Nov 17 2011, 10:03 AM
Post #28


Advanced Member
****

Group: Members
Posts: 167
Joined: 2-September 07
From: New Mexico
Member No.: 3,702



QUOTE(Brian Chandler @ Nov 17 2011, 01:27 AM) *

Ephraim said:

QUOTE
It turns out that you can't point to an array because the chances are that the array will be different next time thru. So you have to, instead, individually save the elements of the array that you want.


I don't understand this. (I haven't actually used sessions, but I assume the $_SESSION superglobal is essentially a persistent variable.)

If you just mean storing an array in $_SESSION, that appears to be OK. What do you mean by "point to"? If you mean a _reference_, then obviously storing pointers would be no good. But you can simply copy the whole array, no?


$array2 = $_SESSIONS; doesn't do it. You have to do: $var_x_y = $_SESSIONS[x][y]; for each x and y. I believe I pointed this out above. When you go to use these data you'll have to re-build the old $_SESSIONS array from the pieces you get.

Also, don't forget that sessions work on the NEXT time through for any particular program. $_SESSIONS is a php 'resource identifier' for the $_SESSIONS array which eventually points to the current array. So if the program saves this data structure in the sessions database the NEXT time the program executes the actual array may a) not have been generated, b) been generated but differently than last time or c) the data this time is in a different place than the resource identifier thinks. So you can't save a resource id, of any kind, in an $_SESSIONS array.

Persistence in php is ONLY while the <u>current</u> program is executing. Isn't that the way for ALL programs?

Here I'm on shaky ground! I don't know how actual php does things but I think this is a pretty good guess:

PHP uses 'object' ids for all the variables of a program, not the actual variable. So if you want to add $two and $three
php doesn't actually keep '2' in $two and '3' in $three instead $two is an 'object id' that describes an integer, 8bits, at mem xx, value=2, etc as is $three and as is $sum, etc.

So $sum = $two + $three; is a rather long dance.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Frederiek
post Nov 17 2011, 11:34 AM
Post #29


Programming Fanatic
********

Group: Members
Posts: 5,146
Joined: 23-August 06
From: Europe
Member No.: 9



Mindapolis, get yourself the book "PHP & MySQL Web Development" by Luke Welling and Laura Thompson (it's currently at its fourth edition).

Better yet, there's even a PDF version of the book (second edition) at http://levhita.net/archivos/PHP%20and%20My...Development.pdf .

And follow the sample application of Bob's Auto Parts, at the beginning of the book.
Although it's all about PHP 4, the essence is there for what you try to achieve.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

2 Pages V < 1 2
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 19th April 2024 - 07:00 PM