Jump to content
Larry Ullman's Book Forums

Question About Categories


Recommended Posts

I apologise if I have missed something but the SQL statement that came with the download created the tables but did not populate the categories table.  I try the add_page.php and it errors out because there is no data in the table. I have read through the chapter for the last 2 hours and can't find anything that populates the table but does refer to it such as this

 

Each HTML page is in a single category. That association is made by attaching to each page record a foreign key to the proper value in the categories table.

 

Where does the data for the categories table come from?

 

Thanks,

John

Link to comment
Share on other sites

Yes local setup and using IIS and WAMP - here is all that the sql file has - just the tables creation

 

CREATE TABLE `categories` (
    `id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
    `category` VARCHAR(45) NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE INDEX `category_UNIQUE` (`category` ASC)
) ENGINE = InnoDB DEFAULT CHARSET=utf8;

 

I will try another DL and see what I get

Link to comment
Share on other sites

The first edition book file populates the table - not the ones with the second edition sql file - thanks for pointing me in that direction

 

Even with the table filled the Categories is not a dropbox but a textbox - this line is different from the example

 

select name="category[]"

Link to comment
Share on other sites

seems you are right, for the 1st example it only includes code to create the tables not populate them, however saying that it should be easier enough to do yourself via phpmyadmin, just add the categories pertinent to what you are trying to achieve.

 

regards

Link to comment
Share on other sites

the code you should be using is on page 111, in which a select tag is created, and then a query is made to the database to retrieve all categories, the query results are then transfered into the array ( select name="category[]" ) which will look like 0 ->hats 1 ->gloves 2 -> socks etc... the select tag prints these categories, in your case it seems there is no items in the categories array, somewhere in your code add trigger_error(database_connection_variable);  then look for the categories array to check if its empty.

Link to comment
Share on other sites

Here is the code that pertains to the dropdown

 

echo '"><label for="category" class="control-label">Category</label>
<select name="category[]" class="form-control" multiple size="5">';

// Retrieve all the categories and add to the pull-down menu:
$q = "SELECT id, category FROM categories ORDER BY category ASC";
$r = mysqli_query($dbc, $q);
while ($row = mysqli_fetch_array($r, MYSQLI_NUM)) {
    echo "<option value=\"$row[0]\"";
    // Check for stickyness:
    if (isset($_POST['category']) && ($_POST['category'] == $row[0]) ) echo ' selected="selected"';
    echo ">$row[1]</option>\n";
}

echo '</select>';
if (array_key_exists('category', $add_page_errors)) echo '<span class="help-block">' . $add_page_errors['category'] . '</span>';
echo '</div>';

create_form_input('description', 'textarea', 'Description', $add_page_errors);
create_form_input('content', 'textarea', 'Content', $add_page_errors);

Link to comment
Share on other sites

I'm confused. You say "I can't figure out why the Category will not do a drop down - the database connection is fine." However, the image you show and the code you have does have a dropdown. The only difference is that it has multiple="5", and so it shows multiple categories at once, not a single one. 

Link to comment
Share on other sites

I put the code just below the category section and this displayed

 

Array ( [status] => draft [title] => Text [category] => Array ( [0] => 5 ) [description] => Text [content] =>

Text

[submit_button] => Add This Page )

 

Have tried with IE - Firefox and Chrome - all latest versions

Link to comment
Share on other sites

I tried to with my GoDaddy account but they have too many issues.  Here is the source code of the page after it has ran

 

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Add a Site Content Page</title>

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

    <!-- Bootstrap core CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="css/sticky-footer-navbar.css" rel="stylesheet">

  </head>

  <body>

    <!-- Wrap all page content here -->
    <div id="wrap">

      <!-- Fixed navbar -->
      <div class="navbar navbar-fixed-top">
        <div class="container">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".nav-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="index.php">Knowledge is Power</a>
          <div class="nav-collapse collapse">
            <ul class="nav navbar-nav">
<li><a href="index.php">Home</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Contact</a></li>
    <li><a href="register.php">Register</a></li>
    <li class="dropdown">
    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Account <b class="caret"></b></a>
        <ul class="dropdown-menu">
            <li><a href="logout.php">Logout</a></li>
            <li><a href="renew.php">Renew</a></li>
            <li><a href="change_password.php">Change Password</a></li>
            <li><a href="favorites.php">Favorites</a></li>
            <li><a href="recommendations.php">Recommendations</a></li>
        </ul>
    </li><li class="dropdown">
        <a href="#" class="dropdown-toggle" data-toggle="dropdown">Admin <b class="caret"></b></a>
            <ul class="dropdown-menu">
                <li><a href="add_page.php">Add Page</a></li>
                <li><a href="add_pdf.php">Add PDF</a></li>
                <li><a href="#">Something else here</a></li>
            </ul>
        </li>            </ul>
          </div><!--/.nav-collapse -->
        </div><!--/container-->
      </div><!--/navbar-->

      <!-- Begin page content -->
      <div class="container">
    
        <div class="row">
            
            <div class="col-3">
                <h3 class="text-success">Content</h3>
            <div class="list-group">
<a href="category.php?id=3" class="list-group-item" title="Common Attacks">Common Attacks
    </a><a href="category.php?id=5" class="list-group-item" title="Database Security">Database Security
    </a><a href="category.php?id=1" class="list-group-item" title="General Web Security">General Web Security
    </a><a href="category.php?id=4" class="list-group-item" title="JavaScript Security">JavaScript Security
    </a><a href="category.php?id=2" class="list-group-item" title="PHP Security">PHP Security
    </a>              <a href="pdfs.php" class="list-group-item" title="PDFs">PDF Guides
              </a>
            </div><!--/list-group-->

            </div><!--/col-3-->
          
            
          <div class="col-9">
            <!-- CONTENT -->
Array
(
    [status] => draft
    [title] => Test
    [category] => Array
        (
            [0] => 3
        )

    [description] => Test
    [content] => <p>Test</p>
    [submit_button] => Add This Page
)
<h1>Add a Site Content Page</h1>
<form action="add_page.php" method="post" accept-charset="utf-8">

    <fieldset><legend>Fill out the form to add a page of content:</legend>
<div class="form-group">
<label for="status" class="control-label">Status</label>
<select name="status" class="form-control">
<option value="draft">Draft</option>
<option value="live">Live</option>
</select></div>

<div class="form-group"><label for="title" class="control-label">Title</label><input type="text" name="title" id="title" class="form-control" value="Test"></div><div class="form-group has-error"><label for="category" class="control-label">Category</label>
<select name="category[]" class="form-control" multiple size="1"><option value="3">Common Attacks</option>
<option value="5">Database Security</option>
<option value="1">General Web Security</option>
<option value="4">JavaScript Security</option>
<option value="2">PHP Security</option>
</select><span class="help-block">Please select a category!</span></div><div class="form-group"><label for="description" class="control-label">Description</label><textarea name="description" id="description" class="form-control">Test</textarea></div><div class="form-group"><label for="content" class="control-label">Content</label><textarea name="content" id="content" class="form-control"><p>Test</p></textarea></div>        
        <input type="submit" name="submit_button" value="Add This Page" id="submit_button" class="btn btn-default" />
    
    </fieldset>

</form>

<script type="text/javascript" src="js/tinymce/tinymce.min.js"></script>
<script type="text/javascript">
    tinyMCE.init({
        // General options
        selector : "#content",
        width : 800,
        height : 400,
        browser_spellcheck : true,
        
        plugins: "paste,searchreplace,fullscreen,hr,link,anchor,image,charmap,media,autoresize,autosave,contextmenu,wordcount",

        toolbar1: "cut,copy,paste,|,undo,redo,removeformat,|hr,|,link,unlink,anchor,image,|,charmap,media,|,search,replace,|,fullscreen",
        toolbar2:    "bold,italic,underline,strikethrough,|,alignleft,aligncenter,alignright,alignjustify,|,formatselect,|,bullist,numlist,|,outdent,indent,blockquote,",

        // Example content CSS (should be your site CSS)
        content_css : "/ex1/html/css/bootstrap.min.css",

    });
</script>
<!-- /TinyMCE -->

            <!-- END CONTENT -->
            </div><!--/col-9-->
        
        </div><!--/row-->

      </div><!--/container-->

    </div><!--/wrap-->

    <div id="footer">
      <div class="container">
        <p class="text-muted credit"><span class="pull-left"><a href="site_map.php">Site Map</a> | <a href="policies.php">Policies</a></span> <span class="pull-right">© Knowledge is Power - 2013</span></p>
      </div>
    </div>

    <script src="js/bootstrap.min.js"></script>
  </body>
</html>

Link to comment
Share on other sites

Unfortunately I don't know what to tell you here. I took your code, created a new PHP file on my computer, ran it, and it worked fine for me. I'd recommend retyping it (or using that from my site) to see if the problem goes away.

Link to comment
Share on other sites

If it was just me I would say it is some weird fluke.  But another user stated the same issue.  Must be something on the back end somewhere if you took my code and it worked fine in your envrioment.  I am doing this at work - I will try it at home and see what happens.

Link to comment
Share on other sites

I disagree: it's not on the back end. The problem is you cannot select a category which means it has to be an HTML/CSS/browser issue. I didn't have the CSS when I tried it, so you could remove the CSS from the page and see if that fixes it. 

Link to comment
Share on other sites

  • 10 months later...

I had the same problem with the categories not being able to be selected (they would display but you couldn't 'select' them; and therefore no page could be added).  I resolved it by commenting out some lines of code "Bonus" for Chapt. 12 and UN-commenting replacement lines.

 

See my reply to the topic "Sql Commands For Adding Pages Of Content?" for details.

Link to comment
Share on other sites

 Share

×
×
  • Create New...