用这个PHP代码创建一个函数

时间:2012-10-08 22:54:24

标签: php

我只是需要一些帮助从这段代码创建一个php函数,或者换句话说只是将这段代码包装在一个php函数中:

if (isset($_GET['id'])){

    $username = mysql_real_escape_string($_GET['id']);

    if(ctype_alnum($username)){

        $check = mysql_query("SELECT username,first_name FROM users WHERE username='$username'");
        if(mysql_num_rows($check)===1){

            $get = mysql_fetch_assoc($check);
            $username = $get['username'];
            $firstname = $get['first_name'];

            echo '<div id="mini_profile">This is '.$username.'\'s profile.</div>';

            }else{

            header("Location: index.php");
            exit();


            }
        }
    }

感谢。

4 个答案:

答案 0 :(得分:4)

非常简单:)

function yourFunc() {
if (isset($_GET['id'])){

$username = mysql_real_escape_string($_GET['id']);

if(ctype_alnum($username)){

    $check = mysql_query("SELECT username,first_name FROM users WHERE username='$username'");
    if(mysql_num_rows($check)===1){

        $get = mysql_fetch_assoc($check);
        $username = $get['username'];
        $firstname = $get['first_name'];

        echo '<div id="mini_profile">This is '.$username.'\'s profile.</div>';

        }else{

        header("Location: index.php");
        exit();


        }
    }
}
}

答案 1 :(得分:2)

function getMyDivElement($name) {
    $username = mysql_real_escape_string($name);

    if(ctype_alnum($username)) {
        $check = mysql_query("SELECT username,first_name FROM users WHERE username='{$username}'");
        if(is_resource($check) && ($get = mysql_fetch_assoc($check))) {
            $username = $get['username'];
            $firstname = $get['first_name']; //You need this?

            return '<div id="mini_profile">This is '.$username.'\'s profile.</div>';
        }
    }

    return null;
}

//usage
if (isset($_GET['id'])) {
    $div = getMyDivElement($_GET['id']);
    if($div) {
        echo $div;
    } else {
        header("Location: index.php");
        exit();
    }
}

答案 2 :(得分:1)

另一种方法是将echo语句作为字符串返回。

答案 3 :(得分:0)

创建函数的想法是提供可重用的代码。这意味着您正在封装逻辑,允许您更改代码的内部工作方式,而不会影响函数的实际使用并避免繁琐的重复。

在您的示例中,您应该考虑属于此类别的区域。我个人可以看到可以在这里完成的几个功能。

示例,不要运行,但应该给你一些想法。

<?php

  function getUser($username)
  {
    if (is_string($username) && strlen($username)) {
      $query  = "
        SELECT 
          username, firstname 
        FROM 
          users 
        WHERE 
          username = :username
      ";  
      $result = executeQuery($query, array("username" => $username));
      return $result->fetch();
    }
  }

  function getDatabase($host, $db, $user, $password)
  { 
    return new PDO("mysql:host=$host;dbname=$dbname, $user, $pass");
  }

  function executeQuery($sql, array $params = array())
  {
    $db   = getDatabase();
    $conn = $db->prepare($sql);

    return $conn->execute($params);
  }

  function validateInput($input)
  {
    return ctype_alnum($input);
  }

  function advanceTo($page, $params) 
  {
    header("Location: $page.php");
    exit();
  }


if (isset($_GET["username"])){
  if (validateInput($_GET["username"])) {
    $user = getUser($_GET["username"]);
    if (! empty($user)) {
      // authUserAndSetSessionForUser($user);
      /** This page is then directed to and welcome message shown **/
      advanceTo("user-home-page", array($user));
    } else {
      advanceTo("index");
    }
  }
}

?>