Wednesday, September 14, 2011

Simple Web Application – Data Display page

I can read tutorials 'til I'm blue in the face, but unless I try to adapt the sample code to do something completely different, I don't retain anything. The live "Try it yourself" windows are quite fun, but unless I can get code running on my own or my ISP's server, I can't expose my misunderstandings or the gaps in my knowledge.

The Virginia Tech simple web application sample code works perfectly on their server, but recreating something to perform similar functions with my own database on my own server has been a fairly long and frustrating job.

I said in my last post that I could not see any gaps in the sample code. On closer inspection, I have found a couple, of which the most important was their connection code. They refer to a PHP PEAR database layer in the text and a file called DB.php in their code, but they don’t show what is in the file. So I followed the PHP PEAR link and constructed my own file based on the template given there:

$dbhost = 'localhost';
$dbuser = 'theuser';
$dbpass = 'thepassword';
$dbname = 'phptst';

This meant that my code and the sample code were beginning to diverge from a fundamental point. Because my PHP PEAR database layer was different, my connection code in the main page was different. Because of that, my query code had to be different, and because of that, my display code had to be different. In fact in the end I based the whole thing on the example previously quoted on my Playing with PHP entry.

In the end, the code in my display table was like the inverse, or a mirror image of the sample code. Their display table was written in HTML, peppered with PHP variables. My display table was a long PHP statement, peppered with HTML strings.

My code was as shown below:

// if the user hasn't specified any "sort" the default is "Partid"
if ( isset($_GET['sort']) && $_GET['sort']=='OpCode' ) { $_SESSION['sort'] = 'OpCode'; }
else { $_SESSION['sort'] = 'Partid'; }
$con = mysql_connect( $dbhost, $dbuser, $dbpass );
if (!$con)
die('Could not connect: ' . mysql_error());
mysql_select_db($dbname, $con);
$result = mysql_query("SELECT * FROM mytable") or die(mysql_error());
<title>Session Data</title>
<script language="JavaScript">
function confirmdelete(id)
var c = confirm("Do you really want to delete this line?");
if (c) {
<a href="addcontact.php">Add a Line</a> | My Items
<h1>Session Data</h1>
<table style="text-align:center;">
<tr bgcolor="#CCCCCC">
<td width="70"><strong>Itemid</strong></td>
<td width="70"><strong><?php if ($_SESSION['sort']!='OpCode') { echo '<a href="',$_SERVER['PHP_SELF'],'?sort=OpCode">Partid</a>'; } else { echo 'OpCode ↓'; } ?></strong></td>
<td width="70"><strong><?php if ($_SESSION['sort']!='Partid') { echo '<a href="',$_SERVER['PHP_SELF'],'?sort=Partid">OpCode</a>'; } else { echo 'OpCode ↓'; } ?></strong></td>
<td width="70"><strong>Itemdet</strong></td>
<td width="70"><strong>Raw</strong></td>
<td width="70"><strong>Rate</strong></td>
<td width="70"><strong>Edit</strong></td>
<td width="70"><strong>Delete</strong></td>
while($row = mysql_fetch_array($result))
echo "<tr>";
echo "<td>" . $row['Itemid'] . "</td>";
echo "<td>" . $row['OpCode'] . "</td>";
echo "<td>" . $row['Partid'] . "</td>";
echo "<td>" . $row['Itemdet'] . "</td>";
echo "<td>" . $row['Raw'] . "</td>";
echo "<td>" . $row['Rate'] . "</td>";
echo '<td><a href="editcontact.php?id=' . $row['Itemid'] . '">edit</a></td>';
echo '<td><a href="deletecontact.php?id=' . $row['Itemid'] . '">delete</td>';
echo "</tr>";

And the page looked like this:

No comments: