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.
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'];
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'];
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.
Related
I am trying to create a pivot table to help keep track of "challenges" in my applications. Basically I have a challenge_task pivot table that creates a relationship between a challenge and a task. When a user that is in a challenge completes a task I want to be able to tell so I can track a user's progress. How is the best way to store multiple users completing a task on a challenge?
I was thinking in the pivot table adding a json column called user_completed to handle this and store the user_id for every user that completes the task for a challenge.
So challenge_task would look like
challenge_id | task_id | user_completed
Is this a good way? Or is there anything that fits this better?
I'd recommend a database structure something like this:
challenge: challenge_id | other data
task: task_id | other data
user: user_id | other data
challenge_task: challenge_task_id | challenge_id | task_id
| possibly more data (such as deadline for completion)
challenge_task_users: challenge_task_id | user_id
| possibly more data (such as status: accepted, in progress, completed)
I dont recommend Json if you want to index your data, because Json can not be indexed.
I think you should make a pivot table between the users and the tasks too, and create the neccesary relations.
I wouldn't recommend you inserting multiple values in one database column.
Note: This is my opinion. Just sharing the way I use it.
A table called tasks_settings which has the task settings.
I find this way flexible because I can always edit the title, description, and reward easily. I can also add 2 more fields here which are valid_till and valid_for. So you can make it expire after a period of time and only for a special rank like staff or all users.
Another table called users_tasks
This one controls the users. Whether they have completed the task or not. This could also achieve what you are looking for.
id | challenge_id | task_id | username | user_completed
I hope this has helped you!
I have a database in MySQL that currently lists approximately 1500 concerts and events. Now, the plan is to add setlists (list of the songs performed at the concerts) for all the concerts in the database. Basically this will mean a lot of repeated values (songs performed at many concerts), and I would really appriciate some input on what the best approach would be.
I initially started out with a database similar to this;
| eventID | edate | venue | city | setlist |
The field setlist was basically text data, where I could paste the list of songs and parse through it to put each song on a new line with php. This works, and editing the text and running order was like editing a text document. Now, obviously this was pretty simple, but has drawbacks and limitations. Simple things like getting stats on songs performed is probably very difficult, right?
So, what is the best way to store the setlist value?
Create a new table that adds a new row for each song performed, and that has a foreign key linking to eventID? How would I best retain (and edit, if needed) the running order of the songs in that table? Any other suggestions?
Thanks for any input or advice on this, as I would love to get some help before I start adding all the data.
I would create a table that holds each song performed at a specific event:
| songId | eventID | song |
Where eventID can be duplicated in multiple rows to show each song performed at that event.
This way you can query all the times a specific song was performed, and also get all songs (the setlist) for a specific event by querying on the eventID.
I have a client who wants to have a comment box on each page of her website. The thing is, the website has already been created as a static html site. I've made comment boxes in the past using php and phpMyAdmin, and obviously I'll change each of her pages to .php instead of .html so it'll support my comment box's php code, but I've only ever created comment boxes for a single page before. The website I'm altering now has at least 10 pages that need a comment box, and more will be added in the future.
I like the idea of using a singular database table for all the comments that are submitted on the site (there won't be a high volume). I'm just not sure how to tell the submitted comment which page it's supposed to display on.
I think what I need to do is have the submitted comment store what page it's from in the database, and then I can have a function on each page that looks for all the comments with that particular page's name or id. I'm just not sure how to do the first part where I send in data that identifies the page with the rest of the comment box's data, can anyone point me in the right direction?
Sorry if this a silly question, I'm still pretty new at PHP, thanks!!
you're on the right track with your idea. There are multiple ways of doing it, but a simple solution would be to have a page name field in your database table. when gathering the information for db insertion retrieve the page by putting a hidden field in the comments form, maybe something like...
<input type="hidden" name="page_name" value="<?=$_SERVER['REQUEST_URI']?>" />
then upon fetching comments from the database do a db query like
"SELECT * FROM `comments` WHERE `page_name` = '".$_SERVER['REQUEST_URI']."'";
Always remember to sanitize user information before db insertion!
Whay you need is a tablewith a column that identifies the page you need. For instance:
Table: Comments
---------------------------------
id | page | comment | createdAt |
---------------------------------
1 | 1 | Com1 | 2014-01-01 00:00:00|
2 | 2 | Com2 | 2014-01-01 00:00:22|
3 | 1 | Com3 | 2014-01-01 00:30:00|
4 | 1 | Com4 | 2014-01-01 00:35:00|
Then at the specific page use the column page in your where clause to select only the comments from that page.
For instance:
SELECT comment, createdAt FROM comments WHERE page = '1' ORDER BY createdAt DESC
The ORDER BY will rearrange the order with the newest on top.
When posting to the database insert the page identifier aswell.
INSERT INTO comments (page, comment, createdAt) VALUES ('1','this is a comment', NOW())
Get it?
I'm trying to show or not show links based on a users access level. The links will be different depending on the section of the site the user might be in. The links also may not all be in one menu. They will more than likely be in various places on the page.
Currently I have a database table that contains Users, Groups and Sections. The main menu is built from the Sections database table. I'm thinking I should create an Actions table and add a link that I'd like to show for each section in the action menu. So, my tables so far are like.
Users
user_id
Groups
group_id
group_title
Sections
section_id
section_title
Table I'm thinking of adding.
Actions
action_id
action_title
action_group_id
action_section_id
The part I'm not sure on is should I add the same link multiple times to the Actions table for each group that is allowed access. Or, just add it once and do a if group id is greater than, then show link.
Example for entering the same link multiple times.
action_id action_title action_group_id action_section_id
1 View all 1 1
2 View all 2 1
3 View all 3 1
I was hoping to not flood the page with a bunch of if/then statements. Plus, this doesn't seem like the best way to handle because it requires human interpretation as to what the access levels stand for.
Any help on this is appreciated. I could be going in the complete wrong direction here?
Create a many to many relationship with an additional table where you insert an entry for each permission the group has access to. Am I correct in assuming section is what you're creating permission to?
Table: Group_Section (Or whatever you'd like to name it)
Group_id | Section_ID
---------+-----------
1 | 1
1 | 2
1 | 3
2 | 1
2 | 3
|
You can see that the Group with ID = 1 can access sections 1,2,3 while Group with ID = 2 can access only 1,3. You can then add whatever permissions to the table you want and manage them through the use of foreign keys.
Does that make sense?
Here is a good article but the things are discussed in general http://en.wikipedia.org/wiki/Access_controlenter link description here
In your case, use what TheCapn wrote and I'll just add, that its 'best to start session for every user and just check his access level when he's trying to reach a restricted part.
Personnaly, to do this kind of thing, i set a user level in the user table and a section level in the section table.
Then you simply have to filter the section according to your user level.
You can do this by adding a statemtn to you sql like
AND section_level >= "user_level";
or then again, get all the section and filter tham with php.
foreach($section as $s){
if ($s->level >= user_level) echo $s->title
}
Of course, you'll need to adjust the <. = and > according to the hierachy of your system.
I personnaly use a lowering hierachy, meaning, the lower the level you are the more right you have. This way you can make a 'banned' user by setting his level to 99 or something.
THis would be only for your menus, make sure you control the user_level on each page as well so if someone get to the page directly it get kicked..
Hope it points your in the right direction. ;)
I am trying to figure out the best way to have a page dynamically know which data to output.
I have a index.php that I want to be able to pass some $_GET variables into it and then based on that know what to display. Aside from the content being different the type of the content is also different I have products and other types of pages like blog posts etc. The products are stored in different tables then the other pages. and also have a slightly different table structure as well.
currently I have the following tables.
Subjects, Pages and Categories.
Table = Subjects
id | menu_name | menu_number | category | menu_position | active
Table = Pages
id | subject_id | category_id | page_name
Table = Categories
id | category
These are the main tables that outline the main structure of the site. I then have secondary tables that contain the actual data for the pages.
posts, post_details, post_pypes and
products, product_details, product_types and product_specs.
What I want to active is that a variable will be send along in the query string and based on this the application will know what information to display .
My question is how can I make a table that will catalog all entries (posts and products) with a unique id and that will be the only variable needed to for my application to determine how to proceed. i.e. query the table int he database that is holding that unique id and tell the application its a "product" and the product id is X and to continue querying the needed tables for the info.
Thanks in advance.
I highly recommend looking into a secure, structured environment like CodeIgniter to accomplish whatever it is you are trying to describe above. The scope of that question is really broad which likely indicates that you would profit from a prebuilt framework of some kind.