Archive for ‘PHP’

January 27th, 2012

PHP Array Flip – Array Value Key Flip

PHP example that you can use to flip the values and keys in an array. Essentially all the values will become keys and all the keys will become values.

<php

$sampleArray = array(0=>”one”, 1=>”two”, 2=>”three”);

$flipedArray = array_flip($sampleArray);

endphp>

April 25th, 2011

Array Unique Tool – Removes duplicate values from an array online

ArrayThis is hopefully one of the many online array tools that I am going to publish. In my work I deal with arrays on daily basis. Therefore I have decided to create a simple page where you can manipulate an array in any way I can think of. I am presenting to you the first tool called “Array Unique Tool”. The link is  http://erunways.com/online-array-tools/. Enjoy and let me know if you have any suggestions what so ever.

August 9th, 2010

Simple PHP get URI or Segment element

This script takes any uri or segment from the url of the current page and assigns it to a $uri variable. It is very easy to implement so enjoy.

php 

$uri = $_SERVER['REQUEST_URI'];
$pieces = explode("/", $uri);
$uri_3 = $pieces[3];

/php

Refferenses:

URI -Term used to collectively refer to URLs, URNs, and URCs.
lcweb2.loc.gov/ammem/techdocs/repository/gengloss.html

$_SERVER is an array containing information such as headers, paths, and script locations. http://php.net/manual/en/reserved.variables.server.php

explode() Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter. http://php.net/manual/en/function.explode.php

Tags:
June 28th, 2010

jQuery Tools Form + PHP mail() (PHP 4, PHP 5)

jquery php email form

jquery php email form

This example shows how to integrate php mail which is is used to send emails from inside a script with jQuery Tools form which is a collection of essential form building tools. Validation, range- and date inputs for humans. Those two make a great combination and are easy to implement to any website. Here is the example. The code speaks for itself so there is not much commenting on it.

Standalone example can be found HERE.
Source code HERE.

PHP:


if(isset($_POST['submit'])){
  $to      = $_POST['email'];
  $subject = "You've got mail";
  $message = "*** eMail: ".$_POST['email']."*** Website: ".$_POST['url']."*** Name: ".$_POST['name']."*** Age; ".$_POST['age'];
  $headers = 'From: webmaster@example.com' . "\r\n" .
      'Reply-To: webmaster@example.com' . "\r\n" .
      'X-Mailer: PHP/' . phpversion();

  mail($to, $subject, $message, $headers);
  
  echo "Email to ".$_POST['email']." was sent successfuly! <br />";
}

HTML:


<form id="myform" method="post">

   <fieldset>
      <h3>PHP + JQuery Tools Sample email form</h3>
      
      <p> Enter bad values and then press the email button. </p>
      
      <p>
         <label>email *</label>
         <input type="email" name="email" required="required" />
      </p>
      <p>
         <label>website *</label>
         <input type="url" name="url" required="required" />
      </p>
      <p>
         <label>name *</label>
         <input type="text" name="name" pattern="[a-zA-Z ]{5,}" maxlength="30" />
      </p>
      <p>
         <label>age</label>
         <input type="number" name="age" size="4" min="5" max="50" />
      </p>
      <input type='hidden' name='submit' />
      
      <button type="submit">Email form</button>
      <button type="reset">Reset</button>
   </fieldset>

</form>


<script>
$("#myform").validator();
</script>

February 23rd, 2010

TOP PHP MYSQL SELECT QUERIES EXAMPLES

The SELECT query plays a very

important role when working

with PHP and MYSQL. SELECT

is used when you are trying to

retrieve data from the database.

In this blog I am providing four

examples of how to retrieve that

data in the most efficient and quick

way. If you are planning on using

MYSQL with PHP extension I would

recommend that you become familier

with those examples.

1) The following query retrieves data from one field of the specified row found

in the result set:


$query  = “SELECT itemid, name FROM item ORDER BY name”;

$result_query = mysql_query($query);

$itemid = mysql_result($result_query, 0, “itemid”);

$name = mysql_result($result_query, 0 , “name”);

2) The following query retrieves an entire row of data from result set, placing

the values in an indexed array:


$query  = “SELECT itemid, name FROM item ORDER BY name”;

$result_query = mysql_query($query);

while (list($itemid, $name) = mysql_fetch_row($result_query))

{

Echo “Item: $name ($itemid) &lt;br /&gt;”;

}

3) The following query retrieves results solely by numerical indeces:


$query  = “SELECT itemid, name FROM item ORDER BY name”;

$result_query = mysql_query($query);

while ($row = mysql_fetch_array($result_query, MYSQL_NUM))

{

$name = $row[1];

$itemid = $row[0];

Echo “Item:  $name ($itemid) &lt;br /&gt;”;

}

4) The following query is almost the same as the previews but uses objects

instead of array:


$query  = “SELECT itemid, name FROM item ORDER BY name”;

