2011-12-02 136 views
0

我有一個腳本循環並返回數據庫表中的所有記錄,代碼如下。函數參數,返回數據庫表中的所有記錄

PHP:

for($i=0;$i<$group_layer_row;$i++){ 
$my_layer_string="MyMap_".mb_convert_encoding(mssql_result ($rs_group_layer, $i, 0),"UTF-8","SJIS")."_".mb_convert_encoding(mssql_result ($rs_group_layer, $i, 1),"UTF-8","SJIS"); 
echo "var ".$my_layer_string.";\n"; 
} 

我所試圖做的就是把它變成一個說法。有點像這樣(這是一個例子,請不要判斷)。

PHP:

function getLayers(){ 
$my_layer_string="MyMap_".mb_convert_encoding(mssql_result ($rs_group_layer, $i, 0),"UTF-8","SJIS")."_".mb_convert_encoding(mssql_result ($rs_group_layer, $i, 1),"UTF-8","SJIS"); 
$layers="var ".$my_layer_string.";\n"; 
echo $layers; 
} 
for($i=0;$i<$group_layer_row;$i++){ 
getLayers(); 
} 

任何幫助,在此將是非常讚賞。

僅供參考我包括SQL查詢

$sql= "SELECT * FROM m_group_layer WHERE group_id=\"".$_SESSION["group_id"]."\" ORDER BY display_order"; 
$rs_group_layer= mssql_query ($sql, $con); 
$group_layer_row =mssql_num_rows($rs_group_layer); 

編輯:這幾乎是完全相同的循環只是不同的輸出。

for($i=0;$i<$group_layer_row;$i++){ 
$my_layer_string="MyMap_".mb_convert_encoding(mssql_result ($rs_group_layer, $i, 0),"UTF-8","SJIS")."_".mb_convert_encoding(mssql_result ($rs_group_layer, $i, 1),"UTF-8","SJIS"); 
echo "".$my_layer_string." = new OpenLayers.Layer.WMS(\"".$my_layer_string."\",\"http://192.0.0.0/cgi-bin/mapserv.exe?map=C:/ms4w/Apache/htdocs/mapserver/data/toyama/toyama_mymap.map&service=WMS&SRS=EPSG:2449&VERSION=1.1.1&format=image/PNG&layers=".$my_layer_string."\", {'layers': '".$my_layer_string."'}, {isBaseLayer: false, visibility: false,opacity:0.5,alpha:true}); 
map.addLayer(".$my_layer_string.");\n"; 
} 
+0

$ rs_group_layer和$我需要作爲getLayers函數的參數傳遞,例如,函數getLayers($ rs_group_layer,$ i){...} – subroutines

回答

0

如果我的理解正確,這是創建對象的主要候選人。至於使用它的函數,我認爲在for循環中處理db結果集要明顯得多。我沒有看到創建函數的好處(因爲將循環中的db結果傳遞給循環中的函數效率非常低)。也許你可能會澄清你想要一個功能或你想要完成的理由?

但是,如果你真的想要做的功能出來的,那將幾乎看你如何概括它...

function getLayer($result_set, $row) { 
    $str = "MyMap_" . mb_convert_encoding(mssql_result($result_set, $i, 0),"UTF-8","SJIS") 
    $str .= "_".mb_convert_encoding(mssql_result($result_set, $i, 1),"UTF-8","SJIS"); 
    return "var MyMap_$str;\n"; 
} 

// $con = ... 
$sql = "SELECT * FROM m_group_layer WHERE group_id=\"".$_SESSION["group_id"]."\" ORDER BY display_order"; 
$result = mssql_query ($sql, $con); 
$row_count = mssql_num_rows($result); 

for($i=0; $i<$row_count; $i++){ 
    echo getLayer($result, $i); 
} 

UPDATE - 類的例子

好吧,希望這不會嚇跑你。每個人都在某個時刻害怕OOP。重要的是要繼續努力,最終你會喜歡,'OMG I < 3 OOP喜歡GAGA喜歡她的小怪物!

我試圖儘可能地記錄。只有這麼多,你可以解釋,而不是教一個學期的課程:)如何使用它在代碼的底部。

