PDA

View Full Version : Dynamic URL


688206002
10-21-2004, 04:18 AM
As this topic has generated quite a bit of interest in other threads, I've decided to start a thread for it and offer my own method.

For the other thread containing Dynamic URL discussions, visit: this (http://www.hostmatrix.org/forum/index.php?showtopic=197).


I'd like to point out that both of the methods offered in the other thread use the switch statement, which is a more powerful way of doing it (you'll soon see why). There is; however, a much simpler and effective way, which also happens to be my preferred way:

Insert this in your code somewhere:

<?php
$p="includes/" . $_GET['p'] . ".inc";
if($_GET['p']==''){
$p="includes/home.php"; // if no value for p is defined in the URL, display home.php by default
}
?>

and at where you want the content to be displayed, insert:

<?php
include($p);
?>


With this method, an URL such as http://xyz.hostmatrix.org/index.php?p=about would call the include file about.inc (located in the includes directory).

It is simple and takes far less lines than the other methods.

This method, however, has its drawbacks. With the switch statement, one can have an URL like http://xyz.hostmatrix.org/index.php?p=about and call an include file with a totally unrelated file name, such as info.inc, instead of about.inc.

It depends on your preference and your file naming habits. Personally, I find this method much simpler and cleaner, and it gets the job done quite well. If you'd like to have more customizability, visit the above link for the switch statement method.

shwaza
12-08-2004, 04:11 PM
First of all, sorry for digging up an ancient topic, but i am currently learning php and i want to make my new site have some dynamic urls. Ok anyway my question is, would i make a table in a database named 'p' and then have a field in that table named about? again sorry, but this is the most clear explanation ive seen of dynamic urls, and i would appreciate an answer :)

EDIT:

I used a combination of this code and another one i found in this forum. I'll post the code here so you can tell me how stupid i am :)


<html>
<head>
<title>yup</title>
</head>


<body bgcolor="#FFFFFF">

</body>
<?php
$connect = mysql_connect("localhost", "<shwaza>_<shwaza_shwaza>", "<password>")or die("Can't connect: " . mysql_error());
mysql_select_db("<shwaza>_<shwaza>", $connect);

$query = "SELECT <about> FROM <p> WHERE <condition>";
$result = mysql_query ($query)or die("big fat error in request: " . mysql_error());
$row = mysql_fetch_object($result);
<about> = $row-><about>;
echo <about>;
?>

<?php
$p="includes/" . $_GET['p'] . ".inc";
if($_GET['p']==''){
$p="includes/yup.php"; // if no value for p is defined in the URL, display yup.php by default
}
?>
hello
<?php
include($p);
?>

</html>

Thanks in advance, cuz i know SOMEONE must be able to help me ;)

lol edit #2: if anyone RELIABLE thinks that there might be something wrong with the DBs or anything like that, I could pm you my pass and user name :)

mahangee
12-08-2004, 05:41 PM
I hope this is what your looking for :D If not just reply and explain a little clearer and I will try to help.

You would name the database <shwaza>_<shwaza>
You would name the table


You would name the field <about>

