Wednesday, May 18, 2011

PHP - Alternate color divs using variable variables in a loop

If writing repetitive code, it is simple to instead loop thorugh an array, repeating the same code for each value. But to style it in a more interesting way than it all being the same, variable variables can be used to alter the style at each iteration of the loop...


<?php
$array = array("value one", "value two", "value three");
$color1="#e92e27";
$color2="#507FF7";
$color3="#e8e507";

$i="1";

foreach($array as $val)
{
if($i==4){$i="1";}

echo "<div style='width:150px;border:4px ".${color.$i}." solid;background-color:#ffffff;margin:5px;padding:5px;'>";

echo $val;

echo "</div>";
$i++;
}
?>

Any number of colors and any number of values can be added. This is useful if the list of values is long.

The result of the above code would look like this....



value one


value two


value three

Tuesday, May 17, 2011

PHP - Get Current Page URL

Returns variable $url as http://www.site.com/file.htm

//get current url
$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];

Monday, May 16, 2011

php - split mysql results into groups

When displaying a set of mysql results, sometimes it is practical to show them in groups, rather than one long list. This is useful for page design, so you can split results into divs or table cells.

Maybe mysql has a command for that, but so far I can only see that it can be done in php as follows...

(The following php code loops through a mysql result, and every five will be split and displayed into separate inline divs.)

<?php

//get the mysql resource
$result=mysql_query("SELECT column FROM table") or die(mysql_error());

//start $d at 0
$d=0;

//start first div before the loop
echo "<div style='width:150px;border:thin #FF00FF solid;float:left;display:inline;margin:5px;padding:5px;'>";

//loop through results incrementing $d and printing as per if conditions
while ($row = mysql_fetch_array($result))
{
$column=$row['column'];

//the first condition stops zero rows being shown in the first div
if($d== 0){
echo $column."<br />";
$d++;
}

//second condition prints the div code when $d is divisible by 5, so every 5 results
elseif($d % 5 == 0){
echo "</div><div style='width:150px;border:thin #FF00FF solid;float:left;display:inline;margin:5px;padding:5px;'>";
echo $column."<br />";
$d++;
}

//third condition prints results within the divs
elseif($d % 5 != 0){
echo $column."<br />";
$d++;
}
else{}

}//endwhile

//close the last div tag
echo "</div>";
?>


This php code loops through a mysql result, and every five will be split and displayed into separate inline divs.

Friday, May 06, 2011

Kate Middleton Pictures


I took some pictures of Kate Middleton on her wedding day by doing screen shots of the youtube live stream. I think she looks a little like Kate Perry in this one.

I put the rest on hairstylezone.com here ... Kate Middleton Hairstyles

Removing header from mysql output in bash

So I had been using a conditional statement to remove the column name from the output of a mysql query in bash. I found today that the header can be suppressed using --silent and --skip-column-names parameters in the mysql. As shown in mysql command options
##MYSQL QUERY##
variable=$(
mysql -sN -u username --password=password << eof use databasename; SELECT id FROM tablename; eof)


It is possible to alter a variable using parameter substitution as follows :
variable=${variable#id}
variable=${variable#*$'\n'}
variable="${x//$'id\n'}"
The 1st just removes the string "id", the 2nd removes everything up to the first new line and the 3rd removes all instances of the string "id" +newline. As suggested at Linux Questions


When returning the results in a for loop, the -n parameter for echo, removes the new line that is otherwise there.

##for each##
for ARG in $variable
do
echo -n $ARG
done