Web Hosting Talk







View Full Version : (PHP) Almost there, I think; but one problem


calidude
07-17-2008, 06:18 AM
I have made a lot of progress the past few days in my zip code / region project; but I have one small problem.

The data won't display. :(

Here is the code that I have so far:

<?php

error_reporting( E_ALL );

// ------------------------------------------------------------------------------------------
// Script to display the weather forecasts for southern California on OWSweather.com
// ------------------------------------------------------------------------------------------
// Written by: Joshua T. Young and others
// ------------------------------------------------------------------------------------------
// Started on: Sunday, July 13, 2008 at 7:30 PM PDT
// ------------------------------------------------------------------------------------------

// Connect to Mysql. If this doesn't work, then we are SOL. :(
$DB = mysql_connect("***", "***", "***") or die ("Error connecting to MySQL:<br />".mysql_error());
// Connect to the Database
mysql_select_db("***",$DB) or die ("Error connecting to Database:<br />".mysql_error());

// Did we succeed? If not, try again. If so, good. Then let's move on.
// Set $zip from $_POST or elsewhere

// $zip = mysql_real_escape_string( $_POST['zip_code'] );
$region = mysql_real_escape_string( $_POST['Region'] );

/* $result = mysql_query( "SELECT * FROM *** WHERE Zip_Codes = '{$zip}'" )
or die(mysql_error()); */

$result = mysql_query( "SELECT * FROM *** WHERE Region = '{$region}'" );

if( $row = mysql_fetch_assoc( $result ) )
{
// This loops through all the data you've selected from the database
while( $data = mysql_fetch_assoc( $result ) )
{
// Format and output your data here
foreach( $data as $key => $value )
{
echo "{$key}: {$value}<br />";
}
echo '<hr />';
}
}
else
{
$error = "That city does not exist in our database. Please try again.";
}

// Determine the appropriate icons to use in the forecast section. *bleep* this part will be hard too.

/* if $var['Sky_Conditions'] = Sunny
display */

?>

I think the problem is with the part where it says format and output your data here, but I don't know what to do. :( Any ideas?

insanelymacintosh
07-17-2008, 06:42 AM
Are you only retrieving only one record?

right above " // This loops through all the data you've selected from the database "

put in to your code: var_dump($row);

right above "// Format and output your data here"

put: var_dump($data);

and see if it prints anything out.

calidude
07-17-2008, 06:57 AM
Are you only retrieving only one record?

right above " // This loops through all the data you've selected from the database "

put in to your code: var_dump($row);

right above "// Format and output your data here"

put: var_dump($data);

and see if it prints anything out.

It has the data that I input, yes.

array(18) { ["Region"]=> string(2) "11" ["High_Temperature_Day_1"]=> string(2) "80" ["High_Temperature_Day_2"]=> string(2) "80" ["High_Temperature_Day_3"]=> string(2) "80" ["High_Temperature_Day_4"]=> string(2) "80" ["High_Temperature_Day_5"]=> string(2) "80" ["High_Temperature_Day_6"]=> string(2) "85" ["High_Temperature_Day_7"]=> string(2) "85" ["Low_Temperature_Day_1"]=> string(2) "50" ["Low_Temperature_Day_2"]=> string(2) "50" ["Low_Temperature_Day_3"]=> string(2) "50" ["Low_Temperature_Day_4"]=> string(2) "50" ["Low_Temperature_Day_5"]=> string(2) "50" ["Low_Temperature_Day_6"]=> string(2) "55" ["Low_Temperature_Day_7"]=> string(2) "55" ["Winds"]=> string(2) "10" ["Rainfall"]=> string(1) "0" ["Sky_Conditions"]=> string(13) "Partly Cloudy" }

but for some reason that data isn't showing up on the page like it should be. :(

Steve_Arm
07-17-2008, 07:33 AM
You are not selecting the db field to show.

calidude
07-17-2008, 07:45 AM
You are not selecting the db field to show.

What do you mean?

I tried changing it to this:

