I did one webpage with pagination in php. But i want to include some filter to search in this pagination. So if someone has good advice, will be good received.
Here is the file where i did the pagination. The name is, Index.php:
<?php
require_once 'database.php';
$database_connection = database_connect();
$title='hola';
$content='';
//user input
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$perPage = 2;
//Positioning
$start = ($page > 1) ? ($page * $perPage) - $perPage : 0;
$art = $database_connection->query("SELECT id FROM coffee");
//Query
$articles = $database_connection->prepare("SELECT id FROM coffee LIMIT $start,$perPage");
$articles->execute();
$articles = $articles->fetchAll();
$resultado = $database_connection->query("SELECT COUNT(*) AS total FROM coffee");
$fila = $resultado->fetch(PDO::FETCH_ASSOC);
$total = $fila["total"];
$pages = ceil($total/$perPage);
include 'Template_1.php';
?>
And here is my html website. Template:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><?php echo $title; ?></title>
<link rel="stylesheet" type="text/css" href="Styles/Stylesheet.css" />
</head>
<body>
<div id="wrapper">
<div id="banner">
</div>
<nav id="navigation">
<ul id="nav">
<li>Home</li>
<li>Coffee</li>
<li>Shop</li>
<li>About</li>
</ul>
</nav>
<div id="content_area">
<?php
require_once("tool.php");
foreach ($articles as $article):
echo $article['id'];?>
</br><?php
endforeach;?></br><?php
for($x=1;$x<=$pages;$x++):?>
<?php echo $x;?>
<?php endfor;?>
</br>
<?php echo $content; ?>
</div>
<div id="sidebar">
</div>
<footer>
<p>All rights reserved</p>
</footer>
</div>
</body>
</html>
I'm not sure if i know what you mean by "But i can't combine search with my pagination." But as i understand you want to add some kind of a 'where' statement in your query.
Here are examples of doing it and i think the last code from this site is what you want: http://www.webreference.com/programming/php/search/2.html
Related
The list of notes should be displayed within the ul li spans, any reason as to why they aren't showing and instead the array is showing at the top of the page?
The database connection appears to be working perfectly fine, however the notes aren't showing within the spans. It also removes the 'you haven't added any notes text'
code
<?php
require_once 'app/init.php';
$notesQuery = $db->prepare("
SELECT ID, note
FROM notes
");
$notesQuery->execute();
$notes = $notesQuery->rowCount() ? $notesQuery : [];
foreach($notes as $note) {
print_r($note);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Notes</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>
<h1>myNotes</h1>
<nav>
Home
About
Contact
</nav>
</header>
<div class="container">
<form action="add.php" method="post">
<textarea name="note" placeholder="Insert a note..." autocomplete="off" required></textarea>
<input type="submit" value="Add" />
</form>
<div class="notes">
<h2>Notes</h2>
<?php if(!empty($notes)): ?>
<ul>
<?php foreach($notes as $note): ?>
<li>
<span><?php echo $note['note']; ?></span>
</li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p>you haven't added any notes yet.</p>
<?php endif; ?>
</div>
Working Code
<?php
require_once 'app/init.php';
$notesQuery = $db->prepare("
SELECT ID, note
FROM notes
");
$notesQuery->execute();
$notes = $notesQuery->rowCount() ? $notesQuery : [];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Notes</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>
<h1>myNotes</h1>
<nav>
Home
About
Contact
</nav>
</header>
<div class="container">
<form action="add.php" method="post">
<textarea name="note" placeholder="Insert a note..." autocomplete="off" required></textarea>
<input type="submit" value="Add" />
</form>
<div class="notes">
<h2>Notes</h2>
<?php if(!empty($notes)): ?>
<ul>
<?php foreach($notes as $note): ?>
<li>
<span><?php echo $note['note']; ?></span>
</li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p>you haven't added any notes yet.</p>
<?php endif; ?>
</div>
</div>
</body>
</html>
Feel free to use below as an example for your query.
// Your sql query
$sql = "SELECT ID, note FROM notes";
// Prepare your statement
$stmt = $db -> prepare($sql);
// Execute your prepared statement
$stmt -> execute();
// Retreive all rows
$notes = $stmt -> fetchAll();
// Check if array is not empty
if (!empty($notes)) {
//Spit out the array
print_r($notes);
}
I am trying to figure out where I went wrong with my code.
I built my navbar with php in the unordered list of the navbar to retrieve the menu elements from an SQL table called "menu" from a database called "shop", with no avail.
I even have a print_r in place, only to show me that my resource is working, but for some reason the whole code isn't creating the menu items as written.
This is the code:
<?php
$menu = [];
mysql_connect('localhost', 'root', '');
mysql_select_db('shop');
$result= mysql_query("SELECT * FROM menu");
if ($result && mysql_num_rows($result)>0){
while($row = mysql_fetch_assoc($result)){
$menu[] = $row;
}
}
echo '<pre>';
$error= print_r($menu);
$error = '' ;
echo '</pre>';
?>
<!DOCTYPE html>
<html>
<head>
<link href="_CSS/style.css" rel="stylesheet" type="text/css"/>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div class="site-wrapper">
<div class="header">
<div class="navbar">
<ul>
<li>Home</li>
<?php if(count($menu) >0) : ?>
<?php foreach ($menu as $row) : ?>
<li><?php $row = ['link']; ?></li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
</div>
<div class="member">
<ul>
<li>Login</li>
<li>Register</li>
</ul>
</div>
</div>
</div>
The result unfortunately looks like this:
Your problem is this:
<?php $row = ['link']; ?>
It should probably be this:
<?php echo $row['link']; ?>
Maybe you got confused trying to do this:
<?= $row['link']?>
Mistake 1 :
You have used
$row = ['link'];
Where you will get this error Notice: Array to string conversion in ..
So you need to have $row['link'];
Mistake 2 :
You need to output the content using echo
<li><?php echo $row['link']; ?></li>
Warning : Don't use mysql_* functions as it is depreciated instead use mysqli or PDO for safer Operations !
I have my footer.php with admin button in it and when i include it at the end of my page the button doesn't appear.. But if i put it before this code:
<?php if ($row["rights"]=="2"):?>
<div id='cssmenu-admin'>
<ul>
<li name='admin' class='active'><a href='administration.php'><span>Admin</span></a></li>
</ul>
</div>
<?php endif; ?>
from footer.php before while cycle it works like it should.
This my footer.php file:
<div id="footer">
<hr>Copyrighted (C) 2014 by djdanas#gmail.com<br>
<?php if ($row["rights"]=="2"):?>
<div id='cssmenu-admin'>
<ul>
<li name='admin' class='active'><a href='administration.php'><span>Admin</span></a></li>
</ul>
</div>
<?php endif; ?>
</div>
<br><hr>
And this is my page file:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Pagrindinis</title>
<link href="CSS/stilius.css" rel="stylesheet" type="text/css"/>
<link href="CSS/menu.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<?php require("includes/validate.php");?>
<?php require("includes/stilius.php");?>
<?php
require("includes/connection_to_db.php");
$sql = "SELECT prekės.* , CONCAT(vartotojai.name) as v_name
FROM prekės
LEFT JOIN vartotojai
ON vartotojai.V_ID=prekės.V_ID
ORDER BY prekės.date
DESC LIMIT 8";
$result = mysql_query("$sql");
?>
<?php mysql_close(); ?>
<?php while ($row = mysql_fetch_assoc($result)) : ?>
<?php $image = '<td><img src="data:image/jpeg;base64,'.base64_encode($row['image']).'" name="pix" width="270" height="200"/></td>' ?>
<table class="two">
<th><?php echo $row['name'] ?></th>
<th>Prekės savininkas: <?php echo $row['v_name']?></th>
<th><input type="button" value="Mainyti"></th>
<tr>
<?php echo $image?>
<td><textarea disabled style="resize:none; background-color:white" name="about" rows="12" cols="65"><?php echo $row['specs']?></textarea><td>
</table>
<?php endwhile; ?>
<?php require("includes/footer.php");?>
</body>
</html>
EDIT:
Ok, i solved my problem by adding new variable
$rights = $row['rights'];
right after
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Pagrindinis</title>
<link href="CSS/stilius.css" rel="stylesheet" type="text/css"/>
<link href="CSS/menu.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<?php require("includes/validate.php");?>
<?php require("includes/stilius.php");?>
in my page file and changed 1 line in my footer.php file from
<?php if ($row['rights']=="2"):?>
to
<?php if ($rights="2"):?>
now it's working like a charm :)
I'll take a stab in the dark here and say that your test
if ($row["rights"]=="2")
means you are checking to see if the user logged in has the "rights" of 2?
Then when you loop through your query...
while ($row = mysql_fetch_assoc($result))
You are overwriting your $row variable. So whatever you thought was in there to begin with, isn't anymore. You should use a different variable for either your user or looping through the query.
EDIT:
It's also possible that the variable isn't in scope once you get to where you are testing it. You could try using a print_r($row) in footer.php to see what is currently inside of $row -- that should help.
I have been follwing a series of tutorials over at [thedigitalcraft.com][1] building my first ever dynamic website. I recently followed his 15th, and thought I followed along greatly, but for some reason the content that should be showing up isn't when I run the page. No errors, its almost like one of my sql statements is wrong, or it can't connect to my phpMyAdmin database.I'm building a user interface for editing each of the pages, a control panel. I'm running on XAMPP localhost btw, working in dreamweaver. Why is my content not showing up in the form? I know that I am connected to the database.. I've pasted my code from my pages.php and index.php
1. pages.php:
<?php ## Page Manager ?>
<h2>Page Manager</h2>
<div class="col sidebar">
<ul>
<?php
$q = "SELECT * FROM pages ORDER BY name ASC";
$r = mysqli_query($dbc, $q);
if ($r)
{
while($link = mysqli_fetch_assoc($r))
{
echo '<li>'.$link['name'].'</li>';
}
}
?>
</ul>
</div>
<div class="col editor">
<?php if (isset($_GET['id'])) {
$q = "SELECT * FROM pages WHERE id = '$_GET(id)' LIMIT 1";
// the database connection, our query
$r = mysqli_query($dbc, $q);
$opened = mysqli_fetch_assoc($r);
?>
<form action="#" method="post">
<p><label>Page title: </label><input type="text" size="30" name="title" value="<?php echo $opened['title']?>"></p>
<p><label>Page name:</label> <input type="text" size="30" name="name" value="<?php echo $opened['name']?>"></p>
<label>Page body:</label><br>
<textarea name="body" cols="30" rows="8"><?php echo $opened['body'] ?></textarea>
</form>
<?php } ?>
</div>
index.php:
<?php
error_reporting(0);
// Setup document:
include('config/setup.php');
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title><?php //echo $page_title; ?>JakeForDesign - Admin Panel</title>
<link rel="stylesheet" type="text/css" href="css/styles.css">
</head>
<body>
<div class="wrap_overall">
<div class="header"> <?php head(); ?> </div>
<div class="nav_main"> <?php nav_main(); ?> </div>
<div class="content"> <?php include('content/'.$pg.'.php'); ?> </div>
<div class="footer"> <?php footer(); ?> </div>
</div>
</body>
</html>
2. index.php:
<?php
error_reporting(0);
// Setup document:
include('config/setup.php');
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title><?php //echo $page_title; ?>JakeForDesign - Admin Panel</title>
<link rel="stylesheet" type="text/css" href="css/styles.css">
</head>
<body>
<div class="wrap_overall">
<div class="header"> <?php head(); ?> </div>
<div class="nav_main"> <?php nav_main(); ?> </div>
<div class="content"> <?php include('content/'.$pg.'.php'); ?> </div>
<div class="footer"> <?php footer(); ?> </div>
</div>
</body>
</html>
3. setup.php(for connection to database)
<?php
## Setup Document
// host(or location of the database), username, //password, database name
$dbc = #mysqli_connect('localhost', 'root', 'password', 'database') OR die ('Could not connect to the database because: '. mysqli_connect_error() );
include('Functions/sandbox.php');
include('Functions/template.php');
if ($_GET['page'] == '')
{
$pg = 'home';
}
else
{
$pg = $_GET['page'];
}
$page_title = get_page_title($dbc, $pg);
?>
$q = "SELECT * FROM pages WHERE id = '$_GET(id)' LIMIT 1";
This is wrong
$q = "SELECT * FROM pages WHERE id = '" . $_GET['id'] . "' LIMIT 1";
This is good
Don't forget to secure it with intval() if it's a numeric value!
I have a blog and I would like the tag in the head section of the HTML document to change dynamically using PHP. Here is my PHP code:
<?php
session_start();
if(isset($_SESSION['admin']) and !empty($_SESSION['admin'])){
echo 'Logout';
}
require('mysql2.php');
//Displaying the information from the DB to the user
//with pagination, i.e different pages
function post(){
$sp_query = mysql_query("SELECT COUNT(id) FROM posts");
$pages = ceil(mysql_result($sp_query, 0) / 1);
$page = (isset ($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * 1;
$query = mysql_query("SELECT id,title, article, datetime
FROM posts ORDER BY id DESC LIMIT $start, 1 ");
//Check if there are any rows in the DB
if(mysql_num_rows($query)){
while($result = mysql_fetch_assoc($query)){
//displaying the results
echo '<article class="blog-post">
<div class="blog-info">
'.stripslashes($result['title']).' ';
echo
stripslashes($result['datetime']).'<div>';
echo
nl2br(stripslashes($result['article'])).'</article>';
$title[] = $result['title'];
$id =
mysql_real_escape_string((int)$result['id']);
echo '<input type="hidden"
value="'.$result['id'].'" />';
$_SESSION['post_id'] = $id;
//If the admin is logged in, session variables
//for editing and deleting a post must be set
//and the conforming links should be displayed
if(isset($_SESSION['admin']) and
!empty($_SESSION['admin'])){
//$_SESSION['post_id'] = $id;
$_SESSION['edit_post'] = $id;
echo '<article class="blog-post">';
echo '<a href="delete.php"
id="delete" onclick="return del();">Delete this post</a>';
echo '<br />Edit this post';
echo '</article>';
}
}
}
//The drop down menu
function options(){
$new_query = mysql_query("SELECT title FROM posts
ORDER BY id DESC");
$num = 1;
while($array = mysql_fetch_assoc($new_query)){
echo '<option
value="blog.php?page='.$num++.'">'.$array['title'].'</a></option>';
}
}
?>
And here is the HTML:
<?php require('mysql2.php');
require('blog_process.php');
?>
<!DOCTYPE html>
<html>
<head>
<!--Meta Data-->
<meta charset="utf-8">
<meta name="description" content="About Chris Shilts">
<meta name="author" content="Chris Shilts">
<meta name="keywords" content="chris, shilts">
<meta name="viewport" content="width=device-width, initial-scale=1,
maximum-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=9" />
<!--css-->
<link rel="stylesheet" href="reset.css">
<link rel="stylesheet" href="style.css">
<!-- Javascript -->
<script type="text/javascript" src="delete.js"></script>
<!-- Favicons-->
<link rel="shortcut icon" href="favicon.ico">
<!--Title-->
<title id="title"><?php //php code should go here?></title>
</head>
<body>
<!--Contains all content-->
<div id="container">
<!--Content at start of page, generally the same-->
<header>
<div id="logo">
Hello There!
</div>
<!--Primary Navigation-->
<nav>
Link
Link
Link
</nav>
</header>
<!--Blog Posts-->
<?php post();?>
<!-- The navigation bar for the blog posts -->
<select onclick="navigation();" id="select">
<option value="" selected="selected"></option>
<?php options(); ?>
</select>
<!--First Footer-->
<footer id="footer-one">
Site Design By Chris Shilts | Programming by
Stefany Dyulgerova
</footer>
<!--Second Footer-->
<footer id="footer-two">
<a href="http://www.w3.org/html/logo/">
<img id="html5" src="http://www.w3.org/html/logo/badge
with CSS3 / Styling">
</a>
</footer>
<!--/container-->
</div>
</body>
</html>
I have also have a mysql database and here are the fields there:
id title article dateime
Please help me!
I tried to put the $result['title'] into a an array like this
$title[] = $result['title'];
And then to loop it through in the element based on the based on the id in the $_SESSION['post_id'] but the problem is that I have to reload the page twice in order the title to take effect.
Trying to save the blog title in an array isnt't work for you because the $title variable is only accessible within the post function.
You could have post return the $title array...
//initialize the array at the beginning of the post function like so
$title = array();
//return the array at the end of the function like so
return $title;
Then in the page, you'd replace
<?php post();?>
With
<?php $title = post();?>
However, You need $title BEFORE you the call to post. So that won't really help you.
I would suggest...
1) Create a Class Object with a global title variable. Have an initialize function to handle the database call. Then make post a function on the class that prints out the page HTML.
2) Create a title function that returns the correct page title and call it like so..
<title id="title"><?php get_page_title(); ?></title>
Update:
In your php file add the following function after the options function:
public function get_page_title() {
//add code to get the page title
// not sure what title you want since you are showing multiple posts on one page
}
Here is the code snippit that needs to be fixed : <title id="title"><?php echo $result['title']; ?></title>
This will work if there is one title, otherwise take the first : <title id="title"><?php echo $result['title'][0]; ?></title>