Question : problem carrying variable through a loop

Hi,
The attached code allows the user to input city names separated by comma's in the first box and then keywords separated by commas in the second box:

The initial result generates a list of urls
for example

if you enter
Portland (in the top box)
and
attorney (in the bottom box)

it generates the following list of urls which can be seen if the following portion beginning on line 35 is uncommented:

    //TO VIEW CREATED URLS UNCOMMENT THIS
    //foreach ($urls as $key => $value) {
    //    echo $value . "<br>";
    //}
    //exit();

here is the sample list produced
http://www.google.com/search?q=portland+attorney+&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official"
http://www.google.com/search?q=attorney+portland+&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official"

(note that the script generates a version for "Portland attorney" and "attorney Portland")

If I were to enter
Portland, Spokane (top box)
attorney, lawyer (bottom box)
the following urls would be generated:

http://www.google.com/search?q=Portland+attorney+&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official"
http://www.google.com/search?q=attorney+Portland+&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official"
http://www.google.com/search?q=Portland++lawyer+&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official"
http://www.google.com/search?q=+lawyer+Portland+&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official"
http://www.google.com/search?q=+Spokane+attorney+&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official"
http://www.google.com/search?q=attorney++Spokane+&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official"
http://www.google.com/search?q=+Spokane++lawyer+&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official"
http://www.google.com/search?q=+lawyer++Spokane+&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official"

(note that the script just takes each combination and creates a url)

Now the second part of the script is supposed to scrape specific pieces of data from the resulting url.  So I'll put the comments back to the section starting at line 35 that I mentioned above...

and if i enter
Portland (in the top box)
attorney (in the bottom box)

the script produces the correct output (actually in table format)

Position      Domain      Phone
1      www.portlandattorneyatlaw.org      (503) 308-1291
2      www.stahancyk.com      (503) 222-9115
3      www.gevurtzmenashe.com      (503) 227-1515
4      www.yourinjurylawyers.com      (503) 224-1106
5      www.goldbergjones-wa.com      (503) 731-8888
6      www.baxterlaw.com      (503) 297-9031
7      www.portlandlegalservices.com      (503) 222-7401
1      www.portlandattorneyatlaw.org      (503) 308-1291
2      www.stahancyk.com      (503) 222-9115
3      www.gevurtzmenashe.com      (503) 227-1515
4      www.yourinjurylawyers.com      (503) 224-1106
5      www.goldbergjones-wa.com      (503) 731-8888
6      www.baxterlaw.com      (503) 297-9031
7      www.portlandlegalservices.com      (503) 222-7401

It has taken each url (remember there were two produced for the version when there is only one input in each box) and scraped data from the resulting page.

If I entered,

Portland, Spokane (top box)
attorney, lawyer (bottom box)

The result is that is as follows:

