Jump to content

Can I use a $variable to determine which DB row data is entered?


Recommended Posts

I have scraped data that needs to be dispersed INTO it's appropriate rows.

The problem is the data pertains to specific States & Cities so i am wondering if I can do the following instead of creating hundreds of DB fields:

 

$urls = array();
$insert = array();

if ($st == "AL")  \\if State is Alabama
{
$urls = array("http://auburn.craigslist.org", "http://bham.craigslist.org"); //This will be popular cities in Alabama
$insert= array("auburn", "bham"); // This will need to be Inserted INTO proper DB  row to match city selected above
// SO if user selects auburn listings I need to extract data from $insert('auburn')??
}
else if ($st == "AK") 
{
$urls= array("http://anchorage.craigslist.org");
}
else if ($st == "AZ") 
{
$urls = array("http://anchorage.craigslist.org");
}

foreach ($urls as $url) {
    $html = file_get_contents("$url/muc/");

    preg_match_all('/<a href="([^"]+)">([^<]+)<\/a><font size="-1">([^"]+)<\/font>/s', $html,$posts,PREG_SET_ORDER);
    //echo "<pre>";print_r($posts);


    foreach ($posts as $post) {

$dbx= mysql_connect("", "", "");   
if (!$dbx)
{
die('Could not connect: ' . mysql_error());
}
//print $post[0]; //HTML
        $post[2] = str_ireplace($url,"",$post[2]); //remove domain
        echo "<a href=\"$url{$post[1]}\">{$post[2]}<font size=\"3\">{$post[3]}<br />";
        print "<BR />\n";


    }
}
mysql_SELECT_db("db_name", $dbx);
mysql_Query("INSERT INTO $insert // Need to choose the State
VALUES ("Value1', 'Value2', 'Value3')"); //Need to choose the city

mysql_close($dbx);
?>

 

Essentially what I am trying to accomplish is> avoid having to manually create a field for each city as there are hundreds.  I would like to have the States set up and then just auto add the data INTO appropriate cities.

 

Thank You.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.