Store data from array to database use checkbox

Discussion in 'PHP' started by omccom, Jan 7, 2011.

  1. #1
    I want to store some data/list from array with checkboxes to the database, so I checked the list that will entrance to the database:

    class function thus:
    <?php
    class st_exchange_conv {
    	var $from, $to, $bid, $ask, $stocks, $localised_stocks, $source ;
    	function st_exchange_conv( $source = 'Site_A' ) {
    		$this->from = NULL ;
    		$this->to = NULL ;
    		$this->price = NULL ;
            $this->source = $source ;
            switch ( $source ) {
                case 'Site_A':
                    $this->stocks = $this->localised_stocks = array(
    				'TSM' => 'Tams Star Management',
    				'BVM' => 'Benthul Valid M',
    				'GRT' => 'Guard Rising Tld',
    				'RPI' => 'RP Invesments',
    				'BGS' => 'Belgrado SSd') ;
                    break ;
                case 'Site_B':
                default:
                    $this->stocks = $this->localised_stocks = array(
    				'BCA' => 'Bursa Asi',
    				'DZD' => 'Data ZeendetDung',
    				'XAL' => 'Aluminium Mining',
    				'RPI' => 'RP Invesments',
    				'AWG' => 'Aqua Water Gas',
    				'VAL' => 'Volun All Lintas',
    				'BSD' => 'Bumi SeeDs') ;
                    break ;
            }
    	}
    	function convert( $from, $to, $date = NULL ) {
    		$this->from = $from ;
    		$this->to = $to ; 
            if ( !$date )
            {  // Date parameter absent - default to now
                $date = time() ; 
            } else if ( !is_numeric( $date ) )
            { // Date parameter isn't a timestamp, so treat as text representation of a date
                $date = strtotime( $date ) ;
            } 
            if ( $from == $to )     {
                $this->price = 1 ;
                return TRUE ;
            }
            switch ( $this->source )
            {
                case 'Site_A':
                    $url = 'http://www.site_a.com/conv?a=1&from=' . $from . '&to=' . $to ;
                    $pattern = '#\sid=stocks[^0-9]*[0-9.]+[^A-Z]*' . $from . '[^A-Z][^0-9]+([0-9.]+)[^A-Z]*' . $to . '[^A-Z]#' ;
                    return $this->fetchSourceData( $url, $pattern, 1, 0, 1, 'curl' ) ;    
                    break; 
                case 'Site_B':
                default:
                    $url = 'http://site_b.com/conv?e=sv&f=st1&s='. $from . $to .'=V';
                    $pattern = '#"' . $from . $to . '=V",([0-9.]*),"#' ;
                    $max_matches = 1 ;
                    $result_match = 0 ;
                    $conv_amt = 1 ;
                    $method = 'fopen' ;
                    return $this->fetchSourceData( $url, $pattern, $max_matches, $result_match, $conv_amt, $method ) ;           
                    break ;
            }
    	}
        function fetchSourceData( $url, $pattern, $max_matches, $result_match, $conv_amt, $method="curl" ) {
            // Fetch source page
            switch ( $method ) {
                case 'curl':       
                    $ch = curl_init();
                    curl_setopt( $ch, CURLOPT_URL, $url ) ;
                    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
                    curl_setopt( $ch, CURLOPT_HEADER, 1 );
                    curl_setopt( $ch, CURLOPT_VERBOSE, 1 ) ;
                    $page = curl_exec( $ch ) ;
                    curl_close ($ch);
                    break;
                case 'fopen':
                    $ch = @fopen( $url, 'r' ) ;      
                    if ( $ch ) {  
                        $page = fgets( $ch, 4096 ) ;  
                        fclose( $ch ) ;  
                    } else {
                        $page = 'FAIL' ;
                    } 
                    break ;
            }
            // Capture result field
            preg_match_all( $pattern, $page, $matches ) ;
            // Lookup has failed unless at least and no more than the maximum expected one non-zero matches were achieved
            if ( count( $matches[1] ) == 0 OR count( $matches[1] ) > $max_matches OR $matches[1][ $result_match ] == 0 ) 
            {			return FALSE;
            } else {
                $decimal_result = $matches[1][ $result_match ] / $conv_amt ;
                if ( !settype( $decimal_result, 'float' ) ) return FALSE ;
                $this->price = $decimal_result ;
                return TRUE ;
            }
        }
    	function price( $amount = 1.00, $unused = '' ) {
    		return $amount * $this->price ;
    	}
    	function name( $symbol ) {
    		return ( $symbol == 'from' ) ?
    			$this->localised_stocks[ $this->from ] : $this->localised_stocks[ $this->to ] ;
    	}
    	function stocks() {
    		return $this->localised_stocks ;
    	}
    }
    ?>
    PHP:
    script stores the database as follows:
    session_start();
    if(isset($_POST['submit']))
    {
    	include('class/stockconvert_class.php');
    	$st = new st_exchange_conv(DEFAULT_SOURCE);
    	$from = mysql_real_escape_string(stripslashes($_POST['from']));
    	$value = floatval($_POST['amount']);
    	$date = date('Y-m-d H:i:s');
    	$_SESSION['selected'] = $from;
    	$stocks = $st->stocks();
    	asort($stocks);
    	foreach($stocks as $key=>$stock)
    	{
    		$st->convert($from,$key,$date);
    		$stc_price = $st->price($value);
    		$stock = mysql_real_escape_string(stripslashes($stock));
    		$count = "SELECT * FROM oc_stock WHERE stock = '$key'";
    		$result = mysql_query($count) or die(mysql_error());
    		$sql = '';
    		if(mysql_num_rows($result) == 1)
    		{
    			$sql = "UPDATE oc_stock SET stock_title = '$stock', stc_val = '$stc_price', date_updated = '$date' WHERE stock = '$key'";
    		}
    		else
    		{		
    		$sql = "INSERT INTO oc_stock(stock_id,stock_title,stock,decimal_place,stc_val,date_updated) VALUES ('','$stock','$key','2',$stc_price,'$date')";
    		}	
    		$result = mysql_query($sql) or die(mysql_error().'<br />'.$sql);
    	}
    	header("Location: index.php");
    	exit();
    }
    PHP:
    while from its checkbox as follows:
    session_start();
    define('DEFAULT_SOURCE','Site_A'); 
    define('DEFAULT_VALUE',100);
    define('DEFAULT_STC','BGS');
    include('class/stockconvert_class.php');
    $st = new st_exchange_conv(DEFAULT_SOURCE);
    ?>
    <form action="do.php" method="post">
    <label for="amount">Amount:</label>
    <input type="input" name="amount" id="amount" value="1"><input type="submit" name="submit" value="Convert">
    <?php
    $stocks = $st->stocks();
    asort($stocks);
    foreach($stocks as $key=>$stock)
    {
    	if((isset($_SESSION['selected']) && strcmp($_SESSION['selected'],$key) == 0) || (!isset($_SESSION['selected']) && strcmp(DEFAULT_STC,$key) == 0))
    	{
    	?>
    	<br><input type="checkbox" id="scb1" name="from[]" value="<?php echo $key; ?>" checked="checked"><?php echo $stock; ?>
    	<?php
    	}
    	else
    	{
    	?>
    	<br><input type="checkbox" id="scb1" name="from[]" value="<?php echo $key; ?>"><?php echo $stock; ?>
    	<?php
    	}
    }
    ?>
    PHP:
    and error as follows:
    Fatal error: Call to undefined method st_exchange_conv::convert() in C:\xampp\htdocs\test\do.php on line 21

    line 21 is $st->convert($from,$key,$date);

    there seems to be wrong in its checkbox form, but I expect though more definite clues, Thanks in advance..
     
    omccom, Jan 7, 2011 IP
  2. Moustafa.Elkady

    Moustafa.Elkady Member

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    31
    #2
    Moustafa.Elkady, Jan 8, 2011 IP
  3. omccom

    omccom Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the link, but it seem different to me or perhaps I need more pointers how to use it with my case
     
    omccom, Jan 8, 2011 IP