Archive for PHP

PHP Programming

28 February 2010

I’ve been working a lot these days to complete several big projects, and a handful of smaller ones. They’re all related to PHP and optimization. I’ve been thinking of a switch-based PHP preloading technique, which I’m trying to implement in a couple of projects.

What I’m trying to do exactly is update my CMSs and make a general purpose one, with modules, plug-ins and as many options as possible exposed to users. Let’s pretend I have a job CMS, let’s say (em)phasis JMS, which is about to get updated pretty soon. While it needs to have all the required options, it should be simple to manage (via source code) and easy to load. All those nice features should be able to load only if requested by user.

The switch-based PHP preloading technique is itself a framework to be placed inside any CMS. I might go as far as providing hooks like those from WordPress and be able to plug them into any kind of framework.

This is currently in a planning stage, with mockups and sketches. I hope they’ll move from the planning board to a sandbox CMS soon.

Photo by Scorpions and Centaurs

Whiskey Air Dark New Index Template

4 February 2010

Like I promised in a previous post, here is the code required to show latest 5 posts followed by 15 more, like on my homepage. You need to edit the index.php template and add 2 sub-loops separated by wp_reset_query(). Simple as that. Here is the entire index.php code (you should be able to pull the entire loop from there):


<?php get_header();?>
<div id="content">

<br clear="all" /><br />
<?php if(have_posts()) : ?>
 <?php
 query_posts('posts_per_page=5&offset=0');
 while(have_posts()) : the_post();
 ?>
 <div id="post-<?php the_ID();?>" <?php post_class();?>>
 <h2><a href="<?php the_permalink();?>" rel="bookmark" title="Permanent link to <?php the_title();?>"><?php the_title();?></a></h2>
 <small><?php the_time('j F Y');?> by <?php the_author();?></small>
 <div>
 <div style="float:left; margin-right:8px"><?php echo get_the_image_link(array('Thumbnail','My Thumbnail'),'thumbnail');?></div>
 <?php the_excerpt('Read more &raquo;');?>
 </div>
 <div style="clear:both"></div>
 <p>Category: <?php the_category(', ');?> | <?php comments_popup_link('No comments &#187;', '1 comment &#187;', '% comments &#187;');?><br /><?php the_tags(); ?></p>
 </div>
 <?php endwhile;?>

 <?php wp_reset_query();?>

 <h2>More recent articles</h2>
 <br /><br />
 <?php
 query_posts('posts_per_page=15&offset=6');
 while (have_posts()) : the_post();
 ?>
 <div id="post-<?php the_ID();?>" <?php post_class();?>>
 <p>
 <strong><a href="<?php the_permalink();?>" rel="bookmark" title="Permanent link to <?php the_title();?>"><?php the_title();?></a></strong><br />
 <small><?php the_time('j F Y');?> by <?php the_author();?></small>
 </p>
 </div>
 <?php endwhile;?>

 <div>
 <div><?php next_posts_link(__('&laquo; Older Entries','whiskey'));?></div>
 <div><?php previous_posts_link(__('Newer Entries &raquo;','youdrunk'));?></div>
 </div>

 <?php else : ?>
 <h2>Not found</h2>
 <p>No results found.</p>
 <?php endif;?>
</div>
<?php get_sidebar();?>
<?php get_footer();?>

Download index.php here.

You may have noticed that I’m using an unsanitized function, get_the_image_link, to show a thumbnail next to each excerpt. I noticed it when copying the code. Still waiting for a new version of Whiskey Air?

3 Quick PHP Tips

6 December 2009

php

1. Use a redirect function in PHP without header/location stuff:

function redirect($page,$time) {
	echo '<meta http-equiv="refresh" content="'.$time.'; url='.$page.'" />';
}

Now use:

redirect('index.php',3);

in order to redirect to index.php after 3 seconds.

2. Generate 3 characters salt strings:

// Salt Generator
function generate_salt() {
	// Declare $salt
	$salt = '';
	// And create it with random chars
	for ($i = 0; $i < 3; $i++) {
		$salt .= chr(rand(35, 126));
	}
	return $salt;
}

Change $i < 3 with more characters, as suited to your needs.

3. Clean up user input:

$name = ucwords(strtolower($name)); // Transforms "heLLO wOrLD" into "Hello World"

How To Extract Latest Posts From An External WordPress Blog

6 December 2009

RSS

First of all you need the Simplepie library. You only need the simplepie.inc file included in the archive.

My example extracts 5 items from a recipe blog:

<div class="block">
	<h2>Latest Culinary Recipes</h2>
	<div class="content">
		<?php $feed = new SimplePie('http://www.wordpress-site.com/feed');?>
		<ul>
			<?php foreach ($feed->get_items(0, 5) as $item): ?>
				<li>&raquo; <a href="<?php print $item->get_permalink();?>"><?php print substr(($item->get_title()),0,23);?> [...]</a></li>
			<?php endforeach; ?>
		</ul>
	</div>
</div>

I have also included a substr() function to show only the first 23 characters in the title. It fitted my web site better.

Don’t forget to have the simplepie library included at the top:

include('includes/simplepie.inc');

How To Extract Latest Posts From phpBB Forums

5 December 2009

forum

I’m talking here about extracting latest posts to a different site. I’m also assuming that the databases are on the same server.

So here is the script:

<?php
// connect to the database server
mysql_select_db('forum');

// run The query
$results = mysql_query("SELECT * FROM phpbb_topics ORDER BY topic_time DESC LIMIT 10");
if ($results) {
	while($rand = mysql_fetch_array($results)) {
		echo "<div>";
		echo "<a href='http://www.domain.com/forum/viewtopic.php?t=".$rand[0]."' target='_blank' title='Open in new window'>";
		echo $rand[6];
		echo "</a><br /><small>&raquo; added by ".$rand[16]."</small>";
		echo "</div>";
	}
}
else {
	echo "<p>Error: Forum not available.</p>";
}

mysql_select_db('main_site');
?>

The last line of the script reopens the previous connection, if any. If you don’t use any MySQL databases on your main site, then remove the last database selection line. You should take care and replace phpbb_ with your database prefix (usually phpbb_). You should also change the number of posts you want to extract (10 in my example). Feel free to open the phpbb_topics table and extract new info. I’m using $rand[x] to extract my info, where x is the column number in the phpbb_topics table (starting from 0).

Get Adobe Flash playerPlugin by wpburn.com wordpress themes