Hey i'm writing my own first full website with PHP and MySQL and i've come to a problem. I know i need to optimalize it with some fewer echo's and more elses, but this is what i have now and apart from the problem, it works.

On the site at the blog section I have a "views-system", just a file included on every blogpost. And now it inserts a double MySQL row with a different bid "blog id", the one thats rewrited with mod_rewrite and even puts a path of an img thats is not even near the line of code. The bottom one is the correct one:

MySQL rows
ID 3/Donec_sit_amet_ante_at_mauris_porta_commodo/img/social ip date browser
ID 3 ip date browser

inc/views.php:

if(isset($getid)) { $search = $getid; }
	if(!isset($getid)) { $search = $row_b['id']; }

	$browserINF = $_SERVER['HTTP_USER_AGENT'];
	if    (strstr($browserINF,'MSIE'))    {$browser = "Internet Explorer";}
	elseif(strstr($browserINF,'Opera'))   {$browser = "Opera";}
	elseif(strstr($browserINF,'Firefox')) {$browser = "Firefox";}
	elseif(strstr($browserINF,'Chrome'))  {$browser = "Chrome";}
	else                                  {$browser = "Other";}

	if (isset($getid)) {
		$count = mysql_query("SELECT * FROM $db_bvis WHERE ip = '$_SERVER[REMOTE_ADDR]' AND bid = '$getid'");
		$num = mysql_num_rows($count);
		if($num == "0") {
			$date = date('F j, Y, g:i a');
			$sql = ("INSERT INTO $db_bvis SET id = '', bid = '$getid', ip = '$_SERVER[REMOTE_ADDR]', date = '$date', browser = '$browser'");
			$res = mysql_query($sql); }
	}
	$sql = ("SELECT id FROM $db_bvis WHERE bid = '$search'");
	$res = mysql_query($sql);
	$aantal = mysql_num_rows($res);
	if ($aantal > 1 || $aantal == 0) { echo "$aantal views"; }
	else { echo "1 view"; }

This is part of the blogpost (if isset getid view one, if not view all blogposts):

echo "		<div id=\"blogpost\"><ul> \n";
if(!isset($getid)) {
$countblog = mysql_query("SELECT * FROM $db_blog"); $num_blog = mysql_num_rows($countblog);
	echo "<li><div class=\"box\">My Blog<br />Showing ".$num_blog." articles</div></li>";
}
if(isset($getid)) { $result_b = mysql_query("SELECT * FROM $db_blog WHERE id = '$getid'"); }
else { $result_b = mysql_query("SELECT * FROM $db_blog ORDER BY id DESC"); }
while($row_b = mysql_fetch_array($result_b)){
$row_id = $row_b['id'];
$countcom = mysql_query("SELECT * FROM $db_bcom WHERE blog_id = '$row_id'");
$num_com = mysql_num_rows($countcom);
$blogurl = str_replace(" ", "_", $row_b['title'])."/";
$bloglink = $domain."articles/".$row_b['id']."/".$blogurl;
echo "			<li> \n";
echo "				<h3><a href=\"".$bloglink."\">".$row_b['title']."</a></h3>";
if(isset($getid)) { echo "\n				<font>".$row_b['date']." | <a href=\"".$bloglink."\">".$num_com." comment"; if ($num_com != "1") { echo "s"; } echo "</a></font>\n"; }
echo "\n				<img src=\"".$domain."img/blogimg/".$row_b['blogimg'].".png\" alt=\"".$row_s['intro']."\" /> \n";
$text = $row_b['text'];
if(!isset($getid)) { echo "				<p>".bbcode(substr_replace($text,"...</p>\n",450));""; }
if(isset($getid)) { echo "				<p>".bbcode($row_b['text'])."</p>\n"; }
if(!isset($getid)) { echo "				<a class=\"button\" href=\"".$bloglink."\"><span>Read full article</span></a> \n"; }
echo "				<div class=\"tags\">";
echo "\n					".$row_b['date'];
echo "\n					| "; $tags = explode(" ",$row_b["tags"]); foreach($tags as $tag){ echo "<a class=\"tag\" href=\"".$domain."tags/".$tag."/\">".$tag."</a> "; } 
echo " | "; 
include("inc/views.php");
if(!isset($getid)) { echo " | <a href=\"".$bloglink."\">".$num_com." comment"; if ($num_com != "1") { echo "s"; } echo "</a>\n"; }
echo "				</div>\n";
echo "			</li> \n"; } 
echo "		</ul></div> \n";

views.php is included on line 26.
Anybody has a clue?

I found the problem, the queries had to get the id from the row not from the URL.
And i made a few functions:

if (isset($getid)) {
		$search = $getid;
		$num = countRows("SELECT * FROM $db_bvis WHERE ip = '$_SERVER[REMOTE_ADDR]' AND bid = '$row_b[id]'");
		if($num == 0) {
			$browser = getBrowser();
			$date = date('F j, Y, g:i a');
			$sql = ("INSERT INTO $db_bvis SET id = '', bid = '$row_b[id]', ip = '$_SERVER[REMOTE_ADDR]', date = '$date', browser = '$browser'");
			$res = mysql_query($sql);
		}
	}
	else { $search = $row_b['id']; }
	$aantal = countRows("SELECT id FROM $db_bvis WHERE bid = '$search'");
	if ($aantal > 1 || $aantal == 0) { echo "$aantal views"; }
	else { echo "1 view"; }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.