$result = mysql_query( "SELECT * FROM *** WHERE Region = '{$region}'" );
$result = mysql_query( "SELECT * FROM *** WHERE High_Temperature_Day_1 = '{$hiday1}'" );
$result = mysql_query( "SELECT * FROM *** WHERE High_Temperature_Day_1 = '{$hiday2}'" );
$result = mysql_query( "SELECT * FROM *** WHERE High_Temperature_Day_1 = '{$hiday3}'" );
$result = mysql_query( "SELECT * FROM *** WHERE High_Temperature_Day_1 = '{$hiday4}'" );
$result = mysql_query( "SELECT * FROM *** WHERE High_Temperature_Day_1 = '{$hiday5}'" );
$result = mysql_query( "SELECT * FROM *** WHERE High_Temperature_Day_1 = '{$hiday6}'" );
$result = mysql_query( "SELECT * FROM *** WHERE High_Temperature_Day_1 = '{$hiday7}'" );
$result = mysql_query( "SELECT * FROM *** WHERE Low_Temperature_Day_1 = '{$loday1}'" );
$result = mysql_query( "SELECT * FROM *** WHERE Low_Temperature_Day_1 = '{$loday2}'" );
$result = mysql_query( "SELECT * FROM *** WHERE Low_Temperature_Day_1 = '{$loday3}'" );
$result = mysql_query( "SELECT * FROM *** WHERE Low_Temperature_Day_1 = '{$loday4}'" );
$result = mysql_query( "SELECT * FROM *** WHERE Low_Temperature_Day_1 = '{$loday5}'" );
$result = mysql_query( "SELECT * FROM *** WHERE Low_Temperature_Day_1 = '{$loday6}'" );
$result = mysql_query( "SELECT * FROM *** WHERE Low_Temperature_Day_1 = '{$loday7}'" );
$result = mysql_query( "SELECT * FROM *** WHERE Winds = '{$winds}'" );
$result = mysql_query( "SELECT * FROM *** WHERE Rainfall = '{$rainfall}'" );
$result = mysql_query( "SELECT * FROM *** WHERE Sky_Conditions = '{$skycond}'" );

if( $row = mysql_fetch_assoc( $result ) )
{
// var_dump($row);
// This loops through all the data you've selected from the database
while( $data = mysql_fetch_assoc( $result ) )
{
// var_dump($data);
// Format and output your data here
foreach( $data as $key => $value )
{
echo "{$region}";
echo "{$hiday1}";
echo "{$hiday2}";
echo "{$hiday3}";
echo "{$hiday4}";
echo "{$hiday5}";
echo "{$hiday6}";
echo "{$hiday7}";
echo "{$loday1}";
echo "{$loday2}";
echo "{$loday3}";
echo "{$loday4}";
echo "{$loday5}";
echo "{$loday6}";
echo "{$loday7}";
echo "{$winds}";
echo "{$rainfall}";
echo "{$skycond}";
echo "<br />";
}
echo '<hr />';
}
}

but it still didn't work. :(

Steve_Arm
07-17-2008, 08:43 AM
Leave the query as is and try this loop:

$str = '';
while ($data = mysql_fetch_assoc($result)) {
$str .= 'Region: '.$data["Region"];
$str .= 'High_Temperature_Day_1: '.$data["High_Temperature_Day_1"];
// ...
}
echo $str;

insanelymacintosh
07-17-2008, 08:47 AM
try


// Format and output your data here
while( $data = mysql_fetch_assoc( $result ) )
{
echo $data['Region'] . '<br />';
echo $data['High_Temperature_Day_1'] . '<br />';
echo $data['High_Temperature_Day_2'] . '<br />';
}
echo '<hr />';

calidude
07-17-2008, 08:53 AM
Leave the query as is and try this loop:

$str = '';
while ($data = mysql_fetch_assoc($result)) {
$str .= 'Region: '.$data["Region"];
$str .= 'High_Temperature_Day_1: '.$data["High_Temperature_Day_1"];
// ...
}
echo $str;



So change it to this?

