conditionals...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pleigh
Forum Contributor
Posts: 445
Joined: Wed Jan 19, 2005 4:26 am

conditionals...

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

sooo, what's the problem? Your logic and code looks okay, from a quick look over..
wyred
Forum Commoner
Posts: 86
Joined: Mon Dec 20, 2004 1:59 am
Location: Singapore

Post 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.
Post Reply