Page 1 of 1

[SOLVED] IF statements and cases

Posted: Fri Jul 28, 2006 1:34 pm
by Michael.Santangelo
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


OK, so I'm trying to get some IF statements to work so I can do a total of 4 cases (If only the field USERNAME is filled, if only the field WEEKLY is filled, if only the PROBLEMS are used, and if none of them selected [to display the page html to make a selection]).

However, with the code I'm using, all 3 cases are displayed... You can see it here: http://169hamilton.kicks-ass.net:8088/c ... _admin.php

The code is

Code: Select all

<body>
<?php
	// Declare The Needed Variables - DO NOT MODIFY! 
	$host = "localhost";
	$user = "root";
	$pass = "mypass";
	$db = "mydb";
	$table = "mytable";
	$problems = array('Printing (Hardware)','Printing (Software)','Print Retrieval', 'Account Problems', 'Password Changes', 'File Storage/Retrieval', 'Personal Equipment', 'Internet/Networking', 'Dorm/Resnet', 'Wireless', 'Info/Directions', 'IMLs', 'Email', 'Policy', 'Class-Related', 'Phone Support', 'Supplies', 'Hardware', 'Software', 'Other (Must Comment!)');

	// Connect to the Server with Specified Username and Password - DO NOT MODIFY!
	mysql_connect($host, $user, $pass) or die("Connection Failed! " . mysql_error());
			
	// Connect to the Specified Database - DO NOT MODIFY!
	mysql_select_db($db) or die("DB Select Failed! " . mysql_error());

	if (isset($_POST['username']))
	{
		$name = $_POST['username'];

		// Make the Query - DO NOT MODIFY!
		$sql = 'SELECT * FROM data_02 WHERE Username=\''.$name.'\'';

		$data = mysql_query($sql) or die ("Query Failed! " . mysql_error());
	
		$out1 = "Displaying all entries from ".$name."\n";
		echo nl2br($out1);
		while($info = mysql_fetch_array($data))
		{
			$out2 = "\nSubmission on ".$info['Date']." at ".$info['Time'];
			$out3 = "\n--Problem: ".$info['Problem'];
			$out4 = "\n--Comments: ".$info['Comments']."\n";
			echo nl2br($out2);
			echo nl2br($out3);
			echo nl2br($out4);
		}
	}
	else if (isset($_POST['weekly']))
	{
		$weekly = $_POST['weekly'];
		$out1 = "Displaying all submissions received from FROM to TO.";
		echo nl2br($out1);
	}
	else if (isset($_POST['problems']))
	{
		$out1 = "Displaying all submissions of type PROBLEM.";
		echo nl2br($out1);
	}
	else
	{
?>
<div class="main">
...HTML...
<p>Last Updated: <?php print date("F d, Y.  g:i a", getlastmod()); ?>, msantang<br></p>
</div>
<?php
	}
?>
</body>
</html>
If you enter a username, you'll see it goes the way it is supposed to.
If you enter a date, it applies the username AND the date fields.
If you select the problem type, it applies username, date, and problem fields.

I'm really confused... Can someone clarify?

-Mike


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Jul 28, 2006 1:40 pm
by Ollie Saunders

Code: Select all

