Jump to content

minimising the number of php pages


jess345

Recommended Posts

hey

i was hoping someone could help

i have created numerous php web pages but all the pages are almost identical and i know is a ridiculous way to write a web site

im looking for a way of haivng one page and changing variables on the page to create different pages

 

for example all my pages do is insert and select from a database so each page is different to another just by the field of the table in the database that they can access.

 

im hoping someone could direct me to a helpful tutorial or mayb code to get me started

thanks

katy

Link to comment
https://forums.phpfreaks.com/topic/46856-minimising-the-number-of-php-pages/
Share on other sites

You have several options.  All are based on fairly basic PHP concepts, so you can check the manual for usage.

 

First, you can use the GET (or even POST, depending on what you are doing) method to let the script know what it should do.  That would be a URL that looks like:

http://www.yourpage.com/script.php?page=page1&value1=this&value2=that&value3=0

 

Depending on how your queries are constructed, you may be able to keep the queries in an associative array.

 

<?
$inserts = array(
'page1' => "INSERT INTO table1 VALUES('%s','%s','%s')",
'page2' => "INSERT INTO table2 VALUES('%s','%s',%u)"
// ...etc.
);

if (isset($_GET['page']) && isset($inserts[$_GET['page']]) {
mysql_query(sprintf($inserts[$_GET['page']],$_GET['value1'],$_GET['value2'],$_GET['value3']));
}

 

If your queries are very different you could use a switch block to decide which to execute:

 


if (isset($_GET['page']) {
switch($_GET['page']) {
	case 'page1':
		mysql_query(sprintf("INSERT INTO table1 VALUES ('%s',%u,'%s')",
			mysql_real_escape_string($_GET['value1']),
			mysql_real_escape_string($_GET['value2']),
			mysql_real_escape_string($_GET['value3'])
		));
		break;
	case 'page2':
		mysql_query(sprintf("INSERT INTO table1 VALUES ('%s','%s')",
			mysql_real_escape_string($_GET['value1']),
			mysql_real_escape_string($_GET['value2'])
		));
		break;
	default:
		// error code here
}
} else {
// handle other stuff here
}

 

I've left out alot of error checking and/or user-input validation code to keep concepts simple, but if I don't mention it here, someone else will -- you should check input before blindly trusting it that it is only the values you expect, and you should never put user input directly into an SQL statement without at least using one of the quoting functions as I have above in the second example.

 

So, there are a few things you can do.  Essentially you want to find what's being repeated in your scripts and consolidate as much of that as you can into some loop or variable such that code is minimized.  Let me know if you need more ideas.

hey

yea thanks that really helped i got the switch to work and it is actually what i want to happen.............only a slight problem

i want to use a few switch methods through my page as i have four different button that attach to the database and only acess the database if clicked

 

if (isset($_POST['Submit'])) {
$Computer_Science= $_POST['Location_Name'];
$building= "Arts";
switch($building) {

	case 'CSI-WLAN':
		$query = ("INSERT INTO messages(comments, buildings, picture) Values ('$Computer_Science', 'Computer Science', 'No picture')");
		mysql_query($query);
		break;
		case 'Arts':
		$query = ("INSERT INTO messages(comments, buildings, picture) Values ('$Computer_Science', 'Arts', 'No picture')");
		mysql_query($query);
		break;

}}


 

 

thats my code at the moment and works but when i have a few of them is means my variable has to be re declared a few times???

and i want to read in the variable from a file so because the switch are inside if statements the file variable isnt recognised!!do u know of a good way to try do what i want?

so bascially i want the $building to be decided at the top and be useable all through the php page ??? might be really simple

thanks

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.