PHP pages With User-generated Content

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
I need some guidance please.

How do I go about to let the website build pages from content generated(submissions) by a user?

Example:

I am going to use pets as an example:

Website:
index
find my pet
Lost pets
contact

When the user go to find my pet they enter:
pet name
location last seen
image of pet

That gets stored in the DB

Now I want the website to then generate a new link/page with that data so that when a user navigates to lost pets the newly added pet is listed there and when they click the link it goes to the generated page for that pet.

Hope this makes sense.

I know how to do everything I only need guidance one what is the most ideal way to go about create a new page(file) populated with the data from the submission upon submission later I will ad moderation once I figure out how to generate the file first.

the files(pages) will be stored in a subfolder so on the lost pets page I will do a simple loop inside the folder to list all the available pages which should sort out the picking up newly added pages part.
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
I assume it would be better to create one php file that generates an html template, and what changes is the text based content on that page retrieved from the DB using a parameter (eg. row id) via POST or GET and then get the info form the database?
 

etienne_marais

Honorary Master
Joined
Mar 16, 2008
Messages
15,093
Not sure why the links can not just be populated from the DB, and when the link is clicked a single php file called with the id of the new pet displaying the relevant information. Why do you want to generate multiple files ?
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
Not sure why the links can not just be populated from the DB, and when the link is clicked a single php file called with the id of the new pet displaying the relevant information. Why do you want to generate multiple files ?

Yea, See below

I assume it would be better to create one php file that generates an html template, and what changes is the text based content on that page retrieved from the DB using a parameter (eg. row id) via POST or GET and then get the info form the database?
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
I still do not know how to actually do it, yet. :crylaugh:
 

etienne_marais

Honorary Master
Joined
Mar 16, 2008
Messages
15,093
You don't need to generate a new php file / template from where the pet information is entered unless you have some specific need to do so. Basically you can have for example:

FindMyPet.php which is linked from index.php/.html

Here you enter the pet information and generate the DB records after submit (can use same page or an extra page for the purpose of populating the DB)

LostPets.php which displays the links to ShowPet.php as pulled from the DB, eg. <a href="ShowPet.php?petId='13235'>Pet Name</a>
(or better an image of the pet when clicked)

ShowPet.php which displays the information for pet with petId = 13235 as pulled from the DB

This can't be what you want though, I think you are already past that level ?
 

Nick333

Honorary Master
Joined
Nov 17, 2005
Messages
35,114
In lost pets page:

foreach ($pets as $pet)
{
<a href="pet.php/<?php echo $pet['id'];?>"> Pet </a>
}

Function to generate page for individual pet:

function pet(id = null)
{
//your code here
}

Something like that.
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
You don't need to generate a new php file / template from where the pet information is entered unless you have some specific need to do so. Basically you can have for example:

FindMyPet.php which is linked from index.php/.html

Here you enter the pet information and generate the DB records after submit (can use same page or an extra page for the purpose of populating the DB)

LostPets.php which displays the links to ShowPet.php as pulled from the DB, eg. <a href="ShowPet.php?petId='13235'>Pet Name</a>
(or better an image of the pet when clicked)

ShowPet.php which displays the information for pet with petId = 13235 as pulled from the DB

This can't be what you want though, I think you are already past that level ?

It is what I want, but it goes a hell of a lot more complex than the pet example hehe, I just had to dumb it down to refresh my brain.

In lost pets page:



Function to generate page for individual pet:



Something like that.

Sweet stuff thank you all very much I am back on track now!
 

Hamish McPanji

Honorary Master
Joined
Oct 29, 2009
Messages
42,087
This can't be what you want though, I think you are already past that level ?


I thought he was past this point too. But i don't see him asking for anything else.

This can actually be done on a single page, since the same db connection is required

Index.php?op=addpetform
Index.php?op=addpetsubmit
Index.php?op=showpetgallery
Index.php?op=searchpetform
Index.php?op=searchpetresults
Index.php?op=searchpetdetail
 

Hamish McPanji

Honorary Master
Joined
Oct 29, 2009
Messages
42,087
side note:

ShowPet.php?petId='13235' is really ugly how does one go about with rewriting urls?

I love mybroadband since the urls are human readable example:

http://mybroadband.co.za/vb/showthread.php/841772-PHP-pages-With-User-generated-Content

compared to:

http://www.platinumwealth.co.za/forum/showthread.php?tid=25&pid=1483#pid1483

or

http://muscletalk.co.za/viewtopic.php?f=9&t=577&sid=2c44a861443dc984d455e8eb87417b90
You use htaccess to redirect all to the same page e.g. index.php

When a new pet is created, you can make up a url scheme for it, so examples:

http://petfinder.com/owner_name/dog_name
http://petfinder.com/dog_name_dbid

Of course you need to check for uniqueness of names before adding to the dB

Someone types In url:
http://petfinder.com/john_smith/Fido

The htaccess file redirects to index.php
In php in index.php ,you split the url by "/", read the last one and do a db search for it.

Your pet display page will also be a template which basically takes the url and convert it to a db search page
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
You use htaccess to redirect all to the same page e.g. index.php

When a new pet is created, you can make up a url scheme for it, so examples:

http://petfinder.com/owner_name/dog_name
http://petfinder.com/dog_name_dbid

Of course you need to check for uniqueness of names before adding to the dB

Someone types In url:
http://petfinder.com/john_smith/Fido

The htaccess file redirects to index.php
In php in index.php ,you split the url by "/", read the last one and do a db search for it.

Your pet display page will also be a template which basically takes the url and convert it to a db search page

Perfect!

thanks so much
 

Nick333

Honorary Master
Joined
Nov 17, 2005
Messages
35,114
Sweet stuff thank you all very much I am back on track now!

No sweat. Thought you just needed a nudge. Good thing too since my code is a bit of an abortion. :D You got the idea though.
 

Noah

Expert Member
Joined
Jan 21, 2008
Messages
1,539
Perfect!

thanks so much

mod_rewrite is what I use for apache.
You write some regex that matches incoming URI's, in my case domain/w{5} //5 chars
If the regex matches it redirects to domain/blalbalba.php?$1

That way I can handle the request for noa.moe/agrmh to get the relevant file and serve it correctly, maybe store some request info to stop retards walking my files.

There should be a rewrite module for all decent web servers.
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
https://caddyserver.com/docs/rewrite

etcetc
 
Last edited:

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
mod_rewrite is what I use for apache.
You write some regex that matches incoming URI's, in my case domain/w{5} //5 chars
If the regex matches it redirects to domain/blalbalba.php?$1

That way I can handle the request for noa.moe/agrmh to get the relevant file and serve it correctly, maybe store some request info to stop retards walking my files.

There should be a rewrite module for all decent web servers.
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
https://caddyserver.com/docs/rewrite

etcetc
Sweet, I was thinking about regex for the rewrite, then I suddenly thought wait a minute.... Will this not cause a speed/load time sacrifice?
Write schit down before you implement it. Will help you find the bugs in the logic early. So write down your naming strategy , test it with values on paper to see if it works
Definitely, I learned that early on. In fact that is how I saw my OP is a terrible initial idea.
 
Top