how do i create a unique php page for each row in a mysql database

后端 未结 3 1013
孤城傲影
孤城傲影 2021-01-02 21:56

I have a mysql database connected with php. However, I want each row in the database to be a unique page. Could anyone please provide some sort of overview for how this woul

3条回答
  •  执笔经年
    2021-01-02 22:47

    Here is an example of how I am taking a URL to display a different page for each row in a Property database.

    I've shown my search mechanism below, but it can take a URL such as /search/id/123 to display property with id:123. $app->Property->search($array) is where the MySQL code is executed. This script exports a JSON object, which is loaded by Ajax and displayed by JavaScript on the other end. You don't have to export JSON, you could also load a PHP/HTML template and export the page in PHP too.

    $value) $qs[$key] = urldecode(htmlspecialchars($value));
    array_shift($qs); #clear blank first element
    
    # Parse URLs with the following format:
    # /search/{$field1}/{$value1}/{$field2}/{$value2}/.../{$fieldN}/{$valueN}
    switch ($val = array_shift($qs)) {
    
        case "search":
            $params = array(); # query array
            $field = true; # switch between field or value
            while (!is_null($val=array_shift($qs))) {
                if ($field) $params[-1] = $val;
                else $params[$params[-1]] = urldecode($val);
                $field = !$field;
            }
            unset($params[-1]);
            $result = $app->Property->search($params);
            header('Content-type: application/json');
            echo json_encode($result, JSON_FORCE_OBJECT);
            exit; #don't load template after exporting JSON object
            break;
    ...
    

    This script works together with a .htaccess script that redirects all requests to my index.php file. The index.php at some point loads this controller.php file, which handles the URLs and loads the page depending on the URL.

    Here is the .htaccess code:

    
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    
    

提交回复
热议问题