Forum Moderators: coopster

Message Too Old, No Replies

Populating case values in switch statement

Using while() loop to gather values from database

         

max4

9:13 pm on Jun 6, 2009 (gmt 0)

10+ Year Member



Hello,

I'm trying to populate the case values of a switch statement using a while() loop but I can't seem to get it to work. I've tried many different methods; currently my code looks like this:

[fixed]
function switch_name()
{
//.. load includes
$name_results = mysql_query("SELECT name FROM `table` WHERE column = 1");
$value_results = mysql_query("SELECT value FROM `table` WHERE column = 1");
$name_row = mysql_fetch_object($name_results);
if(mysql_num_rows($name_results) > 0 && mysql_num_rows($value_results) > 0)
{
while($value_row = mysql_fetch_object($value_results))
{
switch($_POST['$name_row->name']) {
case $value_row->value:
$x = $y;
break;
}
}
}
else
{
echo("");
}
}
[/fixed]

In the page that I use this function, I check to see if $x is equal to $y, and if not then I return an error stating that an invalid value was submitted. With the above code, the invalid value error is always returned.

I think the problem is that this is creating many switch statements for every value, rather than creating one switch statement with multiple cases. Basically, I would like my function to create a switch statement where ever I call it and populate the cases of the switch statement - with the end result looking something like this:

[fixed]
switch($_POST['$name_row->name'])
{
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
$x = $y;
break;
}
[/fixed]

Any suggestions? Thanks.

siMKin

9:27 am on Jun 7, 2009 (gmt 0)

10+ Year Member



why do you want to use a switch statement for this?


while($value_row = mysql_fetch_object($value_results))
{
if ($value_row->value == $_POST['$name_row->name'])
{
$x = $y;
}
}

also does the trick and looks less complex

max4

1:05 am on Jun 8, 2009 (gmt 0)

10+ Year Member



You're right about that siMKin; thanks for the tip. Would it be possible to have $x = $y if a value outside of $value_row->value was delivered? I allow some aspects of my forms to be returned empty and for those I always set their value to 0. So a typical switch statement might look like this:
[fixed]
switch($_POST['$name_row->name'])
{
case '0':
case '220':
case '221':
case '222':
case '223':
case '224':
$x = $y;
break;
}
[/fixed]

Is it possible, with your logic, to have $x = $y if $value_row->value == $_POST['$name_row->name'] and a value of 0 is delivered?

Thank you.

siMKin

6:04 am on Jun 8, 2009 (gmt 0)

10+ Year Member



I'm sorry, but i don't understand what it is exactly that you want....

max4

7:04 pm on Jun 8, 2009 (gmt 0)

10+ Year Member



I solved the problem by creating a handy php script that automatically creates and populates the switch statement, among other things, based on information from the database and variables I pass through a form. Interesting what PHP can do!

siMKin, thank you for your help; I really appreciate it. Your while loop will accomplish what a switch statement aims to accomplish, but it does not allow rogue values to pass. I use switch statements to ensure that the values I provide for input are the only values that make it to my action script. One of those values does not exist in my database, and that is the value of 0. I use 0 to allow users to select a blank option; '<option value="0">&nbsp;</option>'. This:

[fixed]
while($value_row = mysql_fetch_object($value_results))
{
if ($value_row->value == $_POST['$name_row->name'])
{
$x = $y;
}
}
[/fixed]

Is equivalent to this:

[fixed]
switch($_POST['example'])
{
case '220':
case '221':
case '222':
case '223':
case '224':
$x = $y;
break;
}
[/fixed]

But not this:

[fixed]
switch($_POST['example'])
{
case '0':
case '220':
case '221':
case '222':
case '223':
case '224':
$x = $y;
break;
}
[/fixed]

And that is what I was getting at. Actually, it just hit me that I could simply add in the value of 0 before the while loop initiates, and that would do the same thing as a switch with case '0'. Thanks again.