PDA

View Full Version : echo fields from database


gavimobile
09-24-2008, 01:49 PM
i have here a zipcode_details class which i am trying to modify.

as of now i type in a zip code and it SELECT city, state_name, zip_code FROM zip_code

the if statement gives the name of the field followed by a : than the echoed result.

i dont like this.

what i would like to echo $row['city'] . $row['state_name'] . $row['zip_code']
so i can echo "Title: "; before eacho $row
instead of it echoing the field name as a title...

this is probably easier than i thought, im just getting overwealmed with classes and functions. As for these are new to me.

thanks guys


<?php
require_once('../includes/config.inc.php');

// begeinging of class
class zipcode_class {
var $last_error = ""; // last error message set by this class
var $last_time = 0; // last function execution time (debug info)
var $units = _UNIT_MILES; // miles or kilometers
var $decimals = 2; // decimal places for returned distance
// end of class

// begening of function
function get_zip_details($zip) {

// This function pulls the details from the database for a
// given zip code.
$results = mysql_query("SELECT city, state_name, zip_code FROM zip_code WHERE zip_code='$zip'");
if (!$results) {
return false;
} else {
$row = mysql_fetch_array($results, MYSQL_ASSOC);
mysql_free_result($results);
return $row;
}
}
}
// end of function

// variables
$zip_details = $_POST['zip_details'];
$z = new zipcode_class;
$details = $z->get_zip_details($zip_details);
if ($details === false) echo 'Please enter A Valid Zip Code';
else {
foreach ($details as $key => $value) {
echo "$key:&nbsp;$value<br />";
}
}
?>


<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>
<label>Zip Code:</label>
<input type="text" name="zip_details" />
<br /><br />
<input type="submit" value="Submit" />
</p>
</form>

lux
09-24-2008, 04:02 PM
if using classes is a little overwhelming at the moment try to write your code without the class and functions, then come back to this class when you feel ready?

gavimobile
09-24-2008, 04:49 PM
if using classes is a little overwhelming at the moment try to write your code without the class and functions, then come back to this class when you feel ready?
is this a realy a difficult question?

or maybe you ment i make my code, than come back for help to learn how to apply the class to it?

lux
09-24-2008, 05:08 PM
not a difficult question,

just that if you were to write your own code to solve the problem you may better understand how to tackle this problem.

give it a try and if you can't find a solution let me know.

davidj
09-24-2008, 06:40 PM
<?php
require_once('../includes/config.inc.php');

// begeinging of class
class zipcode_class {
var $last_error = ""; // last error message set by this class
var $last_time = 0; // last function execution time (debug info)
var $units = _UNIT_MILES; // miles or kilometers
var $decimals = 2; // decimal places for returned distance
// end of class

// begening of function
function get_zip_details($zip) {

// This function pulls the details from the database for a
// given zip code.
$results = mysql_query("SELECT city, state_name, zip_code FROM zip_code WHERE zip_code='$zip'");
if (!$results) {
return false;
} else {
$row = mysql_fetch_array($results, MYSQL_ASSOC);
mysql_free_result($results);
return $row;
}
}
}
// end of function

// variables
$zip_details = $_POST['zip_details'];
$z = new zipcode_class;
$details = $z->get_zip_details($zip_details);
if ($details === false) echo 'Please enter A Valid Zip Code';
else {

$data = "";

foreach ($details as $key => $value) {

$data .= $value;

}


echo $data;

}
?>

gavimobile
09-24-2008, 07:48 PM
lux... haha i knew it was one of thoese things that i would be kicking myself after.
since i already have the database, this is all i did within 20 lines of code!
hand coded of course! i didnt even need to use the class!


<?php
require_once('../includes/config.inc.php');

$zip_details = $_POST['zip_details'];
$results = mysql_query("SELECT * FROM zip_code WHERE zip_code='$zip_details'");
$row = mysql_fetch_array($results);

echo "City: " . $row['city'] . "<br />" . "State: " . $row['state_name'] . "<br />" . "Zip Code: " . $row['zip_code'];
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>
<label>Zip Code:</label>
<input type="text" name="zip_details" />
<br /><br />
<input type="submit" value="Submit" />
</p>
</form>
dj i didnt refresh the page till i click on reply/quote in this thread. i just noticed you posted something, thanks for the response ill take a look anyways, lux thank you 2. HAHA the amount of hours i wasted today thinking on how i can do this!!!!

edit: im gona try to make an if statement now to display an error if no such zipcode!

gavimobile
09-24-2008, 08:46 PM
finalized!!!

ok you pros... this is working fine, what would you recommend though to improve my script!


<?php
require_once('../includes/config.inc.php');

$zip_details = $_POST['zip_details'];
$results = mysql_query("SELECT * FROM zip_code WHERE zip_code='$zip_details'");
$row = mysql_fetch_array($results);

if ($zip_details == FALSE) {

?>

<div id="header"><h3>Please enter your zipcode.</h3></div>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>
<label>Zip Code:</label>
<input type="text" name="zip_details" />
<input type="submit" value="Find" />
</p>
</form>

<?php

}elseif ($zip_details == $row['zip_code']){

?>

<div id="header"><h3>Results for zipcode <?php echo $row['zip_code']; ?></h3></div>
<ul>
<li><strong>City:</strong> <?php echo $row['city']; ?></li>
<li><strong>State:</strong> <?php echo $row['state_name']; ?></li>
<li><strong>Zip Code:</strong> <?php echo $row['zip_code']; ?></li>
</ul>

<div id="header"><h3>Please enter your zipcode.</h3></div>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>
<label>Zip Code:</label>
<input type="text" name="zip_details" />
<input type="submit" value="Find" />
</p>
</form>

<?php
} else {
?>


<div id="header"><h3>Please enter your zipcode.</h3></div>

<div id="error">
<p>The zipcode you entered is not valid, please try again.</p>
</div>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>
<label>Zip Code:</label>
<input type="text" name="zip_details" />
<input type="submit" value="Find" />
</p>
</form>
<?php
}
?>

davidj
09-24-2008, 10:25 PM
indent and comment everything