PHP for ‘i’ #6


Last part of the C for ‘i’ project added the ability to add new records into the database. The following shows how to add the same functionality to the PHP project.

The code shows how to carry out the process using the i5Toolkit API’s, the use of the db2_ functions will be covered at a later date. Just as an aside, the i5_pconnect() function does have a few problems which despite requests on the Zend Forums have not been cleared up. I think this is because the functions are created outside of Zend itself which could explain why they have little interest in responding to posts about them? The main issue is the use of the options which can be passed into the i5_pconnect() function, I was particularly interested in using the I5_OPTIONS_IDLE_TIMEOUT option which should terminate a persistent connection after a period of time. The documentation shows the option should be coded as I5_OPTIONS_IDLE_TIMEOUT=>120 to give a time out value of 120 seconds, if this is coded in this manner the connection fails because it cannot find a connection to use? I assume this is because the manual states the connection will be terminated after this period of time by the next connection request? I assume the code is looking for an existing connection and when it does not find one it fails? If I code it I5_OPTIONS_IDLE_TIMEOUT=>’120′ the connection is made, it never dies but it is made. I assume the connection would be dropped after the next connection request beyond the timeout value?? There is an option to build a PRIVATE persistent connection which apparently works to some degree? I have yet to play with it to find out what it actually does?

The following code has the I5_OPTIONS_IDLE_TIMEOUT=>’120′ coded for completeness but I have yet to prove either way if the connection is terminated?
This is new code for the index.php which simply adds a link to the page which carries the input form.

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<HTML><HEAD>
<META content=”text/html; charset=iso-8859-1″ http-equiv=Content-Type></HEAD>
<BODY>
<?php
//include(“i5toolkit/Toolkit_classes.php”);
// include the file which holds the user info
include(“../scripts/config.php”);
// connect to the i5
$options = array(I5_OPTIONS_JOBNAME => “PHPPROJ”, I5_OPTIONS_IDLE_TIMEOUT =>’120′);
$conn = i5_pconnect($server,$usr,$pwd,$options);
// check that we connected OK or die()
if (is_bool ( $conn ) && $conn == FALSE) {
die (“No Connection : ” .i5_errormsg() );
}
// open the file if not opened return the error
$file = i5_open(‘CPROJDTA/USERS’,I5_OPEN_READ,$conn);
if ($file === false) {
$tab = i5_error();
echo(var_dump($tab));
}
// list out the fields as the headers could just use text
// if cannot just dump and exit
$list = i5_list_fields($file);
if ($list ==! false) { ?>
<table>
<tr><td><?php echo($list[0]); ?></td><td><?php echo($list[1]);?></td></tr><?php
}
else {
$tab = i5_error();
echo(var_dump($tab));
exit();
}
// loop through the entries in the file and display
do {
$rec = i5_fetch_row($file,I5_READ_NEXT);
if($rec) { ?>
<tr><td><a href=”dspdet.php?id=<?php echo(i5_bookmark($file)); ?>”>Details</a></td><td><?php echo($rec[0]); ?></td><td><?php echo($rec[1]);?></td></tr><?php
}
} while($rec);
// close the file and free resource
i5_free_file($file);
?>
</table>
<a href=”getdets.php”>Add Record</a>
</body></html>

The input form is very simply at this stage, we will change it later to function as an update form.


<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<HTML>
<HEAD>
<META content=”text/html; charset=iso-8859-1″ http-equiv=Content-Type>
</HEAD>
<body>
<form name=”getdets” method=”post” action=”../scripts/storedets.php”>
<table>
<tr><td><label>First Name</label></td><td><input name=”firstname” type=”text” size=”35″ maxlength=”20″></td></tr>
<tr><td><label>Last Name</label></td><td><input name=”lastname” type=”text” size=”35″ maxlength=”20″></td></tr>
<tr><td><label>Address</label></td><td><input name=”addr1″ type=”text” size=”35″ maxlength=”25″></td></tr>
<tr><td><label>Address</label></td><td><input name=”addr2″ type=”text” size=”35″ maxlength=”25″></td></tr>
<tr><td><label>City</label></td><td><input name=”city” type=”text” size=”35″ maxlength=”15″></td></tr>
<tr><td><label>State</label></td><td><input name=”state” type=”text” size=”35″ maxlength=”15″></td></tr>
<tr><td><label>Zip Code</label></td><td><input name=”zip” type=”text” size=”35″ maxlength=”10″></td></tr>
<tr><td><label>Country</label></td><td><input name=”country” type=”text” size=”35″ maxlength=”15″></td></tr>
<tr><td><label>Telephone</label></td><td><input name=”telnum” type=”text” size=”35″ maxlength=”15″></td></tr>
<tr><td><label>Email</label></td><td><input name=”email” type=”text” size=”35″ maxlength=”35″></td></tr>
<tr><td colspan=”2″><input type=”submit” name=”submit” value=”Submit”></input></td></tr>
</table>
</form>
</body>
</HTML>

The processing part of the code is in storedets.php, this page will be called when the submit button is pressed on the input form. We are using POST variables for all of the variables to be written to the file. The first part sets up an array which will be passed into the i5_new_record() function, we are simply mapping the input fields to the file record fields. We have a different layout in the DB file to the POST array so it is very important we map them correctly.

You will also notice we have an i5_pconnect() function at the start of the script but not at the end, this will pick up the existing connection we used in the index.php file making the i5_functions run at an acceptable speed. If we were using the db2_ functions which are faster than the i5_ functions, using a standard db2_connect() and closing with a db2_close() would work without slowing things down between pages.


<?php
//include(“i5toolkit/Toolkit_classes.php”);
// include the file which holds the user info
include(“../scripts/config.php”);
// store the data ready to write to file
$data = array($_POST[‘lastname’],$_POST[‘firstname’],$_POST[‘addr1′],$_POST[‘addr2′],$_POST[‘city’],
$_POST[‘state’],$_POST[‘zip’],$_POST[‘country’],$_POST[‘telnum’],$_POST[‘email’]);
// connect to the i5
$conn = i5_pconnect($server,$usr,$pwd,array (I5_OPTIONS_JOBNAME => “PHPPROJ” ));
// check that we connected OK or die()
if (is_bool ( $conn ) && $conn == FALSE) {
die ( i5_errormsg () );
}
// open the file if not opened return the error
$file = i5_open(‘CPROJDTA/USERS’,I5_OPEN_READWRITE,$conn);
if ($file === false) {
$tab = i5_error();
echo(var_dump($tab));
}
$rec = i5_new_record($file,$data);
if(!$rec) {
$tab = i5_error();
echo(var_dump($tab));
}
header(“Location: /index.php”);
exit();
?>

Thats all there is to it, try it out..

Chris…

Article Global Facebook Twitter Myspace Friendfeed Technorati del.icio.us Digg Google StumbleUpon Eli Pets

This entry was posted in Application Modernization, PHP, PHP Programming by Chris Hird. Bookmark the permalink.

About Chris Hird

Chris Hird President and Owner Shield Advanced Solutions. Hird first worked with high availability at IBM Havant in the United Kingdom in 1989, and he was responsible for the technical interface with the developers of HA products and setting up a support structure in the UK to support the IBM customers. He left IBM to set up Shield Software Services in 1993, which was an IBM business partner and a MiMiX reseller, and in 1997 he moved to Canada and launched Shield Advanced Solutions, which provides tools and utilities aimed mainly at supporting HA Environments. Programs in C and PHP on the i5.

Leave a Reply