Posted on August 31, 2008, Filled under PHP,
Bookmark it
Here’s how you can calculate the percent from a value.
<?php
/* Credits: http://www.bitrepository.com */
// example: number of employees in a company's department (Marketing)
$value = '20';
// total employees that company has
$total = '500';
$percent = number_format(($value * 100) / $total);
// percent of employees from the Marketing Department.
echo $percent.'%';
?>
Posted on August 31, 2008, Filled under PHP,
Bookmark it
This short tutorial is aimed to help you how to calculate the percent value from a total. Let’s suppose you have a e-commerce site and you offer discounts to the customers. You need to show them how much is the discount value worthing.
Here’s a sample from an order total:
Notebook’s price: $1,300
Our special discount: 30% (-$390)
Final price: $910
Here’s the script which calculates the special discount value and the final price:
<?php
$percent = '30'; // without %
$total = '1300'; // initial value
/* Calculate $percent% from $total */
$discount_value = ($total / 100) * $percent;
$final_price = $total - $discount_value;
// Format numbers with number_format()
$total = number_format($total);
$discount_value = number_format($discount_value);
$final_price = number_format($final_price);
echo "Notebook's price: $".$total."<br />Our special discount: <strong>".
$percent."%</strong> (-$".$discount_value.")<br />Final price: $".$final_price;
?>
Any comments and suggestions regarding this script are welcomed.
Posted on August 30, 2008, Filled under PHP,
Bookmark it
To get the maximum id from a table you can use the MAX() function.
Here’s an example:
SELECT MAX(id_field_here) as max_id FROM `table`;
Posted on August 30, 2008, Filled under PHP,
Bookmark it
Hi,
The PHP notice errors are sometimes frustrating and you are tired of seeing them when you are working on your scripts. They are showed at the beggining of your pages and may reveal confidential information to the visitor like the path to the file or the php file name (in case you are using friendly URLs).
There are cases when they are useful (for instance when you defined a constant twice or when you use a variable that hasn’t been defined), but there are situations when everything works fine on your site and you don’t really need them. Here’s the code that you should use:
error_reporting (E_ALL ^ E_NOTICE); /* 1st line (recommended) */
NOTE: Many programmers make mistakes while working on large applications so if there are cases when something is not working right on your site and you hided the notice errors, consider turning them on. They might tell you some bugs in your scripts.
Happy coding!
Posted on August 30, 2008, Filled under PHP,
Bookmark it
An SQL command that selects the field’s values that end with a specific string.
SELECT * FROM `your_table_here` WHERE field_table REGEXP 'string_here$';
The symbol $ is telling MySQL to look at the ending of the value.
Posted on August 30, 2008, Filled under PHP,
Bookmark it
An SQL command that selects the field’s values that start with a specific string.
SELECT * FROM `your_table_here` WHERE field_table REGEXP '^string_here';
The symbol ^ is telling MySQL to look at the beginning of the value.
Posted on August 30, 2008, Filled under PHP,
Bookmark it
This tutorial is aimed to help you how to make an alphabetical search using MySQL. Many sites use it these day and we will show you how do they do it.
Let’s suppose you have a business directory and you want to display the listings that are starting with a specific letter or a non-letter character (numeric, etc).
Let’s start with the non-alphabetic selection in case you need to select listings starting with a number or a non-alphanumeric character. Here’s the command:
SELECT * FROM `listings` WHERE Left(listing_name, 1) REGEXP '^[^a-z]+$';
The regular expressions are used here. The MySQL command selects all listings that have a first non-alphabetical character.
Now let’s move on with the alphabetical selection.
We will choose the letter ‘A’ for our example. Here’s the command that will select all listings starting with ‘A’.
SELECT * FROM `listings` WHERE Left(listing_name, 1) = 'a'; -- case insensitive
You can replace ‘A’ with any letter you wish.
Congratulations! Now you know how to perform an alphabetical search with MySQL.
Posted on August 30, 2008, Filled under PHP,
Bookmark it
Greetings,
Ever wanted to add a preffix to a mysql field’s value? The CONCAT string function can help you. Here’s how you can do it:
UPDATE `your_table` SET field_name = CONCAT('my prefix here ', field_name)
where field_name='the field value here'; #optional condition
Example: You can use this command to add a prefix for a telephone number.
Good luck!
Posted on August 30, 2008, Filled under PHP,
Bookmark it
Greetings,
Ever wanted to add a suffix to a mysql field’s value? The CONCAT string function can help you. Here’s how you can do it:
UPDATE `your_table` SET field_name = CONCAT(field_name, ' my suffix here')
where field_name='the field value here'; #optional condition
So if the field’s value if “New York” and you add the suffix ” City” then our sql command will make the update and the new field’s value will be “New York City”. (notice the space added before the suffix. In our example it was necessary).
Good luck!