Page 1 of 1

[Solved *whew*] "Saving" a list

Posted: Tue Oct 12, 2004 8:28 pm
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 :)

Posted: Wed Oct 13, 2004 2:39 am
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?

:)

Posted: Wed Oct 13, 2004 2:46 am
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.

Posted: Wed Oct 13, 2004 2:42 pm
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....

Posted: Thu Oct 14, 2004 2:32 pm
by Steveo31
bump

Posted: Fri Oct 15, 2004 6:43 pm
by Steveo31
And another bump... I posted more indepth stuff here:

http://www.codingforums.com/showthread.php?t=46093

Posted: Mon Oct 18, 2004 2:59 am
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?

Posted: Mon Oct 18, 2004 3:06 am
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.

Posted: Mon Oct 18, 2004 1:55 pm
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.

Posted: Mon Oct 18, 2004 4:22 pm
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]

Posted: Tue Oct 19, 2004 2:41 am
by Steveo31
My gosh feyd... thanks a ton.

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