Home arrow PHP arrow Page 9 - Revisited: Building Cross Platform GUI Apps With PHP-GTK
PHP

Revisited: Building Cross Platform GUI Apps With PHP-GTK


PHP is not just a web-based scripting language. Did you know that you can also build complete, cross-platform, windowed applications with PHP and MySQL? If not, read on...

Author Info:
By: Mitchell Harper
Rating: 5 stars5 stars5 stars5 stars5 stars / 19
November 17, 2002
TABLE OF CONTENTS:
  1. · Revisited: Building Cross Platform GUI Apps With PHP-GTK
  2. · What is PHP-GTK?
  3. · Downloading and installing PHP-GTK
  4. · Installing PHP-GTK for Linux
  5. · Building our first PHP-GTK app
  6. · Registering multiple callback functions
  7. · Adding GTK widgets to our window
  8. · Other PHP-GTK widgets
  9. · A PHP-GTK database app
  10. · Conclusion

print this article
SEARCH DEVARTICLES

Revisited: Building Cross Platform GUI Apps With PHP-GTK - A PHP-GTK database app
(Page 9 of 10 )

One of the cool things about PHP-GTK is that it integrates seamlessly with PHP, meaning that we can still use all of PHP's regular functions in our PHP-GTK applications. The most popular database to use with PHP is MySQL, so let's create a PHP-GTK app that connects to a MySQL database and retrieves some records.

Enter the following commands at the MySQL console application:

create database people;

use people;

create table programmers

(

pId int auto_increment,

pName varchar(50),

pAge tinyint,

primary key(pId),

unique id(pId)

);


Add some people to our programmer's table with the following insert commands:

insert into programmers values(0, 'Bill Smith', 23);

insert into programmers values(0, 'James Black', 45);

insert into programmers values(0, 'Lyonel Jones', 18);

insert into programmers values(0, 'Phil Brown', 22);

insert into programmers values(0, 'Michael Clay', 56);


Now let's create a PHP-GTK app that connects to our database and retrieves the records from our programmers table. The app is a bit large to post here in its entirety, so download the support material for this article if you want to test it on your machine.

Our app starts by creating several labels and text boxes, as well as one command button, which, when clicked will connect to a MySQL server:

$button = &new GtkButton("Connect and Get Rows");

$button->connect("clicked", "getdata");

$box->pack_start($button);


The layout of our window and its widgets

The getdata function connects to MySQL using the host, username and password values from the GtkEntry controls in our app:

function getdata($theButton)

{

global $server;

global $user;

global $pass;

...

$dServer = $server->get_text();

$dUser = $user->get_text();

$dPass = $pass->get_text();

$s = mysql_connect($dServer, $dUser, $dPass) or die("Couldn't connect to database server");

$d = mysql_select_db("people", $s);

Next, all of the controls that occupy our GtkVBox widget are hidden:

// Hide all controls

$label1->hide();

$label2->hide();

$label3->hide();

$server->hide();

$user->hide();

$pass->hide();

$button->hide();


It then queries the programmer's table, looping through each result and adding it to a string variable:

$result = mysql_query("select * from programmers order by pName asc");

$pList = "";

while($row = mysql_fetch_array($result))

{

// Create a string array

$pList .= $row["pName"] . " is " . $row["pAge"] . "\n";

}


It then creates a new GtkLabel widget and assigns the value of the $pList variable to the label by passing it to the labels constructor. The label is then added to the GtkVBox widget, which is part of our main window:

// Create a GtkCList and show it

$p = &new GtkLabel("$pList");

$box->add($p);

$p->show();


Here's how the window looks before and after I enter my database credentials and click on the connect button:

Before connecting

After connecting

As you can see, the GtkLabel and GtkEntry objects are hidden, and the new label is displayed inside of out GtkVBox object.
blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials