函数参数,返回数据库表中的所有记录

时间:2011-12-02 04:52:11

标签: php

我有一个循环遍历并返回数据库表中所有记录的脚本,代码如下。

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";
}

1 个答案:

答案 0 :(得分:0)

如果我理解正确,这是创建对象的主要候选者。至于使用它来创建一个函数,我认为在for循环中处理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);
}

更新 - 课程示例

好的,希望这不会吓跑你。在某些时候,每个人都害怕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);

?>