Archiv für die Kategorie „PHP“

DaDaBIK Revised

Montag, 8. März 2010

DaDaBik is a “Database Interfaces Kreator”, which helps you building web-interfaces to a database easily. It has been developed by Eugenio Tacchini. Since this is a rather old, yet working, piece of software, no wonder there’s a lot of PHP4 involved :O

I did a redesign of the template in late 2009 and took some time this weekend to get it polished up a bit. What I like about DaDaBIK is that you can provide data editing functionality and are able to adjust the look to a clients CI quickly (just replace one logo and change some colours - the PSD is included).

  • Insert data
  • Login
  • Listing

Although the code of DaDaBIK is from another pre-PHP5-decade, I didn’t see much of a need to change the inner workings of DaDaBIK. So what I did is putting some fresh paint on it using CSS. Since there is no template-engine imlemented into DaDaBik, I had to edit core files here and there for simple markup changes. So! The only thing that makes sense is to offer one complete package here: DaDaBik itself and the new design included.

Installation:
If you’re about to use DaDaBIK for the first time, you might want to take a look at www.dadabik.org as well, since there’s a documentation online giving you more insight. Anyways, the setup is quite easy:

  1. Download the archive
  2. Extract the files, and upload the dadabik folder to your server.
  3. Edit the file includes/config.php and enter your DB settings according to your setup.
  4. Open up the URL yourdomain.com/pathtodadabik/install.php.
  5. DaDaBIK will now set up some internal tables and you’re done.

Notes:
It’s important that you have created the database, you entered in the config.php file first. (for example using phpmyadmin).
If you want to, you can use a pre-made MySQL table structure with some dummy entries that I have included in the zip: just copy and paste the contents of dadabik-example-db.sql into the SQL window of the new database in phpmyadmin.

Once you’re done with the basic installation, head over to the admin.php which gives you all the admin options you need to include/remove secific tables and set human readable aliases for the tables.

The last step to perfect interfaces is to click the Interface configurator link on that admin page.
The configurator gives you all the options to customize the appearance, set default values, define which fields are mandatory, etc. etc.

Demo:
Check out the demo of DaDaBIK with the new design.
You’ll have to enter username test and password: test@ddbdemo to enter.
(please keep in mind that a lot of features are disabled in the demo, since the test-user doesn’t have admin-rights.)

Download:
DaDaBIK Revised 1.0 (1MB, tar.gz)

Subversion Repository:
There is a Subversion repository in case you want to make use of the open source idea or really want to see some old school PHP4 :O

Changelog:
2010-03-09 initial release of DaDaBIK Revised

PHP ZendCon 09

Freitag, 16. Oktober 2009

zendcon Excitement. Excitement. Letzter Tag im Büro. Konferenz und Urlaub klingen zwar zunächst super toll - ein etwas negativer Beigeschmack bleibt aber: ich muss hier das geilste Webseitenprojekt zurücklassen, an dem ich bisher hier werkeln durfte und auch gern fertigstellen würde :(
Nun…
okay, am Wochenende gehts also zur ZendCon nach San Jose, California. Als Neuling auf der Konferenz bin ich mal sehr gespannt, ob diese hält was sie verspricht: 4 Tage intensive Sessions rund um PHP und Sicherheit, Wartbarkeit von Code, Geschwindigkeit, etc. etc. (die Liste der geplanten Sessions liest sich auf jeden Fall sehr vielversprechend). Mal sehen ob ich dazu komme ein bißchen von dort zu berichten und ein paar Sessions zusammenzufassen oder obs nur für 140 Zeichen gezwitscher reicht. Ein paar interessante Einführungen zur ZendCon ach dem Motto “so, you decided to attend ZendCon…” gibts im Netz, die werd ich mir mal noch zu Gemüte führen.

Vorbildlich flexibel: Weblication CMS

Donnerstag, 24. September 2009

Nachdem ich in letzter Zeit etwas weniger mit Weblication CMS Enterprise 4.0 gemacht habe, ist es jetzt geradezu ein Ding der Freude mal wieder ein Design damit umzusetzen. Gerade im Vergleich zu anderen Redaktionssystemen braucht sich das Konzept nicht verstecken: Weblication CMS zerlegt die inhaltlichen Elemente einer typischen Webseite in einzelne Informationsblöcke, wobei alle Daten im XML-Format vorliegen. Über eine Master-XML Datei teilt man dem CMS mit aus welchen Bestandteilen so ein Informationsblock besteht und über ein XSL-Stylesheet hinterlegt man nun die gewünschten Transformierungen die das finale HTML-Markup generieren. Eine simple und ziemlich geniale Idee. Von Standardtemplates, und einem nicht sonderlich hübschem Adminbereich darf man sich nicht abschrecken lassen, denn die Stärken spielt Weblication CMS insbesondere in dieser enorm flexiblen XML-XSL Basis aus.

Wer speziellere Anpassungen benötigt wird um das Weblication Kompendium meines Kollegen Ulf Mayer kaum herum kommen das er unter Weblication Tipps betreibt.

Mehrere Domains – eine WordPress Installation

Dienstag, 24. März 2009

Wer mehrere Domains in ein und dasselbe Verzeichnis seines Webservers zeigen läßt, der hat in der Regel nicht sonderlich viel davon. Klar, meist gibt es dafür irgendeinen Grund. Vielleicht:

  1. gab es mehrere Domains zum Webpaket,
  2. oder man wollte dem Trend zur Zweit- oder Drittdomain folgen
  3. es ließ sich mangels Zugriff auf die httpd- oder vhost.conf nicht anders bewerkstelligen
  4. oder man wollte es genau so haben wie es ist ;)

