Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting Characters and Date Question

Status
Not open for further replies.

t5amec

Programmer
Aug 14, 2003
112
GB
Q1. I am developing a News page for my site and on the Home Page i want to just include a short snippet (approx 177 characters including spaces) on the content, then anything after that would be left out and there would be a "more..." link to the actual news item page.

Q2. The date would be auto generated using the NOW() function, but that come up with YYYY-MM-DD, how can i modify it so it comes up with DD-MMM-YYYY(if possible)

Q3. On the Home Page I only want the five latest news items to be on there. How can I do a query that will only show the five latest posts?

My coding for the Home Page is below:
Code:
<?
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database"); 
$query="SELECT * FROM news ORDER BY id ASC";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "";

?>

<!-- BORING HTML GOES HERE --!>

<? $i=0; 
while ($i < $num) { 
$date=mysql_result($result,$i,"date");
$author=mysql_result($result,$i,"author"); 
$headline=mysql_result($result,$i,"headline"); 
$content=mysql_result($result,$i,"content"); 
$id=mysql_result($result,$i,"id"); 
?>
<table border="0" width="100%" cellpadding="0">
<tr>
<td width="80%" colspan="2" align="center">
<a href="news.php?=<? echo "$id"; ?>"><? echo "$headline"; ?></a>
</td>
</tr>
<tr>
<td width="100" height="75" valign="middle">
<p align="center"><a href="contact?=<? echo "$author"; ?>"><img src="images/<? echo "$author"; ?>.jpg" width="60" height="60" alt="Email <? echo "$author"; ?>" 
border="0" style="border: 1px solid rgb(0,0,0)"></a>
</td>
<td width="81%" valign="middle">
<p align="left"><b><? echo "$date"; ?></b><br><? echo "$content"; ?> <a href="news.php?=<? echo "$id"; ?>" class="news">more...</a>
</td>
</tr>
<tr>
<td height="20">
</td>
</tr>
<?
++$i;
} 
echo "</table>";


?>
<!-- THE REST OF THE HTML GOES HNOIRE --!>

For those interested, this is the addy to the home page: ( )

Make Sense? I hope so (-:
 
for the content you can use the link www.php.net/substring]substr function[/url]

for the date

use the date() to format the output

Code:
$date = date("d-m-Y",$rows['datefield']);

Bastien

I wish my computer would do what I want it to do,
instead of what I tell it to do...
 
and for the limitation on posts, try adding "limit 5" to the end of your sql query.
 
I can't work out the date coding... what part of the PHP coding should I edit for changing it?

I changed it on the first:
Code:
$date=mysql_result($result,$i,"date");
but that changed the dates to the correct format, but returned 01-01-1970...

Make Sense? I hope so (-:
 
Hi

the confusion may be because you are not retrieving the database infor in the way that the majority of users do so (using mysql_fetch_assoc or mysql_fetch_array).

in your method change the relevant line to
Code:
$date=date("d m Y",mysql_result($result,$i,"date"));

hth
Justin
 
returning exactly the same result as before (01-01-1970)...

is that a default date or something?

Make Sense? I hope so (-:
 
oops. i had assumed that you were storing a unix timestamp in your database.

assuming you have a normal mysql format date in your db then change the code to be as follows:

Code:
$date=date("d m Y",strtotime(mysql_result($result,$i,"date")));
 
NICE ONE! Cheers dude.

Make Sense? I hope so (-:
 
forgot to answer your question, though.

jan 1 1970 is the beginning of the unix epoch from which timestamps are taken. eg taking a timestamp now you would receive the number of seconds since midnight in 1/1/70 (this is very useful for doing calculations).

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top