2011-12-02 2 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= "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); 

편집 SQL 쿼리를 포함하고 참고로

이 거의 단지 다른 출력과 동일한 루프입니다.

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 함수의 인수로 전달되어야합니다. function 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 명의 가프가 들려주는 작은 괴물들 !!!

가능한 한 많이 문서화하려고했습니다. 학기 과정을 가르치지 않고 설명 할 수있는 부분이 너무 많습니다. 코드를 사용하는 방법은 코드 하단에 나와 있습니다.

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

확실하게 ... 나는 그것을 먼저했을 것입니다. 그러나 나는 시간을 보냈을 때 유용 할 것입니다. :) soon ... – rdlowrey

+0

권자. 고마워요 – Yus

관련 문제