HTML_PHP
HTML_PHP

Reputation: 43

Create a function out of this php code

I just need some help creating a php function out of this code or in other words just to wrap this code in a php function :

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();


            }
        }
    }

Thanks.

Upvotes: 2

Views: 169

Answers (4)

AlexP
AlexP

Reputation: 9857

The idea of creating a function is to provide reuseable code. This means you are encapsulating the logic, allowing you to change the inner workings of the code without it affecting the actual usage of the function and to avoid tedious repetition.

In your example you should think about the areas that fall into this category. I personally can see that several functions that could be made here.

Example, not run but should give you ideas.

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

?>

Upvotes: 0

DrinkJavaCodeJava
DrinkJavaCodeJava

Reputation: 808

Another way to do it is to return the echo statement as a string.

Upvotes: 1

ermal
ermal

Reputation: 151

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();
    }
}

Upvotes: 2

Laoujin
Laoujin

Reputation: 10229

Really easy :)

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();


        }
    }
}
}

Upvotes: 4

Related Questions