Barra laterale

programmazione:php:gtktreeview_e_db_con_php

Gtktreeview e database con Php

Autore: Fabio Di Matteo
Ultima revisione: 27/11/2015 - 12:25



Questo articolo vuole essere un inizio sulla programmazione di applicazioni che fanno uso di database tramite Php-gtk2 e database mediante sPDO una classe per PDO.
Lo script realizzera' una piccola interfaccia grafica che mostra il risultato di una select. Come da figura.

Lo script

main.php

#! /usr/bin/php
 
<?php
include_once('spdo.php');
 
 
//Variabili globali
$treeview='';
$ID=0;
 
 
//Carico l'interffaccia grafica realizzata con glade per gtk2
$glade = new GladeXML('gui.glade');
 
//Callback per terminare il programma alla chiusura della finestra
$window = $glade->get_widget('window1');
$window->connect_simple('destroy', array('Gtk', 'main_quit'));
 
//La casella di testo del nominativo completo
$Nome=$glade->get_widget('txtNome');
$Cognome=$glade->get_widget('txtCognome');
$Email=$glade->get_widget('txtEmail');
 
//Il modello che aggancero' alla vista ad albero (gtktreeview)
$store = new GtkListStore(Gobject::TYPE_LONG, Gobject::TYPE_STRING, Gobject::TYPE_STRING,Gobject::TYPE_STRING );
 
//Agganciamo il modello al treeview
$treeview=$glade->get_widget('treeviewMain');
$treeview->set_model($store);
 
//Al click sulla tabella prelevo l'id della riga 
$selection = $treeview->get_selection();
$selection->connect('changed', 'selection_changed');
 
 
function selection_changed($selection)
{
 
	//get_selected ritorna lo store e l'iteratore per quella riga
    list($model, $iter) = $selection->get_selected();
 
 
    //Prelevo il valore di ogni singola cella nella riga selezionata
    global $ID;
    $ID=$model->get_value($iter, 0);
    $mCognome=$model->get_value($iter, 1);
    $mNome=$model->get_value($iter, 2);
    $mEmail=$model->get_value($iter, 3);
 
    global $Cognome;
	$Cognome->set_text($mCognome);
 
    global $Nome;
	$Nome->set_text($mNome);
 
	global $Email;
	$Email->set_text($mEmail);	
 
 
	printf("ID: %s\n", $ID);
}
 
 
//E' usata per disegnare testo nelle celle
$cell_renderer = new GtkCellRendererText();
 
//Prima colonna (ID)
$colID = new GtkTreeViewColumn('ID', $cell_renderer, 'text', 0);
$colID->set_visible(false);//rendo invisibile la colonna
$treeview->append_column($colID);
 
//seconda colonna (Cognome)
$colCognome = new GtkTreeViewColumn('Cognome', $cell_renderer, 'text', 1);
$treeview->append_column($colCognome);
 
//terza colonna (nome)
$colNome = new GtkTreeViewColumn('Nome', $cell_renderer, 'text', 2);
$treeview->append_column($colNome);
 
//quarta colonna (Cognome)
$colEmail = new GtkTreeViewColumn('Email', $cell_renderer, 'text', 3);
$treeview->append_column($colEmail);
 
 
//Connessione al db grazie alla classe spdo
$mydb= new sPDO();
$mydb->DSN =  'mysql:host=127.0.0.1;dbname=test-php' ;
$mydb->DBUSER ='root';
$mydb->DBPASS ='';
 
$mydb->connect();
$sql='Select * from persone;' ;
if (!$mydb->execute($sql)) echo 'Problemi nella query';
 
for ($i=1; $i<= $mydb->nrows(); $i++)
{
	$store->append(array($mydb->read($i, "id"),
						$mydb->read($i, "cognome") , 
						$mydb->read($i, "nome")    , 
						$mydb->read($i, "email"))
						);		
}
$mydb->disconnect();
 
 
 
 
//Start the main loop
Gtk::main();
?>

L'interfaccia realizzata con Glade per gtk2 (libglade)

