this file is included in my index file, i want not more than 5 posts on page and under posts 1 2 3 4... and etc. (links to the next pages) and that it look like this index.php?page=2
Sorry for my bad grammar.
<?php
if(isset($_GET['post_edit'])) {
$p_id = $_GET['post_edit'];
$p_query = mysql_query("SELECT title, post FROM posts WHERE id='$p_id'");
$p_array = mysql_fetch_array($p_query);
$title = $p_array['title'];
$post = $p_array['post'];
}
?>
<?php
if(isset($_POST['edit'])){
$title_edit = $_POST['titleedit'];
$post_edit = $_POST['postedit'];
if(empty($title) or empty($post)){
echo "<p>Fields empty!</p>";
} else {
mysql_query("UPDATE posts SET title='$title_edit', post='$post_edit' WHERE id='$p_id'");
echo "Edit succesful!</br>";
header('location: index.php');
}
}
?>
<?php
if(isset($_GET['post_edit']) && !empty($_GET['post_edit'])){
include 'edit_post.php';
} else {
?>
<?php
$query = mysql_query("SELECT * FROM posts ORDER BY date DESC");
while($row = mysql_fetch_array($query)){
echo "<div class='poststitle'>";
echo "<div style='font-weight: bold;'>";
echo $row['title'];
echo "</div>";
echo "</div>";
echo "<div class='posts'>";
echo $row['post'];
echo "</br>";
echo "<hr>";
$user_name = mysql_query("SELECT username FROM users WHERE id = '".$row['user']."' ");
$user_name_array = mysql_fetch_array($user_name);
$post_id = $row['id'];
echo "Posted by: <b>";
echo $user_name_array['username'];
echo "</b> | ";
echo "Views: <b>";
echo $row['views'];
echo "</b> | ";
echo "Posted on: ";
echo "<b>";
echo $row['date'];
echo "</b><hr>";
echo '</div>';
if (loggedin()){
if($user_level == 1){
echo "<div class='postoptions'>";
echo "<a href='index.php?post_edit=$post_id'><img src='img/optionicons/edit.png' width='15' height='15' alt='edit' /></a>";
echo "<a href='del_post.php?del=$post_id'><img src='img/optionicons/cancel.png' width='15' height='15' alt='Delete' /></a>";
echo "</div>";
} else {
echo "";
}
}
}
}
?>
SELECT * FROM table_name LIMIT N, M
Where N - number of limited rows and M(offset) = N * (PAGE -1)
You should use the LIMIT clause in your MySQL query, see the docs
in order to calculate the numbe rof pages, you also need the total number of rows, this can be found using SQL_CALC_FOUND_ROWS, see the docs or this question
Related
I'm wanting to fill a database with news article information such as PostID, PostTitle, PostDescription, PostContent and so on - I tried writing some code to constantly echo out the data in the SQL query but it only works for the first row that the query picks up
$SQL = "SELECT * FROM posts"; // get all info from posts
$result = mysql_query($SQL);
$row = mysql_fetch_array($result);
if (mysql_numrows($result) > 0) {
$row = mysql_fetch_array($result);
$Title = $row["PostTitle"];
$Description = $row["PostDescription"];
$Date = $row["PostDate"];
$AuthorID = $row["AuthorID"];
echo "<div id='preview'>";
echo "<div class='group selection'>";
echo "<div class='col span_1_of_3'>";
echo "<div id='previewmedia'>";
echo "</div>";
echo "</div>";
echo "<div class='col span_2_of_3'>";
echo "<h1>".$Title."</h1>";
echo "<p class='preview'>".$Description."</p>";
echo "<a href='bloglayout.php' class='orange readmore'>Read More...</a>";
echo "</div>";
echo "</div>";
echo "</div>";
}
I then thought about doing a SELECT query to then count the number of ID's in the table and then set that as $ID and then give each variable a number and then +1 after each loop but realised that wouldn't work so I'm kinda stuck on how to loop out several rows automatically from my database.
Also, I know I shouldn't be using mysql_numrows and whatnot in PHP and I should move to mysqli or PDO but this is for something where I've been asked to use mysql specifically.
You're only fetching one row. Try looping through your results.
$SQL = "SELECT * FROM posts"; // get all info from posts
$result = mysql_query($SQL);
while( $row = mysql_fetch_array($result);)
{
$Title = $row["PostTitle"];
$Description = $row["PostDescription"];
$Date = $row["PostDate"];
$AuthorID = $row["AuthorID"];
echo "<div id='preview'>";
echo "<div class='group selection'>";
echo "<div class='col span_1_of_3'>";
echo "<div id='previewmedia'>";
echo "</div>";
echo "</div>";
echo "<div class='col span_2_of_3'>";
echo "<h1>".$Title."</h1>";
echo "<p class='preview'>".$Description."</p>";
echo "<a href='bloglayout.php' class='orange readmore'>Read More...</a>";
echo "</div>";
echo "</div>";
echo "</div>";
}
I have just created a pagination function for a travel company. There are two things I want to change that I can't figure out.
Each trip has a date. What i want to be able to do is stop the trip from displaying in the list one the day of the trip and all subsequent days. I have a rough idea, but that doesn't seem to work.
if (date($row['date']) getdate()) {
// echo trip info here
}
-2. After each of the trips in the list there is a seperator (is that the right word?) to separate each trip's info. On the last trip if the page I don't want that seperator. I have no clue how to do this :/
Thanks in advance for any help & advice.
<?php
if (isset($_GET["p"])) { $page = $_GET["p"]; } else { $page=1; };
$start_from = ($page-1) * 4;
$sql = "SELECT * FROM destinations ORDER BY date ASC LIMIT $start_from, 4";
$result = mysqli_query ($con,$sql);
while($row = mysqli_fetch_array($result))
{
echo "<div class='row'>";
echo "<div class='col-md-4 col-sm-4'>";
echo "<img src='" . $row ['img'] . "' alt='' class='img-responsive'>";
echo "</div>";
echo "<div class='col-md-8 col-sm-8'>";
echo "<h2><a href='" . $row ['url'] . "'>" . $row ['name'] . "</a></h2>";
echo "<ul class='blog-info'>";
echo "<li><i class='icon-calendar'></i> " . date("d/m/y", strtotime($row['date'])) . "</li>";
echo "</ul>";
echo $row ['description'];
echo "<a class='btn theme-btn' href='" . $row ['url'] . "'>View Details <i class='icon-angle-right'></i></a>";
echo "</div>";
echo "</div>";
echo "<hr class='blog-post-sep'>";
}
$sql = "SELECT COUNT(trip_id) FROM destinations";
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_row($result);
$total_records = $row[0];
$total_pages = ceil($total_records / 4);
echo "<div class='text-center'> <ul class='pagination pagination-centered'>";
for ($i=1; $i<=$total_pages; $i++) {
echo "<li><a href='routea.php?p=".$i."'>".$i."</a></li>";
};
echo "</ul></div>";
mysqli_close($con);
?>
Change your SQL to filter out the unwanted trips:
SELECT * FROM destinations
WHERE date < CURRENT_DATE()
ORDER BY date ASC
LIMIT $start_from, 4
You can solve it with CSS:
hr.blog-post-sep:last-of-type {
display: none;
}
I hope that title makes sense.... Basically I created PHP script to take data from a database and display in, I then wrote some code to use a drop down menu to order that data. That all worked ok until I tried to utilise some pagination. I can make the pagination work, and I can make the ordering work, but not at the same time! At the moment the code that I have will allow me to order the list and almost paginate it. There are 40 results and I want to display 10 at a time. When not using the ordering code I can paginate it perfectly but when I try to intergrate the two bits of code it will only display the first 10 results and not give me an option to go to the next page to see the rest! (please bear in mind that I only started learning 5 das ago so I am still getting to grips with it!
The code:
if (!isset($_GET['start']))
{
$_GET['start'] = 0;
}
$per_page = 10;
$start = $_GET['start'];
if (!$start)
$start = 0;
$sort = #$_POST['order'];
if (!empty($sort)) {
$get = mysql_query("SELECT bookname, bookauthor, bookpub, bookisbn
FROM booktable
ORDER BY ".mysql_real_escape_string($_POST['order'])." ASC
LIMIT $start, $per_page");
}
else {
$get = mysql_query("SELECT bookname, bookauthor, bookpub, bookisbn
FROM booktable
ORDER BY bookname ASC
LIMIT $start, $per_page");
}
$record_count = mysql_num_rows($get);
?>
<?php
if (isset($_GET['showerror']))
$errorcode = $_GET['showerror'];
else
$errorcode = 0;
?>
wont include all the html rubbish and the ordering menu!
<div id="mid">
<?php
echo "<table>";
echo "<tr>";
echo "<th>";
echo "</th>";
echo "<th>";
echo "Book Title";
echo "</th>";
echo "<th>";
echo "Book Author";
echo "</th>";
echo "<th>";
echo "Book Publisher";
echo "</th>";
echo "<th>";
echo "Book ISBN";
echo "</th>";
echo "<th>";
echo "</th>";
echo "</tr>";
while ($row = mysql_fetch_assoc($get))
{
// get data
$bookname = $row['bookname'];
$bookauthor = $row['bookauthor'];
$bookpub = $row['bookpub'];
$bookisbn = $row['bookisbn'];
echo "<tr>";
echo "<td>";
echo "<a href='addtolist.php?bookname=".$bookname."&bookauthor=".$bookauthor."&bookpub=".$bookpub."&bookisbn=".$bookisbn."'>Add to basket</a>";
echo "</td>";
echo "<td>";
echo $bookname;
echo "</td>";
echo "<td>";
echo $bookauthor;
echo "</td>";
echo "<td>";
echo $bookpub;
echo "</td>";
echo "<td>";
echo $bookisbn;
echo "</td>";
echo "</tr>";
}
echo "</table>";
$prev = $start - $per_page;
$next = $start + $per_page;
if (!($start<=0))
echo "<a href='products.php?start=$prev'>Prev</a> ";
//set variable for first page number
$i=1;
//show page numbers
for ($x = 0; $x < $record_count; $x = $x + $per_page)
{
if ($start != $x)
echo "<a class='pagin' href='products.php?start=$x'> $i </a>";
else
echo "<a class='pagin' href='products.php?start=$x'><b> $i </b></a>";
$i++;
}
//show next button
if (!($start >= $record_count - $per_page))
echo "<a class='pagin' href='products.php?start=$next'> Next </a>";
?>
Thank you so much for reading!
Simpliest way just change $sort = #$_POST['order']; to $sort = #$_REQUEST['order']; and add to your pagination links &order=$order
I have tried my hand at some pagination but it gives me very unexpected results....
The first is that it on the first page of the pagination it seems to override the CSS and align all divs to the right hand side of the page... when you then click 'next' to view the next three results and so on the page goes back as per the CSS tells it!
The second problem is that the table displaying the results doesn't seem to work. The very first result on each page of the pagination is in the table, the next two results (three results per page) are displayed on the page however below and not in the table!
The code is quite long but here we go:
<?php
session_start();
if ( !isset($_SESSION['username']))
{
header("Location:index.php");
exit();
}
//connect to database
require "dbconn.php";
$per_page = 3;
$start = $_GET['start'];
$sort = #$_POST['order'];
if (!empty($sort)) {
$query = "SELECT bookname, bookauthor, bookpub, bookisbn
FROM booktable
ORDER BY ".mysql_real_escape_string($_POST['order'])." ASC";
}
else {
$query = "SELECT bookname, bookauthor, bookpub, bookisbn
FROM booktable
ORDER BY bookname ASC";
}
$results = mysql_query($query)
or die (mysql_error());
$record_count = mysql_num_rows($results);
if (!$start)
$start = 0;
$get = mysql_query("SELECT * FROM booktable LIMIT $start, $per_page");
?>
<?php
if (isset($_GET['showerror']))
$errorcode = $_GET['showerror'];
else
$errorcode = 0;
?>
Then I will cut out all the unnecesary html
<div id="mid">
<?php
echo "<table border='2px'>";
echo "<tr>";
echo "<th>";
echo "</th>";
echo "<th>";
echo "Book Title";
echo "</th>";
echo "<th>";
echo "Book Author";
echo "</th>";
echo "<th>";
echo "Book Publisher";
echo "</th>";
echo "<th>";
echo "Book ISBN";
echo "</th>";
echo "<th>";
echo "</th>";
echo "</tr>";
while ($row = mysql_fetch_assoc($get))
{
// get data
$bookname = $row['bookname'];
$bookauthor = $row['bookauthor'];
$bookpub = $row['bookpub'];
$bookisbn = $row['bookisbn'];
echo "<tr>";
echo "<td>";
echo "<a href='addtolist.php?bookname=".$bookname."&bookauthor=".$bookauthor."&bookpub=".$bookpub."&bookisbn=".$bookisbn."'>Add to basket</a>";
echo "</td>";
echo "<td>";
echo $bookname;
echo "</td>";
echo "<td>";
echo $bookauthor;
echo "</td>";
echo "<td>";
echo $bookpub;
echo "</td>";
echo "<td>";
echo $bookisbn;
echo "</td>";
echo "</tr>";
echo "</table>";
}
$prev = $start - $per_page;
$next = $start + $per_page;
if (!($start<=0))
echo "<a href='products.php?start=$prev'>Prev</a> ";
//set variable for first page number
$i=1;
//show page numbers
for ($x = 0; $x < $record_count; $x = $x + $per_page)
{
if ($start != $x)
echo "<a href='products.php?start=$x'>$i</a>";
else
echo "<a href='products.php?start=$x'><b>$i</b></a>";
$i++;
}
//show next button
if (!($start >= $record_count - $per_page))
echo "<a href='products.php?start=$next'>Next</a>";
?>
<?php echo $record_count; ?>
This is how the page looks (image 1) (the table border shows the problem)
As I said before I also get the problem when the list first gets displayed and the page ends up looking like this: (image2)
You can see how they differ!
I hope that I have made sense!
For starters, you have your </table> tag in your while loop. Change that and see if everything else falls into place.
i try to store the booking booths into database based on user selection, there are 10 check boxes for each booths and user can choose which day they want to reserve booths. For each check box has it own field in database, if user choose booth A01, D1 and D2, when he press reserve button, it will insert value into D1 and D2. But I dont know how to get the checked checkbox value to store in database
my booth interface
http://i.imgur.com/umYcI.gif
my table structure
http://i.imgur.com/vKh6R.gif
My coding
<?php
session_start();
if ( !isset($_SESSION['AUTHORIZED_USERNAME']) || empty($_SESSION['AUTHORIZED_USERNAME']) ) {
header("location:index.php");
}else{
$user=$_SESSION['AUTHORIZED_USERNAME'];
}
include('db.php');
if($_REQUEST){
$id = $_REQUEST['search_category_id'];
$query2 = mysql_query("SELECT filenameBig, filename, url FROM eventinfo where eventID ='$id'");
$row = mysql_fetch_array($query2, MYSQL_ASSOC);
if($id == -1)
{
echo "<style type='text/css'>#btn_submit{visibility:hidden}</style>";
}
else{
/*echo "<a href='{$row['url']}'>Click me!</a>";*/
echo "<p><br><img src='{$row['filename']}' alt='' /></p>";
echo "<p></p>";
echo "<p align='right'><a href='$row[filenameBig]' target='_blank'>Click to view large image</a></p>";
echo "<hr size='1'>";
echo "<div style='padding-left:4px;' align='left'><strong>Booths Listing</strong>";
echo "<p></p>";
$query = "select boothAlias, totalDay from booths, eventinfo where booths.eventID=eventinfo.eventID && booths.eventID = ".$id."";
$_SESSION['EVENT_ID']=$id;
$result = mysql_query($query);
$result2= mysql_query($query);
echo "<table border='0' style='width:400px;table-layout:fixed' >";
$rows2 = mysql_fetch_array($result);
$Day=$rows2['totalDay'];
echo "<table>";
for ($day = 0; $day <= $Day; ++$day) {
if($day==0){
echo "<th>Booth</th>";
}else{
echo "<th>D".$day."</th>";
}
}
while($rows = mysql_fetch_array($result2)){
$boothAlias=$rows['boothAlias'];
$totalDay=$rows['totalDay'];
echo "<tr><td>$boothAlias</td>";
for ($day2 = 1; $day2 <= $totalDay; ++$day2) {
echo "<td><input name='day2[]' type='checkbox' value='$day2' /></td>";
}
echo "</tr>";
}
echo "</table>";
}
}
?>
I think that SET type would be good solution for this.
http://dev.mysql.com/doc/refman/5.0/en/set.html