Position      Domain      Phone
1      www.laymanlawfirm.com      (509) 455-8883
2      www.maxeylaw.com      (509) 458-0183
3      www.lukins.com      (509) 455-9555
4      thegaiahouse.com      (509) 327-2560
5      www.denniscronin.com      (509) 328-5600
6      ewinganderson.com      (509) 838-4261
7      www.wkdtlaw.com      (509) 624-5265
1      www.laymanlawfirm.com      (509) 455-8883
2      www.maxeylaw.com      (509) 458-0183
3      www.lukins.com      (509) 455-9555
4      thegaiahouse.com      (509) 327-2560
5      www.denniscronin.com      (509) 328-5600
6      ewinganderson.com      (509) 838-4261
7      www.wkdtlaw.com      (509) 624-5265
1      www.laymanlawfirm.com      (509) 455-8883
2      www.maxeylaw.com      (509) 458-0183
3      www.lukins.com      (509) 455-9555
4      thegaiahouse.com      (509) 327-2560
5      www.denniscronin.com      (509) 328-5600
6      ewinganderson.com      (509) 838-4261
7      www.wkdtlaw.com      (509) 624-5265
1      www.laymanlawfirm.com      (509) 455-8883
2      www.maxeylaw.com      (509) 458-0183
3      www.lukins.com      (509) 455-9555
4      thegaiahouse.com      (509) 327-2560
5      www.denniscronin.com      (509) 328-5600
6      ewinganderson.com      (509) 838-4261
7      www.wkdtlaw.com      (509) 624-5265
1      www.laymanlawfirm.com      (509) 455-8883
2      www.maxeylaw.com      (509) 458-0183
3      www.lukins.com      (509) 455-9555
4      thegaiahouse.com      (509) 327-2560
5      www.denniscronin.com      (509) 328-5600
6      ewinganderson.com      (509) 838-4261
7      www.wkdtlaw.com      (509) 624-5265
1      www.laymanlawfirm.com      (509) 455-8883
2      www.maxeylaw.com      (509) 458-0183
3      www.lukins.com      (509) 455-9555
4      thegaiahouse.com      (509) 327-2560
5      www.denniscronin.com      (509) 328-5600
6      ewinganderson.com      (509) 838-4261
7      www.wkdtlaw.com      (509) 624-5265
1      www.laymanlawfirm.com      (509) 455-8883
2      www.maxeylaw.com      (509) 458-0183
3      www.lukins.com      (509) 455-9555
4      thegaiahouse.com      (509) 327-2560
5      www.denniscronin.com      (509) 328-5600
6      ewinganderson.com      (509) 838-4261
7      www.wkdtlaw.com      (509) 624-5265
1      www.laymanlawfirm.com      (509) 455-8883
2      www.maxeylaw.com      (509) 458-0183
3      www.lukins.com      (509) 455-9555
4      thegaiahouse.com      (509) 327-2560
5      www.denniscronin.com      (509) 328-5600
6      ewinganderson.com      (509) 838-4261
7      www.wkdtlaw.com      (509) 624-5265

This result is only partially correct. The results for rows 1-14 (1 through 7 and again 1-7) are correct for Spokane and lawyer but then it just repeats the same thing for the following (42) rows.

What should have happened is:

1-7 should have been results for "Portland attorney"
8-14 should have been results for "attorney Portland"
15-21 should have been results for "Portland lawyer"
22-28 should have been results for "lawyer Portland"
29-35  should have been results for "Spokane attorney"
36-42 should have been results for "attorney Spokane"
43-49 should have been results for "Spokane lawyer"
50-56 should have been results for "lawyer Spokane"

Can someone please take a look at the script beginning on line 48 and try to help me understand what I need to change to allow proper output of the results? I should be able to input several comma separated values in the top box and the bottom box to produce a long list of records.

Thanks very much.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
<?php
set_time_limit(100);
//error_reporting(E_ALL);
extract($_REQUEST);
$keywords = "";
$keywords1 = "w";

