Posts Tagged ‘variable’

Mysql-Avoid variable-length column types when necessary

Thursday, July 24th, 2008

For MyISAM tables that change frequently, you should try to avoid all variable-length columns (VARCHAR, BLOB, and TEXT). The table uses dynamic row format if it includes even a single variable-length column.

Php:Variable variables

Tuesday, July 1st, 2008

A variable variable looks like this: $$var

So, if $var = ‘foo’ and $foo = ‘bar’ then $$var would contain the value ‘bar’ because $$var can be
thought of as $’foo’ which is simply $foo which has the value ‘bar’.

Variable variables sound like a cryptic a useless concept, but they can be useful sometimes. For
example, if we have a configuration file consisting of configuration directives and values in this
format:

foo=bar
abc=123
Then it is very easy to read this file and create corresponding variables:

<?php
$fp = fopen(’config.txt’,'r’);
while(true) {
$line = fgets($fp,80);
if(!feof($fp)) {
if($line[0]==’#’ || strlen($line)<2) continue;
list($name,$val)=explode(’=',$line,2);
$$name=trim($val);
} else break;
}
fclose($fp);
?>
Along the same lines as variable variables, you can create compound variables and variable
functions.

<?php
$str = ‘var’;
$var_toaster = “Hello World”;
echo ${$str.’_toaster’};
$str(); // Calls a function named var()
${$str.’_abc’}(); // Calls a function named var_abc()
?>

PHP:Arrange an array by the first letter

Monday, June 30th, 2008

Here is a small PHP tips&tricks post : we have an array full of values, let’s say a country list (in this example the list is limited, we don’t want to see 190+ values).
PHP  CODE:

Array
(
[0] => Array
(
[0] => Afghanistan
[name] => Afghanistan
[1] => AF
[code] => AF
)

[1] => Array
(
[0] => Albania
[name] => Albania
[1] => AL
[code] => AL
)

[2] => Array
(
[0] => Algeria
[name] => Algeria
[1] => DZ
[code] => DZ
)

[3] => Array
(
[0] => Bangladesh
[name] => Bangladesh
[1] => BD
[code] => BD
)

[4] => Array
(
[0] => Barbados
[name] => Barbados
[1] => BB
[code] => BB
)

[5] => Array
(
[0] => Belgium
[name] => Belgium
[1] => BE
[code] => BE
)

[6] => Array
(
[0] => Brazil
[name] => Brazil
[1] => BR
[code] => BR
)

[7] => Array
(
[0] => Cape Verde
[name] => Cape Verde
[1] => CV
[code] => CV
)

[8] => Array
(
[0] => Cayman Islands
[name] => Cayman Islands
[1] => KY
[code] => KY
)
)

PHP CODE:

Array

(

[0] => Array

(

[0] => Afghanistan

[name] => Afghanistan

[1] => AF

[code] => AF

)

[1] => Array

(

[0] => Albania
[name] => Albania

[1] => AL

[code] => AL

)

[2] => Array

(

[0] => Algeria

[name] => Algeria

[1] => DZ

[code] => DZ

)

[3] => Array

(

[0] => Bangladesh

[name] => Bangladesh

[1] => BD

[code] => BD

)

[4] => Array

(

[0] => Barbados

[name] => Barbados

[1] => BB
[code] => BB

)

[5] => Array

(

[0] => Belgium

[name] => Belgium

[1] => BE

[code] => BE

)

[6] => Array

(

[0] => Brazil

[name] => Brazil

[1] => BR

[code] => BR

)

[7] => Array

(

[0] => Cape Verde

[name] => Cape Verde

[1] => CV

[code] => CV

)

[8] => Array

(

[0] => Cayman Islands

[name] => Cayman Islands

[1] => KY

[code] => KY

)

)

and we want to display like this
-A-

* Afghanistan
* Albania
* Algeria

-B-

* Bangladesh
* Barbados
* Belgium
* Brazil

-C-

* Cape Verde
* Cayman Islands

, the right and easy way to do this would be to retain in a variable the first letter of the last country and in another variable the first letter of the current country. We compare them and if they are different we just output the first letter and then the name of the current country. All this in just few and simple to understand lines, right? :P
PHP  CODE:

//try to do the A-Z list
$v = function to get your country list from a DB sorted by country name!;
$last_letter = ”;
$current_letter = ”;
for ($i=0;$i
” . $current_letter . ”

“;
}
print $v[$i]["name"] . “”;
$last_letter = $current_letter;
}

PHP CODE:

//try to do the A-Z list

$v = function to get your country list from a DB sorted by country name!;

$last_letter = ”;

$current_letter = ”;

for ($i=0;$i<count($v);$i++)

{

$current_letter = substr($v[$i]["name"],0,1);

if ($last_letter != $current_letter)

{

print “<div align=center>” . $current_letter . “</div>”;

}

print $v[$i]["name"] . “<br />”;

$last_letter = $current_letter;

}

PHP:Passing a variable from Javascript to PHP

Saturday, June 28th, 2008

Since Javascript is a client-side technology, and PHP is a server-side technology, the two languages cannot directly share variables. It is, however, possible to pass variables between the two. One way of accomplishing this is to generate Javascript code with PHP, and have the browser refresh itself, passing specific variables back to the PHP script. The example below shows precisely how to do this — it allows PHP code to capture screen height and width, something that is normally only possible on the client side.
<?php
if (isset($_GET['width']) AND isset($_GET['height'])) {
// output the geometry variables
echo “Screen width is: “. $_GET['width'] .”<br />\n”;
echo “Screen height is: “. $_GET['height'] .”<br />\n”;
} else {
// pass the geometry variables
// (preserve the original query string
// — post variables will need to handled differently)

echo “<script language=’javascript’>\n”;
echo “ location.href=\”${_SERVER['SCRIPT_NAME']}?${_SERVER['QUERY_STRING']}”
. “&width=\” + screen.width + \”&height=\” + screen.height;\n”;
echo “</script>\n”;
exit();
}
?>

Getting results from a select multiple HTML tag.

Saturday, June 28th, 2008

The select multiple tag in an HTML construct allows users to select multiple items from a list. These items are then passed to the action c for the form. The problem is that they are all passed with the same widget name. I.e.
<select name=”var” multiple=”yes”>
Each selected option will arrive at the action handler as var=option1, var=option2, var=option3. Each option will overwrite the contents of the previous $var variable. The solution is to use PHP’s “array from form element” feature. The following should be used:
<select name=”var[]” multiple=”yes”>
Now first item becomes $var[0], the next $var[1], etc.

Creating a PHP arrays in a HTML form

Saturday, June 28th, 2008

To get your FORM result sent as an array to your PHP script you name the INPUT, SELECT, TEXTAREA elements like this:
<input name=”MyArray[]“>
<input name=”MyArray[]“>
<input name=”MyArray[]“>
<input name=”MyArray[]“>

If you do not specify the keys, the array gets filled in the order the elements appear in the form. Above example will contain keys 0, 1, 2 and 3. Notice the square brackets after the variable name, that’s what makes it an array. You can group the elements into different arrays by assigning the same name to c elements:
<input name=”MyArray[]“>
<input name=”MyArray[]“>
<input name=”MyOtherArray[]“>
<input name=”MyOtherArray[]“>
This produces two arrays, MyArray and MyOtherArray, that gets sent to the PHP script. It’s also possible to assign specific keys to your arrays:
<input name=”AnotherArray[]“>
<input name=”AnotherArray[]“>
<input name=”AnotherArray[email]“>
<input name=”AnotherArray[phone]“>
The AnotherArray array will now contain the keys 0, 1, email and phone.