Zuerst die altbekannte Theorie.

Nun, sofern man eine .htaccess-Datei anlegen kann und mod_rewrite verfügbar ist, kann man sich ganz gut behelfen und die einzelnen Domains ganz nach Belieben in Unterverzeichnisse weiterschicken. Die zwei, drei Zeilen die man in seine htaccess packt lesen sich in der Regel so:

  1. <IfModule mod_rewrite.c>
  2. RewriteEngine On
  3. RewriteCond %{HTTP_HOST} ^(www.)?example.com$
  4. RewriteCond %{REQUEST_URI} !^/verzeichnis/
  5. RewriteCond %{REQUEST_FILENAME} !-f
  6. RewriteCond %{REQUEST_FILENAME} !-d
  7. RewriteRule ^(.*)$ /verzeichnis/$1
  8. </IfModule>

Damit schickt man nun also alle Anfragen die ein Besucher an www.example.com schickt in ein Unterverzeichnis, in dem sich dann ein fleißiges CMS oder 73 tippende Affen um alles weitere kümmern können. Legt man sowas für jede seiner Domains an, kann man diese für eigenständige Projekte getrennt voneinander verwenden. Immerhin.

Was aber tun, wenn…

… man aus reiner Absicht zwei oder drei Domains auf eine einzige WordPress-Installation zeigen läßt? Ein paar Gründe warum man das vielleicht tun möchte könnten sein:

  1. Man möchte eine mehrsprachige Seite anbieten. Ein Besucher soll allein anhand der Domain entweder einen deutschsprachigen oder aber englischen Artikel zu Gesicht bekommen. Unter www.meinedomain.de/kategorie/mein-artikel/ auf deutsch und unter www.meinedomain.com/kategorie/mein-artikel/auf englisch.
  2. Auch für Entwicklungsumgebungen ist das meist sinnvoll, denn viele Entwickler arbeiten lokal auf einem localhostund nicht direkt auf einem live-Server. Normalerweise muss man sich also händisch einen Virtual Host anlegen oder die Optionen in einer WordPress Datenbank anpassen. Dort nämlich speichert WordPress die Domain unter welcher die Webseite laufen soll.
  3. Ein drittes Szenario wären noch Domain-bezogene Software Lizenzen. Seien es kostenpflichtige Statistik Tools, oder sonstwas. Bitte nicht falsch verstehen - Ich will hier nicht zum Mißbrauch von Software-Lizenzen aufrufen. Aber es kann durchaus vorkommen dass man eine Software auf eine Domain lizensiert, die man zu einem späteren Zeitpunkt nicht mehr nutzt, aber durchaus noch Verwendung für die Software hat.

All diese Szenarien haben eins gemein: man möchte mehrere Domains mit einer WordPress-Installation sinnvoll betreiben. Da WordPress aber wie schon erwähnt die Domain in seiner wp_options-Tabelle speichert geht das nicht ohne weiteres. Natürlich funktionieren die weiter oben schon beschriebenen Bordmittel des Apache auch noch wenn man WordPress verwendet, sobald man aber auf einen Link seiner WordPress-Seite klickt wird man feststellen, dass diese wieder auf die intern gespeicherte Domain verweisen.

Ein Plugin schafft Abhilfe

Im WordPress-Forum hat jemand einen grandiosen Helfer (Plugin) gepostet welches genau für diesen Zweck ideal ist:Using different domains for different languages. Dieses ist zwar eigentlich darauf ausgelegt dem Gengo Plugin einen speziellen Sprachparameter zu liefern, man kann es aber sehr leicht anpassen. Ich verwende es zum Beispiel dazu mein WordPress-Backend mit einer zweiten Domain aufzurufen, ich leite damit auch alle Anfragen auf www. Domains auf eine ohne-www-Version weiter und erlaube zusätzlich eine dritte Domain, die meinem Virtual Host unter Windows entspricht. Somit entfällt nerviges Suchen und Ersetzen wenn ich lokal an der Seite arbeite. Ich kann eine 1 zu 1 Kopie sowohl auf XAMPP wie auch auf dem Live-Server einsetzen.