<?xml version="1.0" encoding="UTF-8"?>
<glade-interface>
  <!-- interface-requires gtk+ 2.24 -->
  <!-- interface-naming-policy project-wide -->
  <widget class="GtkWindow" id="window1">
    <property name="width_request">800</property>
    <property name="height_request">600</property>
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <child>
      <widget class="GtkVBox" id="vbox1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <widget class="GtkMenuBar" id="menubar1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <child>
              <widget class="GtkMenuItem" id="menuitem1">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">_File</property>
                <property name="use_underline">True</property>
                <child>
                  <widget class="GtkMenu" id="menu1">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <child>
                      <widget class="GtkImageMenuItem" id="imagemenuitem1">
                        <property name="label">gtk-new</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </widget>
                    </child>
                    <child>
                      <widget class="GtkImageMenuItem" id="imagemenuitem2">
                        <property name="label">gtk-open</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </widget>
                    </child>
                    <child>
                      <widget class="GtkImageMenuItem" id="imagemenuitem3">
                        <property name="label">gtk-save</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </widget>
                    </child>
                    <child>
                      <widget class="GtkImageMenuItem" id="imagemenuitem4">
                        <property name="label">gtk-save-as</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </widget>
                    </child>
                    <child>
                      <widget class="GtkSeparatorMenuItem" id="separatormenuitem1">
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                      </widget>
                    </child>
                    <child>
                      <widget class="GtkImageMenuItem" id="imagemenuitem5">
                        <property name="label">gtk-quit</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </widget>
                    </child>
                  </widget>
                </child>
              </widget>
            </child>
            <child>
              <widget class="GtkMenuItem" id="menuitem2">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">_Modifica</property>
                <property name="use_underline">True</property>
                <child>
                  <widget class="GtkMenu" id="menu2">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <child>
                      <widget class="GtkImageMenuItem" id="imagemenuitem6">
                        <property name="label">gtk-cut</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </widget>
                    </child>
                    <child>
                      <widget class="GtkImageMenuItem" id="imagemenuitem7">
                        <property name="label">gtk-copy</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </widget>
                    </child>
                    <child>
                      <widget class="GtkImageMenuItem" id="imagemenuitem8">
                        <property name="label">gtk-paste</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </widget>
                    </child>
                    <child>
                      <widget class="GtkImageMenuItem" id="imagemenuitem9">
                        <property name="label">gtk-delete</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </widget>
                    </child>
                  </widget>
                </child>
              </widget>
            </child>
            <child>
              <widget class="GtkMenuItem" id="menuitem3">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">_Visualizza</property>
                <property name="use_underline">True</property>
              </widget>
            </child>
            <child>
              <widget class="GtkMenuItem" id="menuitem4">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">A_iuto</property>
                <property name="use_underline">True</property>
                <child>
                  <widget class="GtkMenu" id="menu3">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <child>
                      <widget class="GtkImageMenuItem" id="imagemenuitem10">
                        <property name="label">gtk-about</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </widget>
                    </child>
                  </widget>
                </child>
              </widget>
            </child>
          </widget>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <widget class="GtkHBox" id="hbox1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <child>
              <widget class="GtkScrolledWindow" id="scrolledwindow1">
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="hscrollbar_policy">automatic</property>
                <property name="vscrollbar_policy">automatic</property>
                <child>
                  <widget class="GtkTreeView" id="treeviewMain">
                    <property name="width_request">500</property>
                    <property name="visible">True</property>
                    <property name="can_focus">True</property>
                  </widget>
                </child>
              </widget>
              <packing>
                <property name="expand">True</property>
                <property name="fill">True</property>
                <property name="position">0</property>
              </packing>
            </child>
            <child>
              <widget class="GtkVBox" id="vbox2">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <child>
                  <widget class="GtkLabel" id="Cognome">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <property name="xalign">0.019999999552965164</property>
                    <property name="yalign">1</property>
                    <property name="label" translatable="yes">Cognome