shwaza
12-08-2004, 06:14 PM
ok, ive got it like that, however its still not working right, you can veiw the page im trying to get to work here (http://shwaza.hostmatrix.org/yup.php)

mahangee
12-08-2004, 07:04 PM
Originally posted by shwaza@Dec 8 2004, 06:14 PM
ok, ive got it like that, however its still not working right, you can veiw the page im trying to get to work here (http://shwaza.hostmatrix.org/yup.php)
16288

Is the source of the page you just gave us, exactly the same as the one you posted. If it is I believe that you cant have variables or anything of the sort with < or > in.

shwaza
12-08-2004, 07:36 PM
well here I might have changed something, this is what shows up with i edit it from file manager


<html>
<head>
<title>yup</title>
</head>


<body bgcolor="#FFFFFF">

</body>
<?php
$connect = mysql_connect("localhost", "<shwaza_shwaza>", "<password>")or die("Can't connect: " . mysql_error());
mysql_select_db("<shwaza>_<shwaza>", $connect);

$query = "SELECT <about> FROM <p> WHERE <condition>";
$result = mysql_query ($query)or die("big fat error in request: " . mysql_error());
$row = mysql_fetch_object($result);
<about> = $row-><about>;
echo <about>;
?>

<?php
$p="includes/" . $_GET['p'] . ".inc";
if($_GET['p']==''){
$p="includes/yup.php"; // if no value for p is defined in the URL, display yup.php by default
}
?>
hello
<?php
include($p);
?>

</html>

mahangee
12-08-2004, 08:36 PM
Out of interest are you changing the pieces of text that are in the <> to other pieces. Because if they are in <> I am sur ethat they will not work. For example are you changing

to you tables name? If not you must.

Tim
12-08-2004, 09:00 PM
I'm not sure what you're doing with mySQL in there. You need to have a corresponding table: and it doesn't look like it. The error that's being produced, however, is right here:<about> = $row-><about>;
echo <about>; Firstly, variables start with a $ sign. So, you would want to make it look like $about = $row->about; and then change the <about> in the echo statement to $about. Variables can not contain less than/greater than signs, either. A pointer: The mysql_fetch_object should be in a loop. Example:while ($row = mysql_fetch_object($result))
{
$about = $row->about;
}

shwaza
12-08-2004, 11:39 PM
tim, if i pm-ed you my pass and databases and stuff to connect to mysql do you think you could just make a simple page that queried that table?

Tim
12-08-2004, 11:42 PM
Yes. But what do you need the query for? Are you retreiving content?

shwaza
12-09-2004, 01:23 AM
yes, my site is going to have many similar pages with only the content being different, so i just want to query it each time instead of making a whole new page. I have no idea how to do the following, query a database, connect to mysql properly, add content to a database... pretty much everything lol

Tim
12-09-2004, 01:49 AM
Ah :) Then, you're just making things harder for yourself.

Get your layout together, first of all.
Make a table, name it "pages", for instance, in PHPMyAdmin with two fields: name and content. In name, make it VARCHAR with a length of 255, and for content, make it TEXT.
Once you have everything done, figure out where the content would go if you are doing HTML editing.
Then, in that area, put this code:
<?php
mysql_connect("localhost", "YourCPanelUsername", "YourCpanel Password") or die("We are having troubles with the site at the moment.");
mysql_select_db("yourDBName") or die("We are having troubles with the site at the moment.");
$p = $_GET['p']; // This is the dynamic URL part.
$result = mysql_query("SELECT * FROM `pages` WHERE `name` = '$p'");
if (mysql_num_rows($result) != 1)
{
echo "There was an error processing your request. Most likely, the page is missing. If you have received this from a link on this site, contact the administator.";
}
else
{
while($cPage = mysql_fetch_object($result))
{
echo $cPage->content;
}
}
?>
Of course, replace your CPanel Username/Password, and DB Name. It should work. In your DB, to add a page, just add a row, and name it whatever you'd like. If you name it nothing, it will be your default page. If you name it, say, "test", accessing http://www.domain.tld/script.php?p=test will display the content.

I hope it works.

Tim :)

shwaza
12-09-2004, 01:52 AM
alright, one more quick question, how do i add content to those pages?

EDIT: SQL-query:

CREATE TABLE `pages` (

`name` VARCHAR( 255 ) NOT NULL ,
`content` TEXT( 255 ) NOT NULL
)

lol not quite sure what i did wrong, but mysql didnt like it

Tim
12-09-2004, 01:57 AM
You would add a row into your table- the field name will control your URL link, and content will be what is displayed. You edit the row to edit your page. This can all be done using PHPMyAdmin. (or, if you don't understand it well enough, if I get time tomorrow, I could make a little admin CP for you.

Tim :)

Edit:
You don't fill in a length for the TEXT field. :)

shwaza
12-09-2004, 01:59 AM
ok, thanks, but do you know what im missing from there that mysql didnt like?

EDIT: lol sorry for wasting so much of your time btw ;)

EDIT2: yay!! i got it to give me the error message There was an error processing your request. Most likely, the page is missing. If you have received this from a link on this site, contact the administator.

EDIT3: ok i have to go now, but ill be back tomorrow and ill check, because it says you've been replying for a while, so ill just read it when i get back ;)

Tim
12-09-2004, 10:07 PM
I wasn't replying :| I went away...
Anyway, are you sure there's a page for the name you put in to your browser, and there are not multiple pages of the same name?

shwaza
12-09-2004, 10:51 PM
i dont think so, would i type in shwaza.hostmatrix.org/yup.php?p=(something that im not sure what to type in)

Tim
12-09-2004, 11:00 PM
The (something that you're not sure) would be the name that you gave to the row of the table. For example, if you inserted with PHPMyAdmin a row with the "name" as "Test", and the "content" as "Test", going to http://www.shwaza.hostmatrix.org/yup.php?p=Test would display "Test".

Tim

shwaza
12-09-2004, 11:08 PM
ok, ill try that