Forum Moderators: coopster

Message Too Old, No Replies

Taking keywords from a referer problem :(

referer keywords parse_url parse_str

         

Afkamm

4:07 pm on Jun 30, 2003 (gmt 0)

10+ Year Member



This has been puzzling me for a while now. I've been saving referers in a MySQL table (id¦referer¦count) in the hope of writing some sort of stats page to display them on.

The short piece of code I've written is just an experiment to see what I can do with the referer which basically takes each referer, parse_url()'s it to get the query part, parse_str()'s that to get the keywords and then displays the keywords next to the referer.

However, if the next referer doesn't have a query part after I parse_url() it, or has a query part that the keywords can be read from, then it echo's out the keywords from the last referer which it shouldn't :(

<?
$query1 = ("SELECT referer, count FROM $table_prefix ORDER BY count DESC LIMIT 100");
$result = mysql_query($query1);

?>
<table border="0" cellpadding="2" cellspacing="0">
<tr>
<td>Count</td>
<td>Keywords</td>
<td>Referer Url</td>
</tr>
<?
while($resulta = mysql_fetch_array($result)) {

$referer2 = parse_url($resulta[referer]);

if ($referer2[query]!= "") {
parse_str($referer2[query]);

if ($q) { $ref = $q; } else { $ref = ""; }

if ($ref!= "") {
$ref = str_replace("+", " ", $ref);
$ref = str_replace("\"", "", $ref);
$ref = str_replace("\?", "", $ref);
$ref = str_replace(" ", " ", $ref);
$ref = stripslashes($ref);
$ref = rawurldecode($ref);
$ref = trim($ref);
?>
<tr>
<td nowrap><?=$resulta[count];?></td>
<td nowrap><?=$ref;?></td>
<td nowrap><?=$resulta[referer];?></td>
</tr>
<?
} else {
?>
<tr>
<td><?=$resulta[count];?></span></td>
<td>Blank</td>
<td><?=$resulta[referer];?></td>
</tr>
<?
}
} else {
?>
<tr>
<td><?=$resulta[count];?></td>
<td>Blank</td>
<td><?=$resulta[referer];?></td>
</tr>
<?
}

}
?>
</table>

Because this is just experimental, I've only used Google's variable which is '$q', can't for the life of me figure out why or how the keywords are being repeated when the table cell should be blank.

Can anyone shed any light on my problem please :)

Thanks

Marc :O)

jatar_k

5:04 pm on Jun 30, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Have you tried just plain and simple initializing $referer2 under the while so that you clear the old data?

Afkamm

5:47 pm on Jun 30, 2003 (gmt 0)

10+ Year Member



Sorry I don't know what you mean, could you be more specific please?

Marc :o)

jatar_k

6:38 pm on Jun 30, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could actually use unset [ca.php.net] at the end of the while loop to unset the var so it won't have old data in it.

unset($referer2);

vincevincevince

7:17 pm on Jun 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<edit>was a wrong answer</edit>

Afkamm

9:05 pm on Jun 30, 2003 (gmt 0)

10+ Year Member



Thanks, unset() does the trick, have to use it on each of the variables that the search engines use. ie. $q, $query, $p, $keywords, $qkw etc. Might see about sticking them all in an array. :)