Guest User

Untitled

a guest
Nov 23rd, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.37 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Funzione che cancella gli spazi finali di una stringa
  5.  *
  6.  * @param String $string
  7.  *
  8.  * @return String Una stringa uguale a quella passata in input ma senza spazi finali
  9.  */
  10. function cancellaSpaziFinali($string) {
  11.         $len = strlen($string)-1;
  12.         for ($i=$len; $i>0; $i--)
  13.                 if ($string[$i] != " ")
  14.                         break;
  15.        
  16.         return substr($string, 0, $i + 1);
  17. }
  18.  
  19.         $query = $_GET[QUERY];
  20.  
  21.  
  22.         $db = pg_connect("host=127.0.0.1 dbname=ingsw12_03 user=postgres password=poppe);
  23.        if ($db) {
  24.  
  25.                $result = pg_query($query);
  26.                if (!$result) {
  27.                    echo "Problem with query " . $query . "<br/>";
  28.                    echo pg_last_error();
  29.                    exit();
  30.                }
  31.              
  32.                $output = "";
  33.              
  34.                /* per ogni riga della tabella */
  35.                while($arr = pg_fetch_array($result, NULL, PGSQL_ASSOC)) {
  36.                      
  37.                        /* leggo le chiavi della riga */
  38.                        $keys = array_keys($arr);
  39.                      
  40.                        /* se รจ il primo ciclo del while scrivo nella prima linea dell'output le chiavi usate nella tabella */
  41.                        if ($output == "") {
  42.                                for ($i=0; $i<count($keys); $i++)
  43.                                        $output .= $keys[$i] . "<tc>";
  44.                                $output .= "<tr>\n";
  45.                        }
  46.                      
  47.                        /* per ogni colonna della tabella */
  48.                        for ($i=0; $i<count($keys); $i++) {
  49.                              
  50.                                /* leggo l'item e lo aggiungo all'output */
  51.                                if ($i == count($keys) - 1)
  52.                                        $output .= cancellaSpaziFinali($arr[$keys[$i]]);
  53.                                else
  54.                                        $output .= cancellaSpaziFinali($arr[$keys[$i]]) . "<tc>";
  55.                        }
  56.                        $output .= "<tr>\n";
  57.                }
  58.              
  59.                //echo("<pre><xmp>");
  60.                header('Content-type: text/plain');
  61.                echo($output);
  62.                //echo("</xmp></pre>");
  63.              
  64.        }
  65.  
  66. ?>
Add Comment
Please, Sign In to add comment