Jump to content

Compact script


horaliar

Recommended Posts

This is my first script that I wrote for my department. It runs queries against a mysql database depending on parameters that the user has entered. I'm running into trouble because I wrote everything in one file, except some javascript functions, and now I'm having problems adding to it. I would like to have some ideas on how to divide the code in different files and make it more manageble. Any ideas will be welcomed. Thanks!

<html>
<head>
<link rel="stylesheet" href="myStyle.css" type="text/css">
<script LANGUAGE="javascript" TYPE="text/javascript" SRC="tools.js"></SCRIPT>
<script LANGUAGE="JavaScript" SRC="CalendarPopup.js"></SCRIPT>
<script LANGUAGE="JavaScript">
var cal = new CalendarPopup();
</SCRIPT>
</head>
<body>

<?php
//Connect to the server.
$link = mysql_connect("", "", "");
//Connect to the db.
mysql_select_db("dbads", $link);
?>

<form method="POST" name="data" action="adserv.php">
<input type="hidden" name="command">
<input type="hidden" name="periodname">
<input type="hidden" name="pstartdate">
<input type="hidden" name="penddate">

<!-- Table Title-->
<table class="title" height="20" cellSpacing="0" cellPadding="0" width="750" border="0">
<tr>
<th class="title" valign="bottom" class="title">
New and Late Ad Report
</th>
</tr>
</table>

<br>

<!-- Table Data to Enter -->
<table height="20" cellSpacing="0" cellPadding="0" width="750" border="0">
<!-- Row Headings -->
<tr>
<th class="heading" width="150" align="left">Select a date:</th>
<th class="heading" width="300" align="left">Select a start/end date:</th>
<th class="heading" width="150" align="left">Select a period:</th>
<th class="heading" width="150" align="left">Select a user:</th>
</tr>

<!-- Row containing items to enter data -->
<tr>
<!-- ENTER A DATE -->
<td class="item" width="150">
<input class="textbox" type="text" name="selDate" value="" size="10">
<input type="image" align="middle" value="cal1" src="pdate.gif" name="cal1" onclick="cal.select(document.forms['data'].selDate,'cal1','MM/dd/yyyy'); return false;">
</td>

<!-- ENTER START AND END DATE -->
<td class="item" width="300">
<input class="textbox" type="text" name="startDate" value="" size="10">
<input type="image" align="middle" value="cal2" src="pdate.gif" name="cal2" onclick="cal.select(document.forms['data'].startDate,'cal2','MM/dd/yyyy');return false;">
<input class="textbox" type="text" name="endDate" value="" size="10">
<input type="image" align="middle" value="cal3" src="pdate.gif" name="cal3" onclick="cal.select(document.forms['data'].endDate,'cal3','MM/dd/yyyy');return false;">
</td>

<!-- SELECT A PERIOD -->
<td class="item" width="150">
<select size="1" class="textbox" id="periodname" name="periodname" onChange="getPeriodDates(this)">
<option value="">-</option>
<?php
//Creates query to retrieve the data to be used in the drop down box.
$query="SELECT PeriodName, StartDate, EndDate FROM tblPeriod";
if ($hResult=mysql_query($query))
{
while ($hFetch=mysql_fetch_array($hResult))
{
$periodname=$hFetch["PeriodName"];
$pstartdate=$hFetch["StartDate"];
$penddate=$hFetch["EndDate"];
echo "<option value='$periodname||$pstartdate||$penddate'\">".$periodname."</option>";
}
}
?>
</select>

</td>

<!-- SELECT A USER -->
<td class="item" width="150">
<select size="1" class="textbox" id="username" name="username">
<option value="">-</option>
/*
<?php
//Creates query to retrieve the data to be used in the drop down box.
$query="SELECT PeriodName, StartDate, EndDate FROM tblPeriod";
if ($hResult=mysql_query($query))
{
while ($hFetch=mysql_fetch_array($hResult))
{
$periodname=$hFetch["PeriodName"];
$startdate=$hFetch["StartDate"];
$enddate=$hFetch["EndDate"];
echo "<option value='$periodname||$startdate||$enddate'\">".$periodname."</option>";
}
}
?>
*/
</select>
</td>

</tr>
</table>

<input class="add" type="button" value="Submit" onclick="doSave(this, 'Save')"></td>



<?php

//COMMAND
if (isset($_POST["command"]))
$command = $_POST["command"];
//SELECTED DATE
if (isset($_POST["selDate"]))
$postselDate = $_POST["selDate"];
//START DATE
if (isset($_POST["startDate"]))
$poststartdate = $_POST["startDate"];
//END DATE
if (isset($_POST["endDate"]))
$postenddate = $_POST["endDate"];
//PERIOD NAME
if (isset($_POST["periodname"]))
$postperiodname = $_POST["periodname"];
//PERIOD START DATE
if (isset($_POST["pstartdate"]))
$postpstartdate = $_POST["pstartdate"];
//PERIOD END DATE
if (isset($_POST["penddate"]))
$postpenddate = $_POST["penddate"];

