Visualizzazione dei risultati da 1 a 5 su 5

Discussione: calcoli con php

  1. #1
    Utente di HTML.it
    Registrato dal
    Feb 2002
    Messaggi
    284

    calcoli con php

    Scusate volevo chiedervi una info

    Ho dei dati in una tabella sql con 5 campi che contengono valori e date

    avrei necessità di fare delle operazioni su questi valori ,

    ad esempio se il valore di un determinato campo è maggiore di un'altro allora si dovrebbe visualizzare una scritta ..

    si può fare con php ?

    Ciao
    Grazie

  2. #2
    si certamente, ti basta richiamare i due campi tramite una query e poi paragonarli con un if.

    Errare e' umano, ma per fare veramente casino serve la password di root.
    Coltiva Linux........Tanto windows si impianta da solo!!!!!
    PHP 4 Ever

  3. #3
    Utente di HTML.it
    Registrato dal
    Feb 2002
    Messaggi
    284
    mettiamo che il mio campo si chiama prezzo

    come faccio ad esempio ha scrivere

    se prezzo con id 2 è maggiore di prezzo con id 10 stampa ok


    Grazie
    Ciao

  4. #4
    la faccenda è diversa da come l'avevo capita, comunque si può fare.

    Io ho pensato a questo metodo, richiami con due query distinte i due valori di prezzo, e poi li compari. T faccio un esempio di codice nel quale metto i due id che mi hai dato in $ida e $idb. Il file db.php che viene incluso è un file che serve a semplificarti la vita con il db, ti riporto il codice.

    codice:
    <?php
    $ida = "2";
    $idb = "10";
    
    include_once("db.php");
    $db = new db;
    $db->dati['sql_user'] = "";
    $db->dati['sql_pass'] = "";
    $db->dati['sql_host'] = "";
    $db->dati['sql_database'] = "";
    $db->connect();
    
    $db->query("SELECT * FROM `tabella` WHERE id = ".$ida);
    		while($row = $db->fetch_row){
    		$prezzo['a'] = $row['prezzo'];
    		}
    $db->query("SELECT * FROM `tabella` WHERE id = ".$idb);
    		while($row = $db->fetch_row){
    		$prezzo['b'] = $row['prezzo'];
    		}
    
    if ( $prezzo['a'] = $prezzo['b']){
    print "OK";
    }
    
    $db->close_db();
    ?>
    db.php:
    codice:
    <?php
    class db{
        var $dati = array ( "sql_database"   => ""         ,
                            "sql_user"       => "root"     ,
                            "sql_pass"       => ""         ,
                            "sql_host"       => "localhost",
                          );
                         
         var $query_id      = "";
         var $connection_id = "";
    	 var $query_d      = "";
    
    function connect(){
    $this->connection_id = mysql_connect( $this->dati['sql_host'] ,
    												  $this->dati['sql_user'] ,
    												  $this->dati['sql_pass'] 
    												);
    											
    if ( !mysql_select_db($this->dati['sql_database'], $this->connection_id) )
            {
                echo ("ERRORE: Impossibile trovare il database ");
            }
    }
    
    function query($the_query = ""){
        	if ($the_query == "")
        	{
        		$the_query = $this->query_d;
        	}
            $this->query_id = mysql_query($the_query, $this->connection_id);
    		return $this->query_id;
    		}
    		
    		function insert($tbl,$nam,$val){
             $sql_ins = "INSERT INTO $tbl ( $nam ) VALUES ( $val )";
             $this->query_d = $sql_ins;
             return $sql_ins;
    		}
    		function select($tbl,$wh,$val){
             $sql_sel = "SELECT $wh FROM $tbl";
             $this->query_d = $sql_sel;
             return $sql_sel;
    		}
         function update($tbl,$val){
              $sql_upd = "UPDATE $tbl SET $val";
              $this->query_d = $sql_upd;
              return $sql_upd;
            }
    	function where($val,$pre = ""){
    	if ($pre == "")
        	{
        		$pre = $this->query_d;
        	}
    		  $sql_wh = $pre." WHERE ".$val;
              $this->query_d = $sql_wh;
              return $sql_wh;
    		  }
    	function order($val,$mod = "ASC",$pre = ""){
    	if ($pre == "")
        	{
        		$pre = $this->query_d;
        	}
    		  $sql_ob = $pre." ORDER BY".$val.$mod;
              $this->query_d = $sql_ob;
              return $sql_ob;
    		  }
    		
    		
    function fetch_row($query_id = "") {
        
        	if ($query_id == "")
        	{
        		$query_id = $this->query_id;
        	}
        	
            $this->record_row = mysql_fetch_array($query_id, MYSQL_ASSOC);
            
            return $this->record_row;
            
        }
    	
    	function get_affected_rows() {
            return mysql_affected_rows($this->connection_id);
        }
    	
    	function get_num_rows() {
            return mysql_num_rows($this->query_id);
        }
    	
    	function get_insert_id() {
            return mysql_insert_id($this->connection_id);
        }
    	
    	function free_result($query_id="") {
        
       		if ($query_id == "") {
        		$query_id = $this->query_id;
        	}
        	
        	@mysql_free_result($query_id);
        }
    	
    	function close_db() { 
            return mysql_close($this->connection_id);
        }
    	
    	function compile_insert($data) {
        
        	$field_names  = "";
    		$field_values = "";
    		
    		foreach ($data as $k => $v)
    		{
    			$v = preg_replace( "/'/", "\\'", $v );
    			
    			$field_names  .= "$k,";
    			$field_values .= "'$v',";
    		}
    		
    		$field_names  = preg_replace( "/,$/" , "" , $field_names  );
    		$field_values = preg_replace( "/,$/" , "" , $field_values );
    		
    		return array( 'FIELD_NAMES'  => $field_names,
    					  'FIELD_VALUES' => $field_values,
    					);
    	}
    	
    	function compile_update($data) {
    		
    		$return_string = "";
    		
    		foreach ($data as $k => $v)
    		{
    			$v = preg_replace( "/'/", "\\'", $v );
    			$return_string .= $k . "='".$v."',";
    		}
    		
    		$return_string = preg_replace( "/,$/" , "" , $return_string );
    		
    		return $return_string;
    	}
    	
    	function exe(){
    	$query_exe = $this->query($this->query_d);
    	return $query_exe;
    	}
    
    
    }
    
    ?>

    Errare e' umano, ma per fare veramente casino serve la password di root.
    Coltiva Linux........Tanto windows si impianta da solo!!!!!
    PHP 4 Ever

  5. #5
    Utente di HTML.it
    Registrato dal
    Feb 2002
    Messaggi
    284
    Grazie Mix, sei stato molto gentile

    possibile che per fare quella cosa sia necessario tutto questo codice ? non ci sarà qualche via piu semplice ..

    Il file db.php a cosa serve ?


    Grazie mille
    A presto.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.