View Full Version : Help with my PHP forum


liam1412
12-12-2006, 14:04
Whats the best way to go about changing a post status images based on several peices of info!

I have a box for an image that I want to be different based on the current status of a post

For example

I will have these images

Standard Envelope
New Posts (Since last visit)
Hot Topic
Hot topic with new posts (since last visit)
Thread you have posted in
locked topic

and so on........

Can anyone tell me what is the bast way to go about this. I presume it would be some sort of switch stetment. Any hints for a good database setup for this. And also databse setup for subscribed threads etc.

I currently have

forum_topic
with the fields topic_id, topic, topic_text, topic_username, topic_start_date_time, topic_view, topic_reply

forum_answer
with the fields ans_topic_id, ans_id, ans_text, ans_username, ans_datetime

Any help with this woud be as always greatly appreciated.

Thanks

Not asking for the code to be written just a pointer in the right direction

JoeP
12-12-2006, 14:10
Are you writing the Forum from the bottom up, rather than using an existing piece of Forum sofwtare like vBulletin or phpBB?

If you are, I'd SERIOUSLY rethink that path - there's a lot of existing expertise invested in open source packages that you might be able to use on your site.

liam1412
12-12-2006, 14:23
Na I decided I wanted to do it all the way from the start. I like to throw myself in at the deep end and to be honest I have learnt so much from doing it. I know there are some good software packages out there but it owuldn't be half as much of a challenge doing it that way. besides I want to do this for a living so I want to know all the ins and outs of wrinting my own software.

JoeP
12-12-2006, 14:31
Good Luck!

I do this sort of thing for a living and I'm lazy - if there's anything around that will do the job I tend to use it! :)

liam1412
12-12-2006, 14:43
Thanks. I think Im gonna need it. lol. So any clue on what im wanting to do then

JoeP
12-12-2006, 14:53
Well, the first thing you need to do is to consider adding some sort of status fields to thread and post tables - basically to say whether the thread or post is open, locked, 'soft deleted', etc.

Don't store the username as a user name. Store a reference to it. Will take less space and allow you to cope with people who want to change their names.

Subscribed threads - store the user id and a thread id, at teh very minimum.

liam1412
12-12-2006, 15:02
Mmmm this maybe more of a mission than I thought............I think i may just go crazy. WHile we are on about programming tho. How do you make those fantastic buttons that input the code for the smiley at the point were you are typing or the code and quote tags that wrap around highlighted text. Javascript I presume but I HAT JAVASCRIPT :rant: :rant:

JoeP
12-12-2006, 15:05
Hang on, let me start the charging clock. ;)

Seriously - if you're new to PHP and Javascript, I'd really consider taking an existing package and learn from changing it.

To get these things right is 'non-trivial'.

liam1412
12-12-2006, 15:16
IThe only trouble is they are all written in object orientated code. Which obviously is far superior. But haven't even looked at that yet as I think that woul dbe a bit oout of my depth. I downloaded one called mercury board and did not have clue what anything was doing so I didn't know what to change.:huh: Do you know what. I think I may just stick to been anadmin assistant. Im sure the boredom is only as likely to make me go cuckoo as trying to learn all this.

JoeP
12-12-2006, 15:29
IThe only trouble is they are all written in object orientated code. Which obviously is far superior. But haven't even looked at that yet as I think that woul dbe a bit oout of my depth. I downloaded one called mercury board and did not have clue what anything was doing so I didn't know what to change.:huh: Do you know what. I think I may just stick to been anadmin assistant. Im sure the boredom is only as likely to make me go cuckoo as trying to learn all this.

It does take a bit of time to get on top of coding and software development - that's why we can charge the rates we do. ;)

But seriously - if you genuinely want to do this for a living, then unfortunately 'grunt' work is the only way to do it. It's a hard job at teh beggining, but it gets easier - sreiously!

liam1412
12-12-2006, 15:42
Aaaaa the good old rates of pay. Always gives me a reason to keep battling through. :hihi: My fingers will be bleeding tonight now you have reminded me of that:D

Ghozer
12-12-2006, 15:46
i say, the best method before you start doing ANYTHING that works with a database, sit down, and think what you need on the site, such as Posts, User Ids's, post id's, reply id's etc...

then Type up a database structure..

I use the format of..

Table_Name ->
id (int 10) (pri key)
name (varchar 25)
Table_2 ->
id (int 10) (pri key)
n_id (int 10) (links back to "ID" in 'Table_Name')
info (Varchar 50)

doing this then allows you to build the databse with everything you need, and also makes it easy to add things on later as you code..

most forum or forum type sites use many tables for 1 post..

Example:

in the "POSTS" table, you may have row fields such as.. post_id, post_name, post_date, post_title, poster

in the "USERS" table you would then have user_id, user_name, password etc..

