Forum Moderators: coopster
I essentially want to have several columns in mysql with various types of crude oils ranging by date. I want to be able to search by date and select which column (type of crude) to display. Ultimately I want to be able to have a graph of the selected date range with the types of crudes and a math formula such as a 3-2-1 crack spread [en.wikipedia.org...] and an option to export as a CSV.
So first thing first getting it to display the search result.
search.php
[codes]
<div id="Header"><a href="" title="Crude Data">Crude Data</a></div>
<div id="Content">
<h1>Edit / Delete An Entry</h1>
<BR>
<?php
$db = mysql_connect($host, $username, $password);
if (!$db)
{
print "Could not connect to MySQL server!<BR>";
exit;
}
$result = mysql_select_db($database, $db);
if (!$result) { // Did selection fail?
// Handle error
print "DB Selection failed.<br>Error # $mysql_errno()<BR>Error msg: $mysql_e
rror()";
exit;
}
// Make a MySQL Connection
$dsql = "select ddates from crude";
$result1 = mysql_query($dsql) or die(mysql_error());
$result2 = mysql_query($dsql) or die(mysql_error());
?>
<form action="view.php" method="POST">
<table width="50%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2">Date Range: <br /></td>
</tr>
<tr>
<td>Start Date: <br /></td>
<td>Stop Date: <br /></td>
</tr>
<tr>
<td><select name='xdate'>
<option value=''>Select Start Time</option>
<?php
while ($row = mysql_fetch_assoc($result1)) {
echo "<option value='{$row['ddates']}'>{$row['ddates']}</option>";
}
?>
</select></td>
<td><select name='zdate'>
<option value=''>Select Stop Time</option>
<?php
while ($row = mysql_fetch_assoc($result2)) {
echo "<option value='{$row['ddates']}'>{$row['ddates']}</option>";
}
?>
</select></td>
</tr>
</table>
<br />
Select Resource: <br />
<select name="fromwhere">
<option value="crude">EIA</option>
<option value="platts">platts</option>
</select>
<br />
<br />
Select Data: <br />
<input name="view[]" type="checkbox" value="USGC Conventional Regular Gasoline (cents per gallon)">USGC Conventional Regular Gasoline (cents per gallon)</input><br><br>
<input name="view[]" type="checkbox" value="Heating Oil (HSD) (cents per gallon)">Heating Oil (HSD) (cents per gallon)</input><br><br>
<input name="view[]" type="checkbox" value="USGC Low Sulfur Diesel (LSD) (cents per gallon)">USGC Low Sulfur Diesel (LSD) (cents per gallon)</input><br><br>
<input name="view[]" type="checkbox" value="WTI Crude Cushing (WTI) (Dollar per barrel)">WTI Crude Cushing (WTI) (Dollar per barrel)</input><br><br>
<input name="view[]" type="checkbox" value="West Texas Sour (WTS) (Dollar per barrel)">West Texas Sour (WTS) (Dollar per barrel)</input><br><br>
<input name="view[]" type="checkbox" value="Louisiana Sweet (LLS) (Dollar per barrel)">Louisiana Sweet (LLS) (Dollar per barrel)</input>
<br />
<br />
<input type ="submit" value="SUBMIT"/>
</form> [/codes]
view.php
<div id="Header"><a href="" title="Crude Chart">Crude Chart</a></div>
<div id="Content">
<?php $db = mysql_connect($host, $username, $password);
if (!$db)
{
print "Could not connect to MySQL server!<BR>";
exit;
}
$result = mysql_select_db($database, $db);
if (!$result) { // Did selection fail?
// Handle error
print "DB Selection failed.<br>Error # $mysql_errno()<BR>Error msg: $mysql_error()";
exit;
}
// Make a MySQL Connection
if(isset($_POST['fromwhere'])):
$fromwhere = $_POST['fromwhere'];
$x = DATE($_POST['xdate']);
$z = DATE($_POST['zdate']);
$select = implode(", ", $_POST["view"]);
$result = @mysql_query("SELECT ".$select.", FROM ddates as date_string FROM $fromwhere WHERE ddates>='$x' AND ddates<='$z'");
if (!$result) {
exit('<p>Error performing query: ' . mysql_error() . '</p>');
}
?>
<table border="1" align="center" cellpadding="2" cellspacing="2" bordercolor="#000000">
<tr><th>Day</th><th>USGC Conventional Regular Gasoline (cents per gallon)</th><th>Heating Oil (HSD) (cents per gallon)</th> <th>USGC Low Sulfur Diesel (LSD) (cents per gallon)</th><th>WTI Crude Cushing (WTI) (Dollar per barrel)</th><th>West Texas Sour (WTS) (Dollar per barrel)</th><th>Louisiana Sweet (LLS) (Dollar per barrel)</th></tr>
<?php
while ($row = mysql_fetch_array($result)) {
// $id=$row['id'];
$dbdate=$row["ddates"];
echo '<tr><td align=center>' . $dbdate . '</td>';
echo '<td align=center>' . $row['USGC Conventional Regular Gasoline (cents per gallon)'] . '</td>';
echo '<td align=center>' . $row['Heating Oil (HSD) (cents per gallon)'] . '</td>';
echo '<td align=center>' . $row['USGC Low Sulfur Diesel (LSD) (cents per gallon)'] . '</td>';
echo '<td align=center>' . $row['WTI Crude Cushing (WTI) (Dollar per barrel)'] . '</td>';
echo '<td align=center>' . $row['West Texas Sour (WTS) (Dollar per barrel)'] . '</td>';
echo '<td align=center>' . $row['Louisiana Sweet (LLS) (Dollar per barrel)'] . '</td>';
echo "</tr>";
}
?>
</table>
upon executing I get an error on line 82 but I can not proceed. i created a free public hosting account here with a preview of this script. any help or guidance appreciated.
thanks in advance
[edited by: eelixduppy at 1:49 am (utc) on Oct. 20, 2009]
[edit reason] no URLs, please [/edit]
I saw a recent thread here re passing info from a form as an array, but it didn't work... I thought it was just the PHP for a while, and usually use 'meaningful names' for variables (IOW I would use view_1 or view_something and loop through them) personally, because I don't like to try and pass arrays through forms, but the other day I was asked to fix a form which was not working correctly... The form suddenly stopped sending the variables passed as arrays to e-mail. I looked at the manual and the form was written correctly according to the book, so, IMO for some reason PHP is not receiving arrays from forms correctly in some 'newer' version.
Try removing the array to be passed from your form and see what happens... I switched the form I was asked to fix to unique variables rather than an array and used a for loop with a conditional (if) and a simple counter to extract the information from the unique variables rather than passing an array.
Just to be clear the search page brings up all the information correctly including the dates. I'll try what you said madscientist and report back.
thanks