Script

Filter
2009-09-07

Validera datum

Php
Inbyggd datumvalidering i php
<?php
// Validate date...
$y = "2009";
$m = "09";
$d = "06";
if (checkdate($m, $d, $y)) {
    echo "$y-$m-$d is a date<br />";
} else {
    echo "$y-$m-$d is not a date<br />";
}
?> 
2009-09-07

DHTML för instant uppdatering

Javascript
Meny för att uppdatera innehållet på en sida direkt
<!-- Dynamisk html-meny med bild från databas -->
<script type="text/javascript">
    window.onload = function() {
        // assign change_postcard_image to select field
        var s = document.getElementById('postcard_select');
        s.onchange = change_postcard_image;
    }
    function change_postcard_image() {
        var s = document.getElementById('postcard_select');
        var i = document.getElementById('postcard');
        var x = s.options.selectedIndex;
        // update image's src and alt attributes
        i.src = s.options[x].value;
        i.alt = s.options[x].text;
    }
</script>
<form>
    <select id="postcard_select" name="postcard">
    <?php $sql = "SELECT image_url, description FROM pc_image ORDER BY description";
    $rs = mysql_query($sql, $conn) or die(mysql_error());
    while ($row = mysql_fetch_assoc($rs)) {
        echo "<option value=\"{$row['image_url']}\">{$row['description']}</option>";
    }
    mysql_free_result($rs); ?>
    </select>
</form> 
2009-09-07

Ignore insert i MySql

Mysql
Sätt in i databasen utan att kontrollera om det redan finns en liknande post
<?php
/*
The IGNORE keyword is a very cool option that allows you to do an insert without
first using a SELECT query to see if the data is already in the table. In this case,
you know there might already be a record for this zip code, so IGNORE tells the
query “If you see this zip code in the table already, then don’t do the INSERT.
The IGNORE statement compares primary keys only. Thus, using IGNORE when inserting
data into a table where the primary key is automatically incremented would have
no effect at all, because the INSERT will always happen in that case.
*/
$zip = mysql_real_escape_string($zip, $conn);
$city = mysql_real_escape_string($city, $conn);
$state = mysql_real_escape_string($state, $conn);
$query = "INSERT IGNORE INTO zipcodes (zip, city, state) VALUES ('$zip', '$city', '$state')";
mysql_query($query, $conn) or die (mysql_error($conn));
?> 
2009-09-07

Felhantering vid uppladdning

Php
Kolla felkonstanter i php vid uppladdning
if ($_FILES['uploadfile']['error'] != UPLOAD_ERR_OK) {
    switch ($_FILES['uploadfile']['error']) {
    case UPLOAD_ERR_INI_SIZE:
        die('The uploaded file exceeds the upload_max_filesize directive in php.ini.');
        break;
    case UPLOAD_ERR_FORM_SIZE:
        die('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.');
        break;
    case UPLOAD_ERR_PARTIAL:
        die('The uploaded file was only partially uploaded.');
        break;
    case UPLOAD_ERR_NO_FILE:
        die('No file was uploaded.');
        break;
    case UPLOAD_ERR_NO_TMP_DIR:
        die('The server is missing a temporary folder.');
        break;
    case UPLOAD_ERR_CANT_WRITE:
        die('The server failed to write the uploaded file to disk.');
        break;
    case UPLOAD_ERR_EXTENSION:
        die('File upload stopped by extension.');
        break;
    }
} 
2009-09-07

Maila direkt från PHP

Php
En enkel mail class
<?php
class SimpleMail {

    private $to;
    private $cc;
    private $bcc;
    private $from;
    private $subject;
    private $send_text; // bool
    private $text_body;
    private $send_html; // bool
    private $html_body;

    public function __construct() {
        $this->to = '';
        $this->cc = '';
        $this->bcc = '';
        $this->from = '';
        $this->subject = '';
        $this->send_text = true;
        $this->text_body = '';
        $this->send_html = false;
        $this->html_body = '';
    }

    public function setTo($value) {
        $this->to = $value;
    }

    public function setCc($value) {
        $this->cc = $value;
    }

    public function setBcc($value) {
        $this->bcc = $value;
    }

    public function setFrom($value) {
        $this->from = $value;
    }

    public function setSubject($value) {
        $this->subject = $value;
    }

    public function setSend_text($value) {
        $this->send_text = $value; // bool
    }

    public function setText_body($value) {
        $this->send_text = true;
        $this->text_body = $value;
    }

    public function setSend_html($value) {
        $this->send_html = $value; // bool
    }

    public function setHtml_body($value) {
        $this->send_html = true;
        $this->html_body = $value;
    }

    public function send($to = null, $subject = null, $message = null, $headers = null) {
        $success = false;
        if (!is_null($to) && !is_null($subject) && !is_null($message)) {
            // Quick message...
            $success = mail($to, $subject, $message, $headers);
            return $success;
        } else {
            $headers = array();
            if (!empty($this->from)) { $headers[] = 'From: ' . $this->from; }
            if (!empty($this->cc))   { $headers[] = 'CC: '   . $this->cc; }
            if (!empty($this->bcc))  { $headers[] = 'BCC: '  . $this->bcc; }
            if ($this->send_text && !$this->send_html) {
                // Only text message...
                $message = $this->text_body;
            } elseif (!$this->sendText && $this->sendHTML) {
                // Only html message...
                $headers[] = 'MIME-Version: 1.0';
                $headers[] = 'Content-type: text/html; charset="iso-8859-1"';
                $headers[] = 'Content-Transfer-Encoding: 7bit';
                $message = $this->html_body;
            } elseif ($this->send_text && $this->send_html) {
                // Both text and html message...
                $boundary = '==MP_Bound_xyccr948x==';
                
                $headers[] = 'MIME-Version: 1.0';
                $headers[] = 'Content-type: multipart/alternative; boundary="' . $boundary . '"';
                
                $message = 'This is a Multipart Message in MIME format.' . "\n" .
                '--' . $boundary . "\n" .
                'Content-type: text/plain; charset="iso-8859-1"' . "\n" .
                'Content-Transfer-Encoding: 7bit' . "\n" .
                "\n" .
                $this->text_body . "\n" .
                '--' . $boundary . "\n" .
                'Content-type: text/html; charset="iso-8859-1"' . "\n" .
                'Content-Transfer-Encoding: 7bit' . "\n\n" .
                $this->html_body . "\n" .
                '--' . $boundary . '--';
            }
            $success = mail($this->to, $this->subject, $message, join("\r\n", $headers));
            return $success;
        }
    }
}
?> 
🙂