Loading a PHP page into a HTML div - php

So I am currently trying to create a portion of my website that fetches a mySQL table. I wrote the php to fetch the data, create a table, and print out the table in a separate php file named display.php. Now i am trying to load it into a div on my html page but for some reason it is not working. I am using XAMPP and php is working as my login page works. Here is the code:
HTML: (I have tried both include and require)
<html>
<body>
<div id="display" class="myCustom1">
<?php require('display.php'); ?>
</div>
<div id="display" class="myCustom1">
<?php include("display.php"); ?>
</div>
</body>
</html>
PHP: (if i run just the php page it creates the table)
<?php
$servername = "localhost";
$username = "test";
$password = "test";
$database = "data1";
//create connection & access DB
$connect = mysqli_connect("$servername","$username","$password","$database");
//if error, display error
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($connect,"SELECT * FROM data1enter");
echo
"
<table border='1'>
<tr>
<th>Song</th>
<th>Author</th>
</tr>
";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['song'] . "</td>";
echo "<td>" . $row['author'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($connect);
?>

You are using <?php ?> tags in the html file, Change the extension of the file containing the below code to .php from .html and you will be able to use include function.
<html>
<body>
<div id="display" class="myCustom1">
<?php require('display.php'); ?>
</div>
<div id="display" class="myCustom1">
<?php include("display.php"); ?>
</div>
</body>
</html>
Also make sure the include path is correct.
Hope it helps !

Display should be blade template ..
#include('display')
Give path to display template

Just put the php in a function. Then you can do this:
<html>
<body>
<?php require_once('display.php'); ?>
<div id="display" class="myCustom1">
<?php render(); ?>
</div>
</body>
</html>
<?php
function render()
{
//YOUR PHP CODE
}
?>

Related

Display search result inside customized div tag using PHP and MySQL

I am working on a search page where I am getting the input from the user and then using PHP, I am connecting to a database, executing a query and trying to display the results on the page. I need to display the result in a div tag where I need various search results to be displayed.
<?php
ob_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test1"; // Database name
$tbl_name="postjob"; // Table name
// Create connection
$conn = new mysqli($host, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$dbconn = mysqli_select_db($conn, $db_name) or die(mysql_error());
echo $dbconn;
echo "Connected to Database<br />";
$output = '';
$keywords=$_POST['keywords'];
$location=$_POST['location'];
$keywords = mysqli_real_escape_string($conn, $keywords);
$location = mysqli_real_escape_string($conn, $location);
$sql="SELECT * FROM $tbl_name WHERE job_title LIKE '%$keywords%' OR job_type
LIKE '%$keywords%' OR job_category LIKE '%$keywords%' OR job_tags LIKE
'%$keywords%' OR description LIKE '%$keywords%' OR company_name LIKE
'%$keywords%' OR location LIKE'%$location%'";
$result=mysqli_query($conn, $sql);
$count=mysqli_num_rows($result);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Sign Up</title>
</head>
<body>
<section class="content_area">
<div class="banner">
<div class ="result">
<table>
<tr>
<?php
while($jobsearch=mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>"."<h3>".$jobsearch['job_title']."</h3>"."
<br>".$jobsearch['company_name']."<br>".$jobsearch['description']."</td>";
echo "</tr>";
}
?>
</tr>
</table>
</div>
</div>
</section>
</body>
</html>
I am calling the above PHP file using post action from HTML form page which has Keywords and location textbox. I am fetching job_title, company_name and description from Database and displaying it. Here I am using a table to display. But I wanted use a div where I need to do some styling. I tried to use below code replacing table with div, but it is not displaying any result.
<?php
while($jobsearch=mysqli_fetch_assoc($result)) {
echo "<div>"."<h3>".$jobsearch['job_title']."</h3>"."
<br>".$jobsearch['company_name']."<br>".$jobsearch['description'].
</div>";
}
?>
I am expecting result like below inside a div
job_title
company_name
description
<button>Apply</button>
I tried the below code, but it is not displaying any result.
<?php
while($jobsearch=mysqli_fetch_assoc($result)) {
echo "<div><h3>".$jobsearch['job_title']."</h3></div>
<div>".$jobsearch['company_name']."</div>
<div>".$jobsearch['description']. "</div>";
}
?>
I need to customize this div to do some styling. Could someone help me how to achieve this?
Hope it will work.
<ul>
<?php
while($jobsearch=mysqli_fetch_assoc($result)) {
echo "<li>"."<h3>".$jobsearch['job_title']."</h3>"."</li>"
echo "<li>"."<h3>".$jobsearch['company_name']."</h3>"."</li>"
echo "<li>"."<h3>".$jobsearch['description']."</h3>"."</li>"
echo "<button>Apply</button>";
}
?>
</ul>
your code should throw an error as " is missing in your echo statement
try this
echo "<div><h3>".$jobsearch['job_title']."</h3></div>
<div>".$jobsearch['company_name']."</div>
<div>".$jobsearch['description']. "</div>";
attached is the output, this output is by replacing your variables with some defined values
Try this.
<div class ="result">
<table>
<?php
while($jobsearch=mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>"."<h3>".$jobsearch['job_title']."</h3></td>";
echo "</tr>";
echo "<tr>";
echo "<td>"."<h3>".$jobsearch['company_name']."</h3></td>";
echo "</tr>";
echo "<tr>";
echo "<td>"."<h3>".$jobsearch['description']."</h3></td>";
echo "</tr>";
echo "<br />";
}
?>
</table>
</div>

Delete PDO OOP Problems

Good day everyone.
I totally need your help.I am pretty new to oop & pdo and I am having problems with my delete function. The item will not be delete whenever I click the delete button. It seems like I can't call the id. I just don't know what to do. Please help me solve this problem.
Here is my code.
For the class(codex):
public function deleteData($id,$table)
{
$q = "DELETE FROM $table WHERE id = :id";
$stmt = $this->con->prepare($q);
$stmt->execute(array(':id'=>$id));
}
For the UI and the page where I call the id:
<?php
include_once "../styles/header-menu-out.php";
include_once "dbconnection.php";
function __autoload($class){
include_once("../main/".$class.".php");}
$code = new codex("localhost","library_books","root","" );
$books = $code->showData("book_info");
if(isset($_REQUEST['id'])){
if($code->deleteData($_REQUEST['id'],"book_info")){
echo "<script type='text/javascript'>
alert('Book Information have been deleted.');
window.location='bookDeleteUI.php';
</script>";
}}
?>
<html>
<head><link rel="stylesheet" type="text/css" href="../styles/library_style.css"></head>
<title>Book-A-Holic: Delete & Update Books</title>
<body><br /><center>
<div id="content"><div class="echoname"><br/><br/><b><h2>Book Settings</h2></b><br/></div>
<table id="tablecolor" class="echoname" border="1">
<td>ID</td>
<td>Title</td>
<td>Author</td>
<td>ISBN</td>
<td>Publisher</td>
<td>Language</td>
<td>Genre</td>
<td>Quantity</td>
<td>Availability</td>
<td>Queue</td>
<td><center>Settings</center></td>
<?php
echo "<pre>";
foreach ($books as $book)
{
echo "<tr>";
extract($book);
echo "<td>".$id."</td>";
echo "<td>".$title."</td>";
echo "<td>".$author."</td>";
echo "<td>".$isbn."</td>";
echo "<td>".$publisher."</td>";
echo "<td>".$language."</td>";
echo "<td>".$genre."</td>";
echo "<td>".$quantity."</td>";
echo "<td>".$availability."</td>";
echo "<td>".$queue."</td>";
?>
<td><button class="btn"><a href="bookUpdateUI.php?update=$id" >Edit</a></button>
<button class="btn">Delete</button></td>
<?php echo "</td>";
}
echo "</pre>";
?>
</table><br />
</div></center>
</body>
</hmtl>
<?php include_once "../styles/footer-admin.php"; ?>
Looks like you've almost got it and well done on using PDO with parametized queries. The problem is that you're not outputting the $id through the php interpreter. You need to do this:
<button class="btn">Delete</button></td>

How do i access a php file in a search query?

Ok, so I'm making a somewhat simple search feature that connects to a MySQL server.
From my understanding the way to do this is to:
Make a search page on the site.
The search page will then accept a query and run it in the .php you provide
A result page will come out.
This is the basic stock code i will use:
<html>
<body>
<form action="searchtemplate.php" method="post">
Part ID: <input type="text" name="partID" />
<input type="submit" /></form>
</body>
</html>
I got this from the w3 school site.. it's simple and it works.
The php file i'm using is:
<?php
/**
Template Name: My Template
*/
get_header(); ?>
<div class="clearfix left-sidebar">
<!--BEGIN #primary-->
<div id="primary">
<?php the_post(); ?>
<?php $con=mysqli_connect("server","user","pass","database");
// Check connection
if (mysqli_connect_errno())
{
throw new Exception(mysqli_connect_error(), mysqli_connect_errno());
}
$part_query = $_POST['partID'];
$result = mysqli_query($con,"SELECT * FROM Parts WHERE part_id = $part_query");
echo "<table border='1'>
<tr>
<th>part_id</th>
then rest of columns
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['part_id'] . "</td>";
then echo rest of columns
echo "</tr>";
}
echo "</table>";
mysqli_close($con); ?>
<!--END #primary-->
</div>
<?php get_sidebar(); ?>
</div>
<?php get_footer();
?>
Ok, so I am using wordpress and I set this php up in the main theme directory and it can be accessed as a template. My main problem is that the search page appears fine but when I click submit it takes me to a blank page and not to the result page. I'd like to know if this is the correct way of doing what i am trying to accomplish or if there is a better way.
Your form is likely posting to the template, searchtemplate.php, and not a Wordpress page using the template to execute the code.
<form action="searchtemplate.php" method="post">
The "action" parameter in your form tag should point to the actual page on your site that is using your custom template.

PHP Combining search function with pagination

So I'm trying to make a page where i can display results from my database table. You should be able to search and there should be some pagination as there are thousands of results.
I've managed to make a page which just has the search, and works perfect. So now i need to know how would would integrate some pagination into that.
index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div id="container">
<form action="" method="get">
<input type="text" name="search" id="search" placeholder="Search" />
<input type="submit" />
</form>
<?php include 'process.php'; ?>
</div> <!-- /container -->
<script src="js/jquery.js"></script>
<script src="js/script.js"></script>
</body>
</html>
process.php
<?php include 'dbconfig.php'; ?>
<?php include 'connect.php'; ?>
<?php
$search = $_GET['search'];
$result = mysql_query("SELECT * FROM oantkb WHERE Name LIKE '%$search%' ORDER BY `INDEX` DESC");
echo '<table class="table">';
echo '<thead>';
echo '<tr>';
echo '<th>#</th>';
echo '<th>Pic</th>';
echo '<th>Name</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
while($row = mysql_fetch_assoc($result)) {
$pic = $row['Pic'];
$name = $row['Name'];
echo '<tr>';
echo '<td>#</td>';
echo '<td><img src="'.$pic.'" height="50" width 50"></td>';
echo '<td>'.$name.'</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
?>
Right now it works like it should. When i search it will say index.php?search=banana, but i need some pagination added so it will say for example index.php?search=banana&?page=2. Or something along those lines. Hope it makes sense...i'm a php newb :)
Include at the end of your sql query the following:
$resultsPerPage=10;
$page = ($_GET["page"]-1)*$resultsPerPage;
$query = $query." LIMIT $page,$resultsPerPage";
mysql_query($query);
By the way the mysql_ library is deprecated in favor of mysqli.
Also the above is susceptible to sql injection attacks because $_GET["page"] isn't first sanitized, but for simplicity I did it this way.
This assumes a paging scheme that starts at 1.
i've been using Pear Pagination for a long time. you can try it.
here is a good tutorial for setting it up
Simple Pagination in PHP tutorial
a good thing to add is clean your variable before using them in your query.

PHP HTML Template with Loop capabilities

I would like to ask some help and ideas on how to implement a loop inside the template. I can do foearch below but how can i include it to the template and show it in the results.
foreach($results as $row) {
$name = $row['name'];
$address = $row['address'];
}
What i want to achieve the results is something like below and how do I put the $template->publish(); in a variable so I can use it to store that data to the DB. thanks a lot.
<html>
<head>
<title>My Template Class</title>
</head>
<body>
<table><tr>
<td>
<h3>Hello William!</h3>
<p>The time is: 03/10/04</p>
<p>Embedded PHP works too!</p>
<p>Name goes here</p>
<p>Address goes here </p>
</td>
<td>
<h3>Hello William!</h3>
<p>The time is: 03/10/04</p>
<p>Embedded PHP works too!</p>
<p>Name goes here</p>
<p>Address goes here </p>
</td>
<td>
<h3>Hello William!</h3>
<p>The time is: 03/10/04</p>
<p>Embedded PHP works too!</p>
<p>Name goes here</p>
<p>Address goes here </p>
</td>
</tr>
</table>
</body>
</html>
The template class
<?
class Template {
public $template;
function load($filepath) {
$this->template = file_get_contents($filepath);
}
function replace($var, $content) {
$this->template = str_replace("#$var#", $content, $this->template);
}
function publish() {
eval("?>".$this->template."<?");
}
}
?>
The template design.html
<html>
<head>
<title>#title#</title>
</head>
<body>
<h3>Hello #name#!</h3>
<p>The time is: #datetime#</p>
<? echo "<p>Embedded PHP works too!</p>"; ?>
</body>
</html>
the index.php
<?
include "template.class.php";
$template = new Template;
$template->load("design.html");
$template->replace("title", "My Template Class");
$template->replace("name", "William");
$template->replace("datetime", date("m/d/y"));
$template->publish();
?>
PHP itself is as good at templates as any other engine.
No need anything else
$pagetitle = "My Template Class";
foreach($results as $row) {
$row['date'] = date("m/d/y");
$data[] = $row;
}
$data = chunk_split($data,3);
Then in template
<html>
<head>
<title><?=$pagetitle?></title>
</head>
<body>
<table>
<?php foreach ($data as $chunk): ?>
<tr>
<?php foreach ($chunk as $row): ?>
<td>
<h3>Hello <?=$name?>!</h3>
<p>The time is: <?=$date?></p>
<p>Embedded PHP works in the template</p>
<p><b>But embed PHP in the data is a VERY BAD IDEA</b></p>
<p><?=$address?></p>
</td>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</table>
</body>
</html>
I made your example a bit more complicated yet closer to the real life.
It will print your table in the rows by 3 columns in each
Just don't re-invent the wheel, PHP works wonderfully as a templating language:
The template class
<?
class Template
{
private $template;
private $vars;
function load($filepath) {
$this->template = $filepath;
}
function replace($var, $content)
{
$this->vars[$var] = $content;
}
function publish()
{
extract($this->vars);
include($this->template);
}
}
?>
The template design.phtml
<html>
<head>
<title><?php echo $title ?></title>
</head>
<body>
<?php foreach($rows as $row) { extract($row); ?>
<h3>Hello <?php echo $name; ?></h3>
<p>The time is: <?php echo $datetime; ?></p>
<?php echo "<p>Embedded PHP works too!</p>"; ?>
<?php } ?>
</body>
</html>
The use is pretty much the same, just assign more than one row to make use of it:
<?
include "template.class.php";
$template = new Template;
$template->load("design.phtml");
$template->replace("title", "My Template Class");
$rows = array();
$rows[] = array(
"name" => "William",
"datetime" => date("m/d/y"),
);
$template->replace("rows", $rows);
$template->publish();
?>
Hope this is helpful.
Your PHP code:
$htmldata ="";
($results as $row) {
$name = $row['name'];
$address = $row['address'];
$htmldata .="
<tr><td>
<h3>Hello William!</h3>
<p>The time is: 03/10/04</p>
<p>Embedded PHP works too!</p>
<p>".$name."</p>
<p>".$address." </p>
</td>
</tr>
";
}
Then in your template design.html, you will pass the $htmltable variable and embedd there:
<html>
<head>
<title>#title#</title>
</head>
<body>
<h3>Hello #name#!</h3>
<p>The time is: #datetime#</p>
<? echo "<p>Embedded PHP works too!</p>"; ?>
<table>
<?php echo $htmltable; ?>
</table>
</body>
</html>

Categories