<?php
#########################################################
# class05-sites5.php - Submit Your Favorite Site with Category
#########################################################
$scriptName = $_SERVER['PHP_SELF'];
$pageTitle = "Class 5 Example - Submit Your Favorite Site with Category";
# Make sure we display errors to the browser
error_reporting(E_ALL ^ E_NOTICE);
ini_set('display_errors', 1);
# Get our DB info
require "info.php";
#########################################################
# Connect to the database.
#########################################################
$connection = mysql_connect($mySqlHostname, $mySqlUsername, $mySqlPassword);
if (!$connection)
die("Error " . mysql_errno() . " : " . mysql_error());
# Select the DB
$db_selected = mysql_select_db($mySqlDatabase, $connection);
if (!$db_selected)
die("Error " . mysql_errno() . " : " . mysql_error());
#########################################################
# Define our widgets and initial values
#########################################################
### New site suhmission widgets ###
$textTitleName = "title"; # The name of text widget that has new website name
$textTitleValue = $_POST[$textTitleName]; # The value of title widget
$catName = "category"; # The name of select widget that has the relevant category
$catValue = $_POST[$catName]; # The value of category widget
$textUrlName = "url"; # The name of text widget that has new website url
$textUrlValue = $_POST[$textUrlName]; # The value of url widget
if (empty($textUrlValue)) $textUrlValue = "http://"; # Initialize if blank
$textDescName = "desc"; # The name of text widget that has new website description
$textDescValue = $_POST[$textDescName]; # The value of desc widget
$submitName = "DataAction";
$submitValue = "Submit New Site"; # This is what it will say on our submit site button
### Status variables ###
$statusMsg = ""; # Gives response back to user (i.e. "Thank you for your ...")
$hasErrors = 0; # Keeps track of whether there are input errors
#########################################################
# Update the votes for selected site if submit button was pressed
#########################################################
if ($_POST[$submitName]==$submitValue)
{ # Someone submitted new site
# Error Checking
$noTitle = 0; # Flag that is set if title widget was blank
$noUrl = 0; # Flag that is set if url widget was blank
$noDesc = 0; # Flag that is set if description widget was blank
$title = trim($textTitleValue);
if (empty($title))
{ # If blank, set general error flag, name error flag, and general error message
$hasErrors = 1;
$noTitle = 1;
$statusMsg = "There were errors in your site submission.";
}
$url = trim($textUrlValue);
if (empty($url) || !strcasecmp($url,"http://"))
{ # If blank, set general error flag, name error flag, and general error message
$hasErrors = 1;
$noUrl = 1;
$statusMsg = "There were errors in your site submission.";
}
$desc = trim($textDescValue);
if (empty($desc))
{ # If blank, set general error flag, name error flag, and general error message
$hasErrors = 1;
$noDesc = 1;
$statusMsg = "There were errors in your site submission.";
}
# If no value for category, assume it is ID 1, meaning undefined
if (empty($catValue))
$catValue = 1;
# If we had no errors, we can put it in the database
if (!$hasErrors)
{ # Replace any single quotes in our firstname
$titleDB = str_replace("'", "''", $title);
$urlDB = str_replace("'", "''", $url);
$descDB = str_replace("'", "''", $desc);
# Create the SQL query
$SqlStatement = "INSERT INTO class05_table2 (title,url,description,category_id)
VALUES ('$titleDB','$urlDB','$descDB',$catValue) ";
$result = mysql_query($SqlStatement,$connection);
if (!$result) die("Error " . mysql_errno() . " : " . mysql_error());
$statusMsg = "Thank you for your site submission!";
# Reset the text widgets to accept input once again for the next submission
$textTitleValue = "";
$textUrlValue = "http://";
$textDescValue = "";
$catValue = "";
}
}
#########################################################
# Visual Info
#########################################################
$table_header_color = "#99cccc";
$table_row_color = "#e8e8e8";
?>
<html>
<head>
<title><?=$pageTitle?></title>
<link rel="stylesheet" href="css/classnotes.css" type="text/css" media="all" />
</head>
<body>
<b><?=$pageTitle?></b>
<p>
Submit Your Favorite Website:
<p>
<?
# If we put anything in the general status message, then print it
if (!empty($statusMsg))
{ print '<font color="#990000"><b>'.$statusMsg.'</b></font> <p>';
}
# Start the form and write the title widget
echo <<<END
<table border=0 cellpadding=3 cellspacing=1 class="arial13">
<tr bgcolor="$table_row_color">
<td align="left">
<form action="$scriptName" method="POST" enctype="application/x-www-form-urlencoded">
<nobr><b>Title:</b></nobr>
</td>
<td align="left">
<input type="text" name="$textTitleName" value="$textTitleValue" size="32" maxlength="255">
END;
# If we had a problem with the title, show error message here
if ($hasErrors && $noTitle)
{ print '<br><font color="#ff0000"><b>Please provide a website title</b></font>';
}
# Write the url widget
echo <<<END
</td>
</tr>
<tr bgcolor="$table_row_color">
<td align="left">
<nobr><b>URL:</b></nobr>
</td>
<td align="left">
<input type="text" name="$textUrlName" value="$textUrlValue" size="32" maxlength="255">
END;
# If we had a problem with the url, show error message here
if ($hasErrors && $noUrl)
{ print '<br><font color="#ff0000"><b>Please provide a website URL</b></font>';
}
# Write the description widget
echo <<<END
</td>
</tr>
<tr bgcolor="$table_row_color">
<td align="left">
<nobr><b>Description:</b></nobr>
</td>
<td align="left">
<input type="text" name="$textDescName" value="$textDescValue" size="32" maxlength="255">
END;
# If we had a problem with the description, show error message here
if ($hasErrors && $noDesc)
{ print '<br><font color="#ff0000"><b>Please provide a description of this website</b></font>';
}
# Write the category widget
echo <<<END
</td>
</tr>
<tr bgcolor="$table_row_color">
<td align="left">
<nobr><b>Category:</b></nobr>
</td>
<td align="left">
<select name="$catName">
<option value="">--- Select ---
END;
# Write the available categories from the DB
$SqlStatement = "SELECT id, name FROM class05_category WHERE id>1 ORDER BY name";
$result = mysql_query($SqlStatement,$connection);
if (!$result) die("Error " . mysql_errno() . " : " . mysql_error());
while ($row = mysql_fetch_array($result,MYSQL_NUM))
{ $id = $row[0];
$name = $row[1];
$extraText = "";
if ($id==$catValue) $extraText = " selected";
echo <<<END
<option value="$id" $extraText>$name
END;
}
# Now finish our select and table, put in our submit widget and end the form
echo <<<END
</select>
</td>
</tr>
<tr><td height="5" colspan="2"></td></tr>
<tr>
<td></td>
<td align="left" valign="top">
<input type="submit" name="$submitName" value="$submitValue"></td></tr>
</table></form>
<p>
END;
#########################################################
# Use SELECT to show favorite sites
#########################################################
echo <<<END
<table cellspacing="1" cellpadding="5" border="0" class="arial13">
<tr bgcolor="$table_header_color">
<td align="left"><b>Title</b></td>
<td align="left"><b>URL</b></td>
<td align="left"><b>Category</b></td>
<td align="left"><b>Description</b></td>
<td align="left"><b>Votes</b></td>
</tr>
END;
# Retrieve all rows from the table and put in normal array.
$SqlStatement = "SELECT s.id, s.title, s.url, s.description, s.votes, c.id, c.name
FROM class05_table2 s, class05_category c
WHERE s.category_id=c.id
ORDER BY s.votes desc, s.title";
# Run the query on the database through the connection
$result = mysql_query($SqlStatement,$connection);
if (!$result)
die("Error " . mysql_errno() . " : " . mysql_error());
while ($row = mysql_fetch_array($result,MYSQL_NUM))
{
# Show category text if defined
$category_text = "";
if ($row[5]>1) $category_text = $row[6];
echo <<<END
<tr bgcolor="$table_row_color">
<td align="left"><a target="newSite" href="$row[2]">$row[1]</a></td>
<td align="left">$row[2]</td>
<td align="left">$category_text</td>
<td align="left">$row[3]</td>
<td align="right">$row[4]</td>
</tr>
END;
}
echo <<<END
</table><p>
END;
#########################################################
# Disconnect from the database.
#########################################################
mysql_close($connection);
?>
</body>
</html>