Create a new function that pre-fill in an argument to another function.
Use it like this:
// a function that generates a new function for adding numbers
function addGenerator(num) {
return function(toAdd) {
return num + toAdd;
}
}
// using above, make this function
var addFive = addGenerator(5);
alert(addFive(4) == 9);
.
Wednesday, April 6, 2011
Saturday, February 5, 2011
XML - namespace
<!-- "xmlns" This is default namespace consturctor.
"xmlns:xhtml" This is prefixed, and belongs xhtml.
"xmlns:my"
-->
<feed xlmns="http://www.w3.org/2005/Atom"
xmlns:xhtml="http://xml.w3.org/1999/xhtml"
xmlns:my="hthp://xmlportfolio.com/xmlmyguid-examples">
<title>Example Feed</title>
<!--
"right" is an element.
"type" is attribute.
"my" can be any word, and makes no difference
-->
<right type="xhtml" my:type="silly">
<xhtml:div>
You may not read, utter, interpret, or otherwise
contained in this feed...
</xhtml:div>
</right>
</feed>
"xmlns:xhtml" This is prefixed, and belongs xhtml.
"xmlns:my"
-->
<feed xlmns="http://www.w3.org/2005/Atom"
xmlns:xhtml="http://xml.w3.org/1999/xhtml"
xmlns:my="hthp://xmlportfolio.com/xmlmyguid-examples">
<title>Example Feed</title>
<!--
"right" is an element.
"type" is attribute.
"my" can be any word, and makes no difference
-->
<right type="xhtml" my:type="silly">
<xhtml:div>
You may not read, utter, interpret, or otherwise
contained in this feed...
</xhtml:div>
</right>
</feed>
Thursday, February 3, 2011
CSS - Fixed Width In a Span
Although the assignment was for the Javascript class, I just wanted make it look pretty, and that started this whole thing...
I wanted the ouput to look like:
Name: Pretty Chindonya
ID: 19191919
Assignment: #01
Date: 02/03/2011
I thought since the table stuff is going under the drain, why not use div stuff? It sounded great at start. The key is use <li> and <span>, and in <span> in CSS file, fix the width.
Like this in HTML file:
<li><span>Assignment:</span>#01</li>
Like this in CSS file:
span
{
width: 100px;
}
It worked with Internet Explorer, but it didn't with Firefox. Firefox ignored the width assigned. ummmmm. To fix the Firefox problem, added "inline-block".
span
{
display: inline-block;
width: 100px;
}
This solved the problem.
Note that, while I was at it, I noticed the difference between Internet Explorer and Firefox. Firefox has the extra space at the left side of text. After spending time for try and error, I found out the I had to explicitly make padding 0 for "ul". Does this mean Firefox's default configuration for "ul" will add extra space at left? Must be, right?
Here is the working code.
HTML File:
<body>
<div id ="wrapper">
<div class ="assignmentTitle">
<ul>
<li><span>Name:</span>Pretty Chindonya</li>
</ul>
<ul>
<li><span>ID:</span>191919</li>
</ul>
<ul>
<li><span>Assignment:</span>#01</li>
</ul>
<ul>
<li><span>Date:</span>02/03/2011</li>
</ul>
</div>
</div>
</body>
</html>
CSS File:
body
{
background: #ffffff;
text-align: center;
font-size: medium;
font-family: Sans-serif;
color: #505050;
}
#wrapper{
margin: auto;
width: 900px;
text-align: left;
background: #f0f0f0;
}
.assignmentTitle
{
color: #505050;
font-size: medium;
}
.assignmentTitle ul
{
list-style: none;
font-size: 15px;
font-family: Sans-serif;
list-style: none;
margin: 0 0 0 4px;
/* With Firefox,
there is an extra space at left.
below "padding: 0;" helped to get rid of it */
padding: 0;
}
.assignmentTitle li
{
/* used them for just debugging,
but ie did NOT display the outline.
ummmmm...
outline-style: solid;
outline-width: 1px; */
/* With Firefox,
there is an extra space at left.
below did not help to get rid of it
margin-left: 0;
padding-left: 0; */
}
.assignmentTitle span
{
/* For Firefox,
this "inline-block" is needed.
without it, it will not reserve the space */
display: inline-block;
width: 100px;
/* With Firefox,
there is an extra space at left.
below did not help to get rid of it.
margin-left: 0; */
}
I wanted the ouput to look like:
Name: Pretty Chindonya
ID: 19191919
Assignment: #01
Date: 02/03/2011
I thought since the table stuff is going under the drain, why not use div stuff? It sounded great at start. The key is use <li> and <span>, and in <span> in CSS file, fix the width.
Like this in HTML file:
<li><span>Assignment:</span>#01</li>
Like this in CSS file:
span
{
width: 100px;
}
It worked with Internet Explorer, but it didn't with Firefox. Firefox ignored the width assigned. ummmmm. To fix the Firefox problem, added "inline-block".
span
{
display: inline-block;
width: 100px;
}
This solved the problem.
Note that, while I was at it, I noticed the difference between Internet Explorer and Firefox. Firefox has the extra space at the left side of text. After spending time for try and error, I found out the I had to explicitly make padding 0 for "ul". Does this mean Firefox's default configuration for "ul" will add extra space at left? Must be, right?
Here is the working code.
HTML File:
<body>
<div id ="wrapper">
<div class ="assignmentTitle">
<ul>
<li><span>Name:</span>Pretty Chindonya</li>
</ul>
<ul>
<li><span>ID:</span>191919</li>
</ul>
<ul>
<li><span>Assignment:</span>#01</li>
</ul>
<ul>
<li><span>Date:</span>02/03/2011</li>
</ul>
</div>
</div>
</body>
</html>
CSS File:
body
{
background: #ffffff;
text-align: center;
font-size: medium;
font-family: Sans-serif;
color: #505050;
}
#wrapper{
margin: auto;
width: 900px;
text-align: left;
background: #f0f0f0;
}
.assignmentTitle
{
color: #505050;
font-size: medium;
}
.assignmentTitle ul
{
list-style: none;
font-size: 15px;
font-family: Sans-serif;
list-style: none;
margin: 0 0 0 4px;
/* With Firefox,
there is an extra space at left.
below "padding: 0;" helped to get rid of it */
padding: 0;
}
.assignmentTitle li
{
/* used them for just debugging,
but ie did NOT display the outline.
ummmmm...
outline-style: solid;
outline-width: 1px; */
/* With Firefox,
there is an extra space at left.
below did not help to get rid of it
margin-left: 0;
padding-left: 0; */
}
.assignmentTitle span
{
/* For Firefox,
this "inline-block" is needed.
without it, it will not reserve the space */
display: inline-block;
width: 100px;
/* With Firefox,
there is an extra space at left.
below did not help to get rid of it.
margin-left: 0; */
}
XML - New Line
I had a heck of time with putting a new line for XML.
Why?
Here is an assignment for the XML class that I was (and still am) taking.
Let’s start with something that we all should know how to do.
Write an XML document and a Cascading Style Sheet (CSS) that will display your name, the date, and the assignment number, all on separate lines.
No kiddin' :O
I didn't have a clue. Searched everywhere, including W3C site, but no one, no where else shows me how to put a new line. There are a lot of discussions about it, but none worked. So, after several hours of agony, this is what I came up with.
CSS File:
body
{
background-color: #ffffff;
font-size: medium;
font-family: Sans-serif;
}
Col
{
width: 100%;
}
nameHead, dateHead, assignmentHead
{
/* clear: left; */
color: #505050;
width: 160px;
}
name, date, assignment
{
color: #505050;
}
XML File:
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/css" href="StyleSheet1.css"?>
<body>
<col>
<nameHead>
Name:
</nameHead>
<name>
Tetsuro Hirouji
</name>
</col>
<col>
<dateHead>
Date:
</dateHead>
<date>
02/02/2011
</date>
</col>
<col>
<assignmentHead>
Assignment #:
</assignmentHead>
<assignment>
1
</assignment>
</col>
</body>
The key is to use a container with 100% width, and put the text inside. I don't know how else to do it.
Why?
Here is an assignment for the XML class that I was (and still am) taking.
Let’s start with something that we all should know how to do.
Write an XML document and a Cascading Style Sheet (CSS) that will display your name, the date, and the assignment number, all on separate lines.
No kiddin' :O
I didn't have a clue. Searched everywhere, including W3C site, but no one, no where else shows me how to put a new line. There are a lot of discussions about it, but none worked. So, after several hours of agony, this is what I came up with.
CSS File:
body
{
background-color: #ffffff;
font-size: medium;
font-family: Sans-serif;
}
Col
{
width: 100%;
}
nameHead, dateHead, assignmentHead
{
/* clear: left; */
color: #505050;
width: 160px;
}
name, date, assignment
{
color: #505050;
}
XML File:
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/css" href="StyleSheet1.css"?>
<body>
<col>
<nameHead>
Name:
</nameHead>
<name>
Tetsuro Hirouji
</name>
</col>
<col>
<dateHead>
Date:
</dateHead>
<date>
02/02/2011
</date>
</col>
<col>
<assignmentHead>
Assignment #:
</assignmentHead>
<assignment>
1
</assignment>
</col>
</body>
The key is to use a container with 100% width, and put the text inside. I don't know how else to do it.
Saturday, January 29, 2011
WAMP and IIS
If both WAMP and IIS7 are installed, IIS7 will be displayed.
To solve this problem, do this:
To change WAMP,
click on WAMP Server icon on taskbar and go to Apache > httpd.conf
Change ‘Listen 80’ to ‘Listen 8080’ (As shown in the screenshot). Save and restart WAMP.
Reference:
http://www.programmerfish.com/how-to-run-wamp-server-parallel-with-iis-7-on-windows/
Now WAMP Server is accessible on http://localhost:8080/
Added Note:
With above change only, I have to manually enter 8080 to "http://localhost/"
I am going to edit the same file, "httpd.conf" file.
Original:
#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
ServerName localhost:80
Changed:
#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
ServerName localhost:8080
This did NOT do anything.
Put back the original setting.
Change it back to:Listen 80
(it was changed to "Listen 8080")
Then, DO this, instead!
Theory, you can have both, but IIS and Apache/Wamp are both web server and might conflict in some way, so you have to disable IIS in order for Wamp to work
Disable IIS in Vista:
Control Panel, Uninstall Programs, Turn Widows Features On or Off, uncheck Internet Information Services
In the I"Uninstall or Change Program" window, click "repair" icon, then uncheck "Internet Information Services".
To solve this problem, do this:
- WAMP to run on http://localhost:8080/
- IIS7 to run on http://localhost/ (no change)
To change WAMP,
click on WAMP Server icon on taskbar and go to Apache > httpd.conf
Change ‘Listen 80’ to ‘Listen 8080’ (As shown in the screenshot). Save and restart WAMP.
Reference:
http://www.programmerfish.com/how-to-run-wamp-server-parallel-with-iis-7-on-windows/
Now WAMP Server is accessible on http://localhost:8080/
Added Note:
With above change only, I have to manually enter 8080 to "http://localhost/"
I am going to edit the same file, "httpd.conf" file.
Original:
#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
ServerName localhost:80
Changed:
#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
ServerName localhost:8080
This did NOT do anything.
Put back the original setting.
Change it back to:Listen 80
(it was changed to "Listen 8080")
Then, DO this, instead!
Theory, you can have both, but IIS and Apache/Wamp are both web server and might conflict in some way, so you have to disable IIS in order for Wamp to work
Disable IIS in Vista:
Control Panel, Uninstall Programs, Turn Widows Features On or Off, uncheck Internet Information Services
In the I"Uninstall or Change Program" window, click "repair" icon, then uncheck "Internet Information Services".
Monday, January 24, 2011
visual studio 2010 C++ note
Problem: start without debugging missing in visual studio 2010 C++
When use ctrl-F5, it automatically closes console window.
To fix this, do the following:
When use ctrl-F5, it automatically closes console window.
To fix this, do the following:
- right click the project name,
- go to Properties page,
- expand Configuration Properties -> Linker -> System,
- select Console (/SUBSYSTEM:CONSOLE) in SubSystem dropdown.
Friday, January 7, 2011
php - working with function (2)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
//
// accept a list of email address,
// split address, add domain part to another array
//
function getUniqueDomains($list){
$domains=array();
foreach($list as $l){
//
// separate xxx@yyy, and save arr[0]=xxx, arr[1]=yyy
//
$arr=explode("@", $l);
//
// save yyy into domain[]
//
$domains[]=trim($arr[1]);
}
//
// array_unique() removes duplicates elements
//
return array_unique($domains);
}
// read the file with email list
$fileContents=file("data.txt");
// pass the deliminated array
$returnedArray=getUniqueDomains($fileContents);
// print returned array
foreach($returnedArray as $d){
print "$d<br />";
}
?>
</body>
</html>
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
//
// accept a list of email address,
// split address, add domain part to another array
//
function getUniqueDomains($list){
$domains=array();
foreach($list as $l){
//
// separate xxx@yyy, and save arr[0]=xxx, arr[1]=yyy
//
$arr=explode("@", $l);
//
// save yyy into domain[]
//
$domains[]=trim($arr[1]);
}
//
// array_unique() removes duplicates elements
//
return array_unique($domains);
}
// read the file with email list
$fileContents=file("data.txt");
// pass the deliminated array
$returnedArray=getUniqueDomains($fileContents);
// print returned array
foreach($returnedArray as $d){
print "$d<br />";
}
?>
</body>
</html>
php - working with function and sprintf()
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
// define function
function getCircumference($radius){
// return value
return (2*pi()*$radius);
}
//
// ex 1.
//
print "The answer is ".sprintf("%4.2f",getCircumference(20));
echo '<br />';
//
// ex 2.
//
$num=getCircumference(20);
$txtt='.';
$txt=sprintf("The answer is %4.2f%s", $num, $txtt);
echo $txt;
echo '<br />';
//
// ex 3.
//
// this prints
printf("printf: The answer is %4.2f%s", $num, $txtt);
echo '<br />';
// but, this does NOT print!!!
sprintf("sprintf: The answer is %4.2f%s", $num, $txtt);
echo '<br />';
?>
</body>
</html>
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
// define function
function getCircumference($radius){
// return value
return (2*pi()*$radius);
}
//
// ex 1.
//
print "The answer is ".sprintf("%4.2f",getCircumference(20));
echo '<br />';
//
// ex 2.
//
$num=getCircumference(20);
$txtt='.';
$txt=sprintf("The answer is %4.2f%s", $num, $txtt);
echo $txt;
echo '<br />';
//
// ex 3.
//
// this prints
printf("printf: The answer is %4.2f%s", $num, $txtt);
echo '<br />';
// but, this does NOT print!!!
sprintf("sprintf: The answer is %4.2f%s", $num, $txtt);
echo '<br />';
?>
</body>
</html>
Thursday, January 6, 2011
php - working with files (4)
It reads a text file.
Make the first line title, and print.
Remove the line.
Using nl1br(), converting text line breaker to the <br /> tag,
print the rest.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
// read file
$data=file('C:\wamp\www\testFolder\tetsuro\tetsuroP04_php_file3\omelette.txt')
or die('Could not read file!');
/* first line is title. read it into variable */
$title=$data[0];
// remove first line from array
array_shift($data);
?>
<h2><?php echo $title; ?></h2>
<?php
//
// nl2br() function converts regular text linebreaks
// into the HTML equivalent, the <br /> tag.
//
/* iterate over content and print it */
foreach($data as $line){
echo nl2br($line);
}
?>
</body>
</html>
Make the first line title, and print.
Remove the line.
Using nl1br(), converting text line breaker to the <br /> tag,
print the rest.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
// read file
$data=file('C:\wamp\www\testFolder\tetsuro\tetsuroP04_php_file3\omelette.txt')
or die('Could not read file!');
/* first line is title. read it into variable */
$title=$data[0];
// remove first line from array
array_shift($data);
?>
<h2><?php echo $title; ?></h2>
<?php
//
// nl2br() function converts regular text linebreaks
// into the HTML equivalent, the <br /> tag.
//
/* iterate over content and print it */
foreach($data as $line){
echo nl2br($line);
}
?>
</body>
</html>
php - working with files (3)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
// if form has not yet been submitted,
// display input box
if(!isset($_POST['file'])){
?>
<form action"<?php echo $_SERVER['SCRIPT_NAME'];?>" method="post">
Enter file path
<input type="text" name="file">
</form>
<?php
}
//else process from input
else{
echo 'File name: <b>'.$_POST['file'].'</b><br />';
/* check is file exist */
if(file_exists($_POST['file'])){
// print file size
echo 'file size: '.filesize($_POST['file']).'byets. <br />';
// print file owner
echo 'file owner: '.fileowner($_POST['file']).'<br />';
// print file group
echo 'file group: '.filegroup($_POST['file']).'<br />';
// print file permission
echo 'file permission: '.fileperms($_POST['file']).'<br />';
// print file type
echo 'file type: '.filetype($_POST['file']).'<br />';
// print last access time
echo 'file last accessed on: '
.date('Y-m-d', fileatime($_POST['file'])).'<br />';
// print last modification time
echo 'file last modified on '
.date('Y-m-d', filemtime($_POST['file'])).'<br />';
// is it directory?
if(is_dir($_POST['file'])){
echo 'file is directory <br />';
}
// is it file?
if(is_file($_POST['file'])){
echo 'file is a regular file <br />';
}
// is it a link?
if(is_link($_POST['file'])){
echo 'file is a symbolic link <br />';
}
// is it executable?
if(is_executable($_POST['file'])){
echo 'file is executable <br />';
}
// is it readable?
if(is_readable($_POST['file'])){
echo 'file is readable <br />';
}
// it is writable?
if(is_writable($_POST['file'])){
echo 'file is writable <br />';
}
}
else{
echo 'file does not exist! <br />';
}
}
?>
</body>
</html>
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
// if form has not yet been submitted,
// display input box
if(!isset($_POST['file'])){
?>
<form action"<?php echo $_SERVER['SCRIPT_NAME'];?>" method="post">
Enter file path
<input type="text" name="file">
</form>
<?php
}
//else process from input
else{
echo 'File name: <b>'.$_POST['file'].'</b><br />';
/* check is file exist */
if(file_exists($_POST['file'])){
// print file size
echo 'file size: '.filesize($_POST['file']).'byets. <br />';
// print file owner
echo 'file owner: '.fileowner($_POST['file']).'<br />';
// print file group
echo 'file group: '.filegroup($_POST['file']).'<br />';
// print file permission
echo 'file permission: '.fileperms($_POST['file']).'<br />';
// print file type
echo 'file type: '.filetype($_POST['file']).'<br />';
// print last access time
echo 'file last accessed on: '
.date('Y-m-d', fileatime($_POST['file'])).'<br />';
// print last modification time
echo 'file last modified on '
.date('Y-m-d', filemtime($_POST['file'])).'<br />';
// is it directory?
if(is_dir($_POST['file'])){
echo 'file is directory <br />';
}
// is it file?
if(is_file($_POST['file'])){
echo 'file is a regular file <br />';
}
// is it a link?
if(is_link($_POST['file'])){
echo 'file is a symbolic link <br />';
}
// is it executable?
if(is_executable($_POST['file'])){
echo 'file is executable <br />';
}
// is it readable?
if(is_readable($_POST['file'])){
echo 'file is readable <br />';
}
// it is writable?
if(is_writable($_POST['file'])){
echo 'file is writable <br />';
}
}
else{
echo 'file does not exist! <br />';
}
}
?>
</body>
</html>
php - working with files (2)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
/*
* fopen()
*/
// set file to write
$file = 'tmp/dump.txt';
echo "file opened: $file";
echo "<br />";
// open file
$fh = fopen($file, 'w') or die('Could not open file!');
echo "file opened, and fh returned: $fh";
echo "<br />";
// write to file
fwrite($fh, "Look, Ma, I wrote a file! ") or die('Could not write to file');
echo "finished writing.";
echo "<br />";
// close file
fclose($fh);
echo "finished closing file";
echo "<br />";
/*
* file_put_contents()
*/
// set file to write
$filename = 'tmp/dump.txt';
// write to file
file_put_contents($filename, "Look, Pa, I wrote a file! ") or die('Could not write to file');
echo "file opened, written, and closed: $file";
echo "<br />";
?>
</body>
</html>
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
/*
* fopen()
*/
// set file to write
$file = 'tmp/dump.txt';
echo "file opened: $file";
echo "<br />";
// open file
$fh = fopen($file, 'w') or die('Could not open file!');
echo "file opened, and fh returned: $fh";
echo "<br />";
// write to file
fwrite($fh, "Look, Ma, I wrote a file! ") or die('Could not write to file');
echo "finished writing.";
echo "<br />";
// close file
fclose($fh);
echo "finished closing file";
echo "<br />";
/*
* file_put_contents()
*/
// set file to write
$filename = 'tmp/dump.txt';
// write to file
file_put_contents($filename, "Look, Pa, I wrote a file! ") or die('Could not write to file');
echo "file opened, written, and closed: $file";
echo "<br />";
?>
</body>
</html>
Sunday, January 2, 2011
php - header and footer
Header Section:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title><?php echo $page['title'];?></title>
</head>
<body>
<!-- top menu bar -->
<table width="90%" border="0" cellspacing="5" cellpadding="5">
<tr>
<td><a href="#">Home</a></td>
<td><a href="#">Site Map</a></td>
<td><a href="#">Search</a></td>
<td><a href="#">Help</a></td>
</tr>
</table>
<!-- header ends -->
Main Section:
<?php
// create an array to set page-level variables
$page = array();
$page['title'] = 'Product Catalog';
/* once the file is imported,
the variables set above will become available to it */
// include the page header
include('header.php');
?>
<!-- HTML content here -->
<?php
// include the page footer
include('footer.php');
?>
Footer Section:
<!-- footer begins -->
<br />
<center>
Your usage of this site is subject to its published
<a href="tac.html">terms and conditions</a>
. Data is copyright Big Company Inc, 1995-<?php
echo date("Y", mktime()); ?></center
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title><?php echo $page['title'];?></title>
</head>
<body>
<!-- top menu bar -->
<table width="90%" border="0" cellspacing="5" cellpadding="5">
<tr>
<td><a href="#">Home</a></td>
<td><a href="#">Site Map</a></td>
<td><a href="#">Search</a></td>
<td><a href="#">Help</a></td>
</tr>
</table>
<!-- header ends -->
Main Section:
<?php
// create an array to set page-level variables
$page = array();
$page['title'] = 'Product Catalog';
/* once the file is imported,
the variables set above will become available to it */
// include the page header
include('header.php');
?>
<!-- HTML content here -->
<?php
// include the page footer
include('footer.php');
?>
Footer Section:
<!-- footer begins -->
<br />
<center>
Your usage of this site is subject to its published
<a href="tac.html">terms and conditions</a>
. Data is copyright Big Company Inc, 1995-<?php
echo date("Y", mktime()); ?></center
</body>
</html>
php - working with files
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
/*
* fopen() * returns "file handle"
*
* fread() *
*/
/*
// this does NOT work!
$file =
'http://localhost/testFolder/tetsuro/tetsuroP03_php_file/RandomSentences.txt'
or die('Could not open file!');
*/
// this works
$file =
'C:\wamp\www\testFolder\tetsuro\tetsuroP03_php_file\RandomSentences.txt'
or die('Could not open file!');
/*
// this works too
// note: the file is in the same folder as this php file.
$file = 'RandomSentences.txt' or die('Could not open file!');
*/
// open file
$fh = fopen($file, 'r') or die('Could not open file!');
// read file contents
$data = fread($fh, filesize($file)) or die('Could not read file!');
//
// some debug stuff
//
echo "<br /><br />";
$size=filesize($file);
echo "total bytes in the file is $size";
echo "<br />";
echo "this is what's in the file handle: $fh";
echo "<br /><br />";
// close file
fclose($fh);
// print file contents
echo $data;
echo "<br /><br />";
/*
* file() * opens the file, reads it into an array and closes the file - all in one
*
* foreach() * using loop, use this command to read array
*
*/
echo 'using file()';
echo "<br /><br />";
$file =
'C:\wamp\www\testFolder\tetsuro\tetsuroP03_php_file\RandomSentences.txt'
or die('Could not open file!');
// read file into array
$data = file($file) or die('Could not read file!');
// loop through array and print each line
foreach ($data as $line) {
echo $line;
}
echo "<br /><br />";
/*
* file_get_contents() * reads the entire file into a string - all in one
*
*/
echo 'using file_get_contents()';
echo "<br /><br />";
$file =
'C:\wamp\www\testFolder\tetsuro\tetsuroP03_php_file\RandomSentences.txt'
or die('Could not open file!');
// read file into string
$data = file_get_contents($file) or die('Could not read file!');
// print contents
echo $data;
echo "<br /><br />";
?>
</body>
</html>
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
/*
* fopen() * returns "file handle"
*
* fread() *
*/
/*
// this does NOT work!
$file =
'http://localhost/testFolder/tetsuro/tetsuroP03_php_file/RandomSentences.txt'
or die('Could not open file!');
*/
// this works
$file =
'C:\wamp\www\testFolder\tetsuro\tetsuroP03_php_file\RandomSentences.txt'
or die('Could not open file!');
/*
// this works too
// note: the file is in the same folder as this php file.
$file = 'RandomSentences.txt' or die('Could not open file!');
*/
// open file
$fh = fopen($file, 'r') or die('Could not open file!');
// read file contents
$data = fread($fh, filesize($file)) or die('Could not read file!');
//
// some debug stuff
//
echo "<br /><br />";
$size=filesize($file);
echo "total bytes in the file is $size";
echo "<br />";
echo "this is what's in the file handle: $fh";
echo "<br /><br />";
// close file
fclose($fh);
// print file contents
echo $data;
echo "<br /><br />";
/*
* file() * opens the file, reads it into an array and closes the file - all in one
*
* foreach() * using loop, use this command to read array
*
*/
echo 'using file()';
echo "<br /><br />";
$file =
'C:\wamp\www\testFolder\tetsuro\tetsuroP03_php_file\RandomSentences.txt'
or die('Could not open file!');
// read file into array
$data = file($file) or die('Could not read file!');
// loop through array and print each line
foreach ($data as $line) {
echo $line;
}
echo "<br /><br />";
/*
* file_get_contents() * reads the entire file into a string - all in one
*
*/
echo 'using file_get_contents()';
echo "<br /><br />";
$file =
'C:\wamp\www\testFolder\tetsuro\tetsuroP03_php_file\RandomSentences.txt'
or die('Could not open file!');
// read file into string
$data = file_get_contents($file) or die('Could not read file!');
// print contents
echo $data;
echo "<br /><br />";
?>
</body>
</html>
Saturday, January 1, 2011
php - working with array
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
//
// print_r()
// use it to debug array
//
echo '<i>using print_r() to debug anime list.</i><br />';
$animeGroupList=array('ninja'=>'naruto','zonbie'=>'high school of dead',
'high shool'=>'bakuman','monster'=>'claymore');
print_r($animeGroupList);
echo '<br />';
$animeList=array('naruto','high school of dead','bakuman','claymore');
print_r($animeList);
echo '<br />';
echo '<br />';
//
// array_push()
// add an element to the end
//
// note: does not work with associative arrays
//
$anime='bleach';
echo "<i>using array_push(), added $anime at the end.</i><br />";
array_push($animeList,'bleach');
print_r($animeList);
echo '<br />';
echo '<br />';
//
// array_pop()
// remove an element from the end,
// and returns it
//
$anime=array_pop($animeList);
print_r($animeList);
echo '<br />';
echo "<i>using array_pop(), $anime was removed from the end of anime list.</i>";
echo '<br />';
echo '<br />';
//
// array_shift()
// take an element off the beginning
//
$anime=array_shift($animeList);
print_r($animeList);
echo '<br />';
echo "<i>using array_shift(), removing $anime the beginning.</i>";
echo '<br />';
echo '<br />';
//
// array_unshift()
// add an element to the beginning
//
// note: does not work with associative arrays
// note: returns the shifted value - NOT the element!
//
$anime=array_unshift($animeList, 'naruto');
print_r($animeList);
echo '<br />';
echo "<i>using array_unshift(), added $anime to beginning.</i><br />";
echo '<br />';
echo '<br />';
//
// explode()
// using a deliminater, splits a string into array elements
//
echo "using explode()<br />";
$str = 'shinigami, zombie, ninja';
echo "$str <br />";
$charactorTypeList = explode(', ', $str);
print_r($charactorTypeList);
echo '<br />';
echo '<br />';
//
// inplode()
// opposite of explode()
//
echo "using implode(), putting it back to a string<br />";
$str=implode(', ', $charactorTypeList);
echo $str;
echo '<br />';
echo '<br />';
//
// sort() and rsort()
//
echo 'original list';echo '<br />';
print_r($charactorTypeList);
echo '<br />';
echo '<br />';
sort($charactorTypeList);
echo 'after sort()';
echo '<br />';
print_r($charactorTypeList);
echo '<br />';
echo '<br />';
rsort($charactorTypeList);
echo 'after rsort() ';
echo '<br />';
print_r($charactorTypeList);
echo '<br />';
echo '<br />';
//
// getting elements out of array
//
$artists = array('hikaru', 'yui', 'aki', 'ROOKiEZ is PUNK\'D');
for ($x = 0; $x < sizeof($artists); $x++){
echo "<li> $artists[$x]";
// echo '<li>'.$artists[$x]; //this works too
}
print "<br />";
print "<br />";
//
// array_key()
// array_values()
// for associative array
//
echo 'print keys and values of array of:<br />';
echo "'ninja'=>'naruto','zonbie'=>'high school of dead',
'high shool'=>'bakuman','monster'=>'claymore'";
print "<br />";
print "<br />";
$animeGroupList=array('ninja'=>'naruto','zonbie'=>'high school of dead',
'high shool'=>'bakuman','monster'=>'claymore');
$result = array_keys($animeGroupList);
print_r($result);
print "<br />";
$result = array_values($animeGroupList);
print_r($result);
print "<br />";
print "<br />";
//
// foreach()
//
echo 'the list of artists: ';
$artists = array('hikaru', 'yui', 'aki', 'ROOKiEZ is PUNK\'D');
foreach($artists as $a){
echo '<li>'.$a;
}
?>
</body>
</html>
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
//
// print_r()
// use it to debug array
//
echo '<i>using print_r() to debug anime list.</i><br />';
$animeGroupList=array('ninja'=>'naruto','zonbie'=>'high school of dead',
'high shool'=>'bakuman','monster'=>'claymore');
print_r($animeGroupList);
echo '<br />';
$animeList=array('naruto','high school of dead','bakuman','claymore');
print_r($animeList);
echo '<br />';
echo '<br />';
//
// array_push()
// add an element to the end
//
// note: does not work with associative arrays
//
$anime='bleach';
echo "<i>using array_push(), added $anime at the end.</i><br />";
array_push($animeList,'bleach');
print_r($animeList);
echo '<br />';
echo '<br />';
//
// array_pop()
// remove an element from the end,
// and returns it
//
$anime=array_pop($animeList);
print_r($animeList);
echo '<br />';
echo "<i>using array_pop(), $anime was removed from the end of anime list.</i>";
echo '<br />';
echo '<br />';
//
// array_shift()
// take an element off the beginning
//
$anime=array_shift($animeList);
print_r($animeList);
echo '<br />';
echo "<i>using array_shift(), removing $anime the beginning.</i>";
echo '<br />';
echo '<br />';
//
// array_unshift()
// add an element to the beginning
//
// note: does not work with associative arrays
// note: returns the shifted value - NOT the element!
//
$anime=array_unshift($animeList, 'naruto');
print_r($animeList);
echo '<br />';
echo "<i>using array_unshift(), added $anime to beginning.</i><br />";
echo '<br />';
echo '<br />';
//
// explode()
// using a deliminater, splits a string into array elements
//
echo "using explode()<br />";
$str = 'shinigami, zombie, ninja';
echo "$str <br />";
$charactorTypeList = explode(', ', $str);
print_r($charactorTypeList);
echo '<br />';
echo '<br />';
//
// inplode()
// opposite of explode()
//
echo "using implode(), putting it back to a string<br />";
$str=implode(', ', $charactorTypeList);
echo $str;
echo '<br />';
echo '<br />';
//
// sort() and rsort()
//
echo 'original list';echo '<br />';
print_r($charactorTypeList);
echo '<br />';
echo '<br />';
sort($charactorTypeList);
echo 'after sort()';
echo '<br />';
print_r($charactorTypeList);
echo '<br />';
echo '<br />';
rsort($charactorTypeList);
echo 'after rsort() ';
echo '<br />';
print_r($charactorTypeList);
echo '<br />';
echo '<br />';
//
// getting elements out of array
//
$artists = array('hikaru', 'yui', 'aki', 'ROOKiEZ is PUNK\'D');
for ($x = 0; $x < sizeof($artists); $x++){
echo "<li> $artists[$x]";
// echo '<li>'.$artists[$x]; //this works too
}
print "<br />";
print "<br />";
//
// array_key()
// array_values()
// for associative array
//
echo 'print keys and values of array of:<br />';
echo "'ninja'=>'naruto','zonbie'=>'high school of dead',
'high shool'=>'bakuman','monster'=>'claymore'";
print "<br />";
print "<br />";
$animeGroupList=array('ninja'=>'naruto','zonbie'=>'high school of dead',
'high shool'=>'bakuman','monster'=>'claymore');
$result = array_keys($animeGroupList);
print_r($result);
print "<br />";
$result = array_values($animeGroupList);
print_r($result);
print "<br />";
print "<br />";
//
// foreach()
//
echo 'the list of artists: ';
$artists = array('hikaru', 'yui', 'aki', 'ROOKiEZ is PUNK\'D');
foreach($artists as $a){
echo '<li>'.$a;
}
?>
</body>
</html>
php- form submit (another one)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
/*
* if the "submit" variable does not exist,
* the form has not been submitted - display initial page
*/
if(!isset($_POST['submit'])){
?>
<form action="
<?php echo $_SERVER['SCRIPT_NAME']; ?>"
method="post">Print all the squares between 1 and
<input type="text" name="limit" size="4" maxlength="4">
<input type="submit" name="submit" value="Go">
</form>
<?php
}
else{
/*
* if the "submit" variable exists,
* the form has been submitted - look for and process form data
*/
// set variables from form input
$upperLimit = $_POST['limit'];
$lowerLimit = 1;
// keep printing squares until lower limit = upper limit
while ($lowerLimit <= $upperLimit) {
echo ($lowerLimit * $lowerLimit).' ';
$lowerLimit++;
}
// print end marker
echo 'END';
}
?>
<!--
Use $_SERVER['SCRIPT_NAME'] instead of $_SERVER['PHP_SELF'].
because
$_SERVER['PHP_SELF'] will contain not just login.php, but the entire login.php/nearly_arbitrary_string.
-->
</body>
</html>
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
/*
* if the "submit" variable does not exist,
* the form has not been submitted - display initial page
*/
if(!isset($_POST['submit'])){
?>
<form action="
<?php echo $_SERVER['SCRIPT_NAME']; ?>"
method="post">Print all the squares between 1 and
<input type="text" name="limit" size="4" maxlength="4">
<input type="submit" name="submit" value="Go">
</form>
<?php
}
else{
/*
* if the "submit" variable exists,
* the form has been submitted - look for and process form data
*/
// set variables from form input
$upperLimit = $_POST['limit'];
$lowerLimit = 1;
// keep printing squares until lower limit = upper limit
while ($lowerLimit <= $upperLimit) {
echo ($lowerLimit * $lowerLimit).' ';
$lowerLimit++;
}
// print end marker
echo 'END';
}
?>
<!--
Use $_SERVER['SCRIPT_NAME'] instead of $_SERVER['PHP_SELF'].
because
$_SERVER['PHP_SELF'] will contain not just login.php, but the entire login.php/nearly_arbitrary_string.
-->
</body>
</html>
php- form submit
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
/* if the "submit" variable does not exist,
* the form has not been submitted - display initial page */if(!isset($_POST['submit'])){
?>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
Enter your age: <input name="age" size="2">
<input type="submit" name="submit" value="Go">
</form>
<?php
}
else{
/* if the "submit" variable exists,
* the form has been submitted - look for and process form data
*/ // display result
$age = $_POST['age'];
if($age >= 21){
echo 'Come on in, we have alcohol and music awaiting you!';
}
else{
echo 'You\'re too young for this club,
come back when you\'re a little older';
}
}
?>
<!--
Use $_SERVER['SCRIPT_NAME'] instead of $_SERVER['PHP_SELF'].
because
$_SERVER['PHP_SELF'] will contain not just login.php, but the entire login.php/nearly_arbitrary_string.
-->
</body>
</html>
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
/* if the "submit" variable does not exist,
* the form has not been submitted - display initial page */if(!isset($_POST['submit'])){
?>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
Enter your age: <input name="age" size="2">
<input type="submit" name="submit" value="Go">
</form>
<?php
}
else{
/* if the "submit" variable exists,
* the form has been submitted - look for and process form data
*/ // display result
$age = $_POST['age'];
if($age >= 21){
echo 'Come on in, we have alcohol and music awaiting you!';
}
else{
echo 'You\'re too young for this club,
come back when you\'re a little older';
}
}
?>
<!--
Use $_SERVER['SCRIPT_NAME'] instead of $_SERVER['PHP_SELF'].
because
$_SERVER['PHP_SELF'] will contain not just login.php, but the entire login.php/nearly_arbitrary_string.
-->
</body>
</html>
php - "===" operator
<?php
$str = '7';
$int = 7;
//usual stuff
$result = ($str == $int);
echo "result is $result<br />";
//returns false because type is not the same
$result = ($str === $int);
echo "result is $result<br />";
?>
$str = '7';
$int = 7;
//usual stuff
$result = ($str == $int);
echo "result is $result<br />";
//returns false because type is not the same
$result = ($str === $int);
echo "result is $result<br />";
?>
php - String Concatenation Operator
The string concatenation operator, . is like + for numbers.
<?php
$str='the';
$str.='n';
echo $str; //prints out "then"
?>
<?php
$str='the';
$str.='n';
echo $str; //prints out "then"
?>
Subscribe to:
Posts (Atom)

