07-01-2008, 12:01 AM
|
#17 (permalink)
|
|
is cute and cuddly
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
|
Okay, first of all, and please don't take offense, but that's some messy code. Even starting out you should start to learn and employ practices that will greatly reduce and aid in your future debugging issues. You can learn some of these practices by paying attention to the coding practices of other members here (something that has greatly influenced and helped mine), or you can develop your own style ground up, but the more readable your code is, the better you are going to code, period.
Edit: I just noticed sketch told you this too, I'm not trying to rag on you for it, and trust me mine gets messy when I get going too. It's just good practice to clean up a little, especially when you start to have problems you can't figure out. Sometimes just cleaning things up will help you discover the problem.
Here's what I think you're trying to do, and with as little modification as possible (I left your tables in, as opposed to doing up some CSS for you, for example), how I might do it. See if it works for you.
PHP Code:
<?php
// Turn on error reporting and start the session error_reporting(E_ALL); session_start();
include('db_connect.php');
// Look for our logged status, if not found redirect the user if($_SESSION['logged'] != 1) { header("Location: users.php"); exit(); }
define('DEBUG', 1); ?> <!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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css"> h1 { font-size: 2em; }
h2 { margin-bottom: 1em; font-size: 1.6em; color: #FF9900; font-weight: bold; }
table, td { border: none; border-collapse: collapse; } </style>
<title>Codefreek's Page</title> </head>
<body>
<h1>This is the main page!</h1>
<?php
if (isset($_GET['cat'])) {
$category = (int) $_GET['cat'];
$q = sprintf("SELECT des, rest FROM `news` WHERE valid = 1 AND cat_id = %d", $category); $result = mysql_query($q);
if( ! $result) {
if (defined('DEBUG')) { echo $q; // so we can verify the query was properly formatted. Not really necessary here, but useful when you // you're using dyanmically created query strings (such as those using $_GET variables) echo mysql_error(); // Useful for debugging, but for a live site this will give useful information to a potential hacker, just so you know. } else { echo "Category ID not found."; }
} else { while($row = mysql_fetch_assoc($result)) { echo '<h2>'.$row['des'].'</h2>'; echo '<table><tr><th>NEWS:</th><td>'.$row['rest'].'</td></tr></table>'; }
}
// Add some whitespace echo "<br /><br />";
}
// You had a second query on the 'news' table here, but it wasn't doing anything, so I just removed it? // In addition you were checking $cat_result in your if statement, when $cat_result had yet to be assigned.
$q = "SELECT id, name FROM `cat`"; $result = mysql_query($q);
if( ! $result) { echo mysql_error(); } else {
while($row = mysql_fetch_assoc($result)) {
echo '<a href="index.php?cat='.$row['id'].'">'.$row['name'].'</a><br />';
}
}
echo '<br /><a href="logout.php">Logout</a>';
?>
</body> </html>
-m
|
|
|
|