Posts Tagged php

PHP 101. Basic stuff: get value from URL querystring with PHP

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 ;?>" />

No Comments

Getting a page ID in WordPress

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.

,

No Comments

Passing a variable about in a WordPress theme

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; ?>

,

No Comments

Multiple WordPress excerpt lengths

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), ' ' ) ); ?>

 

,

No Comments

Turn a text list into an HTML Select Option list with PHP

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.

,

No Comments

CSS in PHP

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");
?>

,

No Comments

PHP web forms

Websites are a point of contact – and a contact form that sends an email is the way to normally handle enquiries. (Of course phone numbers/addresses must first be easy to find!) A contact form requires a web server to actually do something – ie send you an email – it’s not just displaying HTML code anymore. There’s options to get by without a script on your server – the excellent WuFoo online form builder is a great option here – but if you want a form as part of your page that you can style yourself it ain’t the ticket. So, knowing a little PHP, a PHP script is sought that:

  1. makes sure all input is cleaned and sanitized to avoid nasty things being done on/to your server
  2. checks for errors in the data without discarding the well formed data
  3. uses some form of CAPTCHA to keep those spam bots at bay
  4. sends email through an authorised SMTP email account (Gmail, for example, will either mark as spam or simply not accept mail that isn’t)

Other nice things to consider would be nice, cross-browser (HTML5) placeholder text (that doesn’t get submitted).

Finding a PHP web form framework

Last time I set up a form was a while ago – I’ve been doing mostly WordPress sites (or other CMS work) recently and have found the premium Gravity forms plugin for WordPress to work a treat. Anyway, cut to the chase already… the old script I had used  failed on points 3 & 4 above I’d like to say I can lean on PEAR PHP (which is already on my webserver) but I’m really not a programmer. I found a solution in the excellent PHP form “library” from Dagon Design. I’d never heard off it but it’s the first serious result for “PHP form script” in Google at the moment – so it  must at least be popular. More importantly it ticks all the boxes above (plus much more) and is well documented. Go get it. The only thing it doesn’t do is verify numerical input.

There’s no need for me to go into the setup at all – it is very well documented – just spend a half hour or so to read through it.

Adding placeholder text to the form framework

All I have to add is that I’ve edited the script a bit to set default text as HTML5 placeholder text (and remove default text from text inputs). The form is run in conjunction with Daniel Stocks’ HTML5 Placeholder Plugin for jQuery to make sure that the placeholder text works with IE etc.

To get the placeholder text working I changed the last few lines of the  below (from 754):

function ddfm_gen_text($item) {

	// type=text|class=|label=|fieldname=|max=|req=(TRUEFALSE)|[ver=]|[default=]

	global $form_submitted, $form_input, $show_required;

	$req_text = (($show_required) && ($item['req'] == 'true')) ? '<span class="required">' . DDFM_REQUIREDTAG . '</span> ' : '';

	$gen = "";
	$gen .= '<p class="fieldwrap"><label for="' . $item['fieldname'] . '">' . $req_text . $item['label'] . '</label>';
	$gen .= '<input class="' . $item['class'] . '" type="text" name="' . $item['fieldname'] . '" id="' . $item['fieldname'] . '" value="';

	if ($form_submitted) {
		$gen .= ddfm_bsafe($form_input[$item['fieldname']]);
	} else if (isset($item['default'])) {
		$gen .= ddfm_bsafe($item['default']);
	}

	$gen .= '" /></p>' . "\n\n";

	return $gen;
}

to:

	if ($form_submitted) {
		$gen .= ddfm_bsafe($form_input[$item['fieldname']]);
	}

	$gen .= '" placeholder="' . ddfm_bsafe($item['default']) . '" /></p>' . "\n\n";

	return $gen;

So what’s all the fuss about – well you can see the version I worked on, in action, as part of a website for this fancy (in the good sense) hair salon in Fuengirola

, ,

No Comments