2011-12-02 14 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); 

EDIT含む午前参考

:これは、ほぼちょうど異なる出力とまったく同じループです。

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と$ iは、getLayers関数の引数として渡す必要があります。関数getLayers($ rs_group_layer、$ i){...} – subroutines

答えて

0

私が正しく理解している場合、これはオブジェクトを作成するための主要な候補です。関数を作成する場合は、forループでdbの結果セットを処理する方がはるかにクリーンであると思います。私は関数を作成することに多くの利益を見ません(ループ内の関数に前後のDB結果を渡すことは非常に非効率的です)。おそらく、あなたは機能を望んでいるか、あなたが達成しようとしていることについてのあなたの推論を明確にするかもしれませんか?うまくいけばCLASS例

さて、 - あなたが本当にそれはかなりあなたがそれを概説どのように見えるそれから機能を作りたい場合

しかし、...

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 LIKE 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

ありがとうございました。はい、クラスは実際には最高です。私はこのループのコピーをわずかに異なる出力で数回使っています。したがって、クラスをインスタンス化するだけの方がよいでしょう。残念ながら、私はOOを恐れています。私はこのループの別の例を追加しました。あなたがクラスを手伝うことができたら、私はずっと義務づけられています。再度、感謝します。 – Yus

+0

確かに...私はそれを最初にやっただろうが、私は時間を過ごす前にそれが有用であることを確かめたいと思った:)来るべき... – rdlowrey

+0

笑。ありがとうalot – Yus