function - Simplifying php code -


i'm working on revamping cms right now. currently, i'm trying minimize front end code make theming easier. display latest posts, code using:

<?php     $query = mysql_query("select * posts order id desc") or die(mysql_error());    while($row = mysql_fetch_array($query)) : ?>    <h3><a href="?id=<?php echo $row['id'] ?>"><?php echo $row['title'] ?></a></h3> <?php endwhile; ?> 

what best way simplify front end user? i'm thinking using functions.php file not sure how that. there way make first 2 lines of php function , user have call function?

here function use:

function getposts() {     $query = mysql_query("select * posts order id desc") or die(mysql_error());     $result = array();     while($row = mysql_fetch_object($query)) {         $result[] = $row;     }     return $result; } 

and in template:

<?php foreach(getposts() $post) : ?> <h3><a href="?id=<?php echo $post->id ?>"><?php echo $row->title ?></a></h3> <?php endforeach; ?> 

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -