supernoob with a question...

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
hurdy gurdy
Forum Commoner
Posts: 40
Joined: Mon Jun 09, 2003 8:19 pm

supernoob with a question...

Post by hurdy gurdy »

Hi everyone! I'm new here as you can tell, and I'm new to PHP as well.

I had a question about some nonsense that I've writen, hopefully one of you generous people can help me out.

What I'm trying to accomplish here is to generate links to random images, but not to have repeating images. Meaning the same image doesn't appear twice on the page. I have a directory full of images with the same name except for there being incremental numbers at the end of the filename. I'm trying to plug randomly generated numbers ( $thumb ) into the <img> link, using "if" to check for duplicates. It doesn't work quite the way I want it to. lol

Any pointers would be very helpful!

Code: Select all

<?php
			$thumb1 = rand(0, 4);
			$thumb2 = rand(0, 4);
			$thumb3 = rand(0, 4);
			$thumb4 = rand(0, 4);
			$thumb5 = rand(0, 4);
			
			if (($thumb1 == $thumb2) || ($thumb1 == $thumb3) || ($thumb1 == $thumb4) || ($thumb1 == $thumb5))
			&#123;
				$thumb1 = rand(1, 4);
			&#125;
			echo '<img src="images/thumbs/thumb'.$thumb1.'.jpg" border="0">&nbsp;'."\n";
			
			if (($thumb2 == $thumb1) || ($thumb2 == $thumb3) || ($thumb2 == $thumb4) || ($thumb2 == $thumb5))
			&#123;
				$thumb2 = rand(1, 4);
			&#125;
			echo '<img src="images/thumbs/thumb'.$thumb2.'.jpg" border="0">&nbsp;'."\n";
			
			if (($thumb3 == $thumb1) || ($thumb3 == $thumb2) || ($thumb3 == $thumb4) || ($thumb3 == $thumb5))
			&#123;
				$thumb3 = rand(1, 4);
			&#125;
			echo '<img src="images/thumbs/thumb'.$thumb3.'.jpg" border="0">&nbsp;'."\n";
			
			if (($thumb4 == $thumb1) || ($thumb4 == $thumb3) || ($thumb4 == $thumb3) || ($thumb4 == $thumb5))
			&#123;
				$thumb4 = rand(1, 4);
			&#125;
			echo '<img src="images/thumbs/thumb'.$thumb4.'.jpg" border="0">&nbsp;'."\n";
			
			if (($thumb5 == $thumb1) || ($thumb5 == $thumb3) || ($thumb5 == $thumb3) || ($thumb5 == $thumb1))
			&#123;
				$thumb5 = rand(1, 4);
			&#125;
			echo '<img src="images/thumbs/thumb'.$thumb5.'.jpg" border="0">&nbsp;'."\n";
		?>
I think I should mention that I am trying to learn this laguage (using books and online resources mostly). The above I had given myself as a little assignment. When I look at it, it seems that it should work... Either way, I am really new to this. :)
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

lol. i'm a noob to, but i can help you a lot... specially since i just made something like that for my mom... however the thumb generator doesn't work... gimme a sec to rip off my own code and then explain what's happening

edit:

here's some code snippets and what's happening:

Code: Select all

$d=opendir($dir) or die($php_errormsg); 
while(false !==($f=readdir($d))){ 
  $here=getcwd(); $pospics=$here . '/' . $dir . $f; 
  if(is_file($pospics)){ 
    if((preg_match('/\.jpg$/', $f))||(preg_match('/\.jpe$/', $f))||(preg_match('/\.jpeg$/', $f))){ 
      $jpgs[] = $f; 
    }elseif(preg_match('/\.png$/', $f)){ 
      $pngs[] = $f; 
    } 
  } 
}
this block reads in a directory, omits all non files, then makes sure that the only files it keeps are jpgs and pngs.
i have two arrays cause i have to call another script with a different code for jps and for pngs

from the sound of it, you only need one, and you can make one long if string using preg_match() and change the jpg or whatever to gif, bmp and other image types

Code: Select all

$numjpgs=count($jpgs); $numpngs=count($pngs);
this gets the size of the two arrays. if you use one array you only need one call to count

Code: Select all

for($i=0;$i<$numjpgs;$i++){ ## display jpegs 
      $image=$dir . $jpgs[$i]; 
      echo "<a href="$image" target="_blank"><img src="createthumb.php?pic=$image&type=jpg" border="0"></a> "; 
    }
a for loop to show all the jpgs, and it calls something to make pngs on the fly that are 250 by 250 pixels.. you're not doing that, so you can modify that to your needs
hurdy gurdy
Forum Commoner
Posts: 40
Joined: Mon Jun 09, 2003 8:19 pm

Post by hurdy gurdy »

That went screaming right over my head! lol

Thanks for posting this, I think it'll take me awhile to grok it though. It seems like an awfully complicated procedure just to pull five random numbers (without duplicates) and append them to a filename for displaying on a webpage.

Do you see why the code I wrote doesn't work in this fashion? I'd really like to know whhere I went wrong..lol, or I'll never learn :)

Oh, its obvious that the BBcode tage of "code" doesn't bring up the php code windows that you have.. whats the proper tag for that?

Thanks
SBukoski
Forum Contributor
Posts: 128
Joined: Wed May 21, 2003 10:39 pm
Location: Worcester, MA

Post by SBukoski »

Use the [ PHP ] and [ /PHP ] BBCode tags for the PHP specific code.

