Re: simple mysql
| From: | Adam Rambousek | Date: | Wed, 28 Mar 2001 20:46:30 +0000 |
| Subject: | Re: simple mysql | ||
| References: | 1 | Groups: | php.gtk |
| Request: | Send a blank email to php-gtk+get-1176@lists.php.net to get a copy of this message | ||
well, the script is here
<?
if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')
dl('php_gtk.dll');
else
dl('php_gtk.so');
$windows = array();
function delete_event($window, $event)
{
$window->hide();
return true;
}
function close_window($widget)
{
$window = $widget->get_toplevel();
$window->hide();
}
/*
Called when clist column is clicked. It sets sorting by the clicked column.
*/
function clist_click_column($clist, $column) {
$clist->set_sort_column($column);
$clist->sort();
}
/*
Function displaying the result of query.
*/
function do_query($query)
{
global $windows;
//if the query_window is opened, let's close it
if (isset($windows['query_window'])) {
close_window($windows['query_window']);
}
$window = &new GtkWindow;
$windows['query_window'] = $window;
$window->set_name('query_window');
$window->connect('delete-event', 'delete_event');
$window->set_policy(false, true,false);
$window->set_title('Query result');
$window->set_uposition(220,85);
$box1 = &new GtkVBox();
$window->add($box1);
//frame displaying entered sql query
$frame = &new GtkFrame('MySQL Query');
$box1->pack_start($frame,false);
$label = &new GtkLabel($query->get_text());
$frame->add($label);
//frame displaying clist with the query result
$frame = &new GtkFrame('MySQL Query Result');
$box1->pack_start($frame,true);
//we'll display the result with scrollbars
$scrolled_win = &new GtkScrolledWindow();
$scrolled_win->set_border_width(5);
$scrolled_win->set_policy(GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
$frame->add($scrolled_win);
/*here we deal with the result
if mysql_query called with the query entered in main window, return a result
we can display clist
*/
if ($result = mysql_query($query->get_text())) {
/*
at first, keys array contains the names of columns
*/
if ($data = mysql_fetch_array($result)) {
$i=0;
$keys = array();
while (list($key,$val) = each($data)) {
if ($i%2) $keys[]=$key;
$i++;
}
}
/*
now we can prepare the clist, keys are the titles of columns and the number
of columns is equal to the number of keys
*/
$clist = &new GtkCList(count($keys), $keys);
$clist->connect('click_column', 'clist_click_column');
//we sets the auto_resize for each column
for ($i=0;$i<count($keys);$i++) $clist->set_column_auto_resize($i, true);
$scrolled_win->add($clist);
/*
now the data from result
we get the data from each row to row_data array and then we append this
array to the clist as a new row
*/
while ($data = mysql_fetch_array($result)) {
for($i=0; $i < count($data)/2; $i++){
$row_data[$i] = $data[$i];
}
$clist->append($row_data);
}
}
$button = &new GtkButton('close window');
$button->connect('clicked', 'close_window');
$box1->pack_start($button,false);
$button->set_flags(GTK_CAN_DEFAULT);
$button->grab_default();
$window->show_all();
}
function main_window()
{
$window = &new GtkWindow();
$window->set_policy(false,true,false);
$window->set_name('main_window');
$window->set_title('MySQL Query sample');
$window->set_uposition(80,80);
$window->connect_object('destroy', array('gtk', 'main_quit'));
$window->connect_object('delete-event', array('gtk', 'false'));
$box1 = &new GtkVBox();
$window->add($box1);
$frame = &new GtkFrame('MySQL Query');
$box1->pack_start($frame,false);
$entry = &new GtkEntry();
$frame->add($entry);
$separator = &new GtkHSeparator();
$box1->pack_start($separator,false);
$button = &new GtkButton('Do Query');
$button->connect_object('clicked', 'do_query',$entry);
$box1->add($button);
$separator = &new GtkHSeparator();
$box1->pack_start($separator,false);
$button = &new GtkButton('Quit');
$button->connect_object('clicked', array('gtk', 'main_quit'));
$box1->add($button);
$window->show_all();
}
/*
DON'T FORGET TO SET SERVER, USERNAME AND PASSWORD
*/
mysql_pconnect("SERVER","USERNAME","PASSWORD") or die("can't
connect to server");
main_window();
Gtk::main();
?>
--
Adam Rambousek
email: rambousek@volny.cz PGP KeyID: 33BD6BAE ICQ: 47596279
www: http://adamovy.cestiny.cz http://werich.poda.cz http://rambousek.com