if( $row = mysql_fetch_assoc( $result ) )
{
$result = mysql_query( "SELECT * FROM *** WHERE Region = '{$region}'" )
or die(mysql_error());

// This loops through all the data you've selected from the database
$str = '';
while ($data = mysql_fetch_assoc($result)) {
$str .= 'Region: '.$data["Region"];
$str .= 'High_Temperature_Day_1: '.$data["High_Temperature_Day_1"];
$str .= 'High_Temperature_Day_2: '.$data["High_Temperature_Day_2"];
$str .= 'High_Temperature_Day_3: '.$data["High_Temperature_Day_3"];
$str .= 'High_Temperature_Day_4: '.$data["High_Temperature_Day_4"];
$str .= 'High_Temperature_Day_5: '.$data["High_Temperature_Day_5"];
$str .= 'High_Temperature_Day_6: '.$data["High_Temperature_Day_6"];
$str .= 'High_Temperature_Day_7: '.$data["High_Temperature_Day_7"];
$str .= 'Low_Temperature_Day_1: '.$data["Low_Temperature_Day_1"];
$str .= 'Low_Temperature_Day_2: '.$data["Low_Temperature_Day_2"];
$str .= 'Low_Temperature_Day_3: '.$data["Low_Temperature_Day_3"];
$str .= 'Low_Temperature_Day_4: '.$data["Low_Temperature_Day_4"];
$str .= 'Low_Temperature_Day_5: '.$data["Low_Temperature_Day_5"];
$str .= 'Low_Temperature_Day_6: '.$data["Low_Temperature_Day_6"];
$str .= 'Low_Temperature_Day_7: '.$data["Low_Temperature_Day_7"];
$str .= 'Winds: '.$data["Winds"];
$str .= 'Rainfall: '.$data["Rainfall"];
$str .= 'Sky_Conditions: '.$data["Sky_Conditions"];
// ...
}
echo $str;
}

Steve_Arm
07-17-2008, 11:08 AM
Pretty much, does it work?

Are the 3 asterisks in the query the name of the table or are you hiding it?

calidude
07-25-2008, 06:31 PM
Pretty much, does it work?

Are the 3 asterisks in the query the name of the table or are you hiding it?

I've been loaded with work so I forgot to reply back. Sorry about that. :(

