General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 1st, 2003, 07:51 PM
thecharking thecharking is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 187 thecharking User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via AIM to thecharking
storing user choices across pages

I have many pages with choices that users can select through checkboxes. I was wondering how can I allow users choices to be remembered. Once they choose 10, it will store it all in a database. I have thought about these options:

through the url: this won't work well because these are pages that won't always need the user to make choices, and if the user were to leave these pages to another page, it wouldn't remember previous choices.

cookies: I thought that maybe this would work, but I'm not sure how to store multiple values in one cookie, at different times. I have thought of this:
user checks boxes, which are sent as an array, and then I store in cookie as comma seperated values. One each page requiring it, the page gets the cookie and then adds new array values to it... and then rewrite old cookie. I'm not sure how to do this though...

I could write to a text file, but is this practical? It seems the best way to me...

I could just store in database as user checks them off, but this would mean making MULTIPLE UPDATE statements, rathar than getting all values and then making one query.

Any ideas on what would work best? Or another method?
Thanks.
__________________
hey it's the CHARKING

Reply With Quote
  #2  
Old October 1st, 2003, 09:00 PM
dbalatero dbalatero is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 8 dbalatero User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
PHP Code:
<?php
// code at the top of every page to remember
$sessName "mysession";
session_name($sessName);
session_start();

// initialize the array to hold the user's saved values
// we call it 'savedData'
if (!is_array($_SESSION['savedData'])) {
  
$_SESSION['savedData'] = array();
}

// deal with POST variables from checkboxes
if (isset($_POST['checkbox1'])) {
  
// add to array
  
$_SESSION['savedData'][] = $_POST['checkbox1'];
}

// ... and so on for POST variables
?>

<?php
// check if we need to insert into DB
// condition - if saved entries >= 10
if (count($_SESSION['savedData']) >= 10) {
  foreach (
$_SESSION['savedData'] as $key=>$value) {
    
$sql "INSERT INTO blah blah blah...";
    
mysql_query($sql);
  }
}
?>


The only consideration with sessions is maintaining their state. You can set session.gc_maxlifetime (i believe that's the variable, this is off the top of my head) to something like 9999999999 by using the ini_set() function (www.php.net/ini_set)

Also, check out the PHP documentations for sessions for more examples.

http://www.php.net/session_start

Hope this helped!

EDIT: forgot to call session_name() in the above code

Last edited by dbalatero : October 1st, 2003 at 09:07 PM.

Reply With Quote
  #3  
Old October 1st, 2003, 09:06 PM
dbalatero dbalatero is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 8 dbalatero User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
OH, and, as for cookies, there's no reason you couldn't set a cookie with the comma-separated values as you wrote above, then when you want to add to it, you can take the string and write to the end of it with a new entry.

If you wanted to edit that CSV string and remove an item, you'd just do the following:

PHP Code:
<?php
$remove 
"56"// item to remove
$_COOKIE['csv_string'] = "45,56,38,28,14";

$split explode(","$_COOKIE['csv_string']);
$new_string = array();

foreach (
$split as $item) {
  if (
$item != $remove) {
    
// add it to the new string array
    // as long as it isn't the one we want to remove
    
$new_string[] = $item;
  }
}

// set the COOKIE variable to the new string
// as comma separated
$_COOKIE['csv_string'] = implode(","$new_string);
?>

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > storing user choices across pages


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway