Jump to content

Sql switch statement


Tasos

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.