Jump to content

Recommended Posts

Hey, all, I have this script:

 

 

<?
function getRandomFile($start_dir)
        {
        /*
        returns the name of one random file from within a directory
        */
       
        chdir($start_dir);
        $dir = opendir('.');
        while (($myfile = readdir($dir)) !==false)
                {
                if ($myfile != '.' && $myfile != '..' && is_file($myfile) && $myfile != 'resource.frk')
			{
                    $files[] = $myfile;
                }
                }
        closedir($dir);
        chdir('../');
        srand ((float) microtime() * 10000000);
        $file = array_rand($files);
        return $files[$file];
        }
	 for ( $counter = 1; $counter <= 3; $counter += 1) {
	$imagesDir = 'photos';
	$imageURL = getRandomFile($imagesDir);

			$im	= imagecreatefromjpeg($imagesDir."/".$imageURL);
			//$img = imagejpeg($img,null,85);
			$ox = imagesx($im);  
			$oy = imagesy($im);
			$final_width_of_image = 76;
			$ratio = $final_width_of_image / $ox;
			$ny = $oy * $ratio; 
			$photoID = explode(".",$imageURL);

	echo '<img style="margin-left:5px;" src="'.$imagesDir.'/'.$imageURL.'" height="'.$ny.'" width="'.$final_width_of_image.'" />';
	}
?>

 

Any ideas upon how it can only display one file only once,and not twice?

 

Thanks all!

Link to comment
https://forums.phpfreaks.com/topic/202138-rand-help/
Share on other sites

<body>

<?php

 

// Check for form submission:

if (isset($_POST['submitted'])) {

 

  // Minimal form validation:

  if (!empty($_POST['name']) && !empty($_POST['business']) && !empty($_POST['tel']) && !empty($_POST['email']) ) {

 

      // Create the body:

      $body = "Name: {$_POST['name']} \n\nBusiness: {$_POST['business']} \n\nTel: {$_POST['tel']} ";

     

      // Make it no longer than 70 characters long:

      $body = wordwrap($body, 70);

 

      // Send the email:

      //mail('', 'Contact Form Submission', $body, "From: {$_POST['email']}");

     

      // Print a message:

      $success = mail($to, $subject, $body);

      ########################################################Added this line to make $message empty when success####################333

      $message='';

      }

      if ($success){

      header("location: http://");

     

     

     

      // Clear $_POST (so that the form's not sticky):

      $_POST = array();

 

  } else {

      $message = '<p style="font-weight: bold; color: #C00">Please fill out the form completely.</p>';

     

  }

 

} // End of main isset() IF.

 

// Create the HTML form:

?>

<div id="wrapper">

<div id="header">

<div id="headerImages"> <img src="images/image1.gif" alt="image1"/> <img src="images/image2.gif" alt="image2"/> <img src="images/image3.gif" alt="image3"/> </div>

<div id="nav">

<ul>

<li class="home"><a href="index.html">Home</a></li>

<li class="services"><a href="services.html">Services</a></li>

<li class="about"><a href="about.html">About</a></li>

<li class="products"><a href="products.html">Products</a></li>

<li class="contactActive"><a href="contact.php">Contact</a></li>

<li class="request"><a href="request.php">Request</a></li>

</ul>

</div>

</div>

<div id="background">

<div id="container">

<div id="requestACall">

<div id="requestCallForm"><h2 class="requestCall">REQUEST CALL-BACK</h2>

<br />

<?php if (!empty($message)) {

        echo "<p class=\"messge\">" . $message . "</p>";

        } ?>

<form action="contact.php" method="post"  />

<h3 class="requestCall">NAME</h3>

<input type="text" name="name" id="name" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>" />

<h3 class="requestCall">BUSINESS</h3>

<input type="text" name="business" id="business" value="<?php if (isset($_POST['business'])) echo $_POST['business']; ?>" />

<h3 class="requestCall">TEL</h3>

<input type="text" name="tel" id="tel" value="<?php if (isset($_POST['tel'])) echo $_POST['tel']; ?>" />

<h3 class="requestCall">EMAIL</h3>

<input type="text" name="email" id="email" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" />

<p class="submit"><input type="submit" name="submitted" value="" /></p>

</div>

        </div> 

            </div>

<div id="footer1">

<a href="index.html">HOME</a> | <a href="services.html">SERVICES</a> | <a href="about.html">ABOUT</a> | <a href="products.html">PRODUCTS</a> | <a href="contact.php">CONTACT</a> | <a href="terms.html">TERMS</a> | <a href="request.php">QUOTES</a> </div>

</div>

<div id="footer">

</div>

</div>

</body>

</html>

 

 

Link to comment
https://forums.phpfreaks.com/topic/202138-rand-help/#findComment-1060541
Share on other sites

Change your getRandomFile() function to return an array of the image files in the directory (and maybe change the name to getFileArray() or something).  Then shuffle() the array (which randomizes it).  Call this new function BEFORE your loop starts. Then in your loop, use array_pop() to get an element from the array (which will be a random filename).  Since array_pop() removes the entry from the array, you will not get the same filename a second time.

 

Calling your current function inside the loop means you are scanning the directory 3 times.  As the directory gets bigger (more files) this scan will take longer.  By calling it only once, you are preventing a problem in the future.

 

Also, you do not need to create an image (imagecreatefromjpeg()) just to find out its size.  I think the getimagesize() function would be more efficient here.

 

 

Link to comment
https://forums.phpfreaks.com/topic/202138-rand-help/#findComment-1060852
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.