Jump to content

Generating Cases of a switch based on number of records in a db table help


CStrauss
 Share

Recommended Posts

I'm back with another question to for my latest idea not sure if this is possible or a good thing to do but in my case it seems logical.Here is what Im doing I got a table that allows people to add a category to it. Now from that I want to count the number of rows in the table which represent a category. In my example I have 8 rows so I want to generate a case for each of the rows represented in the database.So in my category table like i said i have 8 records.Cat1Cat2Cat3Cat4 and so on.Now lets say on category.php page is my switch statement and in the default case it displays a set of links with the req value the category name like this

<a href="category.php?req="Cat1">Cat1</a>

Still Following me?Okay now when they click a link it takes them to the switch part of the script and from here I need to count the number of rows in the table (since the user can add as many categories as the wish this is an unknown number) and loop through the category table and make a case for each of the categories. ie case "Cat1": case "Cat2": etc until all the categories in the databasse have a case then exit the loop and then the default case is next.Now i know how to do the sql count and the while loop to loop through the records just not sure where the best place to put my query to count the number of records and exactly how the syntax to make the cases will go. I usually start my cases off like this

$req = (!isset($_REQUEST['req'])) ? 'default' : $_REQUEST['req'];switch($req){case:default:}// End switch

should my record count be outside the first line of code then after the switch keyword have my loop to make the cases or should have it all after the switch statement?If anyone has done something similar to what im describing can you please post an example of how you did it. I cant seem to get my idea to work. Thanks sorry for the long post :)

Link to comment
Share on other sites

Okay i been playing around trying to get this to work and I seem to have a solution but with a problem here is my code I came up with.

<?phpinclude $_SERVER['DOCUMENT_ROOT'].'/inc/database.php';	// Get Number of Categories In table to count how many cases I need$sql = mysql_query("SELECT COUNT(*) as number_items FROM category_table");$total = mysql_result($sql,0,'number_items');echo "number of records is: ".$total;// Set Loop variable and create an array with the range of the total number of records retrieved above.$count = 1;$case = range($count,$total);// My switch statement checking the value passed through the link $req = (!isset($_REQUEST['req'])) ? 'default' : $_REQUEST['req'];switch($req){	// Generate Caseswhile($count <= $total){$sql = mysql_query("SELECT * FROM category_table WHERE cat_id='$count'");$row = mysql_fetch_assoc($sql);stripslashes(extract($row));$case[$count]=$cat_name;//echo "<p>".$case[$count] ."</p>";case ($case[$count]):echo "<p>".$case[$count]."</p>";$count++;break;}default:	// Generate Inventory Links$sql = mysql_query("SELECT * FROM category_table ORDER BY cat_name");while($row=mysql_fetch_assoc($sql)){	stripslashes(extract($row));	echo '<p><a href="inventory.php?req='.$cat_name.'">'.$cat_name.'</a></p>';}	break;} // End Switch?>

Now the problem is im getting this error:Parse error: parse error, unexpected T_WHILE, expecting T_CASE or T_DEFAULT or '}' in C:\wamp\www\PoserInventory\inventory.php on line 16Which line 16 is the while statement while($count<=$total){Now something is telling me that the switch statement dose not like the { inside switch and before a case is made is that a correct assumtion. If so what is something else i can do to parse my array to make cases.I did a test and my array works as i want it to if i take that whole while loop and place it outside my switch and test the values by commenting out my code so I know the problem is with that while loop inside my switch any ideas?????? thanks

Link to comment
Share on other sites

The only thing you can have in a switch statement are case statements and the default statement. You can't have a while inside of a switch, the while needs to go inside of a case. If you want to build your list of cases dynamically, then you will probably need to have a while loop that builds a string of PHP code, and then use eval to execute it.

$switch = "switch (\$req) {\n";while (...){  $switch .= "case \"$case\":\n";  ...  $switch .= "break;\n";}$switch .= "}\n";eval ($switch);

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...