php question

Status
Not open for further replies.

powell1

RTFM
Jul 14, 2007
298
8
0
Ok, I'm still a phptard. I just started teaching myself about a week ago, and I'm running into some issues. I read through the madlib site idea on bluehat, and I'm trying to get something like that going. I finally got it so that I can connect to the db, pull info and display it. (Small step I realize)

Anyway, I'm trying to put together a site that takes a db of zip codes, cities, states and counties and puts them into some info about local jobs. Here is the code that I have so far (which is not much):


PHP:
<?php
function &connectToDb($host, $dbUser, $dbPass, $dbName)
{
 // Make connection to MySQL server
 if (!$dbConn = @mysql_connect($host, $dbUser, $dbPass)) {
   return false;
 }

 // Select the database
 if (!@mysql_select_db($dbName)) {
   return false;
 }

 return $dbConn;
}

$host   = 'localhost'; // Hostname of MySQL server
$dbUser = 'root';    // Username for MySQL
$dbPass = '';    // Password for user
$dbName = 'zips'; // Database name

$dbConn = &connectToDb($host, $dbUser, $dbPass, $dbName);



$sql = "SELECT * FROM zips WHERE ID=1"; //only selects the one set of city data


$results = mysql_query($sql); 

while ($row = mysql_fetch_array($results)) {
 $city = $row['city'];
 $state =  $row['state'];
 $zip = $row['ZIP'];
}

 
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Jobs in <?php echo "$city, $state, $zip"; ?></title>
</head>

<body>
Hello, it looks like you are looking for a job in <?php echo "$city, $state, $zip"; ?>.  I know how frustrating it can be to find a job in <?php echo $city ?> because I used to live there.  Here are some suggestions for employment in <?php echo "$city, $state"; ?> and the surrounding area.

</body>
</html>
So like I said, I have a begining idea of how to get the info. My biggest question at this point is how do I go about dynamically generating these pages? Coding them by hand like that isn't realistic for a huge database. I just don't really know where to start looking to have those pages generated. How do I automate the creation of the different pages? A bonus would be to have the pages named {specific job}-in-{city}-{state}-{zip}.php

If someone could point me in the right direction I'd appreciate it!
 


to get the file names like that use a .htaccess file and mod_rewrite to redirect all requests to one script like index.php

Code:
RewriteEngine on
RewriteRule ^(.*?)-in-(.*?)-(.*?)-(.*?).php?$ index.php?job=$1&city=$2&state=$3&zip=$4 [L]

Warning, my mod_rewrite skills aren't great so you may need to play around with that.

Then in your code use $_REQUEST['job'], $_REQUEST['city'], $_REQUEST['state'] and $_REQUEST['zip'] where ever you need those values. Be sure to validate the input first though, like ensure zip is a 5 digit number, city/state contain only certain characters same with job. Or if you're less risk adverse just use mysql_real_escape_string() on them before using them.
 
Thanks for your help casidnet. I don't know much (beyond what I just read) about mod_rewrite, but I think I get what you are saying.
So that rule will take "blacksmithing-in-bozeman-mt-59175.php" and redirect it to "index.php?job=$1&city=$2&state=$3&zip=$4" where those
variables represent the requested city. So doesn't that page only exist while the user is on it? Since it is all just the inex.php page?
If a search engine spidered the site, they would only see the index page right? Also, If that's the case, how would I get those variables
into the index.php page other than a direct type in?

I'm sure that I'm missing something. Thanks again.
 
Yes, you want your page to be dynamically generated based on the request. So while there is only actually 1 page it looks like a lot more. If a search engine spidered the site they would see the same pages as the users since it's your server telling the user they exist and doing the work on the back end.

For the last question read the last part I wrote. Those variables will contain the values you're looking for.
 
Status
Not open for further replies.