问题
Ok i have a dynamic page where people can post events in the city, we will call that page: city.php. In order to get to the page though, you must select a state from states.php, then a city from allcities.php. The states and cities are all in mysql database. On the city.php page you can click "add event" and it will take you to createevent.php where you can create and add an event that shows on city.php. But here is what i want to do:
I want to make it so that city.php is the central spot for posting different things for that city. I want a page for events, news, jobs, and for sale. On the city.php you will select a link for those pages taking you to pages such as events.php, news.php, jobs.php, and sale.php. How can i keep all those pages dynamic and related to the selected city? I toyed around and couldn't figure it out.
回答1:
There are a number of ways to do something like this in PHP. I personally disagree with your method as it sounds. I could be wrong about what it seems like you're saying though.
As adam said, $_SESSION is the first thing in mind. You can also look into $_COOKIE: http://php.net/manual/en/features.cookies.php
or just pass the city id as a $_GET var: http://www.yoursite.com/events.php?city=2
Then read it in PHP like
$city_id = $_GET['city'];
回答2:
I'm not sure if I understand you, but do you want variables to cross over into other pages? Take a look at PHP's use of sessions. Sessions are very handy, it essentially allows you to have site-wide variables associated to each of your users.
http://php.net/manual/en/features.sessions.php
Basically at the top of each of your pages just put:
session_start();
And then to assign / access a session variable you just call:
$_SESSION['city'] = $city;
or
echo $_SESSION['city'];
回答3:
Quite generic but key is in the relational db structure. Displaying is easy, so make sure that news, job, events tables has id_city
for building correct queries.
Example DB style as answer to your comment:
cities : id_city | city_name | city_slug | id_state
events : id_event | id_city | event_name | other_event_data | ...
jobs : id_jobs | id_city | job_name | job_salary | etc.
So user clicks to yoursite.com/washington
Then you can query that comes from url (here is washington) like:
SELECT id_city FROM cities WHERE city_slug = "washington"
You got id_city now. Then if it is jobs,
SELECT * FROM jobs WHERE id_city = "above gotten id_city"
Hope this helps.
来源:https://stackoverflow.com/questions/12682660/creating-editing-a-php-dynamic-page