Integrating Clean code when mixing PHP and HTML [closed] - php

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm finding an organisation a major problem with mixing PHP and HTML, it just looks horrible, so i'm wondering if it's a viable option to create a set of object oriented methods such as this:
class MainOO {
public $Database;
public function __construct($Server,$User, $Password, $DB){
if ($this->Database = new mysqli($Server,$User,$Password,$DB)){
return true;
}
return false;
}
public function User_Login(){
$Get_Usr_Info = $this->Database->prepare("SELECT ID, Password, Salt FROM Users WHERE Username=?");
$Get_Usr_Info->bind_param('s',$_POST['username']);
$Get_Usr_Info->execute();
$Get_Usr_Info->store_result();
$User_Number = $Get_Usr_Info->num_rows;
$Get_Usr_Info->bind_result($UserID, $Stored_Password, $Stored_Salt);
$Get_Usr_Info->fetch();
$Get_Usr_Info->close();
if ($User_Number !== 1){
$Error = "Wrong Username Specified Or Password Is Incorrect";
header ("Location: index.php?Errors=".urlencode($Error));
exit;
}
// Continue with login script
}
public function Logout(){
if (session_status() !== PHP_SESSION_DISABLED){
session_destroy();
header ("Location: LoggedOut.php");
exit;
}
}
}
Then HTML side:
<?php
include "MainOO.php";
$MainOO = new MainOO("host","user","password","database");
?>
<div class="example">
<div class="example left">
<?php
$MainOO->User_Login();
?>
</div>
</div>
It's still mixing PHP & HTML, but it's making look a hell of a lot neater than having heaps of PHP in the middle of HTML.
I'm fully aware I could migrate over to a MVC Framework (which this topic is looking like) already setup, or even use a template engine such as smarty, but I want to avoid this as much as possible.. So is this a viable option to have neater PHP code within html?

You will probably want to use an isset() in the code too
e.g
<?= (isset($variable)) ? $variable : ''; ?>
e.g if variable isset then display it otherwise display nothing

Related

