2011-12-02 164 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