Forgot Password / Register
Site Statistics
Total Members: 509
Total Tutorials: 242
Newsest User: Lestop
Todays Unique Hits: 204
0 Users 1 Guests Online

Basics of using Case's in PHP

Hello all,
Do you use PHP? Perhaps a beginner yes? I know from exprience for one PHP function i would have about 2/3 or more files, then i discovered Cases's.

Your probly looking at this like what the ....? Here i will break it down for you.

PHP Code:
Code
<?
include("config.php");

switch($_GET['view']) {
case "delete" :
include("sql.php");

$id = $_GET['id'];
if(!$_GET['id'])
{
header("Location:bugs.php");
}
$select = mysql_query("select * from `form` where id=$id");
if (mysql_num_rows($select) == "0") {
echo 'Unable to find the message info in the database!';
exit();
}
$delete = mysql_query("DELETE FROM `form` WHERE id = '$id'") or die(mysql_error());
echo("<meta http-equiv=\"Refresh\" content=\"1; URL=bugs.php\"/>The Bug report has been deleted! You will now be redirected");
break;
default:
include("sql.php");
$sql = mysql_query("SELECT * FROM `form` WHERE type='5'");
while($r = mysql_fetch_array($sql))
{
$id = $r[id];
$name = $r[user];
$email = $r[email];
$message = $r[msg];
$ip = $r[ip];
echo("
$id. $name
<br />
$email
<br /><br />
$message
<br /><br />
$ip
<br />
<a href=\"bugs.php?view=delete&id=$id\">Delete</a><br /><hr>");
}
break;
?>

Ok so in this instance the file is bugs.php, Once the delete it would send the url "bugs.php?view=delete&id=$id".

the ?view=delete will run this function. Deleteing the form in that database that is equal to the ID!

PHP Code:
Code
include("sql.php");

$id = $_GET['id'];
if(!$_GET['id'])
{
header("Location:bugs.php");
}
$select = mysql_query("select * from `form` where id=$id");
if (mysql_num_rows($select) == "0") {
echo 'Unable to find the message info in the database!';
exit();
}
$delete = mysql_query("DELETE FROM `form` WHERE id = '$id'") or die(mysql_error());
echo("<meta http-equiv=\"Refresh\" content=\"1; URL=bugs.php\"/>The Bug report has been deleted! You will now be redirected");

Now if we did not stop this it would completely mess up, be bad coding .. and personally i hate dodgey code.

PHP Code:
Code
break;


This will end the case function once it has done what it is suppose to do ...

Ok well that about sums up my tutorial on Case's in PHP. Ill be happy to answer any questions.

Yours, Adam
Overload
Author:
Views:
910
Rating:
Posted on Tuesday 13th March 2007 at 04:15 PM
MOD-Dan
MOD-Dan
cool looks good!