[solved] Variables after use in the same script?

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
User avatar
dallasx
Forum Contributor
Posts: 106
Joined: Thu Oct 20, 2005 4:55 pm
Location: California

[solved] Variables after use in the same script?

Post 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.
Last edited by dallasx on Thu Nov 10, 2005 3:21 pm, edited 1 time in total.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

Check the reset() function.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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().
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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..
Post Reply