[Solved *whew*] "Saving" a list

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

[Solved *whew*] "Saving" a list

Post by Steveo31 »

I have a list of words taken from mysql. Now, I'd like to give the user the option to "save" the list, where it emails them the list as well as updates in in the database.

I've tried a million different ways and they all do what I don't want them to. Let me explain:

The words come up like so:

Code: Select all

69654)	Neuroanatomy 	
26444)	Cauline 	
76930)	Phoneticism 	
68923)	Nailless 	
23968)	Buchu 	
82004)	Puggy 	
72758)	Outcharm 
16904)	Aperient 	
14058)	Adhere 	
29330)	Coasting
Total: 39 points
Minimum Length	<input field here>
Maximum Length <input field here>	
# of Words: <input field here>
Save List as Today's
"Save List as Today's" is a link: wordlist.php?do=savelist. When that is clicked, I'd like to update the DB, email, all that without the list refreshing. Here's my code:

Code: Select all

$_SESSION['words'] = array();
$total = 0;

if(empty($_SESSION['words'])){
    $getWords = mysql_query("SELECT word, id, LENGTH(word) AS len FROM all_words WHERE LENGTH(word) > $min AND LENGTH(word) < $max ORDER BY RAND() LIMIT 0, $limit");

    while($r = mysql_fetch_assoc($getWords)){
        $today[] = $r['word'];
    
        $total += $r['len'];
        //display code removed... just echoing <tr>, <td> etc...
    }
    if(isset($_GET['do']) && $_GET['do'] == 'savelist'){
        foreach($today as $todo){
            $_SESSION['words'][] = $todo;
        }
    }
}
But the list always changes/refreshes, then stored in the session variable. I just need it to store the current list in the session. Hope this makes sense :)
Last edited by Steveo31 on Tue Oct 19, 2004 2:42 am, edited 1 time in total.
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Ok, I know my problem now. The way I rewrote it was to simply throw the foreach loops into a simple array. Buuuut, I'm checking the length of the array before it is set, thus the length is always 0, and the mysql stuff is always executed... man, what a pain. Any ideas on that?

:)
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

repost your new code. other wise you could always do a mysql_num_rows() to see how many results there are and base your decision on that.
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Yeah, that'd probably help.. :-X

Code: Select all

$total = 0;


$getWords = mysql_query("SELECT word, id, LENGTH(word) AS len FROM all_words WHERE LENGTH(word) > $min AND LENGTH(word) < $max ORDER BY RAND() LIMIT 0, $limit");

while($r = mysql_fetch_assoc($getWords)){
    $today[] = $r['word'];
  
    $total += $r['len'];
    //display code... blah blah
}
foreach($today as $todo){
    $words[] = $todo;
}

if(isset($_GET['do']) && $_GET['do'] == "savelist"){
    foreach($today as $str){
        $string .= $str.", ";
    }
    echo "STRING:  ".$string; //debugging
}
Could you elaborate more on that mysql_num_rows idea? Sounds promising....
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

bump
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

And another bump... I posted more indepth stuff here:

http://www.codingforums.com/showthread.php?t=46093
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Ok, I thought FOR SURE this would work... somethin I'm missin. What is it?

Code: Select all

if(!isset($_GET['save'])){
    if(count($_SESSION['words']) > 1){
        foreach($_SESSION['words'] as $delete){
            unset($delete);
        }
    }
    echo "PRE SQL  ".count($_SESSION['words']);
    $getWords = mysql_query("SELECT word, id, LENGTH(word) AS len FROM all_words WHERE LENGTH(word) > $min AND LENGTH(word) < $max ORDER BY RAND() LIMIT 0, $limit");

    while($r = mysql_fetch_assoc($getWords)){
        $_SESSION['words'][] = $r['word'];
  
        $total += $r['len'];
        echo "<tr>\n";
        echo "<td width='70px'>".$r['id'].")</td>";
        echo "<td width='90px'><a target='_blank' class='this' href='http://dictionary.reference.com/search?q={$r['word']}'>".ucfirst($r['word'])."</a></td>\n";
        echo "<td class='points' align='right'>".$r['len']."</td>\n";
        echo "</tr>\n";
    }
    echo "<br />POST SQL  ".count($_SESSION['words']);
}elseif(isset($_GET['save'])){
    echo count($_SESSION['words']); //debug
    foreach($_SESSION['words'] as $val){
        echo $val."<br />";
    }
}
The button that will "save" the list is a submit button with the name "save".

When the page is loaded, the $_SESSION array will be truncated, right? From there, the SQL statement will run, thus filling up the $_SESSION['words'] array once again. These should be stored.... when the "save" button is clicked, then it should pull the $_SESSION['words'] values filled from the previous refresh, and foreach() em.

...right?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Steveo31 wrote:When the page is loaded, the $_SESSION array will be truncated, right?
no. $delete is a copy of the value stored in the array.
provided the save form submits to the $_GET superglobal, the basics appear to work.
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Ok, this is just going nowhere. Do you know what I can do to make this work, the way that I have it? I don't see why the $_SESSION['words'] vars don't stick.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

try:

Code: Select all

if(count($_SESSION&#1111;'words']) &gt; 1){
        foreach($_SESSION&#1111;'words'] as $delete){
            unset($delete);
        }
    }

Code: Select all

$_SESSION&#1111;'words'] = array();

where's

Code: Select all

session_start()[/php_man]
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

My gosh feyd... thanks a ton.

session_start() was there, just figured it was implied ;)
Post Reply