Location: PHPKode > scripts > PHP Commander > commander.php
<!-- 
	PHP Commander
	=============
	This is a free script from BigProf Software (www.bigprof.com).
	You can use it for:
	[1] Learning PHP interactively.
	[2] Testing and debugging your PHP scripts interactively.
	[3] Running programs on your server (as if you have telnet access).
	
	Use this tool wisely, and NEVER allow anyone to access 
	it on your server. I have warned you, so don't blame me!
	
	The latest updates to this tool are available from:
	http://www.bigprof.com/free-php-scripts/php-commander.html
-->


<style type="text/css">
<!--
	select{
		font-family:verdana;
		font-size:9px;
		font-weight:bold;
		color:white;
		background-color:black;
		}
-->
</style>
<?php

$examples[] = new Code;



$examples[0]->Name = "Hello World!";
$examples[0]->Code = 
"// This is the classical Hello World example, as applied to PHP.

echo \"Hello World!\";



// You can use HTML tags as well:

echo \"<br>\";

echo \"<b>HTML-formatted Hello World!</b>\";

// Click the Execute button to see the output.
";

//------------------------------------------------------------------

$examples[1]->Name = "Variables";
$examples[1]->Code = 
"\$a = 12; // this is a variable containing an integer.

\$b = \"Variable containing text\";

\$c = -54.0937; // Variable containing a decimal.

echo \$a;

echo \"<br>\";

echo \"b = \$b <br> c = \$c\"; // See how you can use variables inside strings?
";

//------------------------------------------------------------------

$examples[2]->Name = "if() statment";
$examples[2]->Code = 
"// if statment is used to control program flow

\$x = 205;
\$y = 398;

if(\$x > \$y)
{
	echo \"x is greater!\";
}
else
{
	echo \"y is greater!\";
}

";

//------------------------------------------------------------------

$examples[3]->Name = "if() again!";
$examples[3]->Code = 
"// you can test several conditions in one line

\$x = 205;  // Try changing x and y values here and execute
\$y = 398;  // the script to see various output results.

if(\$x > \$y && \$x > 300)
{
	echo \"x is greater and exceeds 300\";
}
elseif(\$y > \$x && \$y > 300)
{
	echo \"y is greater and exceeds 300\";
}
else
{
	echo \"Conditions not met!\";
}
";

//------------------------------------------------------------------

$examples[4]->Name = "Loops (for)";
$examples[4]->Code = 
"// Use loops for repetitive tasks.
// For example, I'll show you here how to display an html table
// with 10 cells of alternating bgcolors with little coding.

echo \"<table border=1 cellspacing=0><tr>\";

for(\$i = 0; \$i < 10; \$i++)
{
	if(\$i % 2) // % means remainder after division.
	{
		echo \"<td bgcolor=silver>Cell \$i </td>\";
	}
	else 
	{
		echo \"<td bgcolor=white>Cell \$i </td>\";
	}
}

echo \"</tr></table>\";
";

//------------------------------------------------------------------

$examples[5]->Name = "Loops (while)";
$examples[5]->Code = 
"// Use while loops for repeating a task until a condition becomes invalid.
// For example, the loop below simulates 2 dice that will
// keep rolling till both read 6 ...

\$dice1 = 1; // any initial reading
\$dice2 = 3;
\$i = 0; // this will count the dice rolling attempts

// the condition of dice1 or dice2 not reading 6
// will keep the loop running ...
while(\$dice1 < 6 || \$dice2 < 6)
{
	\$dice1 = rand(1, 6); // any random number from 1 to 6
	\$dice2 = rand(1, 6);
	echo \"[\$dice1,\$dice2] \";
	\$i++;
}

echo \"<br><b>\$i attempts to get a [6,6]!</b>\";";

//------------------------------------------------------------------

$examples[6]->Name = "MySQL database query";
$examples[6]->Code = 
"// This example shows you how to connect to MySQL
// and send an SQL query, then retrieve the results
// and display them as an html table


///////////////////////////////////////////////////////////////
// Step 1: change these values according to your MySQL config
///////////////////////////////////////////////////////////////