</property>
                  </widget>
                  <packing>
                    <property name="expand">True</property>
                    <property name="fill">True</property>
                    <property name="position">0</property>
                  </packing>
                </child>
                <child>
                  <widget class="GtkEntry" id="txtCognome">
                    <property name="visible">True</property>
                    <property name="can_focus">True</property>
                    <property name="invisible_char"></property>
                    <property name="primary_icon_activatable">False</property>
                    <property name="secondary_icon_activatable">False</property>
                    <property name="primary_icon_sensitive">True</property>
                    <property name="secondary_icon_sensitive">True</property>
                  </widget>
                  <packing>
                    <property name="expand">True</property>
                    <property name="fill">True</property>
                    <property name="position">1</property>
                  </packing>
                </child>
                <child>
                  <widget class="GtkLabel" id="label1">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <property name="xalign">0.029999999329447746</property>
                    <property name="yalign">1</property>
                    <property name="label" translatable="yes">Nome</property>
                  </widget>
                  <packing>
                    <property name="expand">True</property>
                    <property name="fill">True</property>
                    <property name="position">2</property>
                  </packing>
                </child>
                <child>
                  <widget class="GtkEntry" id="txtNome">
                    <property name="visible">True</property>
                    <property name="can_focus">True</property>
                    <property name="invisible_char"></property>
                    <property name="primary_icon_activatable">False</property>
                    <property name="secondary_icon_activatable">False</property>
                    <property name="primary_icon_sensitive">True</property>
                    <property name="secondary_icon_sensitive">True</property>
                  </widget>
                  <packing>
                    <property name="expand">True</property>
                    <property name="fill">True</property>
                    <property name="position">3</property>
                  </packing>
                </child>
                <child>
                  <widget class="GtkLabel" id="label2">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <property name="xalign">0.029999999329447746</property>
                    <property name="yalign">1</property>
                    <property name="label" translatable="yes">Email</property>
                  </widget>
                  <packing>
                    <property name="expand">True</property>
                    <property name="fill">True</property>
                    <property name="position">4</property>
                  </packing>
                </child>
                <child>
                  <widget class="GtkEntry" id="txtEmail">
                    <property name="visible">True</property>
                    <property name="can_focus">True</property>
                    <property name="invisible_char"></property>
                    <property name="primary_icon_activatable">False</property>
                    <property name="secondary_icon_activatable">False</property>
                    <property name="primary_icon_sensitive">True</property>
                    <property name="secondary_icon_sensitive">True</property>
                  </widget>
                  <packing>
                    <property name="expand">True</property>
                    <property name="fill">True</property>
                    <property name="position">5</property>
                  </packing>
                </child>
                <child>
                  <widget class="GtkHBox" id="hbox2">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <property name="spacing">35</property>
                    <child>
                      <widget class="GtkButton" id="btnDelete">
                        <property name="label">gtk-delete</property>
                        <property name="visible">True</property>
                        <property name="can_focus">True</property>
                        <property name="receives_default">True</property>
                        <property name="use_stock">True</property>
                      </widget>
                      <packing>
                        <property name="expand">True</property>
                        <property name="fill">True</property>
                        <property name="position">0</property>
                      </packing>
                    </child>
                    <child>
                      <widget class="GtkButton" id="btnAdd">
                        <property name="label">gtk-add</property>
                        <property name="visible">True</property>
                        <property name="can_focus">True</property>
                        <property name="receives_default">True</property>
                        <property name="use_stock">True</property>
                      </widget>
                      <packing>
                        <property name="expand">True</property>
                        <property name="fill">True</property>
                        <property name="position">1</property>
                      </packing>
                    </child>
                    <child>
                      <widget class="GtkButton" id="btnSave">
                        <property name="label">gtk-save</property>
                        <property name="visible">True</property>
                        <property name="can_focus">True</property>
                        <property name="receives_default">True</property>
                        <property name="use_stock">True</property>
                      </widget>
                      <packing>
                        <property name="expand">True</property>
                        <property name="fill">True</property>
                        <property name="position">2</property>
                      </packing>
                    </child>
                  </widget>
                  <packing>
                    <property name="expand">True</property>
                    <property name="fill">True</property>
                    <property name="position">6</property>
                  </packing>
                </child>
                <child>
                  <widget class="GtkLayout" id="layout1">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <property name="hadjustment">0 0 293 1 263.69999999999999 293</property>
                    <property name="width">182</property>
                    <property name="height">131</property>
                  </widget>
                  <packing>
                    <property name="expand">True</property>
                    <property name="fill">True</property>
                    <property name="padding">196</property>
                    <property name="position">7</property>
                  </packing>
                </child>
              </widget>
              <packing>
                <property name="expand">True</property>
                <property name="fill">True</property>
                <property name="position">1</property>
              </packing>
            </child>
          </widget>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
        <child>
          <placeholder/>
        </child>
      </widget>
    </child>
  </widget>
</glade-interface>

programmazione/php/gtktreeview_e_db_con_php.txt · Ultima modifica: 18/04/2018 - 15:49 (modifica esterna)