Playing with databases

Try online Examples

Here's how to play with databases...
The HTML looks like:

	<FORM action="/cgi-bin/aptilis.exe" method="POST">
	<INPUT type="hidden" name="file" value="/home/scripts/db1.e.txt">

	<H3>Products Database Search</H3>

	<B>Products</B><BR>
	<SELECT name="sel">
	<OPTION>Ferrari</OPTION>
	<OPTION>PC</PC>
	<OPTION>Pizza</OPTION>
	</SELECT>

	<INPUT type="submit" value="Query...">
	</FORM>

And now, the Aptilis program...

sub main()

	// Magic line.
	print("Content-Type: text/html\n\n")

	// The begining of the page.
	print("<HTML>\n")
	print("<HEAD><BASE href=\"http://localhost/aptilis/\"></HEAD>\n\n")
	print("<BODY bgcolor=#FFFFFF>\n")

 	n = LoadDataBase(db[], "f:/aptilis/scripts/database.txt")
	print(int(n)$," record(s)<BR>\n")

	if n < 1
		print("The database is empty</BODY></HTML>")
		return 0
	end if

	// Remember that sel is the name of our SELECT field.
	x = getRecordIndexByKey(db[], sel$, 0)

	if x = -1
		print("<B>Sorry, the product is not in the database.<BR>\n")
	else

		// x is the index of our record.
		r = db[x] $

		print("<IMG src=\"gifs/", GetField(r$,3)$, ".gif\">\n")
		print("<HR>\n")
		print("<B>", getField(r$, 0)$, ":</B> ")
		print("£", getField(r$, 1)$, "<BR>\n")
		print("<I>", getField(r$, 2)$, "</I><BR>\n")
	end if

	print("</BODY></HTML>\n")

end main

Things learnt

Here is what our database looks like, it's just a text file, that could have been exported by, say, Access. Or you could have typed it in a text editor.
In this example, I have saved it as "c:\webshare\wwwroot\aptilis\examples\database.txt", that is in the same directory as my web pages. In real life, you might want to store a database in another directory to prevent web users to access it directly from their browser. Under Unix and NT, you must make sure that authorisations are set up correctly.

"Ferrari","100000","car","testa"
"PC","1000","computer","pc"
"Pizza"  ,"5.89","food","pizza"