Tutorial Random String from Pre-Determined Characters

by in , 0

<?php
$string = "abcdwxyz456789";
for($i=0;$i<25;$i++){
   $pos = rand(0,13);
   $str .= $string{$pos};
}
echo $str;
?>

Tutorial Random Slogan Displayer

by in , 0

Create a text file called slogans.txt with permissions that it can be read by the server. Put each slogan on a different line, e.g.:

Building websites since before there was a web.
The internet is for lovers.

<?php
       $f_contents = file ("slogans.txt");
       $line = $f_contents[array_rand ($f_contents)];
       print $line;
?>

Tutorial Random Hex Color

by in , 0

Technique #1

<?php 
    
    $rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
    $color = '#'.$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)];
    
?>

Then echo out the $color value anywhere you need it. For example:

<body style="background: <?php echo $color; ?>;">

Technique #2

<?php printf( "#%06X\n", mt_rand( 0, 0xFFFFFF )); ?>

There is also a JavaScript version.

Reference URL

Tutorial Quick Alphabetic Navigation

by in , 0

Could be useful for an address book style navigation.

<?php
foreach (range('a', 'z') as $char) {
 print '<a href="#' . $char . '">' . $char . '</a> | ';
}
?>

Tutorial POST Validation Loop

by in , 0

Assumptions

You have an HTML form with a variety of inputs. The action attribute of the form points to a PHP file that contains the code below.

Notes about code

This code starts by creating an array that holds the name of various inputs being submitted via a POST. getFormData() is then called, where the required fields are passed in. Inside the function an array is created to hold various pieces of data related to the form. $formData['valid'] is a boolean referencing if all data was provided and valid, $formData['fields'] is an array keyed by the name of the input with their respective value from the POST data, $formData['notValidFields'] is an array that will contain the names of any inputs that were not passed or that had non-valid data.

This logic can be easily extended with regular expressions to check for stricter data, such as email addresses and urls.

<?php

$requiredFields = array('field1', 'field2', 'field3', 'field4');
$formData = getFormData($requiredFields);

function getFormData($requiredFields){
       $formData = array();
       $formData['valid'] = true;
       $formData['fields'] = array();
       $formData['notValidFields'] = array();

       for($a = 0; $a < count($requiredFields); $a++){
               $field = $requiredFields[$a];
               if(isset($_POST[$field])){
                       $value = $_POST[$field];
                       if(empty($value)){
                               $formData['valid'] = false;
                               $formData['notValidFields'][] = $field;
                       }else{
                               $formData['fields'][$field] = $value;
                       }
               }else{
                       $formData['valid'] = false;
                       $formData['notValidFields'][] = $field;
               }
       }
       return $formData;
}

Tutorial PHP Zebra Striping a Table

by in , 0

Table row in a loop:

<!-- Before loop -->
<?php $c = 0; ?>

<!-- Start loop -->
<tr class="<?=($c++%2==1) ? 'odd' : 'even' ?>">
<!-- End loop -->

CSS:

.even { background-color:#FFF; }
.odd { background-color:#666; }

Tutorial PHP Redirect

by in , 0

<?php
  header( 'Location: http://www.yoursite.com/new_page.html' ) ;
?>