//COMPARE COMMAND, IF IT IS SAVE, CHECK INFO AND RUN APPROPRIATE QUERIES. DISPLAY INFO.
if (strcmp("Save", $command) == 0)
{
//CHECK FOR MISSING PARAMETERS
//Check that a date has been entered.
if ($postselDate == null AND $poststartdate == null AND $postenddate == null AND $postperiodname == null)
die("Enter a date");
//Check that only a date or period has been entered.
if ($postselDate != null AND $postperiodname != null)
die("Either enter a date or select a period");
//Check that only a date or a date range has been selected.
if ($postselDate != null AND ($poststartdate != null OR $postenddate != null))
die("Either enter a single date or a date range");
//Check that only a period or a date range has been selected.
if ($postperiodname != null AND ($poststartdate != null OR $postenddate != null))
die("Either enter a date range or select a period");
//Check that if a start date has been entered, an end date has also been entered.
if ($poststartdate == null AND $postenddate != null)
die("Enter a start date");
if ($poststartdate != null AND $postenddate == null)
die("Enter an end date");
//Check that end date is higher than start date.
if ($poststartdate > $postenddate)
die("End date must be later than start date.");

//Assign a variable to be used in case statement.
if ($postselDate != null AND $poststartdate == null AND $postenddate == null AND $postperiodname == null)
{
$parameterType = "sglDate";
}
elseif ($poststartdate != null AND postenddate != null AND $postselDate == null AND $postperiodname == null)
{
$parameterType = "dteRange";
}
elseif (postperiodname != null AND $postselDate == null AND $poststartdate == null AND $postenddate == null)
{
$parameterType = "dtePeriod";
}
else
{
die("Error");
}

//Create queries depending on what parameter has been entered.
switch ($parameterType)
{
case "sglDate":
$queryNew = "SELECT COUNT(job_id) from tblads WHERE DATE(action_time)=STR_TO_DATE('$postselDate','%m/%d/%Y');";
$queryLate = "SELECT COUNT(if(TIMEDIFF(deadline, adintime)<0,'late','')) FROM tblads WHERE ((if(TIMEDIFF(deadline, adintime)<0,'late','')='late')) AND DATE(action_time)=STR_TO_DATE('$postselDate','%m/%d/%Y');";
$queryResults = "SELECT * from tblads WHERE DATE(action_time)=STR_TO_DATE('$postselDate','%m/%d/%Y');";
break;
case "dteRange":
$queryNew = "SELECT COUNT(job_id) from tblads WHERE DATE(action_time)>=STR_TO_DATE('$poststartdate','%m/%d/%Y') AND DATE(action_time)<=STR_TO_DATE('$postenddate','%m/%d/%Y');";
$queryLate = "SELECT COUNT(if(timediff(deadline,adintime)<0,'late','')) FROM tblads WHERE ((if(timediff(deadline,adintime)<0,'late','')='late')) AND DATE(action_time)>=str_to_date('$poststartdate','%m/%d/%Y') AND DATE(action_time)<=str_to_date('$postenddate','%m/%d/%Y');";
break;
case "dtePeriod":
$queryNew = "SELECT COUNT(job_id) FROM tblads WHERE DATE(action_time)>='$postpstartdate' AND DATE(action_time)<='$postpenddate';";
$queryLate = "SELECT COUNT(if(timediff(deadline,adintime)<0,'late','')) FROM tblads WHERE ((if(timediff(deadline,adintime)<0,'late','')='late')) AND DATE(action_time)>='$postpstartdate' AND DATE(action_time)<='$postpenddate';";
} //Closes switch

//Runs both queries
$resultNew = mysql_query($queryNew, $link);
if(!$resultNew)
die("Invalid query: ".mysql_error()."<br>".$queryNew);
$resultLate = mysql_query($queryLate, $link);
if(!$resultLate)
die("Invalid query: ".mysql_error()."<br>".$queryLate);
//Fetches the query's result
while($row = mysql_fetch_array($resultNew))
{
$NewAds = $row[0];
}
while($row = mysql_fetch_array($resultLate))
{
$LateAds = $row[0];
}

if ($NewAds == 0)
{
echo "There are no ads for this date.";
}
else
{
//Create table to hold queries results.
?>
<table class="title" height="20" cellSpacing="0" cellPadding="0" width="300" border="0">
<tr>
<th class="heading" width="100" align="center">Total New Ads</th>
<th class="heading" width="100" align="center">Total Late Ads</th>
<th class="heading" width="100" align="center">Percentage</th>
</tr>
<tr>
<td class="item" width="100" align="center"><?php echo $NewAds ?></td>
<td class="item" width="100" align="center"><?php echo $LateAds ?></td>
<td class="item" width="100" align="center"><?php echo number_format($LateAds/$NewAds*100,2,'.','').'%' ?></td>
</tr>
</table>
<?php
echo '<input type="button" value="Go To" onClick="location.href=\'view_results.php\'">';
}
}
?>

</table>
</form>
</body>
</html>
Link to comment
https://forums.phpfreaks.com/topic/8660-compact-script/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.