|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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:
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? |
|
#2
|
||||
|
||||
|
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.
|
|
#3
|
|||
|
|||
|
"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. |
|
#4
|
|||
|
|||
|
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 |
|
#5
|
|||
|
|||
|
You can't index a BLOB field, and you most certainly should NOT be using blobs to store non-binary data.
|
|
#6
|
||||
|
||||
|
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.
|
![]() |
| Viewing: Dev Articles Community Forums > Databases > General SQL Development > php list last 3 entries |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|