Category Archives: PHP - Page 2

Build MySQL insert Query from Array that contains fields and values

The following function takes table name, and array where its keys are the table fields and it’s values are the values to insert in each field, it constructs the MySQL Query based on the input and executes it to insert the data.

function insertRow($table_name, $input_array){
	$link = mysql_connect(SERVER, USER, PASS);
	mysql_select_db(DATABASE, $link);
	$SQL = "INSERT INTO $table_name ";
	$fields = "(";
	$values = "(";
	foreach ($input_array as $k => $v) {
		$fields .= "`$k` ,";
		$values .= "'$v' ,";
	}
	$fields .="#";
	$values .="#";
	$fields = explode(",#", $fields);
	$fields = $fields[0];
	$values = explode(",#", $values);
	$values = $values[0];
	$fields .= ")";
	$values .= ")";
	$SQL .= $fields . " VALUES " . $values;
	mysql_query($SQL);
	mysql_close($link);
}

List array Recursively in PHP

Code :

function listArrayRecursive($array_name, $ident = 0){
	if (is_array($array_name)){
		foreach ($array_name as $k => $v){
			if (is_array($v)){
				for ($i=0; $i < $ident * 10; $i++){ echo "&nbsp;"; }
				echo $k . " : " . "<br>";
				listArrayRecursive($v, $ident + 1);
			}else{
				for ($i=0; $i < $ident * 10; $i++){ echo "&nbsp;"; }
				echo $k . " : " . $v . "<br>";
			}
		}
	}else{
		echo "Variable = " . $array_name;
	}
}

Usage :

$ages = array(
				"ahmed" => "25",
				"mohamed" => "35",
				"group" => array("omar" => "15", "abdalla" => "20", "sub group" => array("john" => "10", "peter" => "20")));
 
listArrayRecursive($ages);

Output will be printed array in indented way.

ZendCasts – Free Zend Framework Screencasts

A really good Zend Framework Resource that has been posted in the ZF mailing lists couple of days ago.

very usefull

URL : www.ZendCasts.com

Render Nothing in Zend Framework

This is useful if u want to have the output without rendering a view or the layout, like in ajax or developing we services

to do that you need to call the following 2 methods in your Action :

$this->_helper->viewRenderer->setNoRender(); // Disable the viewscript
$this->_helper->layout->disableLayout();  // Disable the layout

Zend Framework Free Book : Surviving The Deep End

Hey folks,
today Padriac Brady has released a new Zend Framework Book,

About the author :
http://www.survivethedeepend.com/zendframeworkbook/en/1.0/introduction#zfbook.introduction.me

To read the book :

http://www.survivethedeepend.com/zendframeworkbook/en/1.0

About the book (quoted from the book):

Zend Framework: Surviving The Deep End is written in the form of a detailed tutorial following a step by step approach to building a real life application. Topics are grouped where it makes sense and there will be continual references to earlier chapters which serves to reinforce what you’re learning as you read. The book was designed to bring together elements of the Reference Guide, the growing body of community knowledge and my own personal experience so developers can see the bigger picture of developing a real application with the Zend Framework.No comments

To my mind that’s always been the framework’s main problem since the Reference Guide adds little beyond explaining each framework component in total isolation. It doesn’t offer a development approach, ways of thinking or a list of advanced topics which combine components. You should note though that this book is not a replacement for the Zend Framework Reference Guide. It’s assumed you can do some independent reading of the Reference Guide. The Guide is free, detailed, and reasonably easy to search. This book is a complement to it, not a replacement.No comments

The book also includes the full source code of the application within the text, and may repeat it several times to highlight new changes I am making. I understand that pages of source code can sometimes be frustrating but it does enforce clarity and I value clarity a great deal. For simplicity the full finalised source code of each chapter is available as a separate internet download.No comments

I will over time refer to several external libraries, other than the Zend Framework, which you are expected to install. These will include PEAR, Blueprint CSS Framework, jQuery, HTMLPurifier and PHPUnit. I know from experience this can be unpopular with some people but I assure you that their installation will be covered in detail and is quite straightforward even for beginners. You should bear in mind a real life application will require numerous external libraries!No comments

Finally, note that this book assumes a basic working knowledge of PHP 5, SQL, and Object Oriented Programming (OOP). These are necessary skills if you intend learning the Zend Framework but will not be covered by this book in detail. Since PHP is so simple to learn though, I don’t doubt you can find countless resources online to get you started down the road towards PHP Guru status.

———————-

enjoy reading ;)

To read the book :

