Jump to content

Linking to DB Categories


webweever

Recommended Posts

I've read over about 100 posts on this topic in the forum but can't find anything to get me pointed in the right direction.

 

I'm looking to create links to my categories which I have with the code below. The part I can't get is when I click on the link I want all the records in that particular category echoed below my links.

 

<?PHP
include("../includes/db.php");
?>
<?php

$sql = "SELECT * FROM category";
$result = mysql_query($sql);
$marker_cats = "";

while ($i = mysql_fetch_array($result)) {
	$catname = $i['cat_name'];
	$marker_cats .= "<a href=\"test.php?marker_cat=$catname\">$catname</a> <b>|</b> ";
}

echo $marker_cats
?>
<?php
$fetch = mysql_query("SELECT * FROM markers WHERE marker_cat LIKE '%$catname%'")
or
die(mysql_error());

$num=mysql_numrows($fetch);
?>
<?php
$i=0;
	while ($i < $num) {
		$marker_id=mysql_result($fetch,$i,"marker_id");
		$name=mysql_result($fetch,$i,"name");
		$street=mysql_result($fetch,$i,"street");
		$city=mysql_result($fetch,$i,"city");
		$state=mysql_result($fetch,$i,"state");
		$zip=mysql_result($fetch,$i,"zip");
		$url=mysql_result($fetch,$i,"url");
		$lat=mysql_result($fetch,$i,"lat");
		$lng=mysql_result($fetch,$i,"lng");
		$marker_cat=mysql_result($fetch,$i,"marker_cat");
?>
<?php
if (isset($_GET['marker_cat'])) {
	echo '<br>'.$marker_cat. '<br>'.$name;
}
?>
<?php
$i++;
}
mysql_close($dbh);
?>

 

Any push in the right direction would be greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/160620-linking-to-db-categories/
Share on other sites

I changed that and it now appears no matter what category I click on I get all records that are in a category. I get the same result no matter what link I click on so it's still not catching the marker_cat variable from the links.

 

On tesp.php I get nothing which is correct.

On test.php?marker_cat=CAT1 I get the result as test.php?marker_cat=CAT2. 

Why are you constantly closing and opening PHP tags?

 

<?php
require '../includes/db.php';

$sql = 'SELECT cat_name FROM category';
$result = mysql_query($sql) or trigger_error('Select SQL failed.', E_USER_ERROR);
while ($row = mysql_fetch_assoc($result)) {
     echo '<a href="test.php?marker_cat=' . $row['cat_name'] . '">' . $row['cat_name'] . '</a> <b>|</b> ';
}

if (isset($_GET['marker_cat'])) {
     $marker_cat = mysql_real_escape_string($_GET['marker_cat']);
     $sql = 'SELECT * FROM category WHERE marker_cat = "' . $marker_cat . '"';
     $result = mysql_query($sql) or trigger_error('Marker Category Select SQL failed.', E_USER_ERROR);
     while ($row = mysql_fetch_assoc($result)) {
           // echo your data out here
     }
}

Archived

This topic is now archived and is closed to further replies.

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