General SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesGeneral SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old August 1st, 2003, 04:12 AM
velocityX velocityX is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 72 velocityX User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 36 sec
Reputation Power: 6
Send a message via AIM to velocityX
php list last 3 entries

I made a news script and I made it list last 3 news....but I want to make the script show the last row going up, so it will list the newest entry then the entry above it.

the code for my list news script is

PHP Code:
 $username="*****";
$password="*****";
$database="*****";

mysql_connect(localhost,$username,$password);
@
mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM velocity_news ORDER BY date DESC LIMIT 3";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo 
"<b><center>news</center></b><br><br>";

$i=0;
while (
$i $num) {

$news=mysql_result($result,$i,"news");
$date=mysql_result($result,$i,"date");
$topic=mysql_result($result,$i,"topic");

?>
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" height="67" width="342">
  <tr>
    <td background="newsfor.jpg" height="38" width="129">&nbsp;</td>
    <td background="newsfor2.jpg" height="38" width="213">
    <font size="2" face="Verdana" color="#FFFFFF"><?php echo $date ?></font></td>
  </tr>
  <tr>
    <td bgcolor="3d729c" height="19" width="342" colspan="2">
    <p align="center"><b><font size="2" face="Verdana" color="#FFFFFF"><?php echo $topic ?></font></b></td>
  </tr>
  <tr>
    <td bgcolor="3d729c" height="10" width="342" colspan="2">
    <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="330">
      <tr>
        <td width="13">&nbsp;</td>
        <td width="317"><font face="Verdana" size="2" color="#FFFFFF"><?php echo $news ?>
        </font></td>
      </tr>
    </table>
<? 



I also want to ask, how to make it do a check so it wont enter another entry for the same date to the mysql table?

Reply With Quote
  #2  
Old August 1st, 2003, 07:06 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
I'm not sure I follow what you're wanting to do in the first question. Regarding your question about distinct dates, I don't know of a way to limit your result set without having nested queries -- one to get distinct dates and the other to select (ORDER BY date DESC LIMIT 1) from the database where the date equals each of the distinct dates obtained in the parent query. Others may know of more elegant methods.

Reply With Quote
  #3  
Old August 1st, 2003, 08:50 AM
crazytrain81 crazytrain81 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 232 crazytrain81 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
"I also want to ask, how to make it do a check so it wont enter another entry for the same date to the mysql table?"

I'm not really sure what you mean by this. Do you mean you don't want to return more than one news entry per date when you are displaying them, or do you only want to allow one news entry per date? If the latter is the case, simply add a unique index to your date column. You can do that with this query: ALTER TABLE velocity_news ADD UNIQUE date (date);

If you only want to display 1 entry per date in your display, a temporary table is the best way to go, unless you're on MySQL 4.0 which supports subselects.

CREATE TEMPORARY TABLE dateTemp
SELECT DISTINCT(date) as date FROM velocity_news
ORDER BY date DESC
LIMIT 0, 3;

SELECT v.*
FROM velocity_news as v
LEFT JOIN dateTemp as d ON d.date = v.date
ORDER BY v.date DESC;

DROP TABLE dateTemp;

That should do it.

Reply With Quote
  #4  
Old August 1st, 2003, 03:33 PM
velocityX velocityX is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 72 velocityX User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 36 sec
Reputation Power: 6
Send a message via AIM to velocityX
ahh none of it work I'm using mysql 4.0 and phpmyadmin with it. When i tried to do ALTER TABLE velocity_news ADD UNIQUE date (date) in phpmyadmin I get a blob error in (date). I want to allow only one entry per date and in the script that shows the news i want it to show the newest to the 2 old posts.

like:
8/1/03
7/28/03
7/16/03

Reply With Quote
  #5  
Old August 4th, 2003, 03:58 PM
crazytrain81 crazytrain81 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 232 crazytrain81 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
You can't index a BLOB field, and you most certainly should NOT be using blobs to store non-binary data.

Reply With Quote
  #6  
Old August 5th, 2003, 07:03 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Let your code do the work for you. Whether the field is unique or not, you should probably check and do a custom error if somebody tries to insert a duplicate (lest they get a nasty SQL error message). Select a count where the date equals the submitted date. If greater than zero, return an error; else do the insert. It can't hurt to make your field unique, but you should validate your input in this way anyway for the sake of usability.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesGeneral SQL Development > php list last 3 entries


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT