Page 1 of 1

A Few Quick Questions

Posted: Sun Jun 19, 2005 1:22 pm
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

Posted: Sun Jun 19, 2005 1:24 pm
by John Cartwright
You have an invalid timestamp.
Take a look over at functions like date(), time(), strtotime()

Posted: Sun Jun 19, 2005 1:31 pm
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,

Posted: Sun Jun 19, 2005 2:08 pm
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.

Posted: Sun Jun 19, 2005 2:27 pm
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

Posted: Sun Jun 19, 2005 2:29 pm
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...

Posted: Sun Jun 19, 2005 4:40 pm
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.

Posted: Sun Jun 19, 2005 4:58 pm
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.

Posted: Sun Jun 19, 2005 5:02 pm
by patrikG
does table have timestamp column (datetime)? If not, it won't work. See my first post.

Posted: Sun Jun 19, 2005 5:21 pm
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 ;

Posted: Sun Jun 19, 2005 6:13 pm
by John Cartwright
What do your time fields look like? Are they being inputted correctly?

Posted: Sun Jun 19, 2005 6:42 pm
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]

Posted: Sun Jun 19, 2005 6:57 pm
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]);
}

Posted: Sun Jun 19, 2005 6:58 pm
by Burrito
you should probably change your `time` field to a datetime type in mysql.

then you can easily sort it by those values.

Posted: Sun Jun 19, 2005 11:09 pm
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?