Hi

I am attempting to build my own cms (although very basic) using php and mysql. Instead of having a static homepage, I want the content displayed to be dynamic. In other words, I want the homepage to be flexible/changeable based on whatever the latest 'content' entry into the database is.

I currently have two tables: 'user', and 'homepage'.

My homepage table is structured as follows:

home_id (int)
user_id (index, fk)
title_cont (varchar)
home_cont  (varchar)
home_date (timestamp)

I want to be able to display the latest 'title_cont', and the 'latest home_cont' entry. I am assuming that the date of when an entry is made is going to be useful in order to achieve this, so have included it in the table using a timestamp type.

Here is what I think a solution may be:

<?
$sql = "SELECT * FROM home ORDER BY home_date DESC LIMIT 1";
$display = $db->Execute( $sql );
?>

Then to display on my homepage:

<h2><?=$display->fields['title_cont']?></h2>
<p><?=$display->fields['home_cont']?>

Is this correct? Or is there a better way? Also, should I be using timestamp or datetime?

Thanks

Is this correct?

Yes.

The useful part of datetime over a timestamp is that you can use the MySQL date functions.

Thanks - would you recommend using datetime over timestamp in this case then? I only need it to sort the latest entry, so that php knows which entry to display.

Does the code look ok? I was going to use:

<?php
while ($row = mysql_fetch_array($sql)) {
echo "<h1>".$row{'title_cont'}."</h1>" "<p>".$row{'home_cont'}."</p>";
}
?>

But then I cam across the other approach - which looks a little unusual to me i.e

<?=$display->fields['value']?>

...I'm not familiar with using "<?=" instead of "echo" or "print". Is it an OO approach?
Try as I have done to pick up OO PHP, I have found it impossible to learn on my own (without having £££ to spend on Zend Training etc) so I have reverted to traditional methods...

Thanks

The benefit of using DateTime is that you can perform actions on the data in the query, MySQL has a host of functions that will work on DateTime fields.
If you really need the date as a Unix timestamp, then there is a MySQL function to convert DateTime to a timestamp, and also PHP has a function that will do the same.

As for <?=, it is simply shorthand for <?php echo.... It should not be used on projects that will, or could, be deployed on unknown configurations, as it requires the PHP short tags option to be enabled.

Use the select top 1 query statement. Here! and order it by ID, set to asc if you want the first row and desc if last row.

commented: No 'Top' for MySQL -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.