$result_query = mysql_query($query);

while ($row  = mysql_fetch_object($result_query))

{

$name = $row -&gt; name;

$itemid = $row -&gt; itemid;

Echo “Item:  $name ($itemid) &lt;br /&gt;”;

}

February 18th, 2010

GoDaddy .htaccess PHP RewriteRule RewriteEngine Rewrite rules For SEO URLS

This is a very common issue that

people run into when they decide to

switch to the GoDaddy’s amazingly

cheap hosting. I struggled with the

RewriteEngine after switching to GoDaddy

and finally came up with a simple fix. For

all of you that want to use the rewrite rules

in the .htaccess file for SEO purposes here is

the simple solution I came up with. By

default if you have a rewrite rule that has

the same name as the php file you will run

into trouble. The following example takes care of it.


########## Begin - Rewrite rules For SEO urls ##
#
Options -MultiViews
Options +FollowSymlinks

RewriteEngine on
#
#
#
#URL Rewriting for Videos
RewriteRule ^videos videos.php [nc]
########## End – Rewrite rules For SEO urls ##

December 19th, 2009

Best Coding And Debugging Guide

* Write small simple programs to test constructs you haven’t used before or about which you feel shaky. It’s easier to locate your misunderstanding in a short program than in a long one.

* When you’re debugging a program, pay attention only to the first error. The others may simply be side-effects of that first error.

* When you’re writing a program, keep the specification in front of you. How else can you be sure that you’re solving the correct problem?

* Don’t attempt to write out all the code at once and then try to debug it.

* Always build on existing code which compiles and runs.

* Grow programs incrementally. If you extend a working program and it doesn’t work anymore, then you can localize the error.

* Keep working checkpoint programs as you move along. If you seriously wound your code, you can retreat to a prior version which worked.

* Implement a large program in well-defined steps. Examine the problem and determine how you can implement the required capabilities in an incremental manner.

* Learn how to document errors:

· they must be repeatable;

· the code should be short;

· collect hardcopy of the program, the compile, and the test run.

* Human memory is fallible, and computers are unforgiving, so no one will believe you if you don’t have documentation. Experts can’t help you if you come in with a folktale — “I tried that, but it gave me an error.” You must be able to replicate the error.

* If you encounter an error that you can’t understand, then keep the code which generated the error and find out what was wrong. You may produce a working program by trying another approach, but if you don’t know why you got the error, then there’s a gap in your knowledge.

Write small simple programs to test constructs you haven’t used before or about which
you feel shaky. It’s easier to locate your misunderstanding in a short program than in a
long one.
When you’re debugging a program, pay attention only to the first error. The others may
simply be side-effects of that first error.
When you’re writing a program, keep the specification in front of you. How else can you
be sure that you’re solving the correct problem?
Don’t attempt to write out all the code at once and then try to debug it.
Always build on existing code which compiles and runs.
Grow programs incrementally. If you extend a working program and it doesn’t work
anymore, then you can localize the error.
Keep working checkpoint programs as you move along. If you seriously wound your
code, you can retreat to a prior version which worked.
Implement a large program in well-defined steps. Examine the problem and determine
how you can implement the required capabilities in an incremental manner.
Learn how to document errors:
· they must be repeatable;
· the code should be short;
· collect hardcopy of the program, the compile, and the test run.
Human memory is fallible, and computers are unforgiving, so no one will believe you if
you don’t have documentation. Experts can’t help you if you come in with a folktale —
“I tried that, but it gave me an error.” You must be able to replicate the error.
If you encounter an error that you can’t understand, then keep the code which generated
the error and find out what was wrong. You may produce a working program by trying
another approach, but if you don’t know why you got the error, then there’s a gap in your
knowledge of Java.
December 18th, 2009

PHP Copyright Date Script

This is one of the most basic scripts you can

ever create with PHP but also extreemly

useful for web site copyright notices. It

doesn’t take any effort to replace your

current static year with the following

script.

DEMO

© <?php echo date("Y"); ?> – eRunways

December 14th, 2009

PHP Form Copy Files Between Folders

PHP Form Copy Files Between Folders

PHP Form Copy Files Between Folders

This form is used to copy all the files from one

dir to seperate folders with x amount of files

each.  It gives you the option to choose

directory with the files, the path to the dir

where the files are going to (MAKE SURE

THIS EXISTS AND IS CHMOD 777,

the max number of pics or other files,

prefix, and file types to be

copied.

DEMO

GET PHP SOURCE CODE

GET HTML SOURCE CODE

November 21st, 2009

PHP + JavaScript System Info >> Simple script to provide information about the site visitor

This is a simple script that provides information about the site visitor.
* What version of JavaScript the vistor browser supports
* Visitor IP Address
* Visitor Hostmask
DEMO at erunways.com/ip

PHP JAVASCRIPT SYSTEM INFO

PHP JAVASCRIPT SYSTEM INFO