4 Apr, 2012
I’m working on a project that sends transactional emails from templates using variables in the %%varname%% format, but defines variables in another table as a lookup value, or query. So, I needed to match strings of type %%varname%% which screams regular expressions, but also needed varname to look up what to replace it with. So, instead of using multiple functions, I was able to do this with a callback and preg_replace_callback.
<?php
$string = "A complicated gentleman allow me to present,
Of all %%asdf%% the arts and faculties the terse embodiment,
He's a great arithmetician who can demonstrate with ease %%name%%
That two and two are three, or five, or anything you please;
An eminent Logician who can make it clear to you
That black is white – when looked at from the proper point of view;
A marvelous Philologist who'll undertake to show %%asdf%%
That 'yes' is but another and a neater form of 'no'.";
echo preg_replace_callback('(%%.*%%)',
create_function(
'$matches',
'return getValue($matches)'
),
$string
);
function getValue($var) {
return toupper(str_replace('%%', '', $var));
}
/* Outputs:
A complicated gentleman allow me to present,
Of all ASDF the arts and faculties the terse embodiment,
He's a great arithmetician who can demonstrate with ease NAME
That two and two are three, or five, or anything you please;
An eminent Logician who can make it clear to you
That black is white – when looked at from the proper point of view;
A marvelous Philologist who'll undertake to show ASDF
That 'yes' is but another and a neater form of 'no'.
*/
I use the function getValue to query the database and get the value of the variable. This function is pretty slick; now, PHP just needs to support callbacks that aren’t created as strings and we’ll be all set.
12 Mar, 2012
If you compare your site in multiple browsers and find that the margins are twice as big in IE6, you’ve run into a big called the double margin IE6 issue. A known issue, it happens when you style a margin onto a floated element. Luckily it’s an easy fix: add the style attribute “display:inline” to the column with margin.
Then your IE6 version should obey your stylistic vision. Unless of course you have more style problems, but at least this won’t be one of them…
2 Mar, 2012
One thing that comes up fairly frequently with SQL Server permissions is allowing execute permissions to all user created programmability on a database. This frequently needs to be done when migrating servers, allowing the user connecting to execute stored procedures. One way to do this is to set explicit permissions for all securables for each user. This is effective with a limited amount of stored procedures.
In doing so however, as the number of stored procedures increases, the feasibility of setting explicit permissions decreases. The above also requires that permissions be explicitly granted for newly created stored procedures.
Another way of doing so is to create a role (which we’ll call db_executor) and granting execute permissions to all programmability (or a well-defined subset if you prefer) to said role, then adding desired users to the role. Doing so looks like this:
CREATE ROLE db_executor
GRANT EXECUTE TO db_executor
EXEC SP_ADDROLEMEMBER 'db_executor', 'username'
The above:
- Creates the role db_executor
- Grants execute permissions to the role
- Adds the desired user to the role
Because roles exist within a database, this should be done in the context of the database desiring the programmability required.
29 Feb, 2012
I had to migrate a Magento store last week and since I haven’t had much experience with that framework, I was at a loss with where the database configuration file was located. No matter how hard I looked there wasn’t an apparent config file named config.php or any of the sort.
I did eventually find it in a very non-conspicuous place. The configurations are listed in /app/etc/local.xml. Not exactly a descriptive name for what I was looking for. You will then want to look for this block of code in that file:
<resources>
<db>
<table_prefix><![CDATA[]]></table_prefix>
</db>
<default_setup>
<connection>
<host><![CDATA[localhost]]></host>
<username><![CDATA[your_db_username]]></username>
<password><![CDATA[your_db_password]]></password>
<dbname><![CDATA[your_db_name]]></dbname>
<active>1</active>
</connection>
</default_setup>
</resources>
I need this data only to create the database on the new server. I am not sure if changing this file will actually change the configuration of the application.
27 Feb, 2012
Facebook fans pages that have been designed by the user stand out much more than if you just used the pages provided by Facebook. I.E Wall, Info, Friend Activity, Photos… However if you can design your own page, you can create content only viewable to those who “like” you page while promoting the “like” action to get more content. This is accomplished through Facebook applications. There are a lot of options, but one that I feel is very under rated is “Static HTML Plus”. It’s free, and they don’t make you have their logo forever imprinted upon your material for it being free.
So after you created your page, you can install the application below to start making tabs:
https://apps.facebook.com/static_html_plus/
You can get more tabs by using the following applications:
https://apps.facebook.com/static_html_two/
https://apps.facebook.com/static_html_three/
https://apps.facebook.com/static_html_four/
13 Feb, 2012
I’ve been working with Google’s Tasks API and was looking to have a way a user would only need to authenticate their account once. Google does not explain very well on how to achieve this.
With php you will need to get the api setup HERE. You then need to then setup up an authentication stream:
require_once 'src/apiClient.php';
require_once 'src/contrib/apiTasksService.php';
$client = new apiClient();
$tasksService = new apiTasksService($client);
On the callback of the user allowing your app to access their tasks, you will get an access token and more importantly here a refresh token.
$auth = $client->authenticate();
$token = $client->getAccessToken();
$refresh_token = $token['refresh_token'];
Save the refresh token to somewhere you are able to access it again whenever you’d like. After that we can use that token to get the users tasks at any moment.
$client->refresh_token($refresh_token);
31 Jan, 2012
In a project I’ve been working on I needed to get a bunch of different stores of data and then have a separate store that would contain all of them combined. This was to save on ajax calls. I couldn’t find anything as far as native methods that would merge stores but I did come up with a solution.
var emptyArr = [];
var aggregatedStore = new Ext.data.ArrayStore({
data: emptyArr,
fields: [{name: 'field1'},{name: 'field2'}]
});
var singularStore = new Ext.data.JsonStore({
autoload:true,
proxy: {
type: 'ajax',
url: '/getDataPlease',
reader: {
type: 'json',
root: 'fieldRoot'
}
},
fields: [{name: 'field1'},{name: 'field2'}]
});
So what I want to do here is push the contents of singularStore into aggregatedStore. What I ended up doing is adding a listener on singularStore load to accomplish this:
var emptyArr = [];
var aggregatedStore = new Ext.data.ArrayStore({
data: emptyArr,
fields: [{name: 'field1'},{name: 'field2'}]
});
var singularStore = new Ext.data.JsonStore({
autoload:true,
proxy: {
type: 'ajax',
url: '/getDataPlease',
reader: {
type: 'json',
root: 'fieldRoot'
}
},
fields: [{name: 'field1'},{name: 'field2'}],
listeners: {
'load': function(store, records, successful) {
aggregatedStore.loadData(records,true);
}
}
});
Now I can use singularStore to get just the data from that url. I can then add that load listener to any other store and use aggregatedStore to show the combined data.
31 Jan, 2012
The biggest change in PHP 5.4 is the addition of traits to the object-oriented programmability. Traits prevent code duplication by allowing multiple classes to include a collection of methods implemented as a trait. This means multiple classes can include the same method implementations without duplicating code.
Traits improve on interfaces because they include not only the function definition, but also the implementation. Traits can also use other traits, meaning that one trait could be a collection of others. So, if you had traits Roll and Bounce, you could have trait Movements that used both Roll and Bounce.
trait HelloWorld {
public function sayHello() {
echo 'Hello ';
}
public function sayWorld() {
echo 'World!';
}
}
class MyClass {
use HelloWorld;
public function sayHelloWorld() {
$this->sayhello();
$this->sayWorld();
}
}
$h = new MyClass();
$h->sayHelloWorld(); // outputs "Hello World!"
In implementing traits, the keywords trait and insteadof were added to PHP. Usage of trait is outlined above. The PHP keyword insteadof is used for conflict resolution. Here’s an example of how to use insteadof:
trait A {
public function smallTalk() {
echo 'a';
}
public function bigTalk() {
echo 'A';
}
}
trait B {
public function smallTalk() {
echo 'b';
}
public function bigTalk() {
echo 'B';
}
}
class Talker {
use A, B {
B::smallTalk insteadof A;
A::bigTalk insteadof B;
}
}
class Aliased_Talker {
use A, B {
B::smallTalk insteadof A;
A::bigTalk insteadof B;
B::bigTalk as talk;
}
}
30 Jan, 2012
On a previous post I told you all about this amazing plugin I found that would create a slider that takes advantage of the iPad’s touch and drag functionality.
However, if you didn’t use it just for images and had the slides contain text, you may have noticed that the links within the slides would not work. This plugin appears as it was mainly created to swipe through images, and not necessarily for content with text links.
I went into the plugin (jquery.iphone-slide.js) and changed a few things so it would do my bidding:
OLD CODE:
helpers.goto_url(thislink);
NEW CODE:
var thislink = $(event.target).parent("a").attr("href");
if(!thislink){var thislink = $(event.target).attr("href");}
if(thislink){
window.location(thislink); //similar behavior as clicking on a link
}
Turns out the __mouseup function was preventing the default action and the “helpers.goto_url” function only opened image links. But now it’s set up to do everything, Cheers!
23 Jan, 2012
CSS has continued to simplify common design operations and CSS3 doesn’t show any signs of that trend changing. Gradients, which have been around for awhile now have commonly replaced the old way of creating custom images for a couple quick css lines of code.
.orange-gradient
{
background: #f78d1d; /* initial background for default */
background: -webkit-gradient(linear, left top, left bottom, from(#faa51a), to(#f47a20)); /*Webkit Chrome/Safari etc.. */
background: -moz-linear-gradient(top, #faa51a, #f47a20); /*mozilla*/
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#faa51a', endColorstr='#f47a20'); /* IE */
}
This would make a great orange gradient for a background. Another great trick is to reverse the gradient on hover
.orange-gradient:hover
{
background: -webkit-gradient(linear, left top, left bottom, from(#f47a20), to(#faa51a));
background: -moz-linear-gradient(top, #f47a20, #faa51a);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#f47a20', endColorstr='#faa51a');
}
Add this class to a button or link, add some padding and you have a quick and great-looking button.