LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   PHP Link Handling (https://www.linuxquestions.org/questions/programming-9/php-link-handling-6872/)

Citizen Bleys 09-25-2001 09:10 AM

PHP Link Handling
 
Can anybody tell me a way to make clicking on a link in a PHP-generated page execute a function in the PHP script instead of loading a new page?

I'm redesigning my website from the ground up as an object-oriented PHP project. I want index.php to require page.inc, with the source code to class page, set a few variables, and then call the display() method of class page. When the user clicks a link, I want the script to load the new content from a MySQL database and then call display() again--Hence the header, footer, and other static content is unchanged, but the new content is displayed in a table frame at the center of the page.

Since I'll be keeping downloads and images for each section in a separate directory, just making a whole bunch of nearly identical "index.php" files is a pain in the hole--especially since loading a new index.php requires a new connection to the database! Why terminate a script and start an identical one when I can just keep running the same script?

daholygoat 10-10-2001 03:02 PM

Well you can actually run the same script you know. A simple example on how to go around these things:
You create that index.php, and make it look like this, for example (assuming you've already done your PHP connection and there's a field 'article' and 'articlename' in a table 'tblContent':
PHP Code:

<?
switch($content)
{
    case 
"apples":
         
$query "SELECT article FROM tblContent WHERE articlename =               'apples'";
         break;
    case 
"pears":
         
$query "SELECT article FROM tblContent WHERE articlename =               'pears'";
         break; 
     default:
         
$query "SELECT article FROM tblContent WHERE articlename =               'main'";
         break;
}
$result mysql_query($query) or die;
list(
$article) = mysql_fetch_row($result);
echo(
$article);
?>

Now if you call your script like this: index.php?content=apples , the "apples" case will get executed, and the correct content will be loaded whereever you want it to be loaded (well, whereever you echo() it, in this case). Same thing for the other menu items of course. If none of the cases are true, the 'main' content will be loaded. And that's that. Have a lot of fun :-).


All times are GMT -5. The time now is 04:38 AM.