if (isset($submit)) {
    $list1 = explode(",", $input1);
    $list2 = explode(",", $input2);
    foreach ($list1 as $word1) {
        foreach ($list2 as $word2) {
            $keywords = $keywords . "\n" . $prefix .
                    trim($word1) . " " .
                    trim($word2) . " ";

            $keywords1 = $keywords1 . "\n" . $prefix .
                    trim($word2) . " " .
                    trim($word1) . " ";
            $keyws[] = $word1 . ' ' . $word2;
            $keyws[] = $word2 . ' ' . $word1;
        }
    }
    $keywords .= $keywords1;
    $urlelements = array();
    foreach ($keyws as $k => $v) {
        $urlelements = explode(' ', $v);
        $url = 'http://www.google.com/search?q=';
        foreach ($urlelements as $k2 => $v2) {
            $url .= $v2 . '+';
        }
        $url .= '&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official"';
        $urls[] = $url;
    }
    //TO VIEW CREATED URLS UNCOMMENT THIS
    //foreach ($urls as $key => $value) {
    //    echo $value . "<br>";
    //}
//exit();
    echo "<pre>\n"; // IMPROVE READABILITY
    //

    echo "<table border=\"1\" align=\"center\">";
    echo "<tr><th>Position</th>";

    echo "<th>Domain</th>";
    echo "<th>Phone</th></tr>";
    foreach ($urls as $u){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
        $file=curl_exec ($ch) or die(curl_error());
        curl_close ($ch);
        // CREATE AN ARRAY FROM THE HTML-----------------------------1. Domain
        $arr = explode('</a></h4><cite>', $file);
        // DISCARD THE UNWANTED STUFF AT THE TOP OF THE HTML
        unset($arr[0]);
        // TIDY UP EACH ELEMENT OF THE ARRAY
        foreach ($arr as $ptr => $string)
        {
        // LOCATE THE END OF USEFUL DATA
            $poz = strpos($string, '</cite>&nbsp;- <nobr>');
        // END OF DATA NOT FOUND - SKIP THIS ELEMENT
            if ($poz === FALSE)
            {
                unset($arr[$ptr]);
                continue;
            }
        // REMOVE USELESS TRAILING DATA AND REPAIR HTML (XML) ENTITIES
            $arr[$ptr] = substr($string, 0, $poz);
            $arr[$ptr] = str_replace('&amp;', '&', $arr[$ptr]);
        }

        // CREATE AN ARRAY FROM THE HTML-----------------------------2. Phone Number
        $arr2 = explode('</cite>&nbsp;- <nobr>', $file);
        // DISCARD THE UNWANTED STUFF AT THE TOP OF THE HTML
        unset($arr2[0]);
        // TIDY UP EACH ELEMENT OF THE ARRAY
        foreach ($arr2 as $ptr => $string2)
        {
        // LOCATE THE END OF USEFUL DATA
            $poz = strpos($string2, '</nobr>&nbsp;- <a class=fl  href');
        // END OF DATA NOT FOUND - SKIP THIS ELEMENT
            if ($poz === FALSE)
            {
                unset($arr2[$ptr]);
                continue;
            }
        // REMOVE USELESS TRAILING DATA AND REPAIR HTML (XML) ENTITIES
            $arr2[$ptr] = substr($string2, 0, $poz);
            $arr2[$ptr] = str_replace('&amp;', '&', $arr2[$ptr]);
        }




        // ACTIVATE THIS TO SEE THE ARRAY
        // var_dump($arr);
        //CJ set up the table

        //Put results in a table

        // $newarr = array_combine($keyws, $arr);

        for($i=1; $i<=count($arr); $i++){
                echo "<tr><td>";
                echo $i;
                echo "</td><td>";
                echo $arr[$i];
                echo "</td><td>";
                echo $arr2[$i];
                echo "</td></tr>";
        }
    }
    echo "</table>";

}else{
echo "<form id='' action='" . $_SERVER['PHP_SELF'] ."' method='post'>";
echo "<table>";
echo "<tr>";
echo "<td><input type='text' id='' name='input1' value='' /></td>";
echo "</tr>";
echo "<tr>";
echo "<td><input type='text' id='' name='input2' value='' /></td>";
echo "</tr>";
echo "<tr>";
echo "<td><input type='submit' id='' name='submit' value='submit' /></td>";
echo "</tr>";
echo "</table>";
echo "</form>";

}

?>

Answer : problem carrying variable through a loop

What version of Access are you using?

If you are using 2003 or earlier, you can Replicate your database and synchronize it when you get back to work.

Just make sure that you read up on Replication, it can cause some problems.

MAKE SURE YOU BACK UP YOUR DB FIRST!!!!!!

Newer versions of Access do not support Replication. Unless your database is in .mdb format.

http://office.microsoft.com/en-us/access-help/methods-and-properties-used-to-programmatically-replicate-a-database-mdb-HP003069832.aspx?queryid=c256aa3f077f46f19ee8a9162905ec05&respos=5&CTT=1&av=ZAC110
http://it.toolbox.com/blogs/kawaii/replicating-microsoft-access-databases-28011


TLH
Random Solutions  
 
programming4us programming4us