FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


need help building a switch/case dynamically w/ a loop &





ammonkc
I'm trying to build a switch/case dynamically. but I'm having problems. I don't even know if its possible to do this kind of thing in php. I want to use a switch to decide what content to display in the body of my index.php (pretty basic). But where it get tricky is I want to store a reference to include file in mysql. Then have a while loop inside of the switch create the "case".
example:
Code:

$get_pages = mysql_query("SELECT * FROM layout");
switch ($_GET['action']) {

     while ($page = mysql_fetch_array($get_pages, MYSQL_ASSOC)) {
          case "$page[page_name]":         //the code seems to break here
               include("inc/".$page[page_file]);
          break;
     }//end while loop

}//end switch


Is something like this possible? Can a switch see the case if its generated in a while loop? if not is there a better way to create a db driven dynamic switch?
Thanks in advance,
Ammon
eepman
Why don't you make $_GET['action'] hold the name of the style and then just compare it to the database. Then if it matches include it. For instance...

Code:

$pages = mysql_query("SELECT page_name, page_file FROM layout");

while ($page = mysql_fetch_array($pages, MYSQL_ASSOC))
{
if ($_GET['action'] == $page['page_name'])
         include '/inc/'.$page['page_file'];
}


Also you could make it faster a few ways. You could get rid of the page_file column and just append the extension in the script such as below.

Code:

include '/inc/'.$page['page_name'].'.ext';


Smile [/code]
ammonkc
yeah, that would work good (I think, there are some other embeded if's and switches that i would have to work out too). I don't know why I didn't think of that first. I feel dumb now.
Thanks,
woja
Since you only want one page, why not do it ALL in the SQL:
Code:

   $page = mysql_query("SELECT page_file FROM layout WHERE (page_name = '" . $_GET['action']  . "');");

Then all you need is the single field (page_file) from the query result (sorry, can't remember all the MySQL functions; use ODBC mostly).
This has the advantage that you only get either one row (the norm: all is OK) or none (error: page not found) rather than ALL the rows in the table (there may not be many, but why bother getting the ones you don't need).
ammonkc
yeah, that is how I ended up doing it. I thought that I would the queries do all the work so that php doesn't have to. in my experience mysql seems to be a lot faster at parsing data than php.
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.