I am new to MySQL and PHP. I am having issues wrapping my mind around how to accomplish something. I am building a site that has basically a forum style post page. Users enter text into a textarea which then posts that text along with a timestamp and $_SESSION['Username'] into a MySQL table titled "campaigns." So the table has postEntry, postName and postDate rows currently.
On this same page that I have the form, I then display the entire contents of the campaigns table into a div. So it can show each post in descending order.
This has been working great for me, but I am now trying to look at the bigger picture and am thinking this is not a good way to do what I need. I basically need the ability to have an endless amount of "campaigns" each with their own set of posts. Then give the user the ability to select which campaign they want to view and show corresponding posts for that campaign in the div.
So the real question is: Is there a way to do this with just one table. Or would each campaign need it's own table in the database?
Add a campaign_id to the POST table and viola!
edit: more info:
you need one table for the campaign like so:
Campaign
-------------
campaign_id
name
then you need another one for all the posts
post
-------------
post_id
campaign_id
post_time
name
this way, each post is associated to a specific named campaign.
Related
I am creating a form to allow admin users to create new 'News' Items. I am using PHP Codeigniter. All works perfect at the moment storing and retrieving data from MYSQL database.
I have now realised I would like the option to add external URL links that relate to each news item. Possibility to add multiple links for each. I was thinking of adding a ´links´table to the database, foreign key relation with News table. Something like below:
tbl_news:
news_id,
title,
descrip,
...etc
tbl_link:
link_id,
title,
URL,
news_id
This will take a bit of processing as it is 2 input fields per link on the form and could become messy if the user adds multiple links per news item. Just wondering is there a better way to allow for URL input in a HTML form?
I have a screen that looks very much like facebook timeline
users can view posts of other users etc.
to get these posts i do something like
select user.id,user.name,posts.title,posts.body from posts left join users;
now data i need to collect is "Who saw this post" .
is there any elegant way to do it ?
right now all what i can think of is every time i fetch posts. i loop over them, then collect all ids of posts that the query returned and then push in another table
user_views [pk:user_id+postId]
userId,postId
1 , 1
Then when i'm fetching posts next time i can do count of user_views.
select *,count(user_views.id) from posts join user_views on post_id = post.id
but this sound like a lot of work for each VIEW, specially that most probably user will see a most multiple times,
is there any known patterns for such need ?
This is a design question and the answer really depends on your needs.
If you want to know exactly who viewed what post and how many times, then you need to collect the data on user - post level.
However, you may decide that you do not really care who viewed which post how many times, you just want to know how many times a post was viewed. In this case you may only have a table with post id and view count fields and you just increment the view count every time a post is being viewed.
Obviously, you can apply a mixed approach and have a detailed user - post table (perhaps even with timestamp) and have an aggregate table with post id and view count fields. The detailed table can be used to analyse your user's behaviour in a greater detail, or present them a track of their own activities, while your aggretage table can be used to quickly fetch overall view counts for a post. The aggregate table can be updated by a trigger.
I have two tables in my database. One is a list of members each, of course, having a different member ID. The second table is a list of items that have been designated to one member.
Please note that each table has a user_id column. Based on user_id I want only certain items displayed on their webpage.
When Joe logs to the members area, he is taken to a welcome page. On that welcome page I want Joe to be able to click on a link which will take him to a page. This page will show items that only Joe is allowed to see. If another member clicks on the link it shows a message that says something like Sorry, not for your eyes.
I really am only learning and have spent the past couple of days looking for help with this issue.
I am thinking, from what I have read, that I may just be able to amend the top of my "special page" with an if statement which would say something like
if user_id from table_members = user_id from table_items
Show all rows from table_items which = user_id
There are huge number of tutorials available on internet on displaying dynamic data. Just try doing a search on "making dynamic webpages using php" like that.
let $user_id be a variable storing userid taken from user logged into the website.
$query="select * from table_items where user_id=".$user_id ;
$result=mysql_query($result);
To fetch a row:
`$row=mysql_fetch_array($result);`
To fetch specific column from that row:
$column_data=$row[$columnNameString];
Execute that query you will only get rows whose user_id column value equals to user_id variable value.
Ok, I find myself doing this often. I'll attach ids to my tables and I'll "link" the tables together. Just a simple example, a team roster might have categories. Each category has an id. When I add players into the roster, I'll assign them a category id as well to signify that they are part of that category.
My question is, for this example, say I'm creating a category and a player at the same. I submit the category but now I have to get the category id to assign it to the player table row. Any suggestions on how to do this efficiently? Currently I would query the database again, and search for specific data related to the category, which doesn't seem very effective.
Note also, the id's Im using are generated by mysql.
Any suggestions would be helpful!
Check out mysql_insert_id()
I'm wokring on a simple social network site and I would like to build a simple news update feed. Feed not in the actual sense but you know like those little reports you get on facebook eg when someone posts a picture you get a simple report saying in your main page that - so and so added a picture, or so and so added a comment. Stuff like that one liners.
However I want to build something similar. I was thinking of running a union based query on all my tables but that is INSANELY impractical. Another idea I had was to create a news feed table which would have fields like:
Who - Action - ON WHAT
Where 'WHo' - refers to the user ID of the individual who did something
Action refers to the action ie.. adding a comment
WHAT refers to like if the action was done ON something like a comment passed on an article.
However I'm not so sure if this is a good idea... I want a simple solution - any ideas would be much appreciated.
I think this sort of depends on what sort of actions you are expecting to be performed on your items. I'm no expert, but I think the approach I would take is to keep each action distinct.
Let's assume that you have news items to display in your feed, and users can vote on them (or even just 'Like' them a la Facebook) or add a comment about the item.
I'd likely set up my database as such:
NewsItems
---------
NewsId
UserId (if this is like Facebook where it's someone posting their item)
Body
Timestamp
Votes
-----
VoteId
NewsId
UserId
VoteType (or possibly VoteValue with values +1 and -1 or something)
Timestamp
Comments
--------
CommentId
NewsId
Body
Timestamp
Using this, you can retrieve the last n items that a user posted from the NewsItems table, and as you display each, you can use it's NewsId to determine it's current vote count from the Votes table, and also use NewsId to retrieve a chronological listing of all comments made on the item.
I suppose you could also replace the Body field in NewsItems with two other fields, like NewsType and TypeId. The former tells you which table to use to lookup an action (since you probably don't want picture BLOBs and status update text in the same field/table. The second gives you the key to lookup in that table.
Just my two cents. Hope it helps.
thats a tricky and not so easy thing to do.
I worked for a start up social network and it was something they wanted as well.
I dont think i still have the code laying around but if i recall correctly i went about it something like this
DB:
USERS
id : guid "a unique identifier for this user"
"other user info"
ACTIONS
when : unix_timestamp
who : guid "the user who made the actions guid"
type : set('image','news') "replace with a list of the type of things you want to track"
what : url "not like a web address but the guid of the thing that was made"
FRIENDS
id1 : guid "one of the 'friends' guid's
id2 : guid "the other persons guid
PHOTOS
id : guid "a unique identifier for the image"
url : varchar(255) "where is the image stored (file name directory etc)
who : guid "the user who posted the photos guid"
"other info you want to keep track of"
NEWS (think status update)
id : guid "a unique identifier for this statu update"
who : guid "the guid of the person who posted this"
when : unix_timestamp "timestamp of when it was posted
what : text "the contents"
using the above structure i would have my code make an entry into the ACTIONS table anytime a user posted a photo or a status update. then when their friend logged on it would go through the ACTIONS table pulling out all the actions of anyone it found was friends (via the FRIENDS table)
the TYPE field is used to differentiate what table to use when linking the IDs of the actions. so if the person posted an image when it writes the action to the screen it can set the link up to point to whatever script your using to display images. etc etc
ill see if i can find the code, if i can ill post it (company went under and i retain ownership of the code)
If my explanation isnt clear ill take some more time later to better document the process and code.