Hi, I have a joke database, and when I click "add a joke" from the main page, I have a form show up where I can enter text, author, and category.
For some reason I am getting duplicate listings for the authors and categories to choose from. Here is the code:
CONTROLLER
FORM FOR ADDING OR EDITING JOKE(S)PHP Code:<?php
// ===== ADD NEW JOKE =====
include_once $_SERVER['DOCUMENT_ROOT'] . '/byo/includes/magicquotes.inc.php';
if (isset($_GET['add'])) {
$pagetitle = 'Add new joke';
$action = 'addform';
$text = '';
$authorid = '';
$id = '';
$button = 'Add joke';
include $_SERVER['DOCUMENT_ROOT'] . '/byo/includes/db.inc.php';
// build the list of authors (using $authors[] array variable)
$sql = 'SELECT id, name FROM author';
$result = mysqli_query($link, $sql);
if (!$result) {
$error = 'Error fetching list of authors.';
include $_SERVER['DOCUMENT_ROOT'] . '/byo/includes/error.html.php';
exit();
}
while ($row = mysqli_fetch_array($result)) {
$authors[] = array('id' => $row['id'], 'name' => $row['name']);
}
// build the list of categories (using $categories[] array variable)
$sql = 'SELECT id, name FROM category';
$result = mysqli_query($link, $sql);
if (!$result) {
$error = 'Error fetching list of categories.';
include $_SERVER['DOCUMENT_ROOT'] . '/byo/includes/error.html.php';
exit();
}
while ($row = mysqli_fetch_array($result)) {
$categories[] = array('id' => $row['id'], 'name' => $row['name'], 'selected' => FALSE);
}
include 'form.html.php';
exit();
}
?>
I'm attaching a screenshot of the output.HTML Code:<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/byo/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 htmlout($pagetitle); ?></title> </head> <body> <h1><?php htmlout($pagetitle); ?></h1> <form action="?<?php htmlout($action); ?>" method="post"> <div> <label for="text">Type your joke here:</label><br /> <textarea id="text" name="text" rows="3" cols="40"> <?php htmlout($text); ?> </textarea> </div> <div> <label for="author">Author:</label> <select name="author" id="author"> <option value="">Select one</option> <?php foreach($authors as $author): ?> <option value="<?php htmlout($author['id']); ?>" <?php if($author['id'] == $authorid) echo 'selected="selected"'; ?>> <?php htmlout($author['name']); ?> </option> <?php endforeach; ?> </select> </div> <fieldset> <legend>Categories:</legend> <?php foreach($categories as $category): ?> <input type="checkbox" name="categories[]" id="category<?php htmlout($category['id']); ?>" value="<?php htmlout($category['id']); ?>"<?php if ($category['selected']) { echo ' checked="checked"'; } ?> /> <label for="category<?php htmlout($category['id']); ?>"><?php htmlout($category['name']); ?></label> <?php endforeach; ?> </fieldset> <div> <input type="hidden" name="id" value="<?php htmlout($id); ?>" /> <input type="submit" value="<?php htmlout($button); ?>" /> </div> </form> </body> </html>
Thanks for any help...



Reply With Quote
Bookmarks