Page 1 of 1

conditionals...

Posted: Thu Feb 17, 2005 11:50 pm
by pleigh
i have this code

Code: Select all

<?
	$id = $_SESSION&#1111;'userID'];
	$query = "SELECT status FROM users WHERE userID='$id'";
	$result = @mysql_query($query);
	if ($result)
	&#123;
		//verify administrator
		if ($row&#1111;0] == 'administrator')
		&#123;
			$query = "SELECT date, title, postID FROM posts ORDER BY date DESC";
			$result = @mysql_query($query);
		
			if ($result)
			&#123;
				echo "<tr><td width=80%><table><tr><td>TOPIC</td></tr></table></td><td width=20%><table><tr><td>DATE</td></tr></table></td></tr>";
		
				while($row = mysql_fetch_array($result, MYSQL_NUM))
				&#123;
					//$post = $row&#1111;2];
					echo "<tr><td width=80%><table><tr><td><a href="reportingview.php?pid=&#123;$row&#1111;2]&#125;" class="under">$row&#1111;1]</td></tr></table></td><td width=20%><table><tr><td>$row&#1111;0]</td></tr></table></td></tr>";
				&#125;
				mysql_free_result($result);			
			&#125;
			else
			&#123;
				echo 'system error!<br>'.mysql_error();
			&#125;
			mysql_close();
		&#125;
		//if not administrator
		else
		&#123;
			$query = "SELECT date, title, postID FROM posts WHERE userID='$id' ORDER BY date DESC";
			$result = @mysql_query($query);
		
			if ($result)
			&#123;
				echo "<tr><td width=80%><table><tr><td>TOPIC</td></tr></table></td><td width=20%><table><tr><td>DATE</td></tr></table></td></tr>";
		
				while($row = mysql_fetch_array($result, MYSQL_NUM))
				&#123;
					//$post = $row&#1111;2];
					echo "<tr><td width=80%><table><tr><td><a href="reportingview.php?pid=&#123;$row&#1111;2]&#125;" class="under">$row&#1111;1]</td></tr></table></td><td width=20%><table><tr><td>$row&#1111;0]</td></tr></table></td></tr>";
				&#125;
				mysql_free_result($result);			
			&#125;
			else
			&#123;
				echo 'system error!<br>'.mysql_error();
			&#125;
			mysql_close();
		&#125;		
	&#125;
				
	?>
my idea is when the user is an administrator, he can view all the posts in the database, otherwise, the user can only view what he posted.can somebody out there advise me on what code to put here?

thanks.

pleigh

Posted: Fri Feb 18, 2005 12:52 am
by feyd
sooo, what's the problem? Your logic and code looks okay, from a quick look over..

Posted: Fri Feb 18, 2005 12:58 am
by wyred
Your code seems to answer your question already. Did I overlook something?

With no offence, I find it a bit messy and here's how I would do it.

1) When user logs in, retrieve his status and store his username and status into session variables.

2) Based on that status, you form your SQL statement and retrieve the results.
E.g.

Code: Select all

if ($_SESSION&#1111;'status']=='admin') &#123;
  $query = "SELECT date, title, postID FROM posts ORDER BY date DESC";
&#125; else &#123;
  $query = "SELECT date, title, postID FROM posts WHERE userID='$id' ORDER BY date DESC"; 
&#125;

$result = mysql_query($query) or die(mysql_error());
3) Loop and display those results.