spanish icon   Spanish version


english icon  English version

News

03-24-2009:PHP Regular Expressions tutorial added to the PHP tutorials section.


03-19-2009: Two old templates (Photoblue and Hardwarezip) added to the webpage.


03-01-2009:Preg and Pattern Functions tutorial added to the PHP tutorials section.


About Redacron:

We are the creators of:


We have worked on the following websites:


Email Icon  Write to Us


Valid XHTML 1.0 Transitional


Links

Managed IT services Managed IT Services can minimise the burden of managing the IT infrastructure. Datashare Solutions Managed IT Services are perfect if your business needs to control costs or ensure a fixed level of IT service performance.

Webmaster Toolkit Collection of webmaster tools developed to help webmasters with our daily webmaster chores.

Geobytes For the web developer, Geobytes content localization technology is 'rain maker' technology.


 



Creating a Simple Photo Album with PHP and MySQL (Part 2)



Introduction

The Database

Getting Access to the Database

The End



 Introduction


In the previous tutorial, we created a primitive Online photo album using a DataSet and a DataReader. If you read the previous tutorial, you may remember that the DataReader ran into problems when the database had a null value for the field called width, which allowed null values. This approach, although similar, uses sqlDataReader and has ASP:Label replacing the DataReader.


Once again the first version of our photo album is a shadow of what the last version will be. You can't update the pictures, and you can't insert new pictures, and you won't be able to click on any links. In other words, this photo album is as simple as a photo album can be, and that makes it easier for us to understand what's going on.


 The Database


The first thing we need to create our simple photo album is a SQL database. The previous tutorial tells you how. An image must have a width and a height if you want it to fix in a webpage. Sometimes, however, the image is small enough to fit on the webpage without being modified through HTML. In cases like this, you may want the width and height to be NULL, and that could become a problem if you are using a Repeater or a DataGrid to display your album pictures.


 Getting Access to the Database


Once again, we will use C-Sharp for our script. We need two namespaces: System.Data and System.Data.SqlClient. The latter is the one that contains most of the ADO.NET classes, and System.Data.SqlClient contains the classes we will need to interact with the SQL database.


OnLoadData is the function that will be called as soon as the page loads for the first time. Inside that function we will get access to the database that contains the landscape pictures.


<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">


public void Page_Load(Object sender, EventArgs e)
{

if (!Page.IsPostBack)
{
OnLoadData(sender, e);
}


}  


 The Photo Album


public void OnLoadData(Object sender, EventArgs e)
{

SqlConnection conn = new SqlConnection
("DATABASE=landscapes;
SERVER=server-name;
UID=login_id;PWD=password");
String strCmd = "SELECT * FROM pictures";

SqlCommand cmd = new SqlCommand(strCmd, conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
Pic_Album.Text += BuildAlbum(dr);

}

dr.Close();
conn.Close();
}

OnLoadData is the function that gets called when the page is loaded for the first time. Once again we use SqlConnection to connect to the database. However in the previous tutorial, we used SqlDataAdapter because this is the class that allows the Dataset and the database to communicate. But now we are using a sqlDataReader, which uses SqlCommand.


The sqlDataReader class reads a stream of rows from a SQL Server database. Notice that once declared, dr gets data from SqlCommand, which in turn uses the ExecuteReader() method to get information from the database. We use while(dr.Read()) to extract everything inside the Landscapes database, and the data is sent to a function called BuildAlbum (more on this function later).


Pic_Album is the id-name of the ASP:label control that will put the data from Landscapes on the webpage. When this process has been completed, dr will be closed, and the connection will be closed.


 



 The BuildAlbum Function



private String BuildAlbum(SqlDataReader dr)
{
StringBuilder sb = new StringBuilder();
string dt, dy, dw;
sb.Append("<table>\n<tr>\n");
sb.Append("<td>\n<img src='images/");
dy = dr["filename"].ToString().Trim();
sb.Append(dy + "'\n");
sb.Append(" alt= '");
dw = dr["filename"].ToString().Trim();
sb.Append(dw + "'\n");

if (!(dr.IsDBNull(3)))
{
dt = dr["width"].ToString().Trim();
sb.Append("\n width='" + dt + "'");
}

sb.Append (" />");
sb.Append("\n</td>\n");
sb.Append("</tr>\n<tr>\n<td><b>");
sb.Append(dr["name"] + "</b>\n</td>\n</tr>");
sb.Append("\n<tr>\n<td>\n");
sb.Append(dr["description"]);
sb.Append("\n</td>\n");
sb.Append("</tr>\n</table>\n");
return sb.ToString();
}

The function BuildMoreInfoText is declared as a private String. The StringBuider class is part of the System.Text namespace, and is used to append, insert, or remove characters from a string. In our case, we use the Append method to add data to the StringBuilder sb.


A funny thing happens when this script becomes HTML code. You can easily get something that looks like this:


<img src="landscape.jpg          " width="300" />


Notice the separation between the ""s in landscape.jpg. Even though it is unlikely that this will affect the performance of the script, it is not exactly pleasing. This is the reason why you see this:


dy = dr["filename"].ToString().Trim();


dr["filename"] is turned into a string and then, using the Trim() method from the String class, we remove the extra spaces that originate not from the script but from the SQL database itself.


We have created an entire table with pictures without using a repeater, or a DataGrid. Now the sb StringBuilder is turned into a string and returned to Pic_Album.Text. The sb.ToString() is what people will see when they get access to the webpage. Nothing more than tables with pictures in them.



The End


What remains is simple HTML, except for the asp:Label control whose id is MoreInfo. This control will receive the data coming from BuildAlbum(dr), which in turn receives data created by the StringBuilder sb using data coming from the SqlDataReader dr. Sounds complex, but it isn't. Read the previous tutorial and then this one, and you'll get a pretty good idea of how you want to design the basis for an Online Photo Album.


</script>
<html>
<head>
<title>Photo Album</title> </head>
<body style="FONT-SIZE: 9pt; FONT-FAMILY: arial" bgcolor="ivory">

<h2>Landscape Pictures
</h2>
<asp:Label id="MoreInfo" runat="server"></asp:Label>
</body>
</html>



Home

Services: Joomla/osCommerce | Search Engine Optimization | Logo Design | Web Design

Portfolio: Catalog Design | Logo and Banner Design | Banner and Header Portfolio | Web Design

Tutorials: All Tutorials



Copyright © 2008 Redacron Studios. Design by R.P Carbonell.

Fatal error: Call to undefined function send_numbers() in /var/www/vhosts/redacron.com/httpdocs/includes/bottom_2.php on line 37