PHP
  Home arrow PHP arrow Page 9 - Revisited: Building Cross Platform GUI App...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Revisited: Building Cross Platform GUI Apps With PHP-GTK
By: Mitchell Harper
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 9
    2002-11-17

    Table of Contents:
  • Revisited: Building Cross Platform GUI Apps With PHP-GTK
  • What is PHP-GTK?
  • Downloading and installing PHP-GTK
  • Installing PHP-GTK for Linux
  • Building our first PHP-GTK app
  • Registering multiple callback functions
  • Adding GTK widgets to our window
  • Other PHP-GTK widgets
  • A PHP-GTK database app
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More PHP Articles
    More By Mitchell Harper


     

    PHP ARTICLES

    - 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
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek