<?php
#########################################################
# class05-sites2.php - Submit and Vote for your Favorite Sites
#########################################################
$scriptName = $_SERVER['PHP_SELF'];
$pageTitle = "Class 5 Example - Submit and Vote for your Favorite Sites";
# 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
#########################################################
# Submit widget
$submitName = "DataAction";
### Voting widgets ###
$radioWidgetName = "fav_site"; # All our radio buttons for voting will be named this
$radioWidgetValue = $_POST[$radioWidgetName]; # Get value of selected radio button if possible
$submitVoteValue = "Vote for Your Favorite Site"; # This is what it will say on our submit button
### New site suhmission widgets ###
$textTitleName = "title"; # The name of text widget that has new website name
$textTitleValue = $_POST[$textTitleName]; # The value of title 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
$submitNewSiteValue = "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]==$submitVoteValue)
{
# Get value of the radio button selected
$site_id = $radioWidgetValue;
if (!isset($site_id))
{ # Submit button was pressed, but no radio button was selected
$hasErrors = 1;
$statusMsg = "Please select a favorite site.";
}
# If we had no errors, we can put it in the database
if (!$hasErrors)
{
# Get current number of votes for this site
$nVotes = 0;
$SqlStatement = "SELECT votes FROM class05_table2 WHERE id=$site_id";
$result = mysql_query($SqlStatement,$connection);
if (!$result) die("Error " . mysql_errno() . " : " . mysql_error());
if ($row = mysql_fetch_array($result,MYSQL_NUM))
{ $nVotes = $row[0];
}
# Increment the number of votes by one
$nVotes++;
# Now update the vote count for this site using an UPDATE query
$SqlStatement = "UPDATE class05_table2 SET votes=$nVotes WHERE id=$site_id ";
$result = mysql_query($SqlStatement,$connection);
if (!$result) die("Error " . mysql_errno() . " : " . mysql_error());
$statusMsg = "Thank you for your vote!";
}
}
elseif ($_POST[$submitName]==$submitNewSiteValue)
{ # 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 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)
VALUES ('$titleDB','$urlDB','$descDB') ";
# Run the query on the database through the connection
$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 = "";
}
}
#########################################################
# 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>
Here are my current favorite websites. Which one is your favorite?
<p>
<?php
# If we put anything in the general status message, then print it
if (!empty($statusMsg))
{ print '<font color="#990000"><b>'.$statusMsg.'</b></font> <p>';
}
#########################################################
# Use SELECT to show favorite sites
#########################################################
echo <<<END
<table cellspacing="1" cellpadding="5" border="0" class="arial13">
<form action="$scriptName" method="POST" enctype="application/x-www-form-urlencoded">
<tr bgcolor="$table_header_color">
<td align="left"></td>
<td align="left"><b>Title</b></td>
<td align="left"><b>URL</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 id, title, url, description, votes FROM class05_table2
ORDER BY votes desc, 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))
{
echo <<<END
<tr bgcolor="$table_row_color">
<td align="left"><input type=radio name="$radioWidgetName" value="$row[0]"></td>
<td align="left"><a target="newSite" href="$row[2]">$row[1]</a></td>
<td align="left">$row[2]</td>
<td align="left">$row[3]</td>
<td align="right">$row[4]</td>
</tr>
END;
}
echo <<<END
</table><br>
<input type="submit" name="$submitName" value="$submitVoteValue">
</form>
END;
#########################################################
# Now show new site submission form
#########################################################
echo <<<END
<p>
<hr size=1 color="#000000">
<p>
With all due respect, none of these are my favorite, how could you have left out this one?
<p>
<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="title" 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>';
}
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="url" 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>';
}
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="desc" 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>';
}
# Now finish our table, put in our submit widget and end the form
echo <<<END
</td>
</tr>
<tr><td height="5" colspan="2"></td></tr>
<tr>
<td></td>
<td align="left" valign="top">
<input type="submit" name="$submitName" value="$submitNewSiteValue"></td></tr>
</table></form>
END;
#########################################################
# Disconnect from the database.
#########################################################
mysql_close($connection);
?>
</body>
</html>