It is currently Thu Mar 28, 2024 5:14 am



Reply to topic  [ 12 posts ] 
PHP and Mysql 
Author Message
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post PHP and Mysql
heh...not phpbb, just php...anyway...

ok, so I created a table. There are a number of records, and each has a number of fields including date.

What's the best way to return only the ten most recent entries? Or at least only print out the ten most recent. I'm mildly stumped. :/

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Tue May 04, 2004 5:27 pm
Profile WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post 
Nevermind, I've discovered the wonders of $num_rows.

I've written this and it seems to work. Any suggestions for improvement or whatever would be greatly appreciated.

Code:
$max=10;
$select = "SELECT * FROM clankiller_news ORDER BY 'date' ASC";
mysql_select_db("luciferia",$db);
$result = mysql_query("$select",$db);
$num_rows = mysql_num_rows($result);

$i = $num_rows;

while ($i >= $num_rows - $max){
   $i--;
   printf("<br>key: %s", mysql_result($result,$i,"key"));
   printf("<br>date: %s", mysql_result($result,$i,"date"));
   printf("<br>author: %s", mysql_result($result,$i,"author"));
   printf("<br>news: %s", mysql_result($result,$i,"news_item"));
   printf("<br><br>");
}

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Tue May 04, 2004 8:43 pm
Profile WWW
Reply with quote
Post Clankiller News
Tweeking your code:

SELECT * FROM clankiller_news ORDER BY 'date' ASC LIMIT 10


Tue May 25, 2004 8:39 am
Duke
User avatar

Joined: Mon Mar 31, 2003 8:59 am
Posts: 1358
Location: right behind you
Reply with quote
Post 
Wow, may 5th. Never saw this post. Yeah, limit 10 is a better way to go. It's also part of pagination if you ever decide to get crazy.


Tue May 25, 2004 12:32 pm
Profile YIM WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post 
ah, yes, I've come quite a bit further since then. :P What were you saying about pagination? Anyway, the stuff I'm having issues with now is too long to post. heheheh.

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Tue May 25, 2004 5:32 pm
Profile WWW
Duke
User avatar

Joined: Mon Mar 31, 2003 8:59 am
Posts: 1358
Location: right behind you
Reply with quote
Post 
Yes, I'm sure your shrink has reams about the help you need...

Pagination is having your results split up onto multiple pages, like these forums.


Tue May 25, 2004 7:06 pm
Profile YIM WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post 
ah, I understand. I don't plan on doing that with my timecard thing. I've already gotten the reporting function to work, which can get onbovious if you choose a really huge timeframe. But blah, it's only for 2 users.

I'm reworking the code on that now. :/ Having issues making it function if lunches aren't taken. But I'll sort it. BTW, I have a love/hate relationship with Unix time.

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Wed May 26, 2004 8:36 pm
Profile WWW
Duke
User avatar

Joined: Mon Mar 31, 2003 8:59 am
Posts: 1358
Location: right behind you
Reply with quote
Post 
??? unix timestamps are the shizzle!!!! Blasphemer. Did you read my tut on working with unix timestamps?

http://www.webhead.cc/main.php?inv=59


Wed May 26, 2004 9:53 pm
Profile YIM WWW
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
Heheh, Pig. You can fix that background imaged code. It would look better if it don't move with scrolls. Just my opinion, of course.

{...

background-image:url('whatIknow.hell');background-attachment: fixed;

...}

_________________
++


Thu May 27, 2004 8:31 am
Profile WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post 
hah...I didn't realize you wrote a tut.

I just read through it... I'm afraid that I already figured out most of that. :P Or all of it. btw, why no mention of mktime()? EH?! heh. I use it to figure out the unix timestamp of today (ie, mktime(0,0,0);). I guess time() is close enough. :/

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Thu May 27, 2004 8:48 am
Profile WWW
Duke
User avatar

Joined: Mon Mar 31, 2003 8:59 am
Posts: 1358
Location: right behind you
Reply with quote
Post 
mktime is retarded in comparison to strtotime(). I honestly can't think of a need for mktime.


Thu May 27, 2004 12:32 pm
Profile YIM WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post 
mktime is a quick and easy way of finding out today at midnight. It's so quick. I use strtotime for virtually everything else, though, so you're correct in most instances.

btw, I finally figured out how to do the no lunch thing. I can't believe I beat my head against a wall for several days for 17 lines of code. :/ Which I coulda squished down into like 9 lines if I really wanted to.

Blah, anyway, I'm proud, I finally got the hard part. Now for the rest.

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Thu May 27, 2004 5:02 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 12 posts ] 

Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware.