Update Values of Entire Table using PHP
This code assumes you are connected to a MySQL database which has a table with Names and Emails. The idea is that it will output a table of every single value from that table,...
This code assumes you are connected to a MySQL database which has a table with Names and Emails. The idea is that it will output a table of every single value from that table,...
Technique #1 <?php function trunc($phrase, $max_words) { $phrase_array = explode(‘ ‘,$phrase); if(count($phrase_array) > $max_words && $max_words > 0) $phrase = implode(‘ ‘,array_slice($phrase_array, 0, $max_words)).’…’; return $phrase; } ?> Technique #2 function limit_words($words, $limit, $append...
The code uses PHP to resize an image (currently only jpeg). Using this method, the resized image is of much better quality than a browser-side resizing. The file size of the new downsized image...
Check if value is in array and outputs Yes or No <?php $names = array( ‘Bob’, ‘Jim’, ‘Mark’ ); echo ‘In Array? ‘; if (in_array(‘foo’, $names)) echo ‘Yes’; else echo ‘No’; ?>
formatcharacter Description Example returned values Day — — d Day of the month, 2 digits with leading zeros 01 to 31 D A textual representation of a day, three letters Mon through Sun j Day of the month...
$html = file_get_contents(‘http://www.example.com’); $dom = new DOMDocument(); @$dom->loadHTML($html); // grab all the on the page $xpath = new DOMXPath($dom); $hrefs = $xpath->evaluate(“/html/body//a”); for ($i = 0; $i < $hrefs->length; $i++) { $href = $hrefs->item($i);...
PHP CODE: function getMSIE6() { $userAgent = strtolower($_SERVER[“HTTP_USER_AGENT”]); if (ereg(“msie 6”, $userAgent) || ereg(“msie 5”, $userAgent)) { return true; } return false; }
The HTTP_X_REQUESTED_WITH header is sent by all recent browsers that support AJAX requests. if ( !empty($_SERVER[‘HTTP_X_REQUESTED_WITH’]) && strtolower($_SERVER[‘HTTP_X_REQUESTED_WITH’]) == ‘xmlhttprequest’ ) { # Ex. check the query and serve requested data
function concatenate($elements, $delimiter = ‘, ‘, $finalDelimiter = ‘ and ‘) { $lastElement = array_pop($elements); return join($delimiter, $elements) . $finalDelimiter . $lastElement; } Usage $array = array(‘John’, ‘Mary’, ‘Ishmal’); echo concatenate($array); // outputs “John,...
This code assumes you are connected to a MySQL database which has a table with Names and Emails. The idea is that it will output a table of every single value from that table,...
function getZeroPaddedNumber($value, $padding) { return str_pad($value, $padding, “0”, STR_PAD_LEFT); } Usage echo getZeroPaddedNumber(123, 4); // outputs “0123”
<?php function current_season() { // Locate the icons $icons = array( “spring” => “images/spring.png”, “summer” => “images/summer.png”, “autumn” => “images/autumn.png”, “winter” => “images/winter.png” ); // What is today’s date – number $day = date(“z”);...
Class to back up entire databases and email them out, or individual tables. <?php class Backup { /** * @var stores the options */ var $config; /** * @var stores the final sql dump...
$string = eregi_replace(‘([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})’,'<a href=”mailto:\\1″>\\1</a>’, $text); echo $string;
Current year only © <?php echo date(“Y”) ?> With start year © 2008-<?php echo date(“Y”) ?> Start date with error protection <?php function auto_copyright($year = ‘auto’){ ?> <?php if(intval($year) == ‘auto’){ $year = date(‘Y’);...
I prefer using one of the native XML extensions since they come bundled with PHP, are usually faster than all the 3rd party libs and give me all the control I need over the markup. DOM...
Start your CSS files with this PHP (and name it style.php): <?php ob_start (“ob_gzhandler”); header(“Content-type: text/css; charset: UTF-8”); header(“Cache-Control: must-revalidate”); $offset = 60 * 60 ; $ExpStr = “Expires: ” . gmdate(“D, d M...
Relying on the default value of an uninitialized variable is problematic in the case of including one file into another which uses the same variable name. It is also a major security risk with register_globalsturned on. E_NOTICE level error...
Use prepared statements and parameterized queries. These are SQL statements that are sent to and parsed by the database server separately from any parameters. This way it is impossible for an attacker to inject malicious...
A Radio Button is a way to restrict users to having only one choice. Examples are : Male/Female, Yes/No, or answers to surveys and quizzes. Here’s a simple from with just two radio buttons...
PHP is probably the most popular scripting language on the web. It is used to enhance web pages. With PHP, you can do things like create username and password login pages, check details from...
One of the main features in PHP is the ability to take user input and generate subsequent pages based on the input. In this page, we will introduce the mechanism by which data is...
<?php function build_calendar($month,$year,$dateArray) { // Create array containing abbreviations of days of week. $daysOfWeek = array(‘S’,’M’,’T’,’W’,’T’,’F’,’S’); // What is the first day of the month in question? $firstDayOfMonth = mktime(0,0,0,$month,1,$year); // How many days...
Here, on the Smashing Editorial team, we always try to meet the expectations of our readers. We do our best to avoid misunderstandings, and we try to spread knowedge and present only the best...