Article

Build Cross-Platform Windowed Apps with PHP

Page: 1 2 3 4 5 6 7 8 Next

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);

697_fig3

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();

If you liked this article, share the love:
Print-Friendly Version Suggest an Article