How to have 2 languages in html so that I can open the page with a flag like lang=en? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am making a html for showing a "Problem Statement" of a contest. The problem has translation in 2 language English and Bengali. I can make this using 2 htmls. But how can I have like this -
desc.html?lang=en or desc.html?lang=bn or some other kind so that I can tell the page to show a specific language?
I am a newbie in web development.
Say, you have 2 files currently, index-english.html and index-german.html
You now create a file called index-global.php. Whether the first two pages are fully HTML or contain PHP, doesn't matter, but you should keep the current extension.Thanks to fred-ii for mentioning that you should change the extension to .php for the HTML-pages
In the index-global.php you'll place the following code
if (isset($_GET['lang']) AND $_GET['lang'] == "en") require("index-english.html");
elseif (isset($_GET['lang']) AND $_GET['lang'] == "de") require("index-german.html");
else {
header("Location: /?lang=en"); //redirect if no language specified
exit();
}
A better way would be to check the language automatically based on the Accept-Language-header, for example
$languages_supported_by_client = explode(",",$_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach ($language_supported_by_client as $language) {
if($language == "en") {
require("index-english.html");
exit();
}
elseif($language == "de") {
require("index-german.html");
exit();
}
}
require("index-english.html");
exit();
In this case the asker wanted to include all HTML in one file
$languages_supported_by_client = explode(",",$_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach ($language_supported_by_client as $language) {
if($language == "en") {
?>English HTML content here<?php
exit();
}
elseif($language == "de") {
?>German HTML content here<?php
exit();
}
}
?>English HTML content here<?php
exit();

How to display pages with different header? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am working on website for my school project. But I came across with a problem. I display a header in every page. One of my header contains login form and other one contains username, search bar, etc..
The questions is, there are 3 pages; faq, contact, about. And I want to show them either user is logged in or not. But the headers are problem. I want user to see the header-after-login if user is logged in if not I wanna show header-before-login.
I have the code any everything. I need a way or a logic to fix this issue.
Thank you.
header();
function header(){
$header = "<div ";
if($_SESSION['userID']){
$header .= "class=\"header user_info_header\"";
$content = "logged user info";
}else{
$header .= "class=\"header login_form_header\"";
$content = "login form";
}
$header .= ">".$content."</div>";
echo $header;
}
There's probably a million and one ways to solve this, and I'm assuming you're able to programatically find out if the user is logged in.
faq.php, contact.php, about.php:
include 'header.php';
//content
include 'footer.php';
header.php:
echo '<!doctype html><html>...';
if($user_is_logged_in == true) {
echo '<body class="logged_in">You are logged in!';
include 'sidebar.php';
} else {
echo '<body class="not_logged_in">You are not logged in!';
include 'user_not_logged_in_header.php';
}
echo '<div class="main">';
footer.php:
echo '</div></body></html>';
sidebar.php:
echo '<div class="sidebar">Sidebar!</div>;
Then the sidebar div and main div can be independently controlled using the classes.

Different Page View for User [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to try this. But I don't know that this will work or not and whether this is a good way for me to achieve what I want.
What I want is to show different page view for users (login and not login user)
I am going to use this method with session, but please check if I do it correctly:
<?php
session_start();
if ( $_SESSION['login_id'] == 0 || $_SESSION['login_id'] == '' ) {
echo "user is not login, and I will show the not login page view to them";
} else {
echo "user is login, and I will show the login page view to them";
}
require_once('configPDO.php');
?>
If you stored the variable in $_SESSION in the right way, you can redirect the user in different pages using header after the login.
<?php
session_start();
if(isset($_SESSION['login_id']) && !empty($_SESSION['login_id'])){
header("location:PAGELOGIN.php");
}
else{
header("location:PAGENOTLOGIN.php");
}
?>
if you want to stay in the same page, you can do:
<?php
session_start();
if(isset($_SESSION['login_id']) && !empty($_SESSION['login_id']))
{
?>
YOUR HTML CODE
<?
} else {
?> YOUR HTML CODE
<?}
?>
this will show your html code using the php condition. tell me if this work ^^
try this...
<?php
session_start();
if(isset($_SESSION['user'])){
echo "iam login";
}
else{
echo "not login";
}
?>

File name as variable+.txt [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
session_start();
$user = $_SESSION['username'];
if( isset($_POST['subm_btn']) ) {
incrementClickCount();
}
function getClickCount()
{
return (int)file_get_contents($user.".txt");
}
function incrementClickCount()
{ $count = getClickCount() + 1;
file_put_contents($user.".txt", $count);
}
User register on my site, then he click on button (name="subm_btn"). I want count clicks and add number of clicks in file with name "username.txt"
I guess you are looking for something like this:
$file=$user.'.txt';
incrementClickCount($file);
function incrementClickCount($file){
$count = getClickCount($file) + 1;
file_put_contents($file, $count);
}
function getClickCount($file) {
return (int)file_get_contents($file);
}
If you want the variable to be available inside a function you either make it global or pass it as an argument (which is better).
You define $user, and then access $user1. That would be my guess as to why it doesn't work. Also, using $file might be a better idea anyway.

Trying to echo a variable ( $i) into another variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a special form I have been making that uses some cusotm post types in wordpress. At one point I need to echo a variable $i into an if statement.
There is some validation stuff at the top that will look like this and the code in the loop is below. Pretty much I have been trying to get the majorCause1Error to be majorCause $i Error if you know what I mean, so all up it will be like 1-13
Edit: Sorry If it is hard to see what I am asking, I am finding it really hard to word my problem.
So there is a loop running around the li tags and it echos $i into the name etc so it becomes majorCause1 then next one majorCause2 and the next one magjorCause3 etc etc
Under the labels there is an if statement that is like - if($majorCause1Error !='') { do something } - I want this to be like if($majorCause1Error !=''){} and then the next one be like if($majorCause2Error !=''){} and then if($majorCause3Error !=''){}
Does this make more sense?
Here is a link to the site http://www.foresightaus.com.au/form/
if(trim($_POST['majorCause1']) === '') {
$majorCause1Error = "Please enter a major cause.";
$hasError = true;
} else {
$majorCause1 = trim($_POST['majorCause1']);
}
if(trim($_POST['majorCause2']) === '') {
$majorCause2Error = "Please enter a major cause.";
$hasError = true;
} else {
$majorCause2 = trim($_POST['majorCause2']);
}
<li class="fill-in">
<label for="majorCause<?php echo($i); ?>"><?php echo($j); ?>. State one major cause:</label>
<input type="text" name="majorCause<?php echo($i); ?>" id="majorCause<?php echo($i); ?>" value=""/>
<?php if($majorCause1Error != '') { ?>
<span class="error"><?=$majorCause1Error;?></span>
<?php } ?>
</li>
You probably want to be using an array but what you are referencing is called a variable variable and is supported by PHP!
Something like this should do it
${"majorCause{$i}Error"}

Categories