background preloader

Php

Facebook Twitter

Sorting a Multi-Dimensional Array with PHP | firsttube.com. Every so often I find myself with a multidimensional array that I want to sort by a value in a sub-array. I have an array that might look like this: //an array of some songs I like $songs = array( '1' => array('artist'=>'The Smashing Pumpkins', 'songname'=>'Soma'), '2' => array('artist'=>'The Decemberists', 'songname'=>'The Island'), '3' => array('artist'=>'Fleetwood Mac', 'songname' =>'Second-hand News') ); The problem is thus: I’d like to echo out the songs I like in the format “Songname (Artist),” and I’d like to do it alphabetically by artist.

So I developed a quick function to sort by the value of a key in a sub-array. Please note this version does a case-insensitive sort. See subval_sort() below. function subval_sort($a,$subkey) { foreach($a as $k=>$v) { $b[$k] = strtolower($v[$subkey]); } asort($b); foreach($b as $key=>$val) { $c[] = $a[$key]; } return $c; } To use it on the above, I would simply type: $songs = subval_sort($songs,'artist'); print_r($songs);

Plugin de commentaire. Dans ce tutoriel je vous propose de découvrir comment créer un système de commentaire modulable et réutilisable (une sorte de plugin). A la fin de ce tutoriel on aura une class Comments que l'on pourra utiliser (et réutiliser) dans nos différents projets. Objectif Avant de se lancer dans le code il faut réfléchir à ce que l'on cherche obtenir : Se créer un "plugin" que l'on va pouvoir utiliser Avoir qqchose qui s'adapte à la pluspart des casOn ne s'occupera pas du code HTML (varie entre chaque projet)Doit être indépendant de la structure du code On va donc utiliser la programmation orientée objet en se créant une classe qui va contenir nos différentes fonctions.

<? Cette super classe va devoir intéragir avec notre base de donnée donc il va nous falloir utiliser une connexion (on choisira pdo ici). ... class Comments{ private $pdo; public function __construct($pdo){ $this->pdo = $pdo; } } Récupération des commentaires Sauvegarde des commentaires Et voila ! Simple non ? Fonction répondre. Create a REST API with PHP « Gen X Design | Ian Selby. Your apps never had it so good One of the latest (sort of) crazes sweeping the net is APIs, more specifically those that leverage REST. It’s really no surprise either, as consuming REST APIs is so incredibly easy… in any language. It’s also incredibly easy to create them as you essentially use nothing more than an HTTP spec that has existed for ages. One of the few things that I give Rails credit for is its well thought-out REST support, both for providing and consuming these APIs (as its been explained by all the Rails fanboys I work with).

Seriously, if you’ve never used REST, but you’ve ever had to work with (or worse, create) a SOAP API, or simply opened a WSDL and had your head explode, boy do I have good news for you! So, What on Earth is REST? Why Should You Care? Before we get into writing some code, I want to make sure everyone’s got a good understanding of what REST is and how its great for APIs. Requests All APIs need to accept requests. Getting Started with REST and PHP. Social Network Style Posting with PHP, MongoDB and jQuery - part 1. Post mechanisms similar to Facebook are nowadays very common within any application.

The concept of Post-Like-Comment is familiar to everyone who ever used a social network. In this article, we will learn how to create a similar working model wherein the user will be able to post his status, like posts and comment on them. What’s more interesting is that after learning things from this article, going forward you will be able to implement a lot of other features on your own. I’ll be using PHP as the coding language, MongoDB as the database and jQuery for JavaScript operations. There are two articles in this series. Before getting started, let us look at the final work we will develop (Fig 1 ). Download Code: Download the code provided with the article from its Github repo.

Index.php : This is the main file which displays all the posts and from where the user will interact. mongo_connection.php : This file contains the common MongoDB connection code. Database Structure: Conclusion: Laravel - a beautiful PHP framework that does not make me feel stupid. {15 Mai 2012} I may have solid artistic skills, and I also enjoy web development (most of the time), but the sad truth is that I am an awful programmer. I came to web development “the wrong way”, inserting <? Php ? > blocks into my HTML code. As any awful developer, I had no idea I was doing something wrong :-) Until now, that I urgently needed to develop (or rather, re-make from scratch) a not-so-simple online translations system for many of my projects (including Vladstudio.com website and Windy Press books).

I got depressed (I do so sometimes!) Another several days passed, I browsed hundreds of discussions, discovered and carefully reviewed dozens of frameworks. My requirements: was easy database interaction as possible;simple! CodeIgniter (+) great docs, lots of users, lots of info, has all I need;(-) lots of what I don’t need; old PHP4 support means more and uglier code (?) Kohana (+) improved clone of CodeIgniter, based on РНР5(-) less users, worse docs, backward compatibility Dingo Symfony. WordPress Serialized PHP Search Replace Tool | Interconnect IT - WordPress Consultants, Web Development and Web Design. Search Replace DB version 3.0.0 (currently a BETA version) allows you to carry out database wide search/replace actions that don’t damage PHP serialized strings or objects with a user friendly interface and experience.

Installation & Use Now acts like a web app! To use the script, download the zip file from below, extract the folder called secret-name-please, renaming it to something secret of your choosing, then navigate to that folder in your browser. The script will automatically attempt to find and populate the database field but you must check that the details are correct and that it is for the database you wish to carry out a search/replace operation on.

