Tasos Posted October 29, 2014 Share Posted October 29, 2014 Hello, I don't know if this is possible. I want to make a switch between pages, lets say i have more than 100 pages and each page have a unique name.php. So what i want to do is when page equal to random-page.php SELECT * FROM `jobs` WHERE title LIKE '%news%' And for example how it should be, but i don't know what is exactly wrong here and how to fix it. $sql_q; $path=$_SERVER['PHP_SELF']; $page=basename($path); switch("$page") { case 'index.php': $sql_q = 'SELECT * FROM `jobs` WHERE `title` LIKE '%news%' LIMIT $p_num, $per_page'; break; case 'jobs.php': $sql_q = 'SELECT * FROM `jobs` WHERE `title` LIKE '%jobs%' LIMIT $p_num, $per_page'; break; case 'region.php': $sql_q = 'SELECT * FROM `jobs` WHERE `title` LIKE '%region%' LIMIT $p_num, $per_page'; break; } And here: $getquery = mysql_query("$sql_q"); Is that possible somehow ? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/292151-sql-switch-statement/ Share on other sites More sharing options...
mac_gyver Posted October 30, 2014 Share Posted October 30, 2014 based on what you are showing, you are planning on creating actual files - index.php, jobs.php, region.php, ..., but put (include) the same code in each file, that than needs to map the base filename to a search value. this is not the correct way to do this, as it results in a bunch of 'wrapper' files that must be maintained and managed every time you add, change, or remove a category/search type. the correct way of creating a content management system, would be to store the categories/search types in a database table, then you would have a single page that uses the contents of that database table to produce navigation links and to take the category/search type id/name from a submitted link to find the content to display on the page. Quote Link to comment https://forums.phpfreaks.com/topic/292151-sql-switch-statement/#findComment-1495210 Share on other sites More sharing options...
Tasos Posted October 30, 2014 Author Share Posted October 30, 2014 Thanks for you answer, Could you give me an example, or point me to the right direction on how to do this ? I add each month more than 10 new page so it would be easy if i can use a SQL switch statement so i could copy and paste the script an let it run automatic. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/292151-sql-switch-statement/#findComment-1495221 Share on other sites More sharing options...
jazzman1 Posted October 30, 2014 Share Posted October 30, 2014 Much, much better than your script to build the query in php, something like: <?php function getJob($p) { switch ($p) { case 'news': $txt = 'news'; break; case 'jobs': $txt = 'jobs'; break; case 'region': $txt = 'region'; break; default: $txt = 'default'; break; } return $txt; } $sql = "SELECT * FROM `jobs` WHERE `title` LIKE '%" . getJob('news') . "%' LIMIT 1 10"; echo $sql; Quote Link to comment https://forums.phpfreaks.com/topic/292151-sql-switch-statement/#findComment-1495227 Share on other sites More sharing options...
mac_gyver Posted October 30, 2014 Share Posted October 30, 2014 when the only thing that varies when an input value changes is an output value (i.e. you are mapping one value to another), you would not use hard-coded conditional logic to do this, as that would mean that you must find where in your code the values are hard-coded at and then alter the program logic every time you you add, change, or remove a value. you would use a data-driven design, where the mapping is stored in a data structure, such as a database table or an array, depending on how much data there is, and the only thing the general-purpose code does is take the input value and make use of the mapping data structure to obtain the result. the code doesn't change just because the amount of data or the data values change. Could you give me an example, or point me to the right direction on how to do this ? it's really beyond the scope of a post in a forum to feed you everything you would need to know to create a proper database driven content management system. you need to do some research and experimentation on your own and post specific questions you may have. here is an example of a data-driven design that took multiple sets of same program logic that only varied in the data values being operated on and changed it to use a mapping data structure, with one general purpose set of code that doesn't change just because the amount of data or the actual data values change - http://forums.phpfreaks.com/topic/291619-multiple-if-statement-not-working/?do=findComment&comment=1493538 Quote Link to comment https://forums.phpfreaks.com/topic/292151-sql-switch-statement/#findComment-1495246 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.