Optimizing this code...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
powerPT
Forum Newbie
Posts: 14
Joined: Tue Apr 04, 2006 2:25 pm
Location: Portugal

Optimizing this code...

Post by powerPT »

I have this code into a site, in every pages of them but it causes slow down.

Code: Select all

<?php
//Copyright by powerPT
$url_rand = array();
$url = "http:/www.xpto.com/";
$path = "/home/xpto/phpBB2/garage/upload";
$cnt=0;
foreach (glob("$path/garage_gallery-*_thumb.jpg") as $nome) {
$aux = "$nome";
$url = substr($aux,17,strlen($aux)-1);
$cnt = $cnt +1;
$url_rand[$cnt] = $url;
}
$valor = array_rand($url_rand,1);
echo "<img src=\"../../../../" . $url_rand[$valor] . " \" width=\"150\" height=\"112\" alt=\"\" />";
exit();
?>
This code shows a image from phpBB garage, from it folder...

I don´t have any ideia to solve this problem :roll:
A friend told me to improve the speed by save the image and just to show every hour or even per day. This can be a good solution but I don´t know how implement them, any ideia is appreciated . :oops:
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

Code: Select all

<?php 
//Copyright by powerPT 
$url_rand = array(); 
$url = "http:/www.xpto.com/"; 
$path = "/home/xpto/phpBB2/garage/upload"; 
$cnt=0; 
foreach (glob("$path/garage_gallery-*_thumb.jpg") as $nome) { 
$aux = "$nome"; 
$num = strlen($aux); //added
$num1 = $num - 1; //added
$url = substr($aux,17,$num1); 
$cnt = $cnt +1; 
$url_rand[$cnt] = $url; 
} 
$valor = array_rand($url_rand,1); 
echo "<img src=\"../../../../" . $url_rand[$valor] . " \" width=\"150\" height=\"112\" alt=\"\" />"; 
exit(); 
?>
optimized a little bette r=). math equations should be done on there own lines.
Post Reply