http://www.survivethedeepend.com/zendframeworkbook/en/1.0

Appcelerator :: Building RIA Apps and Use Cases with ease

Yesterday i was browsing Zend Framework Webinars, and i stopped by the title :

Get “Rich” Quick : Building Ajax-based RIAs with the Zend Framework and Appcelerator

i didn’t hear about the Appcelerator thing before, but the title was really interesting as i use Zend framework extensively and it is a great idea to add some interactivity and UI to my apps.

after watching the webinars, i really liked the idea of being able to build the full prototype and have it working with no server side code, and then in the development phase with some slight changes to the prototype, it becomes the working front end interacting with the server side code with no problems @ all.

i went to try.appcelerator.org and gave it a try on the fly, that got me more interested. so i downloaded the sdk installer for MAC and installed it, the installation was at ease, even creating the projects and testing.

Appcelerator supports what we call “technology driven agile development” and we call that process Interactive Use Cases.

Zend Framework, Flex & PHP – Best Practice Cautions

Hey guys,

recently me and my flex developer colleague had a weird problem while exchanging data from zend framework to flex as web services using json.

well, 1st caution :

<?php echo "Json = $encoded_json_data"; ?>

don’t ever use a closing tag ” ?> ” in your controllers or classes, as it will result in new line in the output json data, which will make flex not able to recognize it correctly.

2nd caution :

<?php echo "Json = $encoded_json_data"; ?>

when u echo output from php don’t leave space between the variable and the equal “=” sign and also between the equal “=” sign and the value.
to be clear :
example 1 :

This won’t work as flex will not recognize a value for the variable Json.
example 2 :

This will work because we removed the spaces.

hope u don’t fall in this trap as we did.
& enjoy ;)

Adobe to contribute AMF support to Zend Framework

Hey folks,
i have read this great news yesterday, that Adobe has made a proposal for an AMF (Action Message Format) component in Zend Framework.
what a great addition to the strong & superior framework.
this will facilitate my work a lot with my colleagues here that works with Flex, we have been developing some Air applications, and as a solution of the interaction between Flex & PHP we used web services to exchange the data in Json format between us, it works fine, but having the options of AMF will make it more powerful and smooth.
long live ZF, long live flex ;)

here is the link for the news i got :
http://andigutmans.blogspot.com/2008/07/adobe-to-contribute-amf-support-to-zend.html

and here is the link for the proposal :
http://framework.zend.com/wiki/display/ZFPROP/Zend_Amf

Enjoy ;)

Google Calendar SMS Reminders Active in Egypt

Well, as u can understand from the subject, now the sms notifications of google calendar works with egyptian providers, i tried with vodafone and mobinil and it works smoothly.

steps are easy :
1- go to www.gmail.com and open your mail account.
2- click on the calendar link on the top left of the page.
3- in the calendar page click on settings on the top right of the page.
4- click on mobile setup tab.
5- enter your mobile number and then you will recieve a confirmation code,
6- enter the confirmation code and verify.
7- you will be redirected to notifications page, enable all SMS notifications, and setup default reminders for each event, you can setup up to 5 reminders.

you are done, and you can setup repeated events, for example i setup a daily event to wake me up by sms every morning.

another trick if you want to send sms to another person and you have no credit , just enter a new event, with the title

Installing ffmpeg-php on Centos 5

It took me a couple of days to figure out the right way to install ffmpeg-php extension for php, so i documented all the instructions to not waste another 2 days for me and anyone interested
i started with http://www.amman-dj.com/Slim/2008/01/29/how-to-installing-ffmpeg-mplayer-flvtool-2-and-ffmpeg-php-4-youtube-clone-script/ but didn’t work with me and i needed more plugins, so i modified it and added the other packages
enjoy :)

##########################
# add dag repository

rpm -Uhv http://apt.sw.be/redhat/el5/en/i386/rpmforge/RPMS/rpmforge-release-0.3.6-1.el5.rf.i386.rpm

##########################
# update system

yum -y update

##########################
# Download Packages

