Question about code.

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
PureGrain
Forum Newbie
Posts: 8
Joined: Tue Jan 06, 2004 11:03 pm
Location: Louisville KY

Question about code.

Post by PureGrain »

Can someone tell me why the date is coming out wrong in this code?

It was pretty much cut and paisted from the tutorial, but when it posts the news, ithe date comes out as December 31 1969 19:00. :?

Code: Select all

<?
//Included the header above.
$db=mysql_connect("localhost","user","pass") or die ("cant connect");
mysql_select_db("news",$db) or die ("cant change");
$news=mysql_query("select * from news ORDER BY date DESC LIMIT 5") or die ("cant get em");
while($rows=mysql_fetch_array($news))&#123; 
$date=date("F d, Y H:i ",$rows&#1111;"date"]);
$author=$rows&#1111;"author"];
$title=$rows&#1111;"title"];
$email=$rows&#1111;"email"];
$content=$rows&#1111;"content"];
$number="100%";
printf("<table cellpadding=2 cellspacing=0 border=0 width=%s>",$number);
echo "<td bgcolor=#333333 class='first'><b>$title</b></td>";
echo "<td align=right bgcolor=#333333 class='first'>$date</td><tr>";
echo "<td colspan=2><i>by <A href="mailto:$email">$author</a></i><br>
$content</td></table><p>";
&#125;
?>
Also, I am no php god or anything, but when the news is published to the page I made for it. I cannot get page breaks to work in it so the text runs off the page all the way to the right. Any help could be great.
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

What does $rows["date"] look like? Can you output it and paste an example one here? It's probably not formatted correctly.

You can make line breaks work by using the function nl2br().

For example, nl2br($content) rather than just $content.
PureGrain
Forum Newbie
Posts: 8
Joined: Tue Jan 06, 2004 11:03 pm
Location: Louisville KY

OK, Hope I understand your needs.

Post by PureGrain »

When I output it, it looks like this.

http://www.completronics.com/news.php

I will wait for your response before I touch any code again, just so I know what you mean about the nl2br() (which I totaly forgot about being able to use.) I knew nl2br would let me line break in the code, but what I am askng is about the output.

Thanks very much for your time. :P

Please keep in mind that this site is not done. I had been using phpnuke but I wanted to learn php and do it all on my own. So what you see is what I learned withen the last two weeks. :D (php that is anyways)

**edit**
I since went ahead and tried the nl2br(), that was not the issue. The <br> tag worked withen the code, it is the output I am refering too. You will see by my testing the news section first post. The one with all the mmm's.
Also, could you tell me if the script at the bottom of tha page works. Does it tell you what your IP is? I am on a LAN and the router IP is what is showing up for me. I used to have it working differant which showed me my LAN IP, but changed the router and since then it is like this.
PureGrain
Forum Newbie
Posts: 8
Joined: Tue Jan 06, 2004 11:03 pm
Location: Louisville KY

Still having trouble.. :(

Post by PureGrain »

If anyone could help, I would be greatful...
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

He means;

Code: Select all

// change
    while($rows=mysql_fetch_array($news)) {
        $date=date("F d, Y H:i ",$rows["date"]);

// to
    while($rows=mysql_fetch_array($news)) {
        echo $rows['date']; // this line
        $date=date("F d, Y H:i ",$rows["date"]);
And let us know what it generates (or what the date-field in the database is of type. The date() function requires a integer timestamp, and I/we suspect that you are feeding it with "YYYY-MM-DD" date.

You can bypass that by selecting;

Code: Select all

$news=mysql_query("select unix_timestamp(date), author, title, email, content from news ORDER BY date DESC LIMIT 5") or die ("cant get em");
...instead (perhaps needs rewriting abit).

Tip:
Use [php_man]wordwrap[/php_man]() to force a line to break at a certain width, or a html/css equilant.
PureGrain
Forum Newbie
Posts: 8
Joined: Tue Jan 06, 2004 11:03 pm
Location: Louisville KY

OK, thanks for the help.

Post by PureGrain »

I tried both methods you posted above.

Code: Select all

<?php
// change 
    while($rows=mysql_fetch_array($news)) { 
        $date=date("F d, Y H:i ",$rows["date"]); 

// to 
    while($rows=mysql_fetch_array($news)) { 
        echo $rows['date']; // this line 
        $date=date("F d, Y H:i ",$rows["date"]);
?>
Did not do anything diff. I do have the date-filed set to INT

However, I tried the second example:

Code: Select all

<?php
$news=mysql_query("select unix_timestamp(date), author, title, email, content from news ORDER BY date DESC LIMIT 5") or die ("cant get em"); 

?>
And it did give me the date I was looking for along with the time.
The only thing is, it changed the date of all my posts to the same date. 8O
I will work on this some more, thanks again for all your help.

**edited**
Thanks very much for the wordwrap() idea, it worked after I inputed it like this:

Code: Select all

<?php
$wordwrap= wordwrap($content, 40, "<br/>",1);
echo "$wordwrap";

?>
PureGrain
Forum Newbie
Posts: 8
Joined: Tue Jan 06, 2004 11:03 pm
Location: Louisville KY

OK, let me give you everything I got.

Post by PureGrain »

Here is the php code.

Code: Select all

<?php
<? 
include 'header.php';?>
<?
//Included the header above.
$db=mysql_connect("localhost","user","pass") or die ("cant connect");
mysql_select_db("news",$db) or die ("cant change");
$news=mysql_query("select * from news ORDER BY date DESC LIMIT 5") or die ("cant get em"); 
while($rows=mysql_fetch_array($news)){ 
$date=date("F d, Y H:i", $rows [$date]);
echo $rows["date"];
$author=$rows["author"];
$title=$rows["title"];
$email=$rows["email"];
$content=$rows["content"];
$number="60%";
$wordwrap= wordwrap($content, 70, "<br/>",1);
printf("<table cellpadding=2 cellspacing=0 border=0 width=%s>",$number);
echo "<td bgcolor=#333333 class='first'><b>$title</b></td>";
echo "<td align=right bgcolor=#333333 class='first'>$date</td><tr>";
echo "<td colspan=2><i>by <A href="mailto:$email">$author</a></i><br>
$wordwrap\n</td></table><p>";
}

?>
<LINK REL=stylesheet HREF="css/layout.css" TYPE="text/css">
 
<?
//included the footer below.
include 'footer.php'; 
?>
?>
here is the form I am using to get this to work.

Code: Select all

<?php
<?php require_once('Connections/news.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO news (author, content, title, email) VALUES (%s, %s, %s, %s)",
                       GetSQLValueString($_POST['author'], "text"),
                       GetSQLValueString($_POST['content'], "text"),
                       GetSQLValueString($_POST['title'], "text"),
                       GetSQLValueString($_POST['email'], "text"));

  mysql_select_db($database_news, $news);
  $Result1 = mysql_query($insertSQL, $news) or die(mysql_error());

  $insertGoTo = "news.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Completronics.com News Entry Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?
include("header.php");?>
<LINK REL=stylesheet HREF="css/layout.css" TYPE="text/css">

<p align="center"><b>Completronics.com News Entry Form</b>

<form action="<?php echo $editFormAction; ?>" name="form1" method="POST">
Title:<input name="title" type="text"><br>
Author Name:<input name="author" type="text"><br>
Date:<input name="date" type="text"><br>
E-mail<input name="email" type="text"><br>
Content of your news:<br><textarea name="content" cols="40" rows="10"></textarea><br>
<input name="submit" type="submit" value="Submit News">
<input type="hidden" name="MM_insert" value="form1">
</form></p>

<?
include("footer.php");
?>


?>
HELP!!! :(
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post by fastfingertips »

As i know, but i'm not sure the Unix time, initial one is "January 1 1970" so as you see you are missing a day, perhaps your Unix is a day younger :D.

Also perhaps this will help you:
MySQL Server uses Unix time functions and has no problems with dates until 2069; all 2-digit years are regarded to be in the range 1970 to 2069, which means that if you store 01 in a year column. MySQL Server treats it as 2001.
Try to see whats happening when you are using 98 (i haven't tried).

You should validate the day before you will insert it into the database.
Anyway would be great if you tell us what date are you expecting to be displayed because we will be able to checks some time stamps.
PureGrain
Forum Newbie
Posts: 8
Joined: Tue Jan 06, 2004 11:03 pm
Location: Louisville KY

OK, if I get you right.

Post by PureGrain »

I want the date and time of the current post to be shown.

ex. If I post some news today Jan 10th 2004 at 9:00 am I want the post to show that is the time and date I posted it.

Then, If I post some news 5 days later, I want it to show the news and the date and time I posted it then.

I hope I am not confusing you.


*EDIT*

Would this being a LEAP year have anything to do with the fact my server posted one day behind? If so, where in the congif file can I fix this?? Anyone know??
PureGrain
Forum Newbie
Posts: 8
Joined: Tue Jan 06, 2004 11:03 pm
Location: Louisville KY

I have searched for a while now.

Post by PureGrain »

Still no luck in figuring this out. :(
PureGrain
Forum Newbie
Posts: 8
Joined: Tue Jan 06, 2004 11:03 pm
Location: Louisville KY

Re-coded

Post by PureGrain »

After several days of trying to get that code to work for me, I decided to recode the whole thing in a differant manner and it works fine now. Thanks for all who helped.. :D
Post Reply