473,385 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Reading PHP file names

1
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.
Jun 24 '08 #1
1 1148
Atli
5,058 Expert 4TB
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.  
Jun 25 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Kevin T. Ryan | last post by:
Hi All - I'm not sure, but I'm wondering if this is a bug, or maybe (more likely) I'm misunderstanding something...see below: >>> f = open('testfile', 'w') >>> f.write('kevin\n') >>>...
7
by: Santah | last post by:
hi I'm new to C++ and I'm currently working on Visual C++ 6.0 I'm trying to open a text file, and read some data from it part of the text file looks like this: --------
1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
1
by: hzgt9b | last post by:
(FYI, using VB .NET 2003) Can someone help me with this... I'm trying to read in an XML file... it appears to work in that the DataSet ReadXML method dose not fail and then I am able to access the...
7
by: jccorreu | last post by:
I've got to read info from multiple files that will be given to me. I know the format and what the data is. The thing is each time we run the program we may be using a differnt number of files,...
9
by: dba123 | last post by:
I need some help and direction on what classes and an example or two (article) on how to read an Excel Worksheet and insert one column into a database table column. I am using .NET 2.0 only. What...
1
by: Anghared | last post by:
I used to study VB in college but it's been a while. Ironically I've forgotten most of it just as I've found the perfect opportunity to utilize it. I want the application to read lines of text...
6
by: chris237 | last post by:
I'm having trouble with the following program. i want it to have the options to save a list of data entered by the user and display it the next time they run it. Could someone please either show me a...
2
by: GeoUK | last post by:
Hi All, New member here with a bit of problem. I have read the FAQ's and searched text books but still cannot solve the problem that I have. As part of a course I am doing at University I had...
2
by: tshad | last post by:
I have a csv file that I am reading into my dataset. But I want to ignore any rows that have nothing in the 3rd column - something like: da = new OleDbDataAdapter("SELECT * FROM " +...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.