if (empty($_POST) {
    // nothing in post
} else {
    $numPost = count($_POST);
    if ($numPost == 1) {
        if (isset($_POST['username'])) {
            // only username has been sent to post
        } else if (isset($_POST['weekly']) {
            // only weekly has been sent to post
        } else if (isset($_POST['problems']) {
            // only problems has been sent to post
        }
    }
}

Posted: Fri Jul 28, 2006 1:55 pm
by Michael.Santangelo
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: Arrow Posting Code in the Forums to learn how to do it too.
[/quote]
Ok - sorry =)

Ole: I'll give that a shot when I get home... I hope it works.  Thanks!

Posted: Mon Jul 31, 2006 12:27 pm
by Michael.Santangelo
ole,

The code you posted does not seem to work.

http://169hamilton.kicks-ass.net:8088/c ... _admin.php

If you put data in the Username field, it works.
If you put data in the Date field, it displays ONLY the Username entry... so it is still running the Username code.
If you select a Problem, it displays ONLY the Username... so again it is still running the Username code.

Any suggestions?

Code follows:

Code: Select all

<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>LCC Customer Service Statistics - Admin View</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
</head>

<body>
<?php
	// Declare The Needed Variables - DO NOT MODIFY! 
	$host = "localhost";
	$user = "root";
	$pass = "pass";
	$db = "csstat";
	$table = "data_02";
	$problems = array('Printing (Hardware)','Printing (Software)','Print Retrieval', 'Account Problems', 'Password Changes', 'File Storage/Retrieval', 'Personal Equipment', 'Internet/Networking', 'Dorm/Resnet', 'Wireless', 'Info/Directions', 'IMLs', 'Email', 'Policy', 'Class-Related', 'Phone Support', 'Supplies', 'Hardware', 'Software', 'Other (Must Comment!)');

	// Connect to the Server with Specified Username and Password - DO NOT MODIFY!
	mysql_connect($host, $user, $pass) or die("Connection Failed! " . mysql_error());
			
	// Connect to the Specified Database - DO NOT MODIFY!
	mysql_select_db($db) or die("DB Select Failed! " . mysql_error());

	if (empty($_POST))
	{	?>
		<div class="main">
			<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
			<input type="text" name="username" size="57" /><br />
			This will return instances of that username being used to enter form data.<br /><br />
			<input type="text" name="weekly" size="57" /><br />This will return whatever date you enter below plus 7. So if you enter 2006/07/07, you will get 2006/07/07 to 2006/07/14.  You MUST&nbsp;enter YYYY/MM/DD or it will NOT&nbsp;work. (NOT&nbsp;YET&nbsp;IMPLEMENTED).<br /><br />Selecting a problem below will return instances where that problem was selected. (NOT&nbsp;YET&nbsp;IMPLEMENTED).<br />
			<?php	foreach($problems as $problem)
			{
				echo "  <input type=\"checkbox\" value=\"{$problem}\" name=\"problems[]\">{$problem}</input><br />\n";
			}
			?>
			<input type="submit" value="Submit" /> 
			<input type="reset" value="Reset" />
			</form>
			<!-----------Bottom Of Page----------->
			<hr>
			<p>Last Updated: <?php print date("F d, Y.  g:i a", getlastmod()); ?>, msantang<br></p>
		</div>
	<?php
	}
	else {
		$numPost = count($_POST);
		if ($numPost != 1) {
			if (isset($_POST['username'])) {
				$name = $_POST['username'];
				
				// Make the Query - DO NOT MODIFY!
				$sql = 'SELECT * FROM data_02 WHERE Username=\''.$name.'\'';

				$data = mysql_query($sql) or die ("Query Failed! " . mysql_error());
	
				$out1 = "Displaying all entries from ".$name."\n";
				echo nl2br($out1);
				while($info = mysql_fetch_array($data))
				{
					$out2 = "\nSubmission on ".$info['Date']." at ".$info['Time'];
					$out3 = "\n--Problem: ".$info['Problem'];
					$out4 = "\n--Comments: ".$info['Comments']."\n";
					echo nl2br($out2);
					echo nl2br($out3);
					echo nl2br($out4);
				}
			}
			else if(isset($_POST['weekly'])) {
				$weekly = $_POST['weekly'];
				$out1 = "Displaying all submissions received from FROM to TO.";
				echo nl2br($out1);
			}
			else if (isset($_POST['problems']))	{
				$out1 = "Displaying all submissions of type PROBLEM.";
				echo nl2br($out1);
			}
		}
	} ?>
</body>
</html>

Posted: Mon Jul 31, 2006 12:40 pm
by Michael.Santangelo
I got it. I think.

Code: Select all

<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>LCC Customer Service Statistics - Admin View</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
</head>

<body>
<?php
	// Declare The Needed Variables - DO NOT MODIFY! 
	// Remote $host = "169hamilton.kicks-ass.net";
	$host = "localhost";
	$user = "root";
	$pass = "ncc1701D";
	$db = "csstat";
	$table = "data_02";
	$problems = array('Printing (Hardware)','Printing (Software)','Print Retrieval', 'Account Problems', 'Password Changes', 'File Storage/Retrieval', 'Personal Equipment', 'Internet/Networking', 'Dorm/Resnet', 'Wireless', 'Info/Directions', 'IMLs', 'Email', 'Policy', 'Class-Related', 'Phone Support', 'Supplies', 'Hardware', 'Software', 'Other (Must Comment!)');

	// Connect to the Server with Specified Username and Password - DO NOT MODIFY!
	mysql_connect($host, $user, $pass) or die("Connection Failed! " . mysql_error());
			
	// Connect to the Specified Database - DO NOT MODIFY!
	mysql_select_db($db) or die("DB Select Failed! " . mysql_error());

	if (empty($_POST))
	{
	}
	else {
		$numPost = count($_POST);
		if ($numPost != 1) {
			if (isset($_POST['username']) && empty($_POST['weekly']) && empty($_POST['problems'])) {
			}
			if(isset($_POST['weekly']) && empty($_POST['username']) && empty($_POST['problems'])) {
			}
			else if (isset($_POST['problems']) && empty($_POST['username']) && empty($_POST['weekly']))	{
			}
		}
	} ?>
</body>
</html>
Thanks for the push in the right direction ole!