As for your problem, instead of an If statement for each, try a while loop. The if will only gaurantee you one shot at a different number. The while will keep going over again and again until it definitely finds a number not used yet.

Of course, you need to make sure you don't get caught in an infinite loop.
hurdy gurdy
Forum Commoner
Posts: 40
Joined: Mon Jun 09, 2003 8:19 pm

Post by hurdy gurdy »

You're absolutely right, I should have known that a while loop would have worked.

LOL, now I have to figure out a way to kep the $thumbX from picking the same unusable numbers.

Thanks for your help.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

hurdy gurdy wrote:That went screaming right over my head! lol

Thanks for posting this, I think it'll take me awhile to grok it though. It seems like an awfully complicated procedure just to pull five random numbers (without duplicates) and append them to a filename for displaying on a webpage.

Do you see why the code I wrote doesn't work in this fashion? I'd really like to know whhere I went wrong..lol, or I'll never learn :)

Oh, its obvious that the BBcode tage of "code" doesn't bring up the php code windows that you have.. whats the proper tag for that?

Thanks
ok. are you trying to GUESS at what's there? do you merely want 5 of x many that are there? or do you want them all and they have 5 random digits at the end?


there is a function in php to read a directory: readdir()
there is a function in php to check if something is a file: is_file()
there is a function in php to use perl's regular expression for matching: preg_match()
there is a function in php to get the number of elements in an array
there is a function in php to get the current directory
there is a function in php to open a directory
there is a way to create an empty array and then add to it later.

if you haven't already, you should buy a book on php. if you're used to programming languages and just need a reference guide, then get the orielly one.

here's something that might help, wwithout the trying to get files that don't exist, and assumes that $dir is a sub directory (ie: pics/ and note the trailing /)of the one you're in that you're trying to find the files in, and it makes sure that the file is marked as a jpeg, png, gif, or bitmap, then sorts them asci-betically and goes through them making an html output:

Code: Select all

$pics=array();
$d=opendir($dir) or die($php_errormsg); 
while(false !==($f=readdir($d))){ 
  $here=getcwd(); $pospics=$here . '/' . $dir . $f; 
  if(is_file($pospics)){ 
    if((preg_match('/\.jpg$/', $f))||(preg_match('/\.jpe$/', $f))||(preg_match('/\.jpeg$/', $f))||(preg_match('/\.pnp$/', $f))||(preg_match('/\.bmp$/', $f))){ 
      $pics[] = $dir . $f;
    } 
  } 
}
sort(pics);
foreach($pics as $pic){
  echo "<img src="$pic"> ";
}
hurdy gurdy
Forum Commoner
Posts: 40
Joined: Mon Jun 09, 2003 8:19 pm

Post by hurdy gurdy »

ok. are you trying to GUESS at what's there? do you merely want 5 of x many that are there? or do you want them all and they have 5 random digits at the end?
I sense that you are getting upset with me.

I don't know how more simply I can explain the situation, let me give it a shot.

There are 5 known image files (thumb0.jpg, thumb1.jpg, thumb2.jpg, thumb3.jpg, thumb4.jpg). They exist in a directory, "images/thumbs/".

I'm trying to write a script that generates 5 random numbers, and checks to see if there are duplicates of those numbers (if there are keep randomizing until there are no duplicates). Then append the numbers to the end of a filename for display on a webpage.

Changing over to a while loop pretty much did the trick, but now I'm in danger of getting caught in an infinite loop, which due to my inexperience, I didn't forsee.
if you haven't already, you should buy a book on php. if you're used to programming languages and just need a reference guide, then get the orielly one.
I have been using a book(and as much online help as I can find), and no I'm not used to programming languages. :) I'm trying very hard though.

If this is not the place for a supernoob to ask questions, could you point me to the correct location?
SBukoski
Forum Contributor
Posts: 128
Joined: Wed May 21, 2003 10:39 pm
Location: Worcester, MA

Post by SBukoski »

If you're trying to get 5 unique number out of a random generator in a set of 5, you'll be fine. If you only want five then why not put the 5 numbers into an array, and then resort the array X number of times so the numbers appear random. Takes less processing time, I would think. Is there ever a chance where you'll be choosing from a random pool greater than 5?
hurdy gurdy
Forum Commoner
Posts: 40
Joined: Mon Jun 09, 2003 8:19 pm

Post by hurdy gurdy »

no sooner than I posted my last post, I wrote this:

Code: Select all

$image_num = array (1, 2, 3, 4, 0); //generating the numbers in an array
shuffle ($image_num); //randomizing the order
			
for ($i = 0; $i <= 4; $i++)
{
				echo '<img src="images/thumbs/thumb'.$image_num[$i].'.jpg" border="0">&nbsp;'."\n";
}
which seems to do the job allright. I feel kinda stupid for writing all of that unnecessary code from before, but its a great way to learn. :) Does anyone see any pitfalls that I have created formyself, doing it this way? I would really appreciate your thoughts.
Is there ever a chance where you'll be choosing from a random pool greater than 5?
Quite possibly, sometime in the future. The image list will continue to expand.

BTW, Thanks for your help.:)
SBukoski
Forum Contributor
Posts: 128
Joined: Wed May 21, 2003 10:39 pm
Location: Worcester, MA

Post by SBukoski »

What you have is fairly straightforward and should work like a charm. Was sort of wondering why you were doing the whole random number thing if you always knew there were 5 options. Shuffling the array is definitely the way to go. Simple enough to expand upon in the future as well.
Post Reply