mkdir ~/ffmpeg-packages
cd ~/ffmpeg-packages
wget http://www3.mplayerhq.hu/MPlayer/releases/codecs/essential-20061022.tar.bz2
wget http://rubyforge.org/frs/download.php/9225/flvtool2_1.0.5_rc6.tgz
wget http://downloads.xiph.org/releases/ogg/libogg-1.1.3.tar.gz
wget http://downloads.xiph.org/releases/vorbis/libvorbis-1.1.2.tar.gz
wget http://liba52.sourceforge.net/files/a52dec-0.7.4.tar.gz
wget http://downloads.sourceforge.net/faac/faad2-2.6.1.tar.gz
wget http://downloads.sourceforge.net/faac/faac-1.26.tar.gz
wget http://superb-east.dl.sourceforge.net/sourceforge/lame/lame-3.98b8.tar.gz
wget http://www.tortall.net/projects/yasm/releases/yasm-0.7.0.tar.gz
wget ftp://ftp.videolan.org/pub/videolan/x264/snapshots/x264-snapshot-20080630-2245.tar.bz2
wget http://downloads.xvid.org/downloads/xvidcore-1.1.3.tar.gz
wget http://www.linux1394.org/dl/libraw1394-1.3.0.tar.gz
wget http://mesh.dl.sourceforge.net/sourceforge/ffmpeg-php/ffmpeg-php-0.5.3.1.tbz2

##########################
# Install SVN/Ruby

yum install subversion
yum install ruby
yum install ncurses-devel

##########################
# Extract all the source files

bunzip2 essential-20061022.tar.bz2; tar xvf essential-20061022.tar
tar zxvf flvtool2_1.0.5_rc6.tgz
bunzip2 ffmpeg-php-0.5.3.1.tbz2; tar xvf ffmpeg-php-0.5.3.1.tar
tar zxvf libogg-1.1.3.tar.gz
tar zxvf libvorbis-1.1.2.tar.gz
tar -zxf a52dec-0.7.4.tar.gz
tar zxf faad2-2.6.1.tar.gz
tar zxfv faac-1.26.tar.gz
tar zxfv lame-3.98b8.tar.gz
tar zfvx yasm-0.7.0.tar.gz
bunzip2 < x264-snapshot-20080630-2245.tar.bz2 | tar -xf -
tar zxfv xvidcore-1.1.3.tar.gz
tar zxfv libraw1394-1.3.0.tar.gz

##########################
#Create the codecs directory & import them

mkdir /usr/local/lib/codecs/
mv essential-20061022/* /usr/local/lib/codecs/
chmod -R 755 /usr/local/lib/codecs/

##########################
# Install Lame

cd lame-3.98b8
export LD_LIBRARY_PATH=/usr/local/lib
./configure
make
make install
cd ..

##########################
# Install libOGG

cd libogg-1.1.3
./configure
make
make install
cd ..

##########################
# Install libVorbis

cd libvorbis-1.1.2
./configure
make
make install
cd ..

##########################
# Install flvtool2

cd flvtool2_1.0.5_rc6
ruby setup.rb config
ruby setup.rb setup
ruby setup.rb install
cd ..

##########################
# Install a52

cd a52dec-0.7.4
./configure --enable-shared=PKGS
make
make install
cd ..

##########################
# Install FAAD2

cd faad2
autoreconf -vif
./configure --disable-drm --disable-mpeg4ip
make
make install
cd ..

##########################
# Install FAAC

cd faac
./bootstrap
./configure --disable-mp4v2
make
make install
cd ..

##########################
# Install yasm

cd yasm-0.7.0
./configure
make
make install
cd ..

##########################
# Install x264

cd x264-snapshot-20080630-2245
./configure --enable-shared
make
make install
cd ..

##########################
# Install Xvid

cd xvidcore-1.1.3/build/generic
./configure
make
make install
cd ../../..

##########################
# Install libraw1394

cd libraw1394-1.3.0
./configure
make dev
make
make install
cd ..

##########################
# Install libdc1394

yum install libdc1394 libdc1394-devel

##########################
# Install libtheora

yum install libtheora libtheora-devel

##########################
# Install vlc & mplayer

yum install vlc mplayer

##########################
# Install ffmpeg

svn checkout svn://svn.mplayerhq.hu/ffmpeg/trunk ffmpeg
cd ffmpeg
./configure --enable-gpl --enable-postproc --enable-nonfree --enable-postproc --enable-libfaad --enable-avfilter --enable-pthreads --enable-libxvid --enable-libx264 --enable-libmp3lame --enable-liba52 --enable-libfaac --disable-ffserver --disable-ffplay --enable-libtheora --enable-libvorbis --enable-shared
 
make
make install
export LD_LIBRARY_PATH=/usr/local/lib
cd ..

##########################
# Install FFMPEG-PHP (make sure the php.ini path is correct.)

cd ffmpeg-php-0.5.3.1
phpize
./configure
make
make install
 
echo 'extension=ffmpeg.so' >> etc/php.ini
cd ..
service httpd restart
 
php -r 'phpinfo();' | grep ffmpeg