{blog}


  • (PHP) Simple gallery with thumbnail generation

    This is an enhanced version of this

    It’s a VERY simple php gallery. You could develop it in any direction. This one only supports jpg, but that could be augmented as well.
    Here’s what it does:

    • Allows the user to choose an image and enter a title (or caption).
    • Uploads the image to a folder on the server.
    • Creates a thumbnail
    • Enters the filename and caption in a MySQL database.
    • Output all the images.

    (more…)


  • Open weblinks in external browser when using Phonegap

    To prevent a Phonegap-wrapped app from opening external links in the apps webview, use javascript to open the link in the external browser.

    This works on Android (probably other platforms as well)

    <a href="#" data-rel="external" target="_blank" onclick="window.open('http://www.jymden.com', '_system')">Jymden</a>
    HTML

  • Convert seconds to days, hours, minutes and seconds

    Converting seconds into days, hours, minutes and seconds

    int days = secondsTotal / 60 / 60 / 24;
    int hours = (secondsTotal / 60 / 60) % 24;
    int minutes = (secondsTotal / 60) % 60;
    int seconds = secondsTotal % 60;


  • (C#) Confirmation when closing a form

    Add an event handler for Form Closing and use this code. This is a really simple solution.
    [c language=”#”]
    private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
    {
    if (MessageBox.Show("Are you sure?", "Close application", MessageBoxButtons.YesNo) == DialogResult.Yes)
    e.Cancel = false;
    else
    e.Cancel = true;
    }
    [/c]


  • Simple function to see if a number is Even or Odd.

    Sometimes you need to check if a number is even or odd. A simple way to do this is using the modulus operator (the %-sign). It will calculate and return the remainder when dividing a value with another value.

    So how do you use that to find out if a number is even or odd?

     

    (more…)


  • (C#) Scroll to bottom of a textbox

    Short snippet:
    [c language=”#”]
    textBox.SelectionStart = textBox.TextLength;
    textBox.ScrollToCaret();
    [/c]


  • (PHP) Simple e-mail contact form.

    A simple contact form, just for reference. Will add more info later on…
    [php]
    <?php
    if (isset($_POST[‘submit’])) {

    $name = $_POST[‘name’]; //name of sender
    $email = $_POST[’email’]; //e-mail of sender
    $email = stripslashes($email);

    $subject = $_POST[‘subject’]; //e-mail subject
    $subject = str_replace(array("\r\n","\r","\n"), "", $subject); //remove any linebreaks to keep from code injections

    $message = $_POST[‘message’]; // e-mail message
    $message = str_replace(array("\r\n", "\r"), "\n", $message); // fix line-breaks
    $message = stripslashes($message);

    //headers
    $headers = "From:$name<$email>\r\n";
    $headers .= "Return-path: <$email>\r\n";
    $headers .= "Content-type: text/plain; charset=UTF-8\r\n";
    $headers .= "Content-Transfer-Encoding: 8bit\r\n";

    $to = ‘myadress@mydomain.com’; //recipients e-mail adress

    //validate email and name variables
    if (!filter_var($email, FILTER_VALIDATE_EMAIL))
    {
    echo ‘not a valid e-mail adress’;
    die();
    }
    if (preg_match(‘/[^a-z åäöÅÄÖüÜ _-]/i’, $name))
    {
    echo ‘not a valid name format’;
    die();
    }

    //send the email
    $send = mail($to, $subject, $message, $headers);
    if ($send)
    echo ’email sent’;
    else
    echo ‘something went wrong, email not sent’;
    }
    ?>

    <!DOCTYPE HTML>
    <html>
    <head>
    <meta charset="utf-8">
    </head>
    <body>
    <form action="" method="post">
    name:<br>
    <input type="text" name="name"/>
    <br>email:<br>
    <input type="text" name="email"/>
    <br>subject:<br>
    <input type="text" name="subject"/>
    <br>message:<br>
    <textarea name="message"/></textarea>
    <br>
    <input type="submit" name="submit"/>
    </form>
    </body>
    </html>
    [/php]


  • A couple of principles when learning how to code.

    Principle 1.
    Always have a clear image of what you want to do BEFORE you start writing your code. Then break it down in logical instructions – making an algorithm.  An algorithm is a step by step instruction on how to solve a specific problem. You might think of it as a recipe. If you want to bake bread – you don’t just start shoving any eatable stuff you find into the oven without a plan. First you decide what kind of bread you want to make, then you find out what ingredients is needed, how much and in what order they are added.

    So basically – if you can’t solve the problem with logic you can’t solve it with code.

    For example:

    (more…)


  • Hide elements with radio buttons

    Sometimes you want to show or hide certain elements of a form, depending on the selection of a radiobutton.

     

    I am by no means any javascript ninja, but this is one simple way I came up with:

    <html>
    <head>
        <script type="text/javascript">
            function displayElement(element) {
                x = element.value;
                if (x === 1) {
                document.getElementById('text').style.display = '';
              } else {
                document.getElementById('text').style.display = 'none';
              }
            }
        </script>
    </head>
    
    <body>
    <form name="form">
        <input name="choice" type="radio" value="1" onclick="DisplayElement(this)" checked/>
        <input name="choice" type="radio" value="2" onclick="DisplayElement(this)"/>
        <input name="text" type="text" id="text"/>
    </form>
    </body>
    </html>
    
    HTML

     
    This could apply to any element. For example, you could put a whole bunch of elements in a div and the hide/show the div. Like this:

    [html]

    <script type=”text/javascript”>
    function DisplayElement(element)
    {
    x = element.value;
    if (x == 1) {
    document.getElementById(‘container’).style.display = ”;
    }
    else {
    document.getElementById(‘container’).style.display = ‘none’;
    }
    }
    </script>

    <form name=”form”>
    <input name=”choice” type=”radio” value=”1″ onclick=”DisplayElement(this)” checked/>
    <input name=”choice” type=”radio” value=”2″ onclick=”DisplayElement(this)”/>
    <div id=”container”>
    <input type=”checkbox”/>
    <input type=”checkbox”/>
    <input type=”text”/>
    <input type=”text”/>
    <input type=”text”/>
    </div>
    </form>
    [/html]

     
     
    Here’s another way to do it. This example takes a number as a parameter instead of using the value of the element. A bit more compact code and it might be more flexible.

    [html]
    <script type=”text/javascript”>
    function DisplayElement(x)
    {
    if (x == 1) {
    document.getElementById(‘text’).style.display = ”;
    }
    else {
    document.getElementById(‘text’).style.display = ‘none’;
    }
    }
    </script>

    <form name=”form”>
    <input name=”choice” type=”radio” onclick=”DisplayElement(1)” checked/>
    <input name=”choice” type=”radio” onclick=”DisplayElement(2)”/>
    <input name=”text” type=”text” id=”text”/>
    </form>
    [/html]

    And here’s an even more flexible version. In this example you can pass the id of the element you want to hide as a second argument to the function. This way you can use the same function to toggle the visibility of many different elements.

    [html]
    <script type=”text/javascript”>
    function DisplayElement(x,target)
    {
    if (x == 1) {
    document.getElementById(target).style.display = ”;
    }
    else {
    document.getElementById(target).style.display = ‘none’;
    }
    }
    </script>

    <form name=”form”>
    <input name=”choice” type=”radio” onclick=”DisplayElement(1,’text’)” checked/>
    <input name=”choice” type=”radio” onclick=”DisplayElement(2,’text’)”/>
    <input name=”text” type=”text” id=”text”/>
    </form>
    [/html]

    Here is another post, to do the same thing with a checkbox.


  • Hide elements with checkbox

    This is a slightly edited version of this post. Just to make it work with a checkbox instead of radio buttons.

     

     

    <html>
    <head>
    <script type="text/javascript">
    function DisplayElement(checked)
    {
      if (checked) {
        document.getElementById('text').style.display = '';
      }
      else {
        document.getElementById('text').style.display = 'none';
      }
    }
    </script>
    </head>
    <body>
    <form name="form">
    <input type="checkbox" onclick="DisplayElement(this.checked)" checked/>
    <input type="text" id="text"/>
    </form>
    </body>
    </html>
    HTML