POG1 Posted November 13, 2008 Share Posted November 13, 2008 I am working on a game and i have decided to have everything work from the index file and use the switch function. I was wondering if there is a better way of how i done it.. <? $allowed = array(town,inventory,mailbox,bank); if(preg_match('/^[a-zA-Z0-9_]/',$page)( { echo '<h2>Page Error</h2>'; endPage(); die(0); } if (in_array($page, $allowed)) { switch($page) { case 'town': include("town.php"); break; case 'inventory': include("inventory.php"); break; case 'mailbox': include("mailbox.php"); break; case 'bank': include(bank.php"); break; // more here } } ?> Would it be safe to just use. include($page.'.php'); Quote Link to comment Share on other sites More sharing options...
POG1 Posted November 16, 2008 Author Share Posted November 16, 2008 Re: My Engine I have another dilemma. I am working on a system that looks in the database and if the page is there it won't display it. The database feild will contain values like.. forum-mailbox-town-events I have used explode to seperate them into an array and i can add to the pages, but i can not take them away. Can someone please help me or tell me what i need? i was thinking of a function to delete a selected array then to put it back together with implode Quote Link to comment Share on other sites More sharing options...
Guest Anonymous Posted November 16, 2008 Share Posted November 16, 2008 Re: My Engine True using , (comma) as a separator... Then... Have a look at http://nyna.co.uk/set-extensions.txt The FIND_IN_SET already in the MySQL function library, however it was missing ADD_TO_SET and REMOVE_FROM_SET. BTW, the reason this is linked - is I so I can update my one copy from time to time without having to repost in a dozen odd forums and hundreds of topics. Quote Link to comment Share on other sites More sharing options...
POG1 Posted November 16, 2008 Author Share Posted November 16, 2008 Re: My Engine hmm how can i add them, would i run them as a sql? i had tried a foreach loop but your sql functions seem better. Quote Link to comment Share on other sites More sharing options...
Guest Anonymous Posted November 16, 2008 Share Posted November 16, 2008 Re: My Engine Basically you past each definition that you need - the CREATE FUNCTION ... RETURN ... ; into phpMyAdmin Try adding ADD_TO_SET Then you can perform: SELECT ADD_TO_SET('A', 'B,C') which means... you can create a field in a table with a comma separated list (CSV field): csv_field: VARCHAR(250) DEFAULT NULL then you can do tricks like: UPDATE table SET csv_field = ADD_TO_SET('A', csv_field) WHERE !FIND_IN_SET('A', csv_field); *** BEWARE *** These functions are NOT fast -- But they do work. I use them a lot, but I have highly optimal queries and look at very small row-sets. Running these functions on tables with several million fields WILL stall your database. Quote Link to comment Share on other sites More sharing options...
POG1 Posted November 16, 2008 Author Share Posted November 16, 2008 Re: My Engine Thanks for that, the add works a treat! does this look ok to you? if(isset($_GET['block']) AND $user_class->staff == 1){ $block = preg_replace("/[^a-zA-Z0-9]/","",strip_tags(trim($_POST['block']))) $r = mysql_fetch_array(mysql_query("SELECT disabledpages FROM serverconfig")); $pageArray = explode(",",$r['disabledpages']); if(in_array($block,$pageArray)) { // unblock page $result = mysql_query(sprintf("UPDATE serverconfig SET disabledpages = REMOVE_FROM_SET('%s',disabledpages)",$block)); } else { // block page $result = mysql_query(sprintf("UPDATE serverconfig SET disabledpages = ADD_TO_SET('%s',disabledpages)",$block)); } } Also when i try to add the remove function i get an error Quote Link to comment Share on other sites More sharing options...
Guest Anonymous Posted November 16, 2008 Share Posted November 16, 2008 Re: My Engine DROP FUNCTION IF EXISTS ADD_TO_SET; should be sufficient Although... I seldom drop them. Once they are installed, I can forget them. Interesting usage though -- Not my preferred method, but at least it does demonstrate the technique which can easily be used in all sorts of areas. Quote Link to comment Share on other sites More sharing options...
POG1 Posted November 19, 2008 Author Share Posted November 19, 2008 Re: My Engine I have another simple query. I added a table into the database and the pages that are disabled depending on status; hospital, jail, staff and admin. I was wondering what would be the best way, all i can think of is keep setting the same variable with a number of if functions. Here is what i got, i don't want the script to be very slow and there probably would be a better and faster way to do what i need, can someone give some pointers or help? please :) $validPage = (preg_match('/^[a-z]+$/',$_GET['page']) AND isset($_GET['page']) AND file_exists($dir.'/'.$_GET['page'].'.php')) ? $_GET['page'] : index ; $pageArray = explode(",",$serverCheck['disabledpages']); $validPage = (in_array($validPage,$pageArray) AND $user_class->staff != 1) ? errorpage : $validPage ; Quote Link to comment Share on other sites More sharing options...
Faz` Posted February 18, 2009 Share Posted February 18, 2009 Re: My Engine I have seen a game run from this method and it is incredibly fast, good luck to you, I hope it goes well, you'll really have pulled off something sweet if you get this done. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.