PHP - Php Page Coding Style
Hello guys,
I am a beginner in PHP, so please be gentle. I am building this website for a friend of mine. This website is made up by HTML/CSS. i.e., I have a page made with HTML & CSS. there are a few menus on the page. say Home, About Us, Contact Us, etc., Now I want to separate the page elements (like header/footer) from the page so as its easy to work on. the source is something like this Code: [Select] <HTML> <HEAD> <TITLE> My Page </TITLE> <LINK REL="STYLESHEET" HREF="./mystyle.css" TYPE="text/css" /> </HEAD> <BODY> <DIV ID="wrapper"> <DIV ID="header"> <DIV ID="logo"> <UL ID="menu"> <LI> <A HREF="./index.php"><SPAN>Home</SPAN></A> </LI> <LI> <A HREF="./login.php"><SPAN>Login</SPAN></A> </LI> <LI> <A HREF="./contactus.php"><SPAN>Contact Us</SPAN></A> </LI> </UL> <DIV ID="date"> <?php echo Date("d M Y");?> </DIV> </DIV> </DIV> <DIV ID="bodywrapper"> <DIV ID="main"> <DIV ID="content"> THIS IS A TEST! </DIV> </DIV> </DIV> <DIV ID ="clearfooter"></DIV> </DIV> <DIV ID="footer"> © <?php echo date("Y");?> </DIV> </BODY> </HTML> now I will have my content in the DIV called 'content'. now I have separated this one page into three different pages. like this: header.php Code: [Select] <HTML> <HEAD> <TITLE> My Page </TITLE> <LINK REL="STYLESHEET" HREF="./mystyle.css" TYPE="text/css" /> </HEAD> <BODY> <DIV ID="wrapper"> <DIV ID="header"> <DIV ID="logo"> <UL ID="menu"> <LI> <A HREF="./index.php"><SPAN>Home</SPAN></A> </LI> <LI> <A HREF="./login.php"><SPAN>Login</SPAN></A> </LI> <LI> <A HREF="./contactus.php"><SPAN>Contact Us</SPAN></A> </LI> </UL> <DIV ID="date"> <?php echo Date("d M Y");?> </DIV> </DIV> </DIV> footer.php Code: [Select] <DIV ID ="clearfooter"></DIV> </DIV> <DIV ID="footer"> © <?php echo date("Y");?> </DIV> </BODY> </HTML> and index.php Code: [Select] <?php include_once('header.php');?> <DIV ID="bodywrapper"> <DIV ID="main"> <DIV ID="content"> THIS IS A TEST </DIV> </DIV> </DIV> <?php include_once('footer.php');?> Is this the right way to code? I am worried because I have to keep in mind the safety of the website as I have to include a basic login module to this. is there any other style to write this? please do let me know.. thanks Similar TutorialsHi, I use the following style for building my websites, I understand this is a fairly common style and was wondering if there is a name for it? Code: [Select] <?php include ('application.php'); include ('cms_config.php'); include ('templates/header.php'); switch ($_GET['action']) { case "home" : display_home(); break; case "services" : display_services(); break; case "testimonials" : display_testimonials(); break; case "contact" : display_contact(); break; default : display_home(); break; } include ('templates/footer.php'); // Function list // Display home page function display_home(){ $qid = mysql_query('SELECT * FROM testimonials ORDER BY RAND() LIMIT 0,1;'); include('templates/home.php'); } // Display services page function display_services(){ include('templates/services.php'); } // Display testimonials page function display_testimonials(){ $qid = mysql_query('SELECT * FROM Testimonials'); include('templates/testimonials.php'); } // Display home page function display_contact(){ include('templates/contact.php'); } ?> I am making a simple shopping cart. To add a product, I use this code: Code: [Select] <?php if (isset ($_GET['action']) && $_GET ['action'] == 'add' ) { $id = intval($_GET['id']); $_SESSION['cart'][] = $id; } $cart = $_SESSION['cart']; if (!$cart) { echo "No product in cart."; } elseif ($cart) { foreach ($_SESSION['cart'] as $id) { echo $id; } } ?> <A href="index.html?action=add&id=1">Add to cart</A> I am going to be selling t-shirts, I'm going to need a size along with the product ID, so now I need to add a 'size' value. I've tried simply doubling up on the code above with this code, but it isn't working. I was wondering if I am going the right way about it? Code: [Select] <?php if (isset ($_GET['action']) && $_GET ['action'] == 'add , size') { $id = intval($_GET['id']); $_SESSION['cart'][] = $id; $size = intval($_GET['size']); $_SESSION['cart'][] = $size; } $cart = $_SESSION['cart']; if (!$cart) { echo "No product in cart."; } elseif ($cart) { foreach ($_SESSION['cart'] as $id) { echo $id; } foreach ($_SESSION['cart'] as $size) { echo $size; } } ?> <A href="index.html?action=add&id=1&size=sizel">Add to cart</A> Hello.
I'm in need of help when it comes to page rendering, is it good practice to have own html file for each controller and controller->method OR 1 view file for each controller and then dynamically change content depending on the method?
My structure is like so:
controllers methods
-------------
services-> repair, car glass.....
info-> contact, about me.....
What is the best or good practice to handle the content in the view? The content is always the same.
Thanks in advance.
<? // Bank Version 1.0.0 21-05-2014 Desmond O'Toole. include ("secure/SecureFunctions.php"); include ("secure/SecureFunctionsLibAdmin.php"); session_start(); Session_Init(); $page = "Bank_EE Doc"; define ('hostname16', 'xxx'); // Des-otoole.co.uk define ('username16', 'xxx'); define ('password16', 'xxx'); define ('database16', 'xxx'); function myErrorHandler($errno, $errstr, $errfile, $errline) { switch ($errno) { case E_USER_ERROR: $_SESSION['MyError'] = "Gotcha: <br>$errstr<br>$errfile<br>$errline"; mailtoX('Error', $errstr,$_SESSION['MyError']); $redirect = "Location: myerror.php"; header($redirect); exit(0); break; case E_USER_WARNING: echo "This is your last warning"; break; case E_USER_NOTICE: echo "This is your final warning"; break; default: echo "Just go away"; break; } /* Don't execute PHP internal error handler */ return true; } $old_error_handler = set_error_handler("myErrorHandler"); function connectDB($db) { $host = hostname16; $user = username16; $pass = password16; $data = database16; if(!$link = @mysql_connect($host, $user, $pass)) trigger_error('Can\'t connect to server: ('. $db . ')', E_USER_ERROR); if(!$database = @mysql_select_db($data, $link)) trigger_error('Can\'t select database on: (' . $db . ')', E_USER_ERROR); } connectDB(CURRENT_DB); echo "Hi there"; ?>Hi this coding works on another website although I have reduced it down here for clarity. I have had my website moved to another server and I can't connect now. If there is a better way? I was given this coding from someone on this website about 4 years ago. I didn't want to use a strait connect because when there was difficulty connecting to the database I received an error giving me and any hacker all the details of the database server. This was to be a more controlled access. Edited by ignace, 09 June 2014 - 01:34 PM. hello everyone. i'm having problem to create a search page. i want to create a search page like this but i dont have the coding source to refer.. can anyone give me the coding for me to refer.... Help Please I need to disable a query on the products page. The URL is www.myjabberbox.com If you goto the products page, you'll see when you hover over one of the three products, you can click it to take you to another page of that product. I want to disable that. I thought the code that I'd need to modify would be under the products-page.php file but I can't seem to find the code. Notice also when you hover over the products, a mouseover color of black happens around the image/text frame, I'd like to keep that, just disable the linking... Thanks Hey there is no error with this code it works just fine but I would love to know if there is unnecessary coding in it for example do i have to do the global variables? is there a better way to do mysql editing page? thanks <?php include "../configdb.php"; $id = $_GET['id']; if(isset($_POST['submit'])) { //global variables $name = $_POST['name']; $footer = $_POST['footer']; //run the query which adds the data gathered from the form into the database $result = mysql_query("UPDATE pages SET name='$name', footer='$footer' WHERE id='$id' ",$connect); echo "<b>Your Page have been edited successfully"; } elseif($id) { $result = mysql_query("SELECT * FROM pages WHERE id='$id' ",$connect); while($row = mysql_fetch_assoc($result)) { $name = $row['name']; $footer = $row['footer']; ?> <h3>::Edit Page</h3> <form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>?id=<?php echo $row['id']?>"> <input type="hidden" name="id" value="<?php echo $row['id']?>"> <input name="name" size="40" maxlength="255" value="<?php echo $name; ?>"> <input name="footer" size="40" maxlength="255" value="<?php echo $footer; ?>"> <input type="submit" name="submit" value="Submit"> <?php } } This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=343748.0 Hi Everyone, I have this small bit of PHP that when you are on a certain page it will add a class called selected Code: [Select] <?php $currentPage = basename($_SERVER['SCRIPT_NAME']); ?> <ul id="subnav"> <li><a href="gallery.php?category=Wedding" id="wedding" <?php if ($currentPage == 'gallery.php?category=Wedding') {echo 'class="selected"';} ?>>Wedding Hair</a></li> <li><a href="gallery.php?category=Men" id="mens-styles" <?php if ($currentPage == 'gallery.php?category=Men') {echo 'class="selected"';} ?>>Mens Styles</a></li> <li><a href="gallery.php?category=Women" id="womens-styles" <?php if ($currentPage == 'gallery.php?category=Women') {echo 'class="selected"';} ?>>Women's Styles</a></li> <li><a href="gallery.php?category=Salon" id="salon" <?php if ($currentPage == 'gallery.php?category=Salon') {echo 'class="selected"';} ?>>Salon</a></li> </ul> This is not working I think it's because of the file extenstion 'gallery.php?category=Wedding' is there any way I can get this to work with these extensions Any help would be greatly appreciated Thanks Barry I have finally gotten my registration form to work on my website but i have another question! When for example someone registers at my website and type in the wrong email address, i've set php to give out an error message like "Please enter a valid email address". But that message shows up at the verry top-left corner of my page and makes some objekts move around on my page and you can't click in the <input> fields anymore . How could i make those error messages show up above my registration form at the top of my site and maybe style them with a border and maybe make the text red and so that it doesnt change anything on my page? could i use css for that somehow or what? This is how i show the error messages right now: public function show_errors() { echo "<h3>Errors</h3>"; foreach($this->errors as $key=>$value) echo $value."<br>"; } public function valid_data() { if($this->user_exists()) $this->errors[] = 'Username already taken, choose another one!'; if(empty($this->username)) $this->errors[] = 'Please enter a valid username!'; if(empty($this->first_name)) onkeydown="this.style.fontStyle='normal';"How can I add color: #000; to this? Thanks,
Background:
Question:
My experience: Code Examples:
function getInfo(ProductNumber){ $.ajax({ url:'Ajax-PHP-Page.php?ProductNumber='+ProductNumber, success: function(html) { document.getElementById("my_div").value = ''; document.getElementById("my_div").value = html; } }); }
function getInfo(ProductNumber) { if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("my_div").value = this.responseText; } }; xmlhttp.open("GET","Ajax-PHP-Page.php?ProductNumber="+ProductNumber,true); xmlhttp.send(); } Thank you!! Edited May 5, 2020 by StevenOliverQuesion: Show each movie in the database on its own page, and give the user links in a "page 1, Page 2, Page 3" - type navigation system. Hint: Use LIMIT to control which movie is on which page. I have provided 3 files: 1st: configure DB, 2nd: insert data, 3rd: my code for the question. I would appreciate the help. I am a noob by the way. First set up everything for DB: <?php //connect to MySQL $db = mysql_connect('localhost', 'root', '000') or die ('Unable to connect. Check your connection parameters.'); //create the main database if it doesn't already exist $query = 'CREATE DATABASE IF NOT EXISTS moviesite'; mysql_query($query, $db) or die(mysql_error($db)); //make sure our recently created database is the active one mysql_select_db('moviesite', $db) or die(mysql_error($db)); //create the movie table $query = 'CREATE TABLE movie ( movie_id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, movie_name VARCHAR(255) NOT NULL, movie_type TINYINT NOT NULL DEFAULT 0, movie_year SMALLINT UNSIGNED NOT NULL DEFAULT 0, movie_leadactor INTEGER UNSIGNED NOT NULL DEFAULT 0, movie_director INTEGER UNSIGNED NOT NULL DEFAULT 0, PRIMARY KEY (movie_id), KEY movie_type (movie_type, movie_year) ) ENGINE=MyISAM'; mysql_query($query, $db) or die (mysql_error($db)); //create the movietype table $query = 'CREATE TABLE movietype ( movietype_id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT, movietype_label VARCHAR(100) NOT NULL, PRIMARY KEY (movietype_id) ) ENGINE=MyISAM'; mysql_query($query, $db) or die(mysql_error($db)); //create the people table $query = 'CREATE TABLE people ( people_id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, people_fullname VARCHAR(255) NOT NULL, people_isactor TINYINT(1) UNSIGNED NOT NULL DEFAULT 0, people_isdirector TINYINT(1) UNSIGNED NOT NULL DEFAULT 0, PRIMARY KEY (people_id) ) ENGINE=MyISAM'; mysql_query($query, $db) or die(mysql_error($db)); echo 'Movie database successfully created!'; ?> ******************************************************************** *********************************************************************** second file to load info into DB: <?php // connect to MySQL $db = mysql_connect('localhost', 'root', '000') or die ('Unable to connect. Check your connection parameters.'); //make sure you're using the correct database mysql_select_db('moviesite', $db) or die(mysql_error($db)); // insert data into the movie table $query = 'INSERT INTO movie (movie_id, movie_name, movie_type, movie_year, movie_leadactor, movie_director) VALUES (1, "Bruce Almighty", 5, 2003, 1, 2), (2, "Office Space", 5, 1999, 5, 6), (3, "Grand Canyon", 2, 1991, 4, 3)'; mysql_query($query, $db) or die(mysql_error($db)); // insert data into the movietype table $query = 'INSERT INTO movietype (movietype_id, movietype_label) VALUES (1,"Sci Fi"), (2, "Drama"), (3, "Adventure"), (4, "War"), (5, "Comedy"), (6, "Horror"), (7, "Action"), (8, "Kids")'; mysql_query($query, $db) or die(mysql_error($db)); // insert data into the people table $query = 'INSERT INTO people (people_id, people_fullname, people_isactor, people_isdirector) VALUES (1, "Jim Carrey", 1, 0), (2, "Tom Shadyac", 0, 1), (3, "Lawrence Kasdan", 0, 1), (4, "Kevin Kline", 1, 0), (5, "Ron Livingston", 1, 0), (6, "Mike Judge", 0, 1)'; mysql_query($query, $db) or die(mysql_error($db)); echo 'Data inserted successfully!'; ?> ************************************************************** **************************************************************** MY CODE FOR THE QUESTION: <?php $db = mysql_connect('localhost', 'root', '000') or die ('Unable to connect. Check your connection parameters.'); mysql_select_db('moviesite', $db) or die(mysql_error($db)); //get our starting point for the query from the URL if (isset($_GET['offset'])) { $offset = $_GET['offset']; } else { $offset = 0; } //get the movie $query = 'SELECT movie_name, movie_year FROM movie ORDER BY movie_name LIMIT ' . $offset . ' , 1'; $result = mysql_query($query, $db) or die(mysql_error($db)); $row = mysql_fetch_assoc($result); ?> <html> <head> <title><?php echo $row['movie_name']; ?></title> </head> <body> <table border = "1"> <tr> <th>Movie Name</th> <th>Year</th> </tr><tr> <td><?php echo $row['movie_name']; ?></td> <td><?php echo $row['movie_year']; ?></td> </tr> </table> <p> <a href="page.php?offset=0">Page 1</a>, <a href="page.php?offset=1">Page 2</a>, <a href="page.php?offset=2">Page 3</a> </p> </body> </html> Is there a way i can make a php code for making a lesson then upload to my made system? Im newbie here. so basically, i need to make an e-learning system that lets the user make a lesson then upload it so that other user can view their made lesson.. Someone help me pls. Hi I need some help. I have a page which will show all the result based on the user sign in. And it will show something like this ID | Event Start Date | Event End Date | Event Details 1 19-11-2011 20-11-2011 View Event Details The view event details is a link so when I click on the link, it will link me to the next page (eventDetails.php?id=1) And at eventDetails.php?id=1 page, it would requires user to input whether they will be attending or not and will they be bringing their friend along then submit the form. When I submit, how do I pass all the data from eventDetails.php?id=1 page to confirmation.php page (after submitting)? You might want to refer to the attached coding. All the 2 php pages are working fine but when submit to confirmation.php page, the data are not post over so I didn't attach the confirmation.php page. Thanks Ben Chew [attachment deleted by admin] Hi guys,
I am needing some help of how to place the coding as I can not get it to work.
--
Basically I am building a website for the local motorclub and want some code that will "add up" each persons results.
Here is what I have come up with but it does not work;
<?php $total = mysql_fetch_array("SELECT cat1 + cat2 + cat3 + cat4 + cat5 + cat6 AS total FROM results") ?> <?php $cautotest_results = mysql_query("SELECT * FROM results WHERE cat ='1' ORDER BY cattotal DESC"); if(mysql_num_rows($cautotest_results) == '') { echo "<p>No Results Available."; } else { echo "<table width=\"800\" border=\"0\" cellpadding=\"2\" cellspacing=\"2\" class=\"greywritinglight\" align=\"center\"> <tr align=\"center\"> <td>Competitor</td> <td>Vehicle</td> <td>Class</td> <td>Skipton</td> <td>Weston</td> <td>King Bros</td> <td>Normans</td> <td>Norwood</td> <td>Uniroyal</td> <td>Total</td> </tr>"; $x=1; while($results_row = mysql_fetch_array($cautotest_results)) { if($x%2): $rowbgcolor = "#FFFFFF"; else: $rowbgcolor = "#EEEEEE"; endif; echo "<tr align=\"center\" bgcolor=\"" .$rowbgcolor. "\">"; echo "<td>" . $results_row['competitor'] . "</td>"; echo "<td>" . $results_row['catvehicle'] . "</td>"; echo "<td>" . $results_row['catclass'] . "</td>"; echo "<td>" . $results_row['cat1'] . "</td>"; echo "<td>" . $results_row['cat2'] . "</td>"; echo "<td>" . $results_row['cat3'] . "</td>"; echo "<td>" . $results_row['cat4'] . "</td>"; echo "<td>" . $results_row['cat5'] . "</td>"; echo "<td>" . $results_row['cat6'] . "</td>"; echo "<td>" . $total . "</td>"; echo "</tr>"; $x++; } echo "</table>"; } ?>Its the top line of coding... and the echo .total. at the bottom. If you could please correct me I would be ever so grateful. If you need more info please just ask. All help is appreciated. JTM. Edited by JTM, 17 August 2014 - 12:45 AM. <?php
$Title=$_POST['title']; why it gives "insertion failed" as a result?
I have a small mp3 script and its running accurately ...i want to check the mp3 URL before playing it whether the mp3 link active or broken ....how to check the link is active or broken in php thanks I am self taught and am trying very hard to get a log in page to work. The way i want it to work is that when they log in it uses a url stored in the database for their account. But i am having some much trouble working out a code that works and this is all i got: <?php include ('connection.php'); $username=$_POST['username5']; $password=$_POST['password5']; $query="SELECT password FROM merchants WHERE username = '$_POST(username5)'"; if ($password == $query) { $url = "SELECT url FROM merchants WHERE username = '$_POST(username5)'"; header("Location: $url"); } else { echo urgh } ?> |