A Few Quick Questions

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
JBrown1045
Forum Newbie
Posts: 10
Joined: Sun Jun 19, 2005 1:19 pm

A Few Quick Questions

Post by JBrown1045 »

hello:

I am very new to PHP. I know some coding, but a few I am not aware of.

On the page danaplatoforever.com/stories.php what code can I use to get the stories to be posted with the newest coming up first?

Secondly on danaplatoforever.com/mmesages.php it keeps showing the date 12/31/1969. WHat am I doing wrong?

Thanks in advance.

Jonathan
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

You have an invalid timestamp.
Take a look over at functions like date(), time(), strtotime()
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Post by andylyon87 »

the message being newest first:

you need a larger description on how the thing runs at the moment, are each of the stories in individual txt files or are they in the same txt file. If they are in the same file you can explode() the file after reverse_array() the order.

If that is no help you may need to give a more in depth description,
JBrown1045
Forum Newbie
Posts: 10
Joined: Sun Jun 19, 2005 1:19 pm

Post by JBrown1045 »

andylyon87 wrote:the message being newest first:

you need a larger description on how the thing runs at the moment, are each of the stories in individual txt files or are they in the same txt file. If they are in the same file you can explode() the file after reverse_array() the order.

If that is no help you may need to give a more in depth description,
I don't have them in any txt fils. I I have them in a database and have them choed to the site from database.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

there are two solution, one PHP one, one in SQL.

PHP has been described above, in SQL you simply change the statement using "ORDER BY".

Assuming you have timestamp field in your db-table (in the statement below called "date")

Code: Select all

SELECT id, author, title, date... FROM ... WHERE ... ORDER BY date ASC LIMIT 0,10
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

probably want to have a look at functions like DATE_FORMAT for MySQL... Instead of performing a php call to date for every row...
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

timvw wrote:probably want to have a look at functions like DATE_FORMAT for MySQL... Instead of performing a php call to date for every row...
Probably. Let's see :)

Code: Select all

$query = mysql_query(&quote;SELECT name,message,time FROM tblmessages WHERE id>0 ORDER BY time ASC LIMIT 0,10&quote;,$link);
You most probably don't need "WHERE id>0" as that will probably be an autoincrementing field and won't have a negative value.

This

Code: Select all

$query = mysql_query(&quote;SELECT name,message,time FROM tblmessages ORDER BY time ASC LIMIT 0,10&quote;,$link);
should do as well.
JBrown1045
Forum Newbie
Posts: 10
Joined: Sun Jun 19, 2005 1:19 pm

Post by JBrown1045 »

patrikG wrote:
timvw wrote:probably want to have a look at functions like DATE_FORMAT for MySQL... Instead of performing a php call to date for every row...
Probably. Let's see :)

Code: Select all

$query = mysql_query(&quote;SELECT name,message,time FROM tblmessages WHERE id>0 ORDER BY time ASC LIMIT 0,10&quote;,$link);
You most probably don't need "WHERE id>0" as that will probably be an autoincrementing field and won't have a negative value.

This

Code: Select all

$query = mysql_query(&quote;SELECT name,message,time FROM tblmessages ORDER BY time ASC LIMIT 0,10&quote;,$link);
should do as well.
Neither worked. They kept the form together with no error but still posted them in oldest first.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

does table have timestamp column (datetime)? If not, it won't work. See my first post.
JBrown1045
Forum Newbie
Posts: 10
Joined: Sun Jun 19, 2005 1:19 pm

Post by JBrown1045 »

patrikG wrote:does table have timestamp column (datetime)? If not, it won't work. See my first post.
Table is set up like this:

Code: Select all