\$dbserver = \"localhost\"; // replace localhost with your MySQL server name
\$username = \"root\";      // your MySQL username here
\$password = \"password\";  // your MySQL password here
\$database = \"mydbase\";   // your database name here
\$table    = \"mytable\";   // the database table that will be queried


///////////////////////////////////////
// Step 2: connect to MySQL server
///////////////////////////////////////

if(@mysql_connect(\$dbserver, \$username, \$password))
{
	///////////////////////////////////////
	// Step 3: select the database
	///////////////////////////////////////
	
	if(@mysql_select_db(\$database))
	{
		///////////////////////////////////////
		// Step 4: send the SQL quesry
		///////////////////////////////////////
		
		if(\$result = @mysql_query(\"select * from \$table limit 10\"))
		{
			echo \"<table border=1 cellspacing=0>\";
			
			///////////////////////////////////////
			// Step 5: retrieve query results
			///////////////////////////////////////
		
			while(\$row = mysql_fetch_row(\$result))
			{
				echo \"<tr>\";
				for(\$i = 0; \$i < mysql_num_fields(\$result); \$i++)
				{
					echo \"<td>\" . \$row[\$i] . \"</td>\";
				}
				echo \"</tr>\";
			}

			echo \"</table>\";

			///////////////////////////////////////
			// Step 6: close connection (optional).
			///////////////////////////////////////
		
			mysql_close();
		}
		else
		{
			echo \"Error: Couldn't query the table.<br>\";
		}
	}
	else
	{
		echo \"Error: Couldn't select the database.<br>\";
	}
}
else
{
    echo \"Error: Couldn't connect to MySQL.<br>\";
}

";

//------------------------------------------------------------------


//------------------------------------------------------------------



if($HTTP_POST_VARS["command"] && $HTTP_POST_VARS["request"])
{
	eval(stripslashes($HTTP_POST_VARS["command"]));
}
elseif($HTTP_POST_VARS["bex"])
{
	foreach($examples as $e)
	{
		if($HTTP_POST_VARS["bex"] == $e->Name)
		{
			$HTTP_POST_VARS["command"] = $e->Code;
			break;
		}
	}
}
?>

<form method=post action=commander.php name=myform>

<table border=1 cellspacing=0 cellpadding=5><tr>
<td valign=top>
	<font size="+1" face=verdana><b>PHP Commander</b></font>
	<br>Free script from <a href="http://www.bigprof.com/">BigProf.com</a></font>
	<br>
	<textarea wrap=off name=command cols=80 rows=20 
	style="	background-color:black; 
			color:yellow; 
			font-family:corrier new,courier; 
			font-weight:normal;
			font-size:12px;"
	><?php echo stripslashes($HTTP_POST_VARS["command"]); ?></textarea>

	<div align=right>
	<input type=submit name="request" value="Execute">
	<input type=reset value="Clear" onclick="window.location='commander.php';">
	</div>
</td>

<td valign=top width=140><font size="-2" face=verdana>
	<font size="-2" face=verdana>
	Type your PHP code in the left textarea and hit "Execute" to see the
	output. Do not start with &lt;? or end with ?>.

	<br><br><br><br><br>
	
	
	
	<!-- Beginners Examples -->
	<center>
	<select name=bex onChange='myform.submit(); return false;'>
	<option value="">PHP Examples:</option>
	<?php
		foreach($examples as $e)
			echo "<option value=\"" . $e->Name . "\">" . $e->Name . "</option>";
	?>
	</select>
	</center>
	We will add more example code regularily. You can check
	for latest updates to PHP Commander
	<a href="http://www.bigprof.com/free-php-scripts/php-commander.html">here</a>.
	<!------------------------->
	
	<br><br><br><br><br>
	<b><font color="red">Important!</font></b><br>
	This page is for your <i><u>personal use only</u></i>.
	NEVER grant your website visitors access to it.
	You could DESTROY your server. I have warned you, so don't blame me!
	
</font></td>

</tr></table>

</form>

<?php

class Code{
	var $Name, $Code;
}

?>
Return current item: PHP Commander