Page 1 of 1
There's *got* to be an easier way to do this.
Posted: Mon Feb 05, 2007 8:10 pm
by Mightywayne
As some of you may know, I'm making a PHP game. I've got one way to design the news system, but I think it's just terrible on both me and the server.
What happens is, let's say the user wins a boss fight or something, I'd then send to the news the fact they beat him, and what it now means. They'd then go to their home page, where I'd include() the news.php file.
But what I'm having a problem with, is figuring out the best method for going about that. I want a max of 10 news items to be there, and all old news would just be thrown into oblivion. I'm thinking I'd be like, after the battle or w/e,
Code: Select all
mysql_query("UPDATE news1 WHERE username blah blah");
And I'd have 10 entries, being news1, news2, and so on. But would I seriously have to (to make the news the freshest) set what's currently in the news3 entry, to then be transferred to the news4 entry, and so on until it got to news9, then put that in 10, and then toss whatever was in 10? That seems like way too many queries, and this is sort of a big thing, to be making so many queries like that. I just think there's GOT to be a better way to manage this.
What do you think?
Update Multiple Rows/Tables Values
Posted: Mon Feb 05, 2007 8:19 pm
by tecktalkcm0391
If they are in the same table...
Code: Select all
mysql_query("UPDATE table SET `news1` = '$news1, `news2` = '$news2' WHERE username = 'name' ");
If they are in different tables...
Code: Select all
mysql_query("UPDATE news1, news2... SET news1.info = 'whatever', news2.info = 'whatever' WHERE username = 'name' ");
Does that help?
Posted: Mon Feb 05, 2007 8:53 pm
by Mightywayne
Tch. It turns from a theory and design question into a coding question.
Thanks, wasn't aware it could be done like that. (and btw, it's the same tables)
Edit: Well, could still be theory... anyone got any better ideas?
Posted: Tue Feb 06, 2007 3:10 am
by Maugrim_The_Reaper
Just to verify - you only want to display 10 news items (including the most recent one) and all others are not needed (i.e. can be deleted).
In that case you could:
Code: Select all
$news = 'some news';
/*
* Outside scope of your question but security is essential not an optional extra
*/
$clean = array();
$clean['news'] = mysql_real_escape_string($news);
/*
* Ignoring error capture
*/
mysql_query('INSERT INTO table_news (text, time) VALUES ("' . $clean['news'] . '",' . time() . ')');
/*
* Remove all old entries.
*
* We're cheating by assuming the LIMIT 10 is used from the beginning of
* the database use since setting an infinite offset is impossible but a large
* number (like 10000) should do the trick (and likely is good practice).
* We need a time field to ORDER BY [DESC] (otherwise we can't figure out which
* news entries are most recent (i.e. have higher UNIX timestamps).
* The ORDER BY [DESC] places larger timestamps (recent news) at the top and our
* LIMIT clause tells MySQL to only delete from row 10 in our ordered list
* (like arrays, 0 is the first index - so rows 0-9 are not deleted).
*/
mysql_query("DELETE FROM table_news ORDER BY time DESC LIMIT 10,10000");
/*
* Now just select all rows (we only have ten left) and display
*/
Wrote this quickly - check if it works on a test install of your game before adding it!
P.S. If your game has a website you might PM me a link. I'm also a game programmer in PHP

.
Posted: Tue Feb 06, 2007 5:41 am
by Mightywayne
Woooah. Cool. Alright, I'll check that out too.
I'll PM you now, also.
Posted: Tue Feb 06, 2007 10:17 am
by pickle
Moved to PHP-Code.
Posted: Tue Feb 06, 2007 10:44 am
by onion2k
For the record, if I was playing your game I'd want to be able to look at old news. It'd be a bit odd seeing a record of my achievements that's cut off after 10 items.
Posted: Tue Feb 06, 2007 10:57 am
by Ollie Saunders
Have a table with fields
Use pagination to display 10 records at a time (LIMIT clause). Insert new news the conventional way.
Don't delete old news it is extra effort for you to do and as you have seen might irritate some people. Keeping the old stuff is no extra strain on the server as long as you paginate with a LIMIT clause.
Posted: Tue Feb 06, 2007 6:27 pm
by Mightywayne
Hmm, now that I look at pagination, it does seem officially the better choice. However, now, I remember a game I used to play when I was a wee lad (2 years ago) where they said clearing everyone's PMs and news was going to help the server, and in fact it did. I'm assuming now that they perhaps had another way of dealing with the news that this.
Thanks folks. And uhm. The thing with my game is, it's not a big section, the news. But if it is indeed so easy, I figure I might as well LIMIT. Maybe I'll consider using the news more often... hmm.