CREATE TABLE `tblstory` (
`id` int(11) NOT NULL auto_increment,
`name` text NOT NULL,
`title` text NOT NULL,
`story` text NOT NULL,
`time` int(11) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

What do your time fields look like? Are they being inputted correctly?
JBrown1045
Forum Newbie
Posts: 10
Joined: Sun Jun 19, 2005 1:19 pm

Post by JBrown1045 »

Jcart wrote:What do your time fields look like? Are they being inputted correctly?
my time fields? I only have one. It just is suppose to show Month/Day/Year The form works it just keeps echoing wrong. This may help you out:

My table:

Code: Select all

CREATE TABLE `tblstory` (
`id` int(11) NOT NULL auto_increment,
`name` text NOT NULL,
`title` text NOT NULL,
`story` text NOT NULL,
`time` int(11) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
mmessages.php code I used(Site page)

Code: Select all

<form method="post" action="insert2.php" name="this">

<table width="100%" cellpadding="2" cellspacing="1">
 <tr><td align="right"><font size="2" color="#0076ec">Name:</font></td><td><input type="text" name="name" maxlength="18" value=""></td></tr>
 <tr><td align="right" valign="top"><font size="2" color="#0076ec">Message:</font></td><td><textarea name="message" class="entry" cols="25" rows="6"></textarea></td></tr>
 <tr><td align="center" colspan="2"><input type="submit" name="sendstory" value="Send Message"></td></tr>
</table>

</form>




<BR><FONT size=5><FONT color=#0076ec><B>Messages For Dana</B></FONT><br><img src=images/break.gif><br> </center> 





<font size="2">
<?php
$username="rlzcjewc_danaweb";
$password="dana5899";
$database="rlzcjewc_danamessages";

$link = mysql_connect(localhost,$username,$password);
mysql_select_db($database) or die("Unable to select database");

$query = mysql_query("SELECT name,message,time FROM tblmessages WHERE id>0",$link);
while($row = mysql_fetch_row($query)) {
$dateis = date("m/d/Y", $row[3]);

echo "<b>".$row[1]."</b>";
echo "<br><br>".$row[0].", ".$dateis;
echo "<br><br><img src=images/break.gif><br><br>";

}//end while.

?>
Insert2.php

Code: Select all

<?
$username="rlzcjewc_danaweb";
$password="dana5899";
$database="rlzcjewc_danamessages";

mysql_connect(localhost,$username,$password);
mysql_select_db($database) or die("Unable to select database");



if($name==""){
  echo "you didn't enter a name!";
  exit();}
elseif($message==""){
  echo "you didn't enter a message!";
  exit();}
else{
$timePosted = time();
  $query = "insert into tblmessages (name,message,time) values ('$name','$message','$timePosted')";
  echo "message added! you are being redirected";
}

$result = mysql_query($query) or die(mysql_error());

mysql_close();
?>

<meta http-equiv="refresh" content="1;url=mmessages.php">
JCART | Please use

Code: Select all

tags when posting php code. Review [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

on mmessage.php change

Code: Select all

$query = mysql_query("SELECT name,message,time FROM tblmessages WHERE id>0",$link);

while($row = mysql_fetch_row($query)) {
   $dateis = date("m/d/Y", $row[3]);
}
to

Code: Select all

$query = mysql_query("SELECT name,message,time FROM tblmessages WHERE id>0",$link);

while($row = mysql_fetch_array($query)) {
   $dateis = date("m/d/Y", $row[2]);
}
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

you should probably change your `time` field to a datetime type in mysql.

then you can easily sort it by those values.
JBrown1045
Forum Newbie
Posts: 10
Joined: Sun Jun 19, 2005 1:19 pm

Post by JBrown1045 »

Jcart wrote:on mmessage.php change

Code: Select all

$query = mysql_query(&quote;SELECT name,message,time FROM tblmessages WHERE id&gt;0&quote;,$link);

while($row = mysql_fetch_row($query)) {
   $dateis = date(&quote;m/d/Y&quote;, $row&#1111;3]);
}
to

Code: Select all

$query = mysql_query("SELECT name,message,time FROM tblmessages WHERE id>0",$link);

while($row = mysql_fetch_array($query)) {
   $dateis = date("m/d/Y", $row[2]);
}
good call it worked perfectly. Any chance you know what to do to get teh database to echo the newest post first?
Post Reply