There's *got* to be an easier way to do this.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

There's *got* to be an easier way to do this.

Post 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?
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Update Multiple Rows/Tables Values

Post 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?
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Post 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?
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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 ;).
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Post by Mightywayne »

Woooah. Cool. Alright, I'll check that out too.

I'll PM you now, also.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Moved to PHP-Code.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Have a table with fields
  • newsId
  • userId
  • newsText
  • time
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.
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Post 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.
Post Reply