Using Fuelux spinner with form

If you want to use Fuelux spinner with <form> tag, you have to do two things:

  1. Add type="button" attribute to ‘up’ and ‘down’ buttons. Form will not send when you click the buttons.
  2. Add following script to your javascript file:

    $('.spinner').each(function(i, item) {
       var val = parseInt($(this).find('.spinner-input').val());
       $(this).spinner('value', val);
    });

    It will set values to Fuelux spinner and won’t count it from 1 but from input value.
Advertisements

PHP WTF foreach loop

I have worked with PHP for a few years but recently i found a big WTF factor for me as a former Java developer. Foreach loop in PHP has not its own scope! It means this:

<?php

$name = 'Peter';
echo '$name = "'. $name . '"; before foreach.<hr>';

$names = array('Mark', 'Peter', 'John');

foreach($names as $name) {
   echo '$name = "'. $name .'"; in foreach.<br>';
}

echo '<hr>$name = "'. $name .'"; after foreach<br>';

will result in:

$name = "Peter"; before foreach.
---------------------------------------------------------
$name = "Mark"; in foreach.
$name = "Peter"; in foreach.
$name = "John"; in foreach.
---------------------------------------------------------
$name = "John"; after foreach

It means the $name variable is rewritten by foreach and it’s BAD!!!
What if i want to say “Hi Peter” after this foreach and instead of it, it will say “Hi John”.
Any suggestions how to avoid this?