开发者

Database or no database?

<?php
$image_url = 'images/';

//User defined variables 开发者_开发知识库for page settings
$rows_per_page = 2;
$cols_per_page = 4;

//Master array of ALL the images in the order to be displayed
$images = array(
'image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg',
'image5.jpg', 'image6.jpg', 'image7.jpg', 'image8.jpg',
'image9.jpg', 'image10.jpg', 'image11.jpg', 'image12.jpg',
'image13.jpg', 'image14.jpg', 'image15.jpg', 'image16.jpg',
'image17.jpg', 'image18.jpg', 'image19.jpg'
);

//END USER DEFINED VARIABLES

//System defined variable
$records_per_page = $rows_per_page * $cols_per_page;
$total_records = count($images);
$total_pages = ceil($total_records / $records_per_page);

//Get/define current page
$current_page = (int) $_GET['page'];
if($current_page<1 || $current_page>$total_pages)
{
$current_page = 1;
}

//Get records for the current page
$page_images = array_splice($images, ($current_page-1)*$records_per_page, $records_per_page);

//Create ouput for the records of the current page
$ouput = "<table border=\"1\">\n";
for($row=0; $row<$rows_per_page; $row++)
{
$ouput .= "<tr>\n";
for($col=0; $col<$cols_per_page; $col++)
{
$imgIdx = ($row * $rows_per_page) + $col;
$img = (isset($page_images[$imgIdx])) ? "<img src=\"{$image_url}{$page_images[$imgIdx]}\" />" : '&nbsp;';
$ouput .= "<td>$img</td>\n";
}
$ouput .= "</tr>\n";
}
$ouput .= "</table>";

//Create pagination links
$first = "First";
$prev = "Prev";
$next = "Next";
$last = "Last";
if($current_page>1)
{
$prevPage = $current_page - 1;
$first = "<a href=\"test.php?page=1\">First</a>";
$prev = "<a href=\"test.php?page={$prevPage}\">Prev</a>";
}
if($current_page<$total_pages)
{
$nextPage = $current_page + 1;
$next = "<a href=\"test.php?page={$nextPage}\">Next</a>";
$last = "<a href=\"test.php?page={$total_pages}\">Last</a>";
}

?>
<html>
<body>
<h2>Here are the records for page <?php echo $current_page; ?></h2>
<ul>
<?php echo $ouput; ?>
</ul>
Page <?php echo $current_page; ?> of <?php echo $total_pages; ?>


<?php echo "{$first} | {$prev} | {$next} | {$last}"; ?>
</body>
</html>

This code above provides an easy way of putting up pictures to my site, whilst having the other pictures move automatically without the need of a database. But I must wonder: Would it make the page load faster if I moved my pictures to a database rather than what I'm doing right now? I mean the example above shows I'm only using 19 pictures, but if I ever reach say 1000 pictures, would the php file be too big? Should I just make a database now or it doesn't matter?

Also, for some reason, pagination is not working. Anyone care to do a diagnosis?

Thanks!


As you have mentioned, if you ever have 1000 pictures the PHP file would be particularly large. While this may well cause the load time to be slightly slower, arrays with 1000 elements are certainly not beyond the ability of PHP to handle efficiently.

Part (in fact, much) of the reason for using a database is that it is easier to manage large datasets. With your current setup, what happens when you want to add pictures somewhere other than at the start or end, especially when it gets large.

Setting yourself up with a database is a good step to ensuring your future sanity. However, you may want to consider the database to use. Given the simple nature of your dataset, you might want to consider using a no-SQL db like mongodb or even something as simple as sqlite.

If you intend to increase the complexity of your site by say adding captions or locations to your images, then you may want to consider using MySQL/PostgreSQL/etc...

As far as the pagination goes, can you provide some more information on what isn't working? Are the pages not showing up, is it the wrong number of pages? Does it always show page 1? I'll check back and update my post if you can give a little more details.


how would you manage to update an array with 1000 entries by hand?

I would put it in the data in a database. That way you can also filter the data via MySQL before being processed by PHP.


You should separate data from the code. In order to add new image you don't want to be editing the code.

Refactor the code by creating a function that reads what images are in your data directory and returns an array with the filenames.

Putting the images into database does not give any further advantage.


First you could easily reduce the size of your array block by using the following (if your images stay in that name scheme):

$images = array();

for($i = 1; $i < 20; $i++)
{
    $images[$i] = 'image'.$i.'.jpg';
}

About the performance, I believe your approach is a bit faster than using a database but your code could get messy pretty soon. It's way better to separate the the stored data from the actual view logic to be more flexible about designing your code.

Second: about the pagination, what exactly doesn't work for you?


It is always better to use database for large amount data. It is easier to manage. And also you may use flat file db.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