Connecting Tech Pros Worldwide Forums | Help | Site Map

Reading PHP file names

Newbie
 
Join Date: Jun 2008
Posts: 1
#1: Jun 24 '08
Hi,

I am currently having a bit of a problem in work with a PHP code. Here is my code:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $nrandom1 = rand(1,3);
  4. $image_name1 = "image01.jpg";
  5.  
  6. if ($nrandom1 == 2) {
  7. $image_name1 = "image01.jpg";
  8. }
  9. if ($nrandom1 == 3) {
  10. $image_name1 = "image02.jpg";
  11. }
  12.  
  13. $nrandom2 = rand(1,3);
  14. $image_name2 = "image03.jpg";
  15.  
  16. if ($nrandom2 == 2) {
  17. $image_name2 = "image03.jpg";
  18. }
  19. if ($nrandom2 == 3) {
  20. $image_name2 = "image04.jpg";
  21. }
  22.  
  23. $nrandom3 = rand(1,3);
  24. $image_name3 = "image05.jpg";
  25.  
  26. if ($nrandom3 == 2) {
  27. $image_name3 = "image05.jpg";
  28. }
  29. if ($nrandom3 == 3) {
  30. $image_name3 = "image06.jpg";
  31. }
  32.  
  33. $nrandom4 = rand(1,3);
  34. $image_name4 = "image07.jpg";
  35.  
  36. if ($nrandom4 == 2) {
  37. $image_name4 = "image07.jpg";
  38. }
  39. if ($nrandom4 == 3) {
  40. $image_name4 = "image08.jpg";
  41. }
  42.  
  43. $nrandom5 = rand(1,3);
  44. $image_name5 = "image09.jpg";
  45.  
  46. if ($nrandom5 == 2) {
  47. $image_name5 = "image09.jpg";
  48. }
  49. if ($nrandom5 == 3) {
  50. $image_name5 = "image10.jpg";
  51. }
  52.  
  53. $nrandom6 = rand(1,3);
  54. $image_name6 = "image11.jpg";
  55.  
  56. if ($nrandom6 == 2) {
  57. $image_name6 = "image11.jpg";
  58. }
  59. if ($nrandom6 == 3) {
  60. $image_name6 = "image12.jpg";
  61. }
  62.  
  63. $nrandom7 = rand(1,3);
  64. $image_name7 = "image13.jpg";
  65.  
  66. if ($nrandom7 == 2) {
  67. $image_name7 = "image13.jpg";
  68. }
  69. if ($nrandom7 == 3) {
  70. $image_name7 = "image14.jpg";
  71. }
  72.  
  73. $nrandom8 = rand(1,3);
  74. $image_name8 = "image15.jpg";
  75.  
  76. if ($nrandom8 == 2) {
  77. $image_name8 = "image15.jpg";
  78. }
  79.  
  80. if ($nrandom8 == 3) {
  81. $image_name8 = "image16.jpg";
  82. }
  83.  
  84. if ...
  85. {
  86.     echo "<img src=\"logo.bmp\" border=\"1\" /><img src=\"$image_name5\" border=\"1\" /><img src=\"$image_name6\" border=\"1\" /><img src=\"$image_name7\" border=\"1\" /><img src=\"$image_name8\" border=\"1\" />";
  87. }
  88. else
  89. {
  90.     echo "<img src=\"logo.bmp\" border=\"1\" /><img src=\"$image_name1\" border=\"1\" /><img src=\"$image_name2\" border=\"1\" /><img src=\"$image_name3\" border=\"1\" /><img src=\"$image_name4\" border=\"1\" />";
  91. }
  92.  
  93.  
  94. ?>
This code is incomplete because I need to insert some sort of variable at the top and also complete the if statement on line 84. At first, I had four images on a banner, each of which were picked by a random generator. Each generator has a choice of two images to pick for that particular space.

However, I have since created more random generators ($nrandom5 - $nrandom8). These work in exactly the same way as the first four did, but what I want to do is to be able to load images from $nrandom5 to $nrandom8 on one page, but images from $nrandom1 to $nrandom4 on the other pages.

Basically, I think I need my code to say "if (name of php file), display $image_name5 to $image_name8, else display $image_name1 to $image_name4". My problem is I am not sure how to get PHP to recognise the names of other PHP files. It is probably a simple function but I am relatively inexperienced in PHP and so any help would be appreciated.

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,748
#2: Jun 25 '08

re: Reading PHP file names


You say this code is beeing used in several pages. How is it beeing called?
Is it beeing included?
You can use the __LINE__ constant to get the fill path of the file beeing executed.
Using basename(__LINE__) will get you the file name.

You could try something like:
Expand|Select|Wrap|Line Numbers
  1. function print_random_images($imgStartIndex, $numImages) {
  2.   for($x = 0, $y = $imgStartIndex; $x < $numImages; $x++, $y += 2)
  3.   {
  4.     echo "<img src='image". ($y + mt_rand(0, 1)) .".jpeg' />\n";
  5.   }
  6. }
  7.  
I use the for loop there to get rid of all the if statements you use.

Then, if you put this function into a file and include it into the files you want you images show on, you can use it like so:
Expand|Select|Wrap|Line Numbers
  1. # From the first file
  2. include("my_image_function.php");
  3. print_random_images(1, 4);
  4.  
  5. # Fromt any other file
  6. include("my_image_function.php");
  7. print_random_images(9, 4);
  8.  
Reply