now this is the complicated bit, when reading the POSTS table to pull out the information on the post, it picks up the "poster" field, and compares it to USERS/user_id, to find who posted it, all these id's are numerical (integers) instead of plain text so as JoeP said, its good if they change thier name etc.. it makes it more dynamic..

then of cause, you move on to a REPLIES table, which has reply_id, post_id, date, content, poster

OBVIOUSLY post_id links back to POSTS/post_id to see which post the reply is from, then, again, also, it has the poster, which links back to USERS/user_id to pull the name and info on who posted the reply..


a good database structure means everything when building php websites :)

as for your problem, I'd have a field on the appropreate table, thats states, 0, 1, 2, 3, 4 etc..
0 being normal
1 being locked
2 being stickied
3 being moved

blah blah blah... and then use a switch statement to read that variable which is pulled from the database to determine which image is displayed... (and anything else depending on the status of the post/topic)

liam1412
12-12-2006, 15:55
A true gentleman.

Thanks Ghozer.

fnkysknky
12-12-2006, 16:41
Personally I'd get an open source PHP/MySQL forum solution and then play around with it first.

I wrote a program from scratch for my A-Level computing that used a relational database containing 7 (I think) tables that all linked to save the data. It also ran various queries for producing reports and the like. I finished it and got top marks for it but it was one hell of a headache. Would have been much easier to use an existing database engine :)

Nazo
12-12-2006, 16:55
If you really want to go the self-made forum route I'd recommend starting very simple in the beginning. Start with the basics, ie, users, topics & replies then once you've got that working start adding in more advanced features. If you try to do it all at once you'll most likely get nowhere fast.

maggi
12-12-2006, 18:19
WHile we are on about programming tho. How do you make those fantastic buttons that input the code for the smiley at the point were you are typing or the code and quote tags that wrap around highlighted text. Javascript I presume but I HAT JAVASCRIPT :rant: :rant:

You don't need to know/like Javascript to use TinyMCE (http://tinymce.moxiecode.com/index.php) which is an open source WYSIWYG editor control.

Ghozer
12-12-2006, 18:30
Mmmm this maybe more of a mission than I thought............I think i may just go crazy. WHile we are on about programming tho. How do you make those fantastic buttons that input the code for the smiley at the point were you are typing or the code and quote tags that wrap around highlighted text. Javascript I presume but I HAT JAVASCRIPT :rant: :rant:


they are often done the same way as bbcode, it would pick up something like ::smile:: from within the message, and use an str_replace to change it to the url of the associated smilie..

Something a little like....


function smilies($text)
{
$smiles = array(
"::smile::",
"::wink::",
"::teeth::"
);
$replace = array(
"<img src=\"smile.png\">",
"<img src=\"wink.png\">",
"<img src=\"teeth.png\">"
):
$newtext = str_replace($smiles, $replace, $text); // First Pass
$newtext = smilies($newtext); //second pass (Just incase)
return $newtext;
}


You would then use <?php echo smiles($text_variable); ?> to output the result..

Sora
13-12-2006, 00:15
I am impressed you are taking up this challange - altho starting with a forum is quite a task

I started learning PHP by just getting stuck in, and i feel it's always the best way to do it. I started with a guestbook, a traffic counter/site stat reporter, then moved onto making changes (mods) to open source projects - I would not class myself as an expert tho LOL

If you are just looking for a top quality forum that's free, PHPBB is pretty good (for the price LOL)

