Posts Tagged php
Dynamically create a PHP variable name
Posted by admin in Web design & development on June 29, 2012
Thanks to this post “Dynamic variable names in PHP” for enlightening me on how easy this is. There’s a lot of stuff out there about PHP variable variables but if you just want to craft a variable’s name based on some other variables that are already set – this is the way to go.
$var = "FooBar";
$extended_FooBar = "</pre>
<h2>An Extension of FooBar</h2>
<pre>
";
echo ${"extended_{$var}"} ;
Basically a regular variable with lots of curly braces and – in this case – not a dot concatenation operator in sight.
So simple when you know how.
PHP 101. Basic stuff: get value from URL querystring with PHP
Posted by admin in Web design & development on July 11, 2011
As I’m still an idiot when it comes to media queries and mobile devices and can pass this stuff on to other folks – but needed to give myself and the interested parties a visual representation of how different sized graphics will be delivered to different mobile devices… I was after a simple ‘switch’ that would allow a single wedge of HTML to render according to a variable.
The example here goes along the lines of http://example.com?width=340 where the width has a few values it accepts or will default to a preset size…
This variable can then get passed around in the PHP file to set image dimaensions CSS etc…
<?php
function getUrlStringValue($urlStringName, $returnIfNotSet) { if(isset($_GET[$urlStringName]) && $_GET[$urlStringName] != "") return $_GET[$urlStringName]; else return $returnIfNotSet; }
$width = getUrlStringValue("width", "480");
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head><title></title><meta name="viewport" content="width=<?php echo $width ;?>" />
Getting a page ID in WordPress
Posted by admin in Web design & development on April 28, 2011
Q. Want to get something in your theme to work with data from a particular page or post – but don’t know for sure what that post’s ID is?
A. Try this in your functions.php file:
$my_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = 'myPost'");
…then you can then pass a variable about in your theme’s templates.
Passing a variable about in a WordPress theme
Posted by admin in Web design & development on April 28, 2011
I’m used to using php variables set in included file being used in another included file if all are included in the same document. This doesn’t work in WordPress theme parts. It’s easy to overcome though… you just need to recall the variable first…
somewhere in functions.php
$my_var = "xyz"
Now i can do this somewhere in header.php
<?php global $myVar ; ?> <?php echo $myVar; ?>
Multiple WordPress excerpt lengths
Posted by admin in Web design & development on April 28, 2011
Q. Got a theme that needs two different excerpt lengths?
A. Found here: set the standard, longer excerpt as normal in the functions.php file and the override locally using this code:
<?php echo substr( get_the_excerpt(), 0, strrpos( substr( get_the_excerpt(), 0, 75), ' ' ) ); ?>
Turn a text list into an HTML Select Option list with PHP
Posted by admin in Asides, Web design & development on February 3, 2011
Easy solution to this basic HTML coding need posted over at Stack Overflow by a random clever bloke. Thanks RCB.
<?php
$fc = file_get_contents('./test.txt');
$fc = utf8_encode ( $fc );
$lines = explode("\n", $fc);
$html = '<select>';
foreach($lines as $line)
$html .= '<option value="' . $line . '">' . $line . '</option>';
$html .= '</select>';
echo $html;
?>
My only issue was with UTF-8 characters coming through garbled. Bodged a workaround using this encode entities plug-in for Dreamweaver.
CSS in PHP
Posted by admin in Web design & development on February 2, 2011
The last time I tried to render CSS through PHP was many, many moons ago. I didn’t work and I couldn’t figure it out. Yesterday I tried again and found out why… the MIME type must be set as text/css. D’oh. Set MIME type like this:
< ?php
header("Content-Type: text/css");
?>