Blog

Newest Post
Blog Archives
Blog Categories

Portfolio

3D Models
Banners
Brochures
Business Cards
Custom Programming
Environments
Flash Projects
Game Design
Image Altering
Web Design

About

Resume
Recommendation Letters

Contact

November 12, 2009

Use PHP to proportionally scale an image

Filed under: PHP,The Coding World — admin @ 3:06 pm

I wrote the following function to proportionally scale images that I was pulling from a directory.

 
function img_size($img,$alt) {
 
  list($width,$height) = getimagesize($img);
 
  $maxwidth = 140; //set max width here
 
  if ($width > $maxwidth) {
	$i = $width - $maxwidth;
	$x = $width - $i;
	$percent = $maxwidth/$width;
  }
  else
  {
    $x = $width;
    $percent = 1;
  }
 
  $y = $percent * $height;
 
  $img_src = '<img src="'.$img.'" width="'.$x.'" height="'.$y.'" alt="'.$alt.'" border="0" />';
 
  return $img_src;
}
 
echo img_size('http://somesite.com/image.jpg', 'some alt text'); 
//result <img src="http://somesite.com/image.jpg" width="140" height="(proportionate to 140)" alt="(alt)" border="0">

October 22, 2009

Latitude and Longitude of a location using PHP and Google

Filed under: PHP,The Coding World — admin @ 12:55 pm

The following PHP code will help you acquire the latitude and longitude of a location (using an address, zip code, or city).

Note: Your will be required to have your own domain specific Google API key.

if ($_POST['address']) {
 
$key = ' '; //you will need yo use your own domain specific API key
 
$address = stripslashes($_POST['address']);
 
$addressurl = urlencode($address);
 
$location = file("http://maps.google.com/maps/geo?q=".$addressurl."&amp;output=csv&amp;key=".$key);
 
list ($stat,$acc,$lat,$lng) = explode(",",$location[0]);
 
echo "LAT:".$lat." - LNG:".$lng;
 
}

Below is the HTML code which used a form to submit the address to the above PHP.

<form action="<?php echo $_SERVER['PHP_SELF']; ?>;" method="post">
<input name="address" type="text" />
<input type="submit" value="Submit" />
</form>

February 9, 2009

Format Phone Number Fields with PHP

Filed under: PHP,The Coding World — admin @ 1:39 pm

Below is a function I wrote to take a phone number entered by a user and convert it into a desired format. The one shown below is 555-555-5555.

function phn_numb($numb) {
if (!is_numeric(substr($numb, 0, 1)) &amp;&amp; !is_numeric(substr($numb, 1, 1))) { return $numb; }
 
$chars = array(' ', '(', ')', '-', '.');
$numb = str_replace($chars, "", $numb);
if (strlen($numb) &gt; 10) {
//you must dial "1" before dialing this number
$numb = substr($numb, 0, 1).'-'.substr($numb, 1, 3).'-'.substr($numb, 4, 3).'-'.substr($numb, 7, 4);
}
else
{
$numb = substr($numb, 0, 3).'-'.substr($numb, 3, 3).'-'.substr($numb, 5, 4);
}
 
return $numb;
}

Therefore to see it in action:

$phonenumber = 2345678901;
 
echo phn_num($phonenumber);
 
//the resulting echo would be 234-567-8901

August 5, 2008

Javascript broke with multiple line variables from PHP

Filed under: JavaScript,PHP,The Coding World — admin @ 7:26 am

I have several sites where I use a pop up table to display information if the user rolls over and image. Example, if the user wants to view notes on a certain client, they can roll over a little “note” image to view the notes about the client pulled from a SQL database. The code I use for a message may be:

View Code JAVASCRIPT
note[0] = new Array("","<?php echo $message; ?>;");

That would work if $message was all one line. But as with all notes fields, you can never determine when the person entering data hits “Enter”. If the person did hit enter, your javascript would be broke and it would look like this in the source code.