<?php 

/** 
* Retrieves layers from the db and provides methods for outputting 
*/ 
class LayerMaker 
{ 
    /** 
    * Our MSSQL database connection 
    * @var MSSQL connection resource 
    */ 
    protected $db_conn; 

    /** 
    * Our array of records from the DB 
    * @var array 
    */ 
    protected $records; 

    /** 
    * Constructor function 
    * 
    * Called when you first instantiate the object. If you specify 
    * the db_conn, it will go ahead and retrieve the records as 
    * soon as the object is created. 
    * 
    * @param MSSQL connection resource $db_conn 
    * 
    * @return void 
    */ 
    public function __construct($db_conn=NULL) 
    { 
    if ($db_conn) { 
     $this->set_db_conn($db_conn); 
     $this->records = $this->query_db(); 
    } 
    } 

    /** 
    * Setter function for protected $db_conn property 
    * 
    * You could just as easily create a method in the object 
    * to create the db connection, but for testing reasons that 
    * you likely don't care about it's better to inject the 
    * db connection into our object using a setter function like this. 
    * 
    * @param MSSQL link identifier $db_conn 
    */ 
    public function set_db_conn($db_conn) 
    { 
    $this->db_conn = $db_conn 
    } 

    /** 
    * How we get the records from the database into our object's $results property 
    * 
    * @return MSSQL record set on success or FALSE if no db connection is set 
    */ 
    protected function query_db() 
    { 
    // make sure we've set a database connection to use 
    // query the db and return the results 
    $sql = 'SELECT * FROM m_group_layer WHERE group_id="' . 
     $_SESSION["group_id"] . '" ORDER BY display_order'; 
    return mssql_query($sql, $this->db_conn); 
    } 

    /** 
    * A function to get a count of the rows in our result set 
    * 
    * @return int Rows in the result property 
    */ 
    public function count_result_rows() 
    { 
    if ($this->records) { 
     return mssql_num_rows($this->records); 
    } 
    return 0; 
    } 

    /** 
    * Wrapper for mb_convert_encoding function 
    * 
    * @return string 
    */ 
    protected function layer_builder($row) 
    { 
    $str0 = mb_convert_encoding(mssql_result($this->records, $row, 0),"UTF-8","SJIS") 
    $str1 = mb_convert_encoding(mssql_result($this->records, $row, 1),"UTF-8","SJIS"); 

    return "var MyMap_$str0_$str1"; 
    } 

    /** 
    * Finally, build our layers! 
    * 
    * @param int $row Result set row number 
    * 
    * @return mixed Layer string if $row specified or Array of all layer strings 
    *    if no specific row requested 
    */ 
    public function get_layers($row=NULL) 
    { 
    if ($row) { 
     // if we want one specific row ... 
     return $this->layer_builder($row); 
    } else { 
     // otherwise, give us back an array of all the rows 
     $layers = array(); 
     for($i=0; $i<$this->count_result_rows(); $i++){ 
     $layers[] = $this->layer_builder($i 
     } 
     return $layers; 
    } 
    } 

    /** 
    * Getter function for protected $records property 
    * 
    * Useful because you might want access to the resultset 
    * outside of the object context. 
    * 
    * @return array MSSQL record set 
    */ 
    public function get_records() 
    { 
    return $this->records; 
    } 
} 


// Now this is how you could use it 
$conn = (however you retrieve a db connection); 
$layer_obj = new LayerMaker($conn); 
$layers = $layer_obj->get_layers(); 

print_r($layers); 

?> 
+0

非常感謝您的迴應。是的,一個類實際上是最好的,因爲我幾次使用這個循環的副本,只是輸出稍有不同。所以最好的辦法是能夠實例化一個類。不幸的是我很害怕面向對象。我已經添加了這個循環的另一個例子。如果你能幫助一個班級,我會非常感激。再次感謝。 – Yus

+0

當然...我會先做,但我想確保它會很有用,然後我花了時間:)即將到來... – rdlowrey

+0

大聲笑。非常感謝 – Yus

相關問題