I was hiding it. Nothing against you, just protecting my @$$ since I am the server admin and in case Google spiders this topic and saw my MySQL server details; and a hacker got into our server; it would not be a good thing at all. :(

But anyway...

I've got the script working on version 1.0 now on the site.

But I want to start prelim work on 2.0; and that version would be nice if it could include sun / rain / wind / etc. icons for the sky conditions; as well as large, red numbers for the high temperatures and large, blue numbers for the low temperatures (and large, green numbers for expected rainfall totals). This is where the code stands as of right now:

zipcodes.php:

<?php

error_reporting( E_ALL );

// -------------------------------------------- ------------------------------------
// Script to display the weather forecasts for southern California on OWSweather.com
// -------------------------------------------- ------------------------------------
// Written by: Joshua T. Young and others
// -------------------------------------------- ------------------------------------
// Started on: Sunday, July 13, 2008 at 7:30 PM PDT
// -------------------------------------------- ------------------------------------

// Connect to Mysql. If this doesn't work, then we are SOL. :(
$DB = mysql_connect("***", "***", "***") or die ("Error connecting to MySQL:<br />".mysql_error());
// Connect to the Database
mysql_select_db("***",$DB) or die ("Error connecting to Database:<br />".mysql_error());

// Did we succeed? If not, try again. If so, good. Then let's move on.
// Set $zip from $_POST or elsewhere

// $zip = mysql_real_escape_string( $_POST['zip_code'] );
$region = mysql_real_escape_string( $_POST['Region'] );

/* $result = mysql_query( "SELECT * FROM Zipcodes WHERE Zip_Codes = '{$zip}'" )
or die(mysql_error()); */

$result = mysql_query( "SELECT * FROM owsforecasts WHERE Region = '{$region}'" )
or die(mysql_error());

// This loops through all the data you've selected from the database
while( $data = mysql_fetch_assoc( $result ) )
{
// Format and output your data here
foreach( $data as $key => $value )
{
echo "{$key}: {$value}<br />";
}
echo '<hr />';
}
// Determine the appropriate icons to use in the forecast section. *bleep* this part will be hard too.

/* if $var['Sky_Conditions'] = Sunny
display */

?>

The above is the PHP code embedded within the HTML page that the output displays on. I want there to be a part, indicated by the comment at the end; which displays the appropriate forecast icon(s) to display; as well as colorize the high / low temperatures; and the amount of rainfall expected - similar to this:

http://www.owsweather.com/coweekly.jpg

(ignore the huge logo and the word Corona on the bottom, it's just the first sample image that I pulled up.)

So any ideas on how I can pull this off?

calidude
07-27-2008, 02:20 AM
* Polite bump *

Someone gave me some pointers on another site but it's not working. So far this is what I have:

zipcodes.php

<?php

error_reporting( E_ALL );

// ------------------------------------------------------------------------------------------
// Script to display the weather forecasts for southern California on OWSweather.com
// ------------------------------------------------------------------------------------------
// Written by: Joshua T. Young and others
// ------------------------------------------------------------------------------------------
// Started on: Sunday, July 13, 2008 at 7:30 PM PDT
// ------------------------------------------------------------------------------------------

// Connect to Mysql. If this doesn't work, then we are SOL. :(
$DB = mysql_connect("***", "***", "***") or die ("Error connecting to MySQL:<br />".mysql_error());
// Connect to the Database
mysql_select_db("***",$DB) or die ("Error connecting to Database:<br />".mysql_error());

// Did we succeed? If not, try again. If so, good. Then let's move on.
// Set $zip from $_POST or elsewhere

// $zip = mysql_real_escape_string( $_POST['zip_code'] );
$region = mysql_real_escape_string( $_POST['Region'] );

/* $result = mysql_query( "SELECT * FROM Zipcodes WHERE Zip_Codes = '{$zip}'" )
or die(mysql_error()); */

$result = mysql_query( "SELECT * FROM owsforecasts WHERE Region = '{$region}'" )
or die(mysql_error());

// We'll store our HTML output in these variables
$highs = '<tr class="highs">';
$lows = '<tr class="lows">';
$imgs = '<tr class="images">';
$rain = '<tr class="rain">';

// There should only be one result here....
if( $data = mysql_fetch_assoc( $result ) )
{
// Build the output here
foreach( $data as $key => $value )
{
if( strpos( $key, 'High_Temperature' ) !== FALSE )
{
// Highs
$highs .= '<td>' . $value . '</td>';
}
else if( strpos( $key, 'Low_Temperature' ) !== FALSE )
{
// Lows
$lows .= '<td>' . $value . '</td>';
}
else if( strpos( $key, 'Rainfall' ) !== FALSE )
{
// Rain
$rain .= '<td>' . $value . '</td>';
}
else if( strpos( $key, 'Sky_Conditions' ) !== FALSE )
{
// Sky condition images
switch( $value )
{
case 'Sunny':
$imgs .= '<td>http://www.owsweather.com/zip_code_project/images/sunny.jpg</td>';
break;
case 'Cloudy':
$imgs .= '<td>http://www.owsweather.com/zip_code_project/images/cloudy.jpg</td>';
break;
case 'Partly_Cloudy':
$imgs .= '<td>http://www.owsweather.com/zip_code_project/images/partlycloudy.jpg</td>';
break;
case 'Rainy':
$imgs .= '<td>http://www.owsweather.com/zip_code_project/images/rainy.jpg</td>';
break;
case 'Windy':
$imgs .= '<td>http://www.owsweather.com/zip_code_project/images/windy.jpg</td>';
break;
case 'Foggy':
$imgs .= '<td>http://www.owsweather.com/zip_code_project/images/foggy.jpg</td>';
break;
case 'Snowy':
$imgs .= '<td>http://www.owsweather.com/zip_code_project/images/snowy.jpg</td>';
break;
case 'Hazy':
$imgs .= '<td>http://www.owsweather.com/zip_code_project/images/hazy.jpg</td>';
break;

// Add more cases here

default:
$imgs .= '<td>unknown.jpg</td>';
}
}
}
}

// Put it all together
echo '<table>' . $imgs . '</tr>' . $highs . '</tr>' . $lows . '</tr>' . $rain . '</tr></table>';

/* {
// Format and output your data here
foreach( $data as $key => $value )
{
echo "{$key}: {$value}<br />";
}
echo '<hr />';
}*/
?>

owsforecasts1.php:

<?php

// ------------------------------------------------------------------------
// Forecast input script for OWSweather.com's new zip code database project
// ------------------------------------------------------------------------
// Written by: Joshua T. Young and others
// ------------------------------------------------------------------------
// Started on: Sunday, July 13, 2008 at 10:10 PM PDT
// ------------------------------------------------------------------------

// Check if the form was submitted
if($_POST['submit'])
{
// Connect to Mysql. If this doesn't work, then we are SOL. :(
$DB = mysql_connect("***", "***", "***") or die ("Error connecting to MySQL:<br />".mysql_error());
// Connect to the Database
mysql_select_db("***",$DB) or die ("Error connecting to Database:<br />".mysql_error());

// Create the Query
$sql = "DELETE FROM `owsforecasts` WHERE Region = '".mysql_escape_string($_POST['Region'])."'";
mysql_query( $sql );
$sql = "INSERT INTO `owsforecasts` ";
$sql .= "VALUES('".mysql_escape_string($_POST['Region'])."',";
$sql .= "'".mysql_escape_string($_POST['High_Temperature_Day_1'])."', '".mysql_escape_string($_POST['High_Temperature_Day_2'])."', ";
$sql .= "'".mysql_escape_string($_POST['High_Temperature_Day_3'])."', '".mysql_escape_string($_POST['High_Temperature_Day_4'])."', ";
$sql .= "'".mysql_escape_string($_POST['High_Temperature_Day_5'])."', '".mysql_escape_string($_POST['High_Temperature_Day_6'])."', ";
$sql .= "'".mysql_escape_string($_POST['High_Temperature_Day_7'])."', '".mysql_escape_string($_POST['Low_Temperature_Day_1'])."', ";
$sql .= "'".mysql_escape_string($_POST['Low_Temperature_Day_2'])."', '".mysql_escape_string($_POST['Low_Temperature_Day_3'])."', ";
$sql .= "'".mysql_escape_string($_POST['Low_Temperature_Day_4'])."', '".mysql_escape_string($_POST['Low_Temperature_Day_5'])."', ";
$sql .= "'".mysql_escape_string($_POST['Low_Temperature_Day_6'])."', '".mysql_escape_string($_POST['Low_Temperature_Day_7'])."', ";
$sql .= "'".mysql_escape_string($_POST['Rainfall_Day_1'])."', '".mysql_escape_string($_POST['Rainfall_Day_2'])."', ";
$sql .= "'".mysql_escape_string($_POST['Rainfall_Day_3'])."', '".mysql_escape_string($_POST['Rainfall_Day_4'])."', ";
$sql .= "'".mysql_escape_string($_POST['Rainfall_Day_5'])."', '".mysql_escape_string($_POST['Rainfall_Day_6'])."', ";
$sql .= "'".mysql_escape_string($_POST['Rainfall_Day_7'])."', '".mysql_escape_string($_POST['Sky_Conditions_Day_1'])."', ";
$sql .= "'".mysql_escape_string($_POST['Sky_Conditions_Day_2'])."', '".mysql_escape_string($_POST['Sky_Conditions_Day_3'])."', ";
$sql .= "'".mysql_escape_string($_POST['Sky_Conditions_Day_4'])."', '".mysql_escape_string($_POST['Sky_Conditions_Day_5'])."', ";
$sql .= "'".mysql_escape_string($_POST['Sky_Conditions_Day_6'])."', '".mysql_escape_string($_POST['Sky_Conditions_Day_7'])."', ";
$sql .= "'".mysql_escape_string($_POST['OWS_Warnings_/_Watches_/_Advisories'])."')";
// Run the Query
$result = mysql_query($sql) or die ("Error inserting data:<br />".mysql_error());
// Write Confirmation
echo "Thank you for your submission.";
}

?>
<p>Region: 1</p>
<form name="input" action="" method="post">
<input type="hidden" name="Region" value="1" />
<span class="style5">HIGH TEMPERATURES</span><br>
Day 1:
<input type="text" name="High_Temperature_Day_1" value="" size="20">
<br>Day 2:
<input type="text" name="High_Temperature_Day_2" value="" size="20">
<br>
Day 3:
<input type="text" name="High_Temperature_Day_3" value="" size="20">
<br>Day 4:
<input type="text" name="High_Temperature_Day_4" value="" size="20">
<br>
Day 5:
<input type="text" name="High_Temperature_Day_5" value="" size="20">
<br>Day 6:
<input type="text" name="High_Temperature_Day_6" value="" size="20">
<br>
Day 7:
<input type="text" name="High_Temperature_Day_7" value="" size="20"><br>
<span class="style5">LOW TEMPERATURES</span><br>
Day 1:
<input type="text" name="Low_Temperature_Day_1" value="" size="20">
<br>Day 2:
<input type="text" name="Low_Temperature_Day_2" value="" size="20">
<br>
Day 3:
<input type="text" name="Low_Temperature_Day_3" value="" size="20">
<br>Day 4:
<input type="text" name="Low_Temperature_Day_4" value="" size="20">
<br>
Day 5:
<input type="text" name="Low_Temperature_Day_5" value="" size="20">
<br>Day 6:
<input type="text" name="Low_Temperature_Day_6" value="" size="20">
<br>
Day 7:
<input type="text" name="Low_Temperature_Day_7" value="" size="20">
<br>
<span class="style5">RAINFALL</span><br>
Day 1:
<input type="text" name="Rainfall_Day_1" value="" size="20">
<br>Day 2:
<input type="text" name="Rainfall_Day_2" value="" size="20">
<br>
Day 3:
<input type="text" name="Rainfall_Day_3" value="" size="20">
<br>Day 4:
<input type="text" name="Rainfall_Day_4" value="" size="20">
<br>
Day 5:
<input type="text" name="Rainfall_Day_5" value="" size="20">
<br>Day 6:
<input type="text" name="Rainfall_Day_6" value="" size="20">
<br>
Day 7:
<input type="text" name="Rainfall_Day_7" value="" size="20">
<br>
<span class="style5">SKY CONDITIONS</span><br>
Day 1:
<input type="text" name="Sky_Conditions_Day_1" value="" size="20">
<br>Day 2:
<input type="text" name="Sky_Conditions_Day_2" value="" size="20">
<br>
Day 3:
<input type="text" name="Sky_Conditions_Day_3" value="" size="20">
<br>Day 4:
<input type="text" name="Sky_Conditions_Day_4" value="" size="20">
<br>
Day 5:
<input type="text" name="Sky_Conditions_Day_5" value="" size="20">
<br>Day 6:
<input type="text" name="Sky_Conditions_Day_6" value="" size="20">
<br>
Day 7:
<input type="text" name="Sky_Conditions_Day_7" value="" size="20">
<br>
<span class="style5">Watches / Warnings / Advisories:</span>
<input type="text" name="OWS Warnings_/_Watches_/_Advisories" value="" size="100" maxlength="5120">
<br>
<input type="submit" name="submit" value="Submit">
</form>
</select>
</p>
<img src="http://www.owsweather.com/owswaa.jpg" height="425" width="600">
</td></tr>
</table>

I put the code in and tested it but it didn't work, what did I do wrong? :( It's still displaying the output in the old format. :(