I personally love to dive into code, to see how it works if you're more intrested in understanding how a forum works, and maybe other things about PHP, I would actually recomend you lookup something called e107 - it's a portal/Content Management System (ie it's a website in a box) it contains lots of plugins that are open source (one being a forum) - so if you want to take a look at how they've done it, change it, or totally re-write it - then you can
The source is pretty easy to understand, and even commented where required :D

(Ghozer is it ok to create that call within the function like that? that second pass looks suspiciously like an infinite loop to me)

liam1412
13-12-2006, 08:50
Ghozer -

they are often done the same way as bbcode, it would pick up something like :: from within the message, and use an str_replace to change it to the url of the associated smilie..

Something a little like....


Code:
function smilies($text)
{
$smiles = array(
"::smile::",
"::wink::",
"::teeth::"
);
$replace = array(
"<img src=\"smile.png\">",
"<img src=\"wink.png\">",
"<img src=\"teeth.png\">"
):
$newtext = str_replace($smiles, $replace, $text); // First Pass
$newtext = smilies($newtext); //second pass (Just incase)
return $newtext;
}
You would then use <?php echo smiles($text_variable); ?> to output the result..


That is a far superior way to how i've been doing it. Can you advise if sora was right about the infonite loop. D I even need the second pass as surely if it doesn't pick it up the first time it won't the second. Thanks

The thing I was actually looking for now I have been unintentionally educated is how you put the smilies at say the top or bottom of the reply pages and the user can just click them rather than having to manually type the code. I think I have a function soewhere in my old code but that only adds the smilie code at the end of the text on pressing submit(bit of a ponitless function really:hihi: ). Not in wherever the cursor or Around highlighted text

I personally love to dive into code,

I never find another way more useful. I read the first few chapters of a book and my brain hurt. So then I started doing the excercizes in the book and my headache just got worse. I find that the only way you can actually learn something programming wise is get a couple of walk through tutorials. I found a few on http://www.phpfreaks.com and only then was when it started to sink in.

Ghozer
13-12-2006, 09:21
nope, im not sure exactly why (perhaps i should read up a bit more on functions and how they work) but i have used this method 100's of times, and it never gets stuck in an infinate loop...

from what i was told by my friend (the one who tought me about second passing things) is that if a funciton calls its self, that is remembered, and its executed the second time, but on the second pass, it doesnt execute its self again... you can add an echo just before the second pass if you wish to try this...

however if your un sure, remove the second pass line and it will still work, i have it in there as a 'just incase' scenario.. but its not REALLY needed..

any ways, what your wanting is an onclick funciton set up for the images...

set the images as links (a href) or as buttons (using css to make them images) and use the following as the href


"document.selection.createRange().text='::smilie::'"

Example

<a href="document.selection.createRange().text='::smilie::'"><img src="smilie1.png"></a>



this would then mean, when you click that button, it will execute the javascript link.. this is un tested, but that is the correct function... I will admit, i have never tried it in a href before, i have always used it in form buttons like this one..


<input onclick="document.selection.createRange().text='textofsomes ort'" type="button" value="button"><br>


clicking the button will insert "textofsomesort" into the textbox where the cursor is..

hope this helps ;)

----------------------------------------------------

by the way, im the same, if i wanna learn a coding/scripting language of any sort, its best for me to just jump in and start coding basic things first, a sort of "learn as I go" scenario :)

liam1412
13-12-2006, 09:39
Does anything need to go in the header in between script tags or do I just use script tags above the table where all the smilies will be displayed, and close script tags below it.

Thanks

liam1412
13-12-2006, 10:11
Sorry to bug on this one again but you also mentioned using CSS to make the buttons images. - I only ever use CSS to keep the majority of style tags out the code. How would I go about creating the button images in CSS. In fact do you want to just give me a quote for writing my software :hihi:

fnkysknky
13-12-2006, 11:55
You should use CSS and keep all formatting out of the code if possible. Data and formatting should be kept seperate. (X)HTML is a markup language - it's not supposed to be used to style stuff but it all went a bit pear shaped in the early days of the web i.e you mark something up as a quote in the HTML and then the CSS syles it. It's a much superior way of handling stuff if you think about it. It also means you can have multiple style sheets and can change the complete look of a site just by changing the style sheet. If you start embedding font tags and the like then you end up in a pickle.

liam1412
13-12-2006, 12:34
I do use it in that way. And also use PHP header and footers. Its just the things such as positioning and for example Gozer's advise

set the images as links (a href) or as buttons (using css to make them images)

I would be okay setting the link image src but im sure the button way is less messy.

Ghozer
13-12-2006, 20:17
in answer to your question, you dont need top open or close any script tags, just use them as-is, as an onclick or a href...

as for css...

use class="button" in the button paramaters, and set the CSS to remove all styling and formatting from the button, and set a 'background' image... just play about with it, and you'll get it. however direct href image links are probably better..

as for positioning with css, you move into floats, padding, and margins, if you look at my blog site (in my signature) that is total CSS, even on the history (the tabulated data) -- You should really use tables for tabulated information, just i wanted to see if i could do it totally CSS ;)

Nazo
13-12-2006, 20:28
if you look at my blog site (in my signature) that is total CSS, even on the history (the tabulated data) -- You should really use tables for tabulated information, just i wanted to see if i could do it totally CSS ;)
You know your blog brings back a lot of html validation errors if you click the validation link on there?

Ghozer
14-12-2006, 09:03
If you read the errors, its not really html errors, its more of confusion

it is/was valid, but since i added the year in the date list on the right, the "&" symbol in there throws it off because of embedded html entities. such as &nbsp; for space, But its infact a link, with a php _get variable, it gets confused with the &year= -- its trying to interprate it as similar to &nbsp; but it cant.. :)

read the description on the error on the WC3 site and you'll understand it ;)

There is a way to fix it, but i havent had the time yet..

and the image link things, again, i just havent had the time to fix it yet ;)

Nazo
14-12-2006, 09:22
Oh ok, just checking you knew, it's easy to add updates that break things without realizing. I always click on the validator links when I see them out of curiosity. It's surprising how often the pages come back with errors.