stbalaji2u Posted August 17, 2008 Share Posted August 17, 2008 Hi guys ... i started to lean php couple of months ago.. when i engaged in very tiny programs it was going smooth and i decided that i should have to move on to the next level.. so i decided to download some cool php scripts .. as i have seen not even one project is properly arranged for learning .. my point is that how programmers really merge php and html.. some one embed's html code in php's echo statement and some others embed php's code in html.. am so irritated in trying to understand those.. what shall i have to do inorder to make my project a clean and crispy one .. and also please let me know whether there is any script avaiable to download that really developed in order to make beginners an intermediate level programmers.. Quote Link to comment Share on other sites More sharing options...
marcus Posted August 17, 2008 Share Posted August 17, 2008 If you want extremely valid HTML then separate PHP from HTML, like so: <?php $name = "Jerald"; $age = "25"; $date = date("M d, Y"); if($age >= 20){ ?> <table border="0" cellspacing="3" cellpadding="3"> <tr> <td>Name</td><td><?php echo $name; ?></td> </tr> <tr> <td>Age</td><td><?php echo $age; ?></td> </tr> <tr> <td>Date</td><td><?php echo $date; ?></td> </tr> </table> <?php }else { ?> This user is not old enough! <?php } ?> Or you can just use echo for a faster method (imo), like so: <?php $name = "Jerald"; $age = "25"; $date = date("M d, Y"); if ($age >= 20) { echo '<table border="0" cellspacing="3" cellpadding="3"> <tr> <td>Name</td><td><?php echo $name; ?></td> </tr> <tr> <td>Age</td><td><?php echo $age; ?></td> </tr> <tr> <td>Date</td><td><?php echo $date; ?></td> </tr> </table>'; } else { echo 'This user is not old enough!'; } ?> Quote Link to comment Share on other sites More sharing options...
stbalaji2u Posted August 17, 2008 Author Share Posted August 17, 2008 Hi mgallforever that really makes sense .. i think following this method makes effortless... thanks in advance anyway do you have simple scripts that makes me understand even better for projects like login register etc.?? Quote Link to comment Share on other sites More sharing options...
marcus Posted August 17, 2008 Share Posted August 17, 2008 Here's a simple version of one of my registration scripts. <?php if(!$_POST['submit']){ echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n"; echo "<form method=\"post\" action=\"register.php\">\n"; echo "<tr><td colspan=\"2\" align=\"center\">Registration Form</td></tr>\n"; echo "<tr><td>Username</td><td><input type=\"text\" name=\"username\"></td></tr>\n"; echo "<tr><td>Password</td><td><input type=\"password\" name=\"password\"></td></tr>\n"; echo "<tr><td>Confirm</td><td><input type=\"password\" name=\"passconf\"></td></tr>\n"; echo "<tr><td>E-Mail</td><td><input type=\"text\" name=\"email\"></td></tr>\n"; echo "<tr><td>Name</td><td><input type=\"text\" name=\"name\"></td></tr>\n"; echo "<tr><td>AIM Address</td><td><input type=\"text\" name=\"aim\"></td></tr>\n"; echo "<tr><td colspan=\"2\" align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Register\"></td></tr>\n"; echo "</form></table>\n"; }else { $username = protect($_POST['username']); $password = protect($_POST['password']); $confirm = protect($_POST['passconf']); $email = protect($_POST['email']); $name = protect($_POST['name']); $aim = protect($_POST['aim']); $errors = array(); if(!$username){ $errors[] = "Username is not defined!"; } if(!$password){ $errors[] = "Password is not defined!"; } if($password){ if(!$confirm){ $errors[] = "Confirmation password is not defined!"; } } if(!$email){ $errors[] = "E-mail is not defined!"; } if(!$name){ $errors[] = "Name is not defined!"; } if(!$aim){ $errors[] = "AIM Screename is not defined!"; } if($username){ if(!ctype_alnum($username)){ $errors[] = "Username can only contain numbers and letters!"; } $range = range(1,32); if(!in_array(strlen($username),$range)){ $errors[] = "Username must be between 1 and 32 characters!"; } } if($password && $confirm){ if($password != $confirm){ $errors[] = "Passwords do not match!"; } } if($email){ $checkemail = "/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i"; if(!preg_match($checkemail, $email)){ $errors[] = "E-mail is not valid, must be name@server.tld!"; } } if($name){ $range2 = range(3,64); if(!in_array(strlen($name),$range2)){ $errors[] = "Your name must be between 3 and 64 characters!"; } } if($aim){ $range3 = range(3,16); if(!in_array(strlen($aim),$range3)){ $errors[] = "Your AIM screenname must be between 3 and 16 characters!"; } } if($username){ $sql = "SELECT * FROM `users` WHERE `username`='{$username}'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) > 0){ $errors[] = "The username you supplied is already in use!"; } } if($email){ $sql2 = "SELECT * FROM `users` WHERE `email`='{$email}'"; $res2 = mysql_query($sql2) or die(mysql_error()); if(mysql_num_rows($res2) > 0){ $errors[] = "The e-mail address you supplied is already in use of another user!"; } } if($aim){ $sql3 = "SELECT * FROM `users` WHERE `aim`='{$aim}'"; $res3 = mysql_query($sql3) or die(mysql_error()); if(mysql_num_rows($res3) > 0){ $errors[] = "The AIM screenname you supplied is already in use of another user!"; } } if(count($errors) > 0){ foreach($errors AS $error){ echo $error . "<br>\n"; } }else { $sql4 = "INSERT INTO `users` (`username`,`password`,`email`,`name`,`aim`) VALUES ('$username','".md5($password)."','$email','$name','$aim')"; $res4 = mysql_query($sql4) or die(mysql_error()); echo "You have successfully registered with the username <b>{$username}</b> and the password of <b>{$password}</b>!"; } } ?> Obviously you'll have to create your own protect function. And login. <?php session_start(); echo "<title>Login</title>\n"; if ($_SESSION['uid']) { echo "You are already logged in, if you wish to log out, please <a href=\"./logout.php\">click here</a>!\n"; } else { if (!$_POST['submit']) { echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n"; echo "<form method=\"post\" action=\"./login.php\">\n"; echo "<tr><td>Username</td><td><input type=\"text\" name=\"username\"></td></tr>\n"; echo "<tr><td>Password</td><td><input type=\"password\" name=\"password\"></td></tr>\n"; echo "<tr><td colspan=\"2\" align=\"right\"><input type=\"submit\" name=\"submit\" value=\"Login\"></td></tr>\n"; echo "</form></table>\n"; }else { $user = mss($_POST['username']); $pass = $_POST['password']; if($user && $pass){ $sql = "SELECT id FROM `users` WHERE `username`='".$user."'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) > 0){ $sql2 = "SELECT id FROM `users` WHERE `username`='".$user."' AND `password`='".md5($pass)."'"; $res2 = mysql_query($sql2) or die(mysql_error()); if(mysql_num_rows($res2) > 0){ $row = mysql_fetch_assoc($res2); $_SESSION['uid'] = $row['id']; echo "You have successfully logged in as " . $user . "<br><br><a href=\"./index.php\">Proceed to the Forum Index</a>\n"; }else { echo "Username and password combination are incorrect!\n"; } }else { echo "The username you supplied does not exist!\n"; } }else { echo "You must supply both the username and password field!\n"; } } } ?> Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted August 17, 2008 Share Posted August 17, 2008 This is incorrect. You have php tags, then an echo statement which has php tags nested within it. <?php $name = "Jerald"; $age = "25"; $date = date("M d, Y"); if ($age >= 20) { echo '<table border="0" cellspacing="3" cellpadding="3"> <tr> <td>Name</td><td><?php echo $name; ?></td> </tr> <tr> <td>Age</td><td><?php echo $age; ?></td> </tr> <tr> <td>Date</td><td><?php echo $date; ?></td> </tr> </table>'; } else { echo 'This user is not old enough!'; } ?> The above snippet should be: <?php $name = "Jerald"; $age = "25"; $date = date("M d, Y"); if ($age >= 20) { echo '<table border="0" cellspacing="3" cellpadding="3"> <tr> <td>Name</td><td>'.$name.'</td> </tr> <tr> <td>Age</td><td>'.$age.'</td> </tr> <tr> <td>Date</td><td>'.$date.'</td> </tr> </table>'; } else { echo 'This user is not old enough!'; } ?> Quote Link to comment Share on other sites More sharing options...
marcus Posted August 17, 2008 Share Posted August 17, 2008 ohaha, i was just editing from my first example Quote Link to comment Share on other sites More sharing options...
stbalaji2u Posted August 17, 2008 Author Share Posted August 17, 2008 nrg_alphas code is easy to understand ... can any one of you provide me the code that is altered from the previous registration and login code which is built in extermely vaild HTML as in the very first code given by mgallforever that second method which uses echo statement to do it faster really irritates me.. ??? Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted August 17, 2008 Share Posted August 17, 2008 use heredoc rather then 1 big echo... <?php echo <<<_PAGE <html> <head> <title>My Title</title> </head> <body> <b>My Content</b> </body> </html> _PAGE; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.