Vielleicht ist dieser Tip ja auch anderen nützlich bzw. eine Arbeitserleichterung.

Using bit masks in PHP

Montag, 2. März 2009

form exampleWhenever you have a set of values that can either be true or false, or rather 1 and 0, you can use bit masks to save yourself some work.
For example, think of a HTML form with a set of checkboxes in it. Each checkbox in the form will have a specific value that is defined by its HTML-attribute value="yes". Asides its actual value and the purpose of the checkbox, it still can only be true or false (checked or unchecked), by design.

An example.

Let’s say we wanted to find out what people prefer for breakfast. We would most likely create a web-based form to store someones individual preferences. You can see what this form might eventually look like in my first screenshot. Let’s write down all the possible options into a single array now - I’ll explain later why this is actually making sense. So the array looks like this:

  1. Array (
  2. [0] => "apple"
  3. [1] => "coffee"
  4. [2] => "newspaper"
  5. [3] => "strawberries"
  6. [4] => "cereals"
  7. [5] => "bread"
  8. [6] => "nutella"
  9. ...
  10. )

Now that we have created a form and have an array of options, let’s take a look what this Bit Masking thing is good for.
The basic idea behind bit masking is to associate each part of an array (where each value can either be true or false) with an exponential value of two.
Say what? Let’s look at this small function and the resulting masked array:

  1. function getMaskedArray($array) {
  2. $i = 0;
  3. foreach ($array as $key => $value) {
  4. $maskedArray[pow(2,$i)] = $value;
  5. $i++;
  6. }
  7. return $maskedArray;
  8. }

Using this function ($masked_breakfast_preferences = getMaskedArray($breakfast_preferences);), gives us the following:

  1. Array (
  2. [1] => "apple",
  3. [2] => "coffee",
  4. [4] => "newspaper",
  5. [8] => "strawberries",
  6. [16] => "cereals",
  7. [32] => "bread",
  8. [64] => "nutella",
  9. )

Okay, now here’s the reason, we are doing all this: once we submit a form for a specific persons’ preferences, we are able to describe these preferences with a unique number: the summary of the corresponding keys.
For example: someone who would like an apple, strawberries, cereals and bread for breakfast would give a sum of 57 (that is 1 + 8 + 16 + 32).
And now you’ll understand why we need to use exponential growth on the key values here: it ensures that the result of our summary is always unique. The advantage is obvious: instead of bloating your database or handling each value separately you can handle complex selections (maybe your culinary experience leads to a form with hundreds of options) in one single number.

The function used to calculate this bit mask could look like this: (in this case, a selected checkbox has a value of 1) <input type="checkbox" name="brekfast_preferences[]" value="1">

  1. function getBitmask($masked_array) {
  2. foreach ($masked_array as $key => $value) {
  3. if ($value === 1) {
  4. $res = $res + $key;
  5. }
  6. }
  7. return $res;
  8. }

Now you’re familiar with the basic concept of bit masking.
But there’s one more thing we need in order to have a practical use for this technique: a function that can reverse the bit mask back into it’s initial Array.
All we need to get rebuild the settings for a specific user now is its stored bitmask 57 and the array of usersettings so we can reassign our bools to it:

  1. function getBinmaskArray($bitmask,$array) {
  2. if (is_array($array)) {
  3. if (is_int($bitmask)) {
  4. $bin_val = base_convert($bitmask,10,2);
  5. $bin_val = strrev($bin_val);
  6. for ($i = 0; $i < count($origArray); $i++) {
  7. $val = substr ($bin_val, $i, 1);
  8. if (!strlen($val)) $val = 0;
  9. $tmpArray[$origArray[$i]] = $val;
  10. }
  11. return $tmpArray;
  12. }
  13. print "The first parameter is expected to be a valid bitmask. ";
  14. }
  15. print "The second parameter is expected to be an array.";
  16. }

Please note that these examples are really simplified to illustrate the concept of bitmasking. In a production environment you would probably code your own bitmasking class or use a pre-made class (PHPClasses.org has a good one)

I do know that the breakfast example is rather trivial, but just think of a CMS where you want to control wether a specific content element should show up in the actual context or not… you have an ‘archive view’ a listing of ‘search results’, an ‘index view’, ‘product detail view’ and ‘product listing view’ and so on…
If you want to place a box in specific contexts of your page you could achieve that by assigning a number, a specific bitmask to it.

Multiple-selects <select multiple="multiple" size="7"> are another great example for this. If you want to highlight a selction that has already been made on such an element you can do so by simply adding the selected="selected" attribute to those <option>s with this technique.

If you think this a few steps further, you could also use this technique to reduce the number of SQL requests necessary to display a certain webpage to a minimum. Not only that elements or boxes of the webpage can be turned on or off with this technique, even functions or methods that get data from the database can be enabled or disabled just by requesting a certain URL and thus context of your webpage. So no need to grab more data from the DB then really needed.

Think about this, you will come up with your own ideas quickly.