Shibing's Blog Powerful New Features of PHP, Powerful New Shibing’s Blog~!

3Feb/110

Assignment #3, FactOrFib – Factorial and Fibonacci Number Calculator

Source Code update,  code is already commented. So they are easy to understand.

JavaDocs are available at: http://doc.shibingsprojects.com/javadoc/FactOrFib

Source codes are below.

The main class and method for the program, aka, the kicker.


package edu.heidelberg.factorfib.cps350.shuang;

/**
* FactOrFib main class.
*
* <p>The main class to drive the whole program.
*
*
* @author Shibing Huang <mr .shibing @ mac.com>
* <p>Heidelberg University
*
* @version 2011.01.23
*/

public class FOFMain {

/**
* Main class for FactOrFib
*
* Just a short and quick driver
*
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub

FOFIODriver pgmStart = new FOFIODriver();

pgmStart.cmdEntry();

}

}

28Apr/100

Final Project Source Code *updating

MySQL PHP Examples

<?php

$sqlUsrName = "iw3htp4";
$sqlPwd = "iw3htp4";
$database = "bookmarks";

mysql_connect(localhost,$sqlUsrName,$sqlPwd);
@mysql_select_db($database) or die( "Unable to select database");

$query = "SELECT * FROM user";
$test = mysql_query($query);

while($row = mysql_fetch_array($test, MYSQL_ASSOC))
{
    printf("ID: %s  Name: %s Password %s", $row["user_id"], $row["user_name"], $row["password"] . "<br />");
}

mysql_free_result($test);

mysql_close();
?>

SQL statement for INNER JOIN two tables

<?php

$sqlUsrName = "iw3htp4";
$sqlPwd = "iw3htp4";
$database = "bookmarks";

mysql_connect(localhost,$sqlUsrName,$sqlPwd);
@mysql_select_db($database) or die( "Unable to select database");

$query = "SELECT * FROM bmark
            INNER JOIN user
            ON bmark.user_id = user.user_id
            WHERE bmark.user_id ='1'";
$test = mysql_query($query);

while($row = mysql_fetch_array($test, MYSQL_ASSOC))
{
    printf("ID: %s Name: %s  Title: %s URL %s", $row["user_id"], $row["user_name"], $row["page_title"], $row["url"] . "<br />");
}

mysql_free_result($test);

mysql_close();
?>

for inner join 3 tables

<?php

$sqlUsrName = "iw3htp4";
$sqlPwd = "iw3htp4";
$database = "bookmarks";

mysql_connect(localhost,$sqlUsrName,$sqlPwd);
@mysql_select_db($database) or die( "Unable to select database");

$query = "SELECT * FROM bmark, category, user
            WHERE bmark.user_id = user.user_id
            AND bmark.category_id = category.category_id
            AND bmark.user_id='1'";
$test = mysql_query($query);

while($row = mysql_fetch_array($test, MYSQL_ASSOC))
{
    printf("ID: %s Name: %s  Title: %s URL %s CAT %s", $row["user_id"], $row["user_name"], $row["page_title"], $row["url"], $row["category_name"]. "<br />");
}

mysql_free_result($test);

mysql_close();
?>

A better habit and better way to organize codes is....
To put the actual functional codes into separate pages...
For example, you would actually split header, content(body) and footer into different pages and put the SQL connection page in a other file. Well, let's take a look...

For a Index page, you only have things that are necessary

<?php

$title ="Index Page";

include 'includes/header.php';
include 'includes/sqlconn.php';

$query = "SELECT * FROM bmark, category, user
            WHERE bmark.user_id = user.user_id
            AND bmark.category_id = category.category_id
            AND bmark.user_id='1'";

$test = sqlConnection($query);

while($row = mysql_fetch_array($test, MYSQL_ASSOC))
{
    printf("ID: %s Name: %s  Title: %s URL %s CAT %s", $row["user_id"], $row["user_name"], $row["page_title"], $row["url"], $row["category_name"]. "<br />");
}

sqlConnClose($test);

include 'includes/footer.php';
?>

So...what you see above are just basically includes and function calls. But in fact, you are implementing a whole page. Think about it.

okay, session control is not that hard at all.
IF you want to start a session, basically just call to session_start(), and it will do it.

to log on, you first need to connect to database and check to see if user and password match the database records.

a password/username check can be easily done with this:

function dataCheck($query)
{
    $result = sqlConnection($query);
    $check = mysql_num_rows($result);
    $checkResult = $check;

    sqlConnClose($result);

    return $checkResult;
}

and...the connection function is easy too

function sqlConnection($query)
{
    $sqlUsrName = "iw3htp4";
    $sqlPwd = "iw3htp4";
    $database = "bookmarks";

    mysql_connect('localhost',$sqlUsrName,$sqlPwd);
    @mysql_select_db($database) or die( "Unable to select database");

    $test = mysql_query($query);

    return $test;
}

so, you only need to check the return of the value is 0 or not. If it is 0, then it means it's mismatch.

To start a check, you simply select username and password and compare them.
Then, if match successful, you can start a session

[code lang = 'php']
include 'includes/sqlconn.php';
$query = "SELECT user_name, password FROM user
WHERE user_name='$usrName'
AND password='$usrPwd'";

$check = dataCheck($query);

//verify name and password
if($check === 0)
{
print ("

User Name and Password Mismatch!

");
}

else
{
session_start();
$_SESSION['username'] = $usrName;

header( 'Location: index.php' ) ;
}
[/code]

well, not you have a session!
the following code is to check the existence of session and return a logout function

<?php

//check session existance and output links
function sessionCheck()
{
    session_start();
    if(isset ($_SESSION['username']))
    {

        $result = '<a href="manage.php" title="Click Here To Manage Your Personal Bookmarks and Account">Logged In as: ' . $_SESSION['username'] . '</a>
                    ' . '&amp;nbsp;' .'<a href = "?action=logout">[Logout]</a>';
        if(isset($_GET['action']))
        {
            $action = $_GET['action'];
            if ($action == "logout")
            {
                session_destroy();
                header( 'Location: index.php' ) ;
            }
        }
    }

    else
    {
        $result = '<a href="login.php" title="Click Here To Register Or Login">Login | Register </a>';
    }

    return $result;
}

//check session existance
function sessionExists()
{
    if(isset ($_SESSION['username']))
    {
        $result = true;

    }

    else
    {
        $result = false;
    }

    return $result;

}

?>
Filed under: Uncategorized No Comments
15Apr/100

Web App Dev. HW #5 Source Codes

processSurvey.php

30Aug/090

其實我也不知道該怎麼說

有個blog已經很久了,從來不維護,也從不更新~~

於是,這裡就看著辦吧~~~該怎麼樣就怎麼樣

Filed under: Uncategorized No Comments
1Apr/090

CPS 311 – April 1st Lecture

Lists

Unsorted list

– A list in which elements are placed in no particular order; 

the only relationship between data elements is the list 

predecessor and successor relationships 

Sorted list Sorted list

– A list that is sorted by some property of its elements; there 

is an ordered relationship among the elements in the list, 

reflected by their relative positions

Indexed list 

– A list in which each element has an index value associated 

with it