zhx Posted May 29, 2007 Share Posted May 29, 2007 Hello, I'm running a blog powered by Wordpress. In my header, there is a simple function that returns a random masthead, like this: function header_graphic() { echo "/images/"; $num=rand(0,1); echo "masthead".$num.".jpg"; This (obviously) returns masthead0.jpg, masthead1.jpg, randomly. My question is, is there a simple way I could modify this so that I could create my mastheads in a variety of graphics formats? For example, make the function randomly return one of masthead0.jpg, masthead1.png, or masthead2.gif? JPG doesn't suit all my graphics the best... Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted May 29, 2007 Share Posted May 29, 2007 <?php function header_graphic() { echo "/images/"; $num=rand(0,1); $fileType = rand(1,3); if ($fileType == "1"){ $type = ".jpg";} if ($fileType == "2"){ $type = ".gif";} if ($fileType == "3"){ $type = ".png";} echo "masthead".$num.$type; ?> Quote Link to comment Share on other sites More sharing options...
zhx Posted May 29, 2007 Author Share Posted May 29, 2007 I think that's the right idea but if I have, for example, masthead2.png, wouldn't we run into the problem that that function could possibly return masthead1.png, which doesn't (in this example) exist? Or am I reading it wrong? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted May 29, 2007 Share Posted May 29, 2007 Yes, that would be a problem. Why don't you just call the extension from the file as well instead of making .jpg the default? Quote Link to comment Share on other sites More sharing options...
spfoonnewb Posted May 29, 2007 Share Posted May 29, 2007 Why limit the extension or name? Just create a directory with the images you want and nothing else.. and then: <?php function change_header($dir) { if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $array[] = $file; } } closedir($handle); } $n = rand(0,count($array)-1); return $dir.$array[$n]; } echo change_header("images/"); ?> ............. If you need to keep it in the /images and with other files you can limit it to the masthead name: <?php function change_header($dir) { if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { if (preg_match("/masthead/", $file)) { $array[] = $file; } } } closedir($handle); } $n = rand(0,count($array)-1); return $dir.$array[$n]; } echo change_header("images/"); ?> Quote Link to comment Share on other sites More sharing options...
zhx Posted May 29, 2007 Author Share Posted May 29, 2007 Wow, that would work great, only I see now that my problem runs a little deeper than this. See, when I created this Wordpress database, my URL was different. It has since changed, but if I use $dir, it seems to be trying to look for /images in the old URL. Say my old URL was www.oldsite.com, and my new URL www.newsite.com, the error message I'm getting is: Warning: opendir(images/): failed to open dir: No such file or directory in /home/oldsite/public_html/wp-content/themes/theme1/header.php on line 5 images/ #header { background: url("http://www.newsite.com/wp-content/themes/theme1 Fatal error: Call to undefined function: header_graphic() in /home/oldsite/public_html/wp-content/themes/theme1/header.php on line 32 How can I update my database to refer to the correct URL/directory (/home/newsite/public_html/wp-content/themes/theme1/images/), or is that way out of the scope of this forum? Quote Link to comment Share on other sites More sharing options...
spfoonnewb Posted May 29, 2007 Share Posted May 29, 2007 -- Referrer to my next post -- Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted May 29, 2007 Share Posted May 29, 2007 Build some multi D arrays $multiimage['jpg'][0] = 1; $multiimage['jpg'][1] = 1; $multiimage['jpg'][2] = 1; $multiimage['jpg'][3] = 1; $multiimage['png'][0] = 1; $multiimage['png'][1] = 1; $multiimage['png'][2] = 1; switch $rndimagtype { case 1: $type = "jpg"; break; case2: $type = "png"; break; default: $type = "jpg"; } if (!ISSET($multiimage[$type][$rndnum]) { function change_header(); } Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted May 29, 2007 Share Posted May 29, 2007 only issue with mine is that theoritically it could result in an endless loop of never matching, but it shoudl be able to run 1000+ sequences in less than a second and if the odds of failing are 1-100 well you got good chances of passing Quote Link to comment Share on other sites More sharing options...
spfoonnewb Posted May 29, 2007 Share Posted May 29, 2007 <?php function change_header($dir, $abs) { if ($handle = opendir($abs)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { if (preg_match("/masthead/", $file)) { $array[] = $file; } } } closedir($handle); } $n = rand(0,count($array)-1); return $dir.$array[$n]; } change_header("/relative/path/to", "/absolue/path/to"); ?> Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted May 29, 2007 Share Posted May 29, 2007 Complete Function: function header_graphic() { //Edit this multi array for Image types and if the image numbers exist (Here .jpg 0-3 and .png 0-2 are out there) $multiimage['jpg'][0] = 1; $multiimage['jpg'][1] = 1; $multiimage['jpg'][2] = 1; $multiimage['jpg'][3] = 1; $multiimage['png'][0] = 1; $multiimage['png'][1] = 1; $multiimage['png'][2] = 1; $num=rand(0,5); $imgtype=rand(1,2); //Add More cases for more types and adjust limits on $imgtype in same way switch $imgtype { case 1: $type = "jpg"; break; case2: $type = "png"; break; default: $type = "jpg"; } if (!ISSET($multiimage[$type][$num]) {function header_graphic()} else { echo "/images/"; echo "masthead".$num.".$type; } } Quote Link to comment Share on other sites More sharing options...
zhx Posted May 29, 2007 Author Share Posted May 29, 2007 <?php function change_header($dir, $abs) { if ($handle = opendir($abs)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { if (preg_match("/masthead/", $file)) { $array[] = $file; } } } closedir($handle); } $n = rand(0,count($array)-1); return $dir.$array[$n]; } change_header("/relative/path/to", "/absolue/path/to"); ?> Seems that even with an absolute link, it's still saying that the directory is not implemented in /home/oldsite/public_html/wp-content/themes/theme1/header.php Quote Link to comment Share on other sites More sharing options...
spfoonnewb Posted May 29, 2007 Share Posted May 29, 2007 That's a server issue, not PHP. Put a file in the images folder, and view it in the browser with: <?php echo getcwd()."/"; ?> Use whatever it outputs as the absolute path. If the /images is on a different server completely, it will not work. Quote Link to comment Share on other sites More sharing options...
zhx Posted May 29, 2007 Author Share Posted May 29, 2007 Complete Function: function header_graphic() { //Edit this multi array for Image types and if the image numbers exist (Here .jpg 0-3 and .png 0-2 are out there) $multiimage['jpg'][0] = 1; $multiimage['jpg'][1] = 1; $multiimage['jpg'][2] = 1; $multiimage['jpg'][3] = 1; $multiimage['png'][0] = 1; $multiimage['png'][1] = 1; $multiimage['png'][2] = 1; $num=rand(0,5); $imgtype=rand(1,2); //Add More cases for more types and adjust limits on $imgtype in same way switch $imgtype { case 1: $type = "jpg"; break; case2: $type = "png"; break; default: $type = "jpg"; } if (!ISSET($multiimage[$type][$num]) {function header_graphic()} else { echo "/images/"; echo "masthead".$num.".$type; } } I tried this, and this is the error I get: Parse error: syntax error, unexpected T_VARIABLE, expecting '(' in /home/oldsite/public_html/wp-content/themes/theme1/header.php Where is it expecting a (? Quote Link to comment Share on other sites More sharing options...
zhx Posted May 29, 2007 Author Share Posted May 29, 2007 That's a server issue, not PHP. I figured it was a SQL thing. If you use $dir, where does your script pull this URL from? Put a file in the images folder, and view it in the browser with: <?php echo getcwd()."/"; ?> Use whatever it outputs as the absolute path. If the /images is on a different server completely, it will not work. Sorry, what? You're dealing with a moron. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted May 29, 2007 Share Posted May 29, 2007 I think this line: if (!ISSET($multiimage[$type][$num]) is missing a ')' at the end. if (!ISSET($multiimage[$type][$num])) Quote Link to comment Share on other sites More sharing options...
zhx Posted May 29, 2007 Author Share Posted May 29, 2007 I think this line: if (!ISSET($multiimage[$type][$num]) is missing a ')' at the end. if (!ISSET($multiimage[$type][$num])) It says it's on line 15, which, in the context of my header.php page, is this line: switch $imgtype Nevertheless, I tried to put a ) where you said, and I get the same error. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted May 29, 2007 Share Posted May 29, 2007 Full Proof Function Tested it out It also will load a default if it fails 15 times function header_graphic() { //Edit this multi array for Image types and if the image numbers exist (Here .jpg 0-3 and .png 0-2 are out there) $multiimage["jpg"][0] = 1; $multiimage["jpg"][1] = 1; $multiimage["jpg"][2] = 1; $multiimage["jpg"][3] = 1; $multiimage["png"][0] = 1; $multiimage["png"][1] = 1; $multiimage["png"][2] = 1; $num = rand(0,5); $imgtype = rand(1,2); //Add More cases for more types and adjust limits on $imgtype in same way switch ($imgtype) { case 1: $type = "jpg"; break; case 2: $type = "png"; break; default: $type = "jpg"; } //Incase we fail a lot $deafult = "0.jpg"; $faillimit = 15; if (!ISSET($multiimage[$type][$num])) { $failcount++; if ($failcount <= $faillimit) { header_graphic(); } else {echo "/images/masthead".$default;} } else { echo "/images/"; echo "masthead".$num.".".$type; } } header_graphic(); Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted May 29, 2007 Share Posted May 29, 2007 that first function i fried out with out testing been coding in C/ASP/PHP/JS today so getting some syntax off, the rewrite i test on my server 4.4.4 and it is flawless Quote Link to comment Share on other sites More sharing options...
zhx Posted May 29, 2007 Author Share Posted May 29, 2007 Full Proof Function Tested it out It also will load a default if it fails 15 times function header_graphic() { //Edit this multi array for Image types and if the image numbers exist (Here .jpg 0-3 and .png 0-2 are out there) $multiimage["jpg"][0] = 1; $multiimage["jpg"][1] = 1; $multiimage["jpg"][2] = 1; $multiimage["jpg"][3] = 1; $multiimage["png"][0] = 1; $multiimage["png"][1] = 1; $multiimage["png"][2] = 1; $num = rand(0,5); $imgtype = rand(1,2); //Add More cases for more types and adjust limits on $imgtype in same way switch ($imgtype) { case 1: $type = "jpg"; break; case 2: $type = "png"; break; default: $type = "jpg"; } //Incase we fail a lot $deafult = "0.jpg"; $faillimit = 15; if (!ISSET($multiimage[$type][$num])) { $failcount++; if ($failcount <= $faillimit) { header_graphic(); } else {echo "/images/masthead".$default;} } else { echo "/images/"; echo "masthead".$num.".".$type; } } header_graphic(); I got this working, but for no good reason, messes up the formatting on my menu bar, which is directly underneath my masthead. It also makes my front page load noticeably slower (about three full seconds to load main page). Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted May 29, 2007 Share Posted May 29, 2007 take the multi array out of the function since it will re populate that for no reason. Also try and mess around so your limits are tighter Quote Link to comment Share on other sites More sharing options...
spfoonnewb Posted May 29, 2007 Share Posted May 29, 2007 Try this... It is kind of hard to explain how to create a php file in your images directory on that other post..... Heres a function-less basic script: <?php //The URL you go to when you view the images directory $http_path = "http://www.yourdomain.com/images/"; //The servers path to your image directory $server_path = "/www/yourdomain.com/public/images/"; if ($handle = opendir($server_path)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $array[] = $file; } } closedir($handle); $n = rand(0,count($array)-1); echo "<img src=\"".$http_path.$array[$n]."\">"; } ?> Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted May 29, 2007 Share Posted May 29, 2007 I moved it outside and did a bit of rework tell me if it helps <?php function header_graphic() { //Edit this multi array for Image types and if the image numbers exist (Here .jpg 0-3 and .png 0-2 are out there) $multiimage["jpg"][0] = 1; $multiimage["jpg"][1] = 1; $multiimage["jpg"][2] = 1; $multiimage["jpg"][3] = 1; $multiimage["png"][0] = 1; $multiimage["png"][1] = 1; $multiimage["png"][2] = 1; //Incase we fail a lot $deafult = "0.jpg"; $faillimit = 15; do { if($failcount >$faillimit) { echo "/images/masthead".$deafult; } else { $failcount++; $num = rand(0,3); $imgtype = rand(1,2); //Add More cases for more types and adjust limits on $imgtype in same way switch ($imgtype) { case 1: $type = "jpg"; break; case 2: $type = "png"; break; default: $type = "jpg"; } } } While(!ISSET($multiimage[$type][$num]) && $failcount <= $faillimit); if ($failcount <=$faillimit) { echo "/images/"; echo "masthead".$num.".".$type; } } header_graphic() ?> Quote Link to comment Share on other sites More sharing options...
zhx Posted May 29, 2007 Author Share Posted May 29, 2007 Try this... It is kind of hard to explain how to create a php file in your images directory on that other post..... Heres a function-less basic script: <?php //The URL you go to when you view the images directory $http_path = "http://www.yourdomain.com/images/"; //The servers path to your image directory $server_path = "/www/yourdomain.com/public/images/"; if ($handle = opendir($server_path)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $array[] = $file; } } closedir($handle); $n = rand(0,count($array)-1); echo "<img src=\"".$http_path.$array[$n]."\">"; } ?> This works by itself...but how would I make this operate as "function header_graphic()" so I don't have to change the calls to this function? Or is there a simpler way to implement this in the context of my page? Quote Link to comment Share on other sites More sharing options...
zhx Posted May 29, 2007 Author Share Posted May 29, 2007 I moved it outside and did a bit of rework tell me if it helps <?php function header_graphic() { //Edit this multi array for Image types and if the image numbers exist (Here .jpg 0-3 and .png 0-2 are out there) $multiimage["jpg"][0] = 1; $multiimage["jpg"][1] = 1; $multiimage["jpg"][2] = 1; $multiimage["jpg"][3] = 1; $multiimage["png"][0] = 1; $multiimage["png"][1] = 1; $multiimage["png"][2] = 1; //Incase we fail a lot $deafult = "0.jpg"; $faillimit = 15; do { if($failcount >$faillimit) { echo "/images/masthead".$deafult; } else { $failcount++; $num = rand(0,3); $imgtype = rand(1,2); //Add More cases for more types and adjust limits on $imgtype in same way switch ($imgtype) { case 1: $type = "jpg"; break; case 2: $type = "png"; break; default: $type = "jpg"; } } } While(!ISSET($multiimage[$type][$num]) && $failcount <= $faillimit); if ($failcount <=$faillimit) { echo "/images/"; echo "masthead".$num.".".$type; } } header_graphic() ?> Works, but again, messes up my formatting for no apparent reason, and pages load slooooow. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.