Creating links to specific categories

Hello all. I am a complete php newbie and am having a hard time creating a links that go to specific categories in mysql db.

It is an internal website for children’s stories(I am a teacher) and the idea is the children can go onto the category page, click one of the links to each category and then look at all the stories from that category. I can create links find but whenever I click one of the links it just outputs the one story in the category marked as 0.

Please help. Here is my controller script:

<?php

include_once $_SERVER[‘DOCUMENT_ROOT’] . ‘/includes/magicquotes.inc.php’;
include $_SERVER[‘DOCUMENT_ROOT’] . ‘/includes/db.inc.php’;

$pagetitle = ‘flashthenovel.com Genres’;
$sql = “SELECT id, name FROM category”;
$result = mysqli_query($link, $sql);
if (!$result)
{
$error = ‘There was an issue obtaining a list of categories from the database’;
include ‘error.html.php’;
exit();
}

while ($row = mysqli_fetch_array($result))
{
$categories = array(‘id’ => $row[‘id’], ‘name’ => $row[‘name’]);
}

if (isset($_GET[‘genre_story_display’]))

{

include $_SERVER[‘DOCUMENT_ROOT’] . ‘/includes/db.inc.php’;

$sql = "SELECT id, name FROM category";
$result = mysqli_query($link, $sql);
if (!$result)
{
    $error = 'Unable to populate category list.';
    include 'error.html.php';
    exit();
}

while ($row = mysqli_fetch_array($result))
{
    $categories[] = array('id' =&gt; $row['id'], 'name' =&gt;$row['name']);
}

$categoryname = $row['name'];
$pagetitle = $categoryname;
$categoryid = $row['id'];
$sql = "SELECT story.id, storytitle, storytext, name FROM story INNER JOIN author ON authorid = author.id WHERE categoryid = '. $categoryid '";
$result = mysqli_query($link, $sql);

if(!$result)
{
$error = ‘Unable to retrieve the stories from the requested category’;
include ‘error.html.php’;
exit();
}

while ($row = mysqli_fetch_array($result))
{
$stories = array(‘id’ => $row[‘id’] , ‘title’ => $row[‘storytitle’] , ‘text’ => $row[‘storytext’], ‘name’ => $row[‘name’]);

}

include ‘category.html.php’;
exit();

}

include ‘categories.html.php’;
exit();

?>

and here is my html category page:

<?php include_once $_SERVER [‘DOCUMENT_ROOT’] . ‘/includes/helpers.inc.php’; ?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=“http://www.w3.org/1999/xhtml” xml:lang=“en” lang=“en”>
<head>
<meta http-equiv=“content-type”
content=“text/html; charset=utf-8”/>
<title><?php echo htmlout($pagetitle); ?></title>
</head>
<body>
<h1>Genres</h1>

<?php foreach($categories as $category): ?>
<div>
<form action=“?” method=“get”>
<ul id=“nav”>
<li><a href=“?genre_story_display” name=“<?php echo htmlout($category[‘name’]); ?>” value=“<?php htmlout($category[‘name’]); ?>”><?php echo htmlout($category[‘name’]); ?>
<input type=“hidden” id=“id” value=“<?php htmlout($category[‘id’]); ?>”>
<input type=“hidden” id=“name” value=“<?php htmlout($category[‘name’]); ?>”>
</a></li>

</ul>
</div>
</form>
<?php endforeach; ?>
</body>
</html>

thank you any help would be much appreciated.

Welcome to the SP forums. :slight_smile:
Next time when you post code, please put the appropriate code tags around it, so it’ll keep indentation and it will be easier to read.

The first thing that caught my eye is this:


<a href="?genre_story_display" name="<?php echo htmlout($category['name']); ?>" value="<?php htmlout($category['name']); ?>"><?php echo htmlout($category['name']); ?>
<input type="hidden" id="id" value="<?php htmlout($category['id']); ?>">
<input type="hidden" id="name" value="<?php htmlout($category['name']); ?>">
</a>

A link isn’t a form, you can’t give it a value, and you can’t send form fields with it. Instead, all data you want to send to the receiving script must be put in the query string. Something like:

<a href="?genre_story_display=1&name=<?php echo htmlout($category['name']); ?>&id=<?php htmlout($category['id']); ?>">
<?php echo htmlout($category['name']); ?></a>

It might not be the only problem in the code, but it’s a start. Try it and see what happens.

Thank you so much! It is all working fine now