To see how you can use this tool to aid migrations, check out our article on WordPress migrations or visit the WP Tuts+ article that mentions this script. If you are in any doubt whatsoever about how to use this standalone script, then please consider getting an expert in. Download v 3.0.0 BETA Problems? Changelog: To Be Done Donations. Nginx and PHP-FPM Configuration and Optimizing Tips and Tricks. I wrote before a guide Howto install Nginx/PHP-FPM on Fedora 20/19, CentOS/RHEL 6.5/5.10, but this guide is just installation guide and many cases Nginx and PHP-FPM basic configuration is good enough, but if you want to squeeze all the juice out of your VPS or web server / servers and do your maintenance work little bit easier, then this guide might be useful. These tips are based entirely on my own experience, so they may not be an absolute truth, and in some situations, a completely different configuration may work better.

It’s also good to remember leave resources for another services also if you run example, MySQL, PostgreSQL, MongoDB, Mail server, Name server and/or SSH server on same machine. And yes here we go… Nginx Configuration and Optimizing Tips and Tricks Nginx Tip 1. – Organize Nginx Configuration Files Normally Nginx configuration files are located under /etc/nginx path. Remember add following includes at the end of your nginx.conf file: PHP-FPM Configuration Tips and Tricks.

PHP 5.4 is Here! What You Must Know. PHP 5.4 is here; the next major step forward since version 5.3 - keeping PHP 6 (full Unicode support) on hold for now. The latest enhancements significantly improve its elegance, while removing deprecated functionality, resulting in a dramatic optimization of the runtime (up to 20% more speed and memory usage reduction).

New Features and Improvements Some of the key new features include traits, a shortened array syntax, a built-in webserver for testing purposes, use of $this in closures, class member access on instantiation, <? = is always available, and more! PHP 5.4.0 significantly improves performance, memory footprint and fixes over 100 bugs. Content-Type: text/html; charset=utf-8 is always sent; so there's no need to set that HTML meta tag, or send additional headers for UTF-8 compatibility. Traits The best demonstration of traits is when multiple classes share the same functionality. The best use of traits is demonstrated when multiple classes share the same functionality. Got it? <? PHP Frameworks comparison matrix (symfony vs cakephp vs zend framework vs CodeIgniter vs Yii) | SocialCompare's blog. How to access cross-domain data with AJAX using JSONP, jQuery and PHP.

I’m guessing that if you are reading this post you have already encountered the problem of the same origin policy with regards to retrieving data with AJAX from urls in other domains than your’s (the requesting domain). This policy, enforced by browsers, means you cannot fetch raw data from other domains with straight AJAX calls. The Problem I Was Trying To Solve (skip this paragraph if you just want the solution) Here’s the situation I ran into. At work, we have a timesheet entry program running on one subdomain, and Flyspray (bug-tracking software) on another subdomain. We wanted to get task descriptions from Flyspray integrated in our employee timesheet notes. JSONP Circumvents the Same Origin Policy Data returned from an AJAX call in JSONP (JSON with padding) format is designed to solve this problem.

JSONP works by injecting what looks like a local function call directly into your script, attaching it to a script tag (in the form of a url). Example 1 That’s it! Templating Engines in PHP. This blog post is not for the faint-hearted! Some people will strongly disagree with me and some others will probably want to kill me at the upcoming Zend Conference. And if starting an argument in the comments can help you feel better, please feel free to do so. If you want to have a more advanced discussion on this topic, vote for my talk at the Zend UnConference. So, you think PHP is a templating engine? For several years now, I have been promoting web development best practices, and one of them is the separation of concerns. And a template language is something that helps you to write templates that respects this separation of concerns. So, when I asked a few days ago about the best and popular templating engines in PHP on Twitter, some people naturally answered "PHP" itself.

Why PHP is not (anymore) a good template language? Why do people still think PHP is a templating engine? Template languages evolved a lot since 1995 and the initial release of PHP/FI: <! Concision <? Reusability <! <? Cross-domain cookies/sessions in Safari and all other browsers | anant garg. The Problem Safari does not allow cross-domain cookies.

In other words, if on X.com, you load an iFrame with contents of Y.com and set a cookie in the iFrame, Safari will not save the cookie. This problem also occurs in IE6/7 but can be resolved by sending a P3P header. The Solution I am not entirely sure why this works, but the cookie gets saved if a post is made to the iFrame. This solution relies of jQuery but can be adapted to any other. Here is the code that goes on the remote domain: The contents for startsession.php would be as simple as: To make sure that your site is compatible with IE6/7, always output the following header: Unfortunately, there is no way to find out when the browser has completed submitting the form. Comments/Suggestions? Spread The Word If you like what you are reading, then please help spread the word by re-tweeting, blogging or using the ShareThis button below.

[PHP] Les ressemblances à ne pas confondre. Introduction Cette astuce présente plusieurs fonction ou astuces en php qui sont différentes alors que beaucoup de monde croit qu'elles agissent de la même façon. Concaténation ou paramètres ? Avec la fonction echo (ou print), on peut afficher plusieurs variables et chaines de caractères à la suite. Exemple : Ici, on utilise la virgule entre la chaine de caractères et la variable. Lorsque l'on utilise des points, on apelle ça la concatenation. J'ai testé les deux méthodes, et le passage de plusieurs paramètres est assurément plus rapide.

Voici d'ailleurs une page de test ou sont comparés les deux méthodes : Cependant, les virgules ne peuvent être utilisés qu'avec un echo ou un print, contrairement à la concaténation. Guillemets/Apostrophe Ou aussi "Doubles quotes/Simples quotes". Exemple : C'est d'ailleurs pour ça qu'il est plus rapide d'utiliser une chaine avec des simples quotes, car php ne "cherche" pas de variables. Structure require/include isset vs empty La fonction empty, elle, vérifie :