View Code JAVASCRIPT
note[0] = new Array("","This is the start of the note field. I hit enter after this sentence twice.
 
Then I started typeing again");

This broke the Javascript. To fix this you can just swap the hidden special characters with a html line break.

Working code:

View Code JAVASCRIPT
note[0] = new Array("","<?php echo str_replace("\r\n","<br>;", $message); ?>;");

This would return:

View Code JAVASCRIPT
note[0] = new Array("","This is the start of the note field. I hit enter after this sentence twice<br><br>;Then I started typeing again");

May 23, 2008

PHP Dynamic Variables

Filed under: PHP,The Coding World — admin @ 9:21 am

Let’s say that you have a bunch of data in a recordset and you want to populate that data into a list of variables. In my example we will draw 20 items from a players inventory from a MySQL table which stores all the items for a player in rows we call “slot_1, slot_2, slot_3, … all the way to 20″. So when we pull all of the items from the MySQL table we want to assign them to variables respectively. For example, our variable of “slot_1″ that we are creating will equal “slot_1″ from the MySQL table.

//the recordset
 
mysql_select_db($database_connect, $connect);
$query_Recordset1 = sprintf("SELECT * FROM inventory WHERE player_id='$hero_id'");
$Recordset1 = mysql_query($query_Recordset1, $connect) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
 
//Loop and assign the variables accordingly
 
$i=1; do {
${"slot_{$i}"} = $row_Recordset1['slot_'.$i];
$i++;} while ($i&lt;=20);

If we were to update the MySQL table with a dynamic variable, it would look like this.

 
$updateItemSQL = sprintf("UPDATE inventory SET slot_$slot=%s WHERE player_id='$hero_id'",
GetSQLValueString($new_item, "int"));

March 25, 2008

MYSQL JOINS

Filed under: PHP — admin @ 6:07 am

MYSQL Joins are confusing, to say the least. I was successfully able to do one a few different ways and thought it be best to write down how I did it so I can do it in the future.

What I had was two different tables. One table was xcart_products_categories which stores all “productid” and matches them with which category they go to “categoryid”. The other table which was xcart_products had the “productid” and all of its information.

So if you are wanting to list all of the products from a certain category only, which I needed to list all the charms in the charms category (#344), you would need to use a MYSQL join.

Here is how I did it:

View Code MYSQL
SELECT xcart_products.*, xcart_products_categories.*
FROM xcart_products_categories 
LEFT JOIN xcart_products ON xcart_products_categories.productid = xcart_products.productid  
WHERE xcart_products_categories.categoryid='344'

There is a reason I did a LEFT JOIN. If I did a regular JOIN or an INNER JOIN then I would only get the products from “xcart_products” that matched on the categoryid, but I even wanted the ones that may have had a blank productid just in case I was retarded and forgot to mark one.

JOIN three tables

Now I needed to join three tables. The actual price of the product was in a third table called “xcart_pricing”. Here is how I joined all three tables.

View Code MYSQL
SELECT xcart_products.*, xcart_products_categories.*, xcart_pricing.price
FROM (xcart_products_categories LEFT JOIN xcart_products ON xcart_products_categories.productid = xcart_products.productid)
INNER JOIN xcart_pricing ON xcart_products.productid = xcart_pricing.productid 
WHERE xcart_products_categories.categoryid='344'

I did a standard INNER JOIN for the pricing table because I only needed the ones with for the productid that matched in both tables. I didn’t need any extras.

March 19, 2008

Update a mysql database and retain special characters

Filed under: PHP — admin @ 10:33 am

To update a mysql database and retain special characters such as possessiveness, or if you are updating a database where you field contains any special characters you will need to use the “addslashes” function to your mysql statement.

$updateSQL = sprintf("UPDATE db_table SET db_field=%s WHERE db_row='".addslashes($title)."'",
GetSQLValueString($value, "text"));

February 22, 2008

Reset a php Recordset

Filed under: PHP — admin @ 4:34 pm

When you run a recordset through a loop, you will not be able to re-run the recordset further down in the same page. Here is how to reset the recordset so you can use it again on the same page. Just place this code after your loop. Change the “$Recordset” to your recordset’s variable.

if (mysql_num_rows($Recordset) > 0)  {
mysql_data_seek($Recordset, 0);
$row_Recordset = mysql_fetch_assoc($Recordset);
}

November 22, 2007

Get Page Name from Web Address using PHP

Filed under: PHP — admin @ 12:55 pm

This syntax is useful for dynamically creating web pages. For example with PHP you can recognize the page you are on and then display a proper header for that page.

i.e. let’s say the weblink is “www.page.com/form.php”

$page_name =  basename($_SERVER['PHP_SELF'],".php");

The “$page_name” would equal “form” for the .php was dropped.

Capitalize the First Letters in a String with PHP

Filed under: PHP — admin @ 12:48 pm

This is a useful syntax just another one that just slips my mind.

$string = 'john doe';
$new_string = ucwords($string);
echo $new_string; //John Doe
Next Page »
© - Digital Epiphany Designs, Online Portfolio for Web Design, Custom Programming, Grapic Design and Game Design.