Page 1 of 1

[solved] Variables after use in the same script?

Posted: Thu Oct 27, 2005 5:00 pm
by dallasx
This seems like a silly question but what happens to variables when you try to reuse them given the following:

Code: Select all

$sql_msizes = "SELECT m_product_size FROM product WHERE product_id='$prodid'";
		$result = mssql_query($sql_msizes);
		$mens_sizes = mssql_fetch_assoc($result);
		$ms = $mens_sizes['m_product_size']; // $ms < this is the one i'm talking about
		$ms = unserialize($ms);
		echo "<select name=\"available_mens_sizes\">";
		echo "<option selected>Select a Size</option>";
		while (list($key, $value) = each($ms)) // < used here
		{
		if($value > 0)
		{
			echo "<option value=\"".$key."\">".$key."</option>";
		}								
	echo "</select> Quantity: <input type=\"text\" size=\"4\" name=\"mens_size_quantity\"><br>";
Two lines down in the same script I tried to run the while statement with the same varible $ms and nothing showed up. That's what I don't understand.

Posted: Thu Oct 27, 2005 6:19 pm
by Bill H
Check the reset() function.

Posted: Thu Oct 27, 2005 6:35 pm
by RobertGonzalez
SORRY. MY ORIGINAL POST WAS DELETED BY ME BECAUSE IT WAS STUPID.

Have you print_r'ed the values of the two $ms's prior to list = each? See what they give you to see if anything is getting passed to each().

Posted: Fri Oct 28, 2005 1:31 am
by Jenk
It shouldn't make any difference what so ever, but try:

Code: Select all

$ms = unserialize($mens_sizes['m_product_size']);
Infact.. I'm certain it won't make any diff.

Debug your code, stick a few extra echo's so you can see where it is going etc., such as

Code: Select all

<?php

        while (list($key, $value) = each($ms)) // < used here
        {
        echo "While Loop\n";
        if($value > 0)
        {
            echo "<option value=\"".$key."\">".$key."</option>";
        }                                 

?>
So you can see if, and how many times the while loop runs..