I got all my archived entries to display like so,
2015 Entries
March [2]
April [10]
November [6]
2004 Entries
January [5]
February [7]
December [19]
Now I'm stuck and I've been trying to figure out how to display archived entries by selecting a specific month and year. Let's say I wanted to view on my all entries during November 2015, then my link on the address bar would look something like...
display.php?year=2015&monthname=November
When I executed the code, the entries shows up as blank. So far, my code looks like this. I also used the type datetime as my date field. Help is appreciated.
$year = strip_tags(trim($_GET['year']));
$monthname = strip_tags(trim($_GET['monthname']));
$q = "select * from blog where year(date)='$year' and monthname(date) ='$monthname' order by date desc ";
$result= mysql_query($q, $connection) or die
("Could not execute query : $q." . mysql_error());
// dynamic navigation variables
$rows_per_page=1; // adjust the number here to display number of entries per page
$total_records=mysql_num_rows($result);
$pages = ceil($total_records / $rows_per_page);
$screen = $_GET["screen"];
if (!isset($screen))
$screen=0;
$start = $screen * $rows_per_page;
$q .= "LIMIT $start, $rows_per_page";
$result= mysql_query($q, $connection) or die
("Could not execute query : $q." . mysql_error());
while ($row=mysql_fetch_array($result))
{
$id=$row["id"];
$name=$row["name"];
$email=$row["email"];
$entry=$row["entry"];
$date=$row["date"];
$icon=$row["icon"];
$title=$row["title"];
?>
<table width="80%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><?php echo "$title"; ?></td>
</tr>
<tr>
<td>
<p><img src="<?php echo "$icon"; ?>" alt="icon" align="left"><?php echo "$entry"; ?></p>
<p>Posted by <a href="mailto:<?php echo "$email"; ?>"><?php echo "$name"; ?> on <?php echo "$date"; ?>.</p>
</td>
</tr>
</table>
<div align="center">
<?php
} #end of while
// Display dynamic navigation here
// create the dynamic links
if ($screen > 0) {
$j = $screen - 1;
$url = "display.php?year=$year&monthname=$monthname&screen=$j";
echo "Prev";
}
// page numbering links now
for ($i = 0; $i < $pages; $i++) {
$url = "display.php?year=$year&monthname=$monthname&screen=" . $i;
$j = $i + 1;
echo " | $j | ";
}
if ($screen < $pages-1) {
$j = $screen + 1;
$url = "display.php?year=$year&monthname=$monthname&screen=$j";
echo "Next";
}
?>
</div>
I forgot to put the word monthname on my address bar and accidentally left it as display.php?year=2015&month=November No code needed to be altered.
Related
Good day, I have an HTML table and I use paging on it so that only a certain amount of items is shown. The problem is that I need to have multiple selections with checkboxes and that works for a single page but I need that to work between pages. So for example on page 1 you choose 3 items and in the next page you choose 5 items and when GET happens I need to have all those items in one place so that I can store them in a variable.
<?php
include("connect.php"); //database connection file
$limit = 7;
if ( isset($_GET['page']) ) {
$page_no = $_GET['page'];
} else {
$page_no = 1;
}
$start_from = ($page_no-1)*$limit;
$sql = "SELECT * FROM emp_info LIMIT $start_from,$limit ";
$result = mysqli_query($conn , $sql);
?>
<form method="GET" action="project.php?name=<?php echo
$data['name']; ?>">
<div class="container">
<h2>employee information:</h2>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>EmpId</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php
$info = "SELECT * FROM emp_info LIMIT $start_from,$limit ";
//query to select the data from database
$query = mysqli_query ($conn , $info);
while ( $data = mysqli_fetch_assoc ($query) )
{ //query to fetch the data
$_SESSION['emp_name']=$data['name'];
?> <tr>
<td><?php echo $data['emp_id'];?></td>
<td>
<a href="project.php?id=<?php echo $data['emp_id'];?>&name=<?php echo $data['name']; ?>">
<input type="checkbox" name="check_list[]" value="<?php echo $data['name'];?>">
</a> <?php echo $data['name'];?>
</td>
<td><?php echo $data['email'];?></td>
</tr>
<?php }
?>
</tbody>
</table>
<ul class="pagination">
<?php
$sql = "SELECT COUNT(*) FROM emp_info";
$result = mysqli_query($conn , $sql);
$row = mysqli_fetch_row($result);
$total_records = $row[0];
// Number of pages required.
$total_pages = ceil($total_records /
$limit);
$pagLink = "";
for ( $i = 1; $i <= $total_pages; $i++) {
if ( $i == $page_no) {
$pagLink .= "<p>Pages:</p><li class='active'><a href='datatable.php?id=" . $data['emp_id'] .
"&page=" . $i ."'>". $i ."</a></li>";
} else {
$pagLink .= "<li><a href='datatable.php?page=". $i ."'>". $i ."</a></li>";
}
};
echo $pagLink;
?>
</ul>
</div>
<button type="submit" formaction="project.php"
name="select_proj">Select Project</button>
<button type="submit"
formaction="addnewproj.php" name="add_proj">Add New
Project</button>
</form>
</body>
</html>
I recommend reseaching abit of Javascript and more specificly aJax to solve this issue.
You need somewhere to store the information that has been selected in order to use it somewhere else.
I added pagination to a table a I created a while back and I have not ever been able to get it to work correctly since.
The table limit works, but that's it. If I select "First, Last or the page number" it just reloads the page, but the new page does not display.
If I set the page limit to a low number like 5 and select 'Last Page', when the page loads it shows =1 like it doesn't know there are other pages.
<?php
$con = mysqli_connect("localhost","root","","bfb");
$per_page=20;
if(isset($_POST["page"])) {
$page = $_POST["page"];
}
else {
$page = 1;
}
//Page will start from 0 and multiply per page
$start_from = ($page-1)*$per_page;
//Selecting the data from the table but with limit
$query = "SELECT * FROM orders LIMIT $start_from, $per_page";
$result = mysqli_query($con, $query);
?>
<table class="tableproduct">
<tr>
<th class="thproduct">Order ID</th>
<th class="thproduct">Customer Name</th>
<th class="thproduct">Product</th>
<th class="thproduct">Total Price</th>
<th class="thproduct"></th>
<th class="thproduct"></th>
</tr>
<?php
if( $result ){
while($row = mysqli_fetch_assoc($result)) :
?>
<form method="POST" action="orderhistory.php">
<tr>
<td class="tdproduct"><?php echo $row['order_id']; ?> </td>
<td class="tdproduct"><?php echo $row['customer_name']; ?> </td>
<td class="tdproduct"><?php echo $row['product_name']; ?> </td>
<td class="tdproduct"><?php echo $row['total_price']; ?> </td>
<td class="tdproduct"><a href='editorderhistory.php?id=<?php echo $row['id']; ?>'>EDIT</a></td>
<input type="hidden" name="product_id" value="<? echo $row['id']; ?>"/>
<td class="tdproduct"><input name="delete" type="submit" value="DELETE "/></td>
</tr>
</form>
<?php
endwhile;
}
?>
</table>
<?php
//Count the total records
if ($result != false) {
$total_records = mysqli_num_rows($result);
if ($total_records == 0) {
echo 'No results founds.';
}
}
//Using ceil function to divide the total records on per page
$total_pages = ceil($total_records /$per_page);
?>
<span class="spandarkblue">
<?php
//Going to first page
echo "<center><a href='orderhistory.php?page=1'>".'First Page'."</a> ";
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='orderhistory.php?page=".$i."'>".$i."</a> ";
};
// Going to last page
echo "<a href='orderhistory.php?page=$total_pages'>".'Last Page'."</a></center>";
?>
Any ideas?
Found below issues in you code.
1) You are trying to get page value from URL using POST, where as you need to GET method to fetch values from URl. Using POST is returning null value, so $page value is always set to 1
So use $_GET["page"] instead of $_POST["page"]
2) You are preparing pagination by considering row count of the query which you are executing. But as you have added limits , your $total_records is always equals to $per_page value or less, resulting in only one page number.
Use the following code for generate pazination
$query1 = "SELECT count(*) as totalRecords FROM orders";
$result1 = mysqli_query($con, $query1);
if ($result1) {
$row1 = mysqli_fetch_assoc($result1);
$total_records = $row1['totalRecords'];
if ($total_records == 0) {
echo 'No results founds.';
}
}
Instead of below code
if ($result != false) {
$total_records = mysqli_num_rows($result);
if ($total_records == 0) {
echo 'No results founds.';
}
}
Hope it helps you.
You need to fetch all the data to know the total of your records, then you show only the desired data whithin a for() loop, so remove LIMIT $start, $per_page from your query, in this node you will always have 20 or less results
In your while() save the data in arrays like this:
$index = 0;
while($row = mysqli_fetch_assoc($result)) {
$ordID[$index] = $row["order_id"];
// The rest of your data...
$index++; // Increment the index
}
Then you chan show only the desired data:
for($i = (($page-1)*$perPage); $i < min(($page*$perPage), $total); $i++) {
echo $orderID[$i];
// And so on...
}
The min() function returns the lowest value between two vars, in this way in the last page, if you have 17 products instead of 20 you will not have errors.
I've come across a problem but any help would be appreciated.
When I query the database using the results posted from a form, the pagination works initially i.e. for the first 10 records but when I click on the 2 hyperlink of the pagination for the second page of results it loses the $_POST variable and returns to the full data set.
What is the best way of keeping these variables available for the second (and further) pages?
The below is my complete php file.
<html>
<head>
<link rel="stylesheet" type="text/css"
href="design.css">
</head>
<body>
<?php
include("header.php");
?>
<center>
<div id="content" class="frm">
<a href='admin.php' style='float:left'>Back!</a>
<h2>Search Result</h2>
<br><br>
<?php
include("../config.inc");
$find=$_GET['find'];
// get page no and set it to page variable, if no page is selected so asign first page bydefualt
if (isset($_GET["page"])){
$page = $_GET["page"];
}
else {
$page=1;
}
// count all record in this table then divide it on 10 in order to find the last page----- every page has 10 record display
$sql = "SELECT COUNT(*) FROM tt where TTT='$find' ";
$rs_result = mysql_query($sql);
$row = mysql_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / 2);
// this line check that page no must be in integer format
$page = (int)$page;
if ($page > $total_pages) {
$page = $total_pages;
} // if
if ($page < 1) {
$page= 1;
} // if
$start_from = ($page-1) * 2;
$q=mysql_query("select * from tt where TTT='$find' order by ID limit $start_from,2");
$c=mysql_query("select count(*) from tt where TTT='$find'");
echo "<center>".mysql_result($c,0)."Filtered</center>";
echo "<center>";
echo "<table border='2' bgcolor=#CCCCCC>
<tr>
<th>TTT</th>
<th>Enroll Date</th>
<th>Gradution Date</th>
<th>ID</th>
</tr>";
while($row=mysql_fetch_array($q))
{
echo "<tr>";
echo "<td>".$row['TTT']."</td>";
echo "<td>".$row['Enroll_Date']."</td>";
echo "<td>".$row['Graduation_Date']."</td>";
echo "<td>".$row['ID']."</td>";
}
echo "</table>";
echo "<center>";
// paginatio start here
if ($page== 1) {
echo " << < ";
} else {
echo " <a href='{$_SERVER['PHP_SELF']}?page=1'><<</a> ";
$prevpage = $page-1;
echo " <a href='{$_SERVER['PHP_SELF']}?page=$prevpage'><</a> ";
} // if
echo " ( Page [$page] of [$total_pages] ) ";
if ($page == $total_pages) {
echo " > >> ";
} else {
$nextpage = $page+1;
echo " <a href='{$_SERVER['PHP_SELF']}?page=$nextpage'>></a> ";
$lastpage=$total_pages;
echo " <a href='{$_SERVER['PHP_SELF']}?page=$lastpage'>>></a> ";
} // if
?>
</div>
</center>
<?php
include("footer.php");
?>
</body>
</html>
You have to pass the filtering criteria along with links to next pages.
echo " <a href='{$_SERVER['PHP_SELF']}?page=1&find=$find'><<</a> ";
and so on with every other link.
I am having trouble with getting my year to echo properly.
Example: I have 2 news articles one with a year of 2011 and the other 2012. They are splitting into the correctly "year" div. But when it comes to rendering out the year, it works. But 2011 and 2012 are stacked above each other. Something like this.
2012
2011
5.1.12 My news article
Where 2011 year is suppose to go
5.1.11 My news article
Here is my code.
<?
$startYear = 2010;
$endYear = 2012;
for($y = $endYear; $y > $startYear -1; $y--) { ?>
<?
$news = query("SELECT * FROM news_entries
WHERE title > ''
AND published = 1
AND date >= '" . mktime(0,0,0,0,0,$y) . "'
AND date < '" . mktime(0,0,0,0,0,$y+1) . "'
ORDER BY date DESC");
?>
The rest of the query.
<? if($news['total']>0) { ?>
<h3><? echo $y; ?></h3>
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<th class="first" width="125">Date</th>
<th>News Headline</th>
<th width="140"> </th>
</tr>
<? do { ?>
<tr>
<td><span><? echo date("F d, Y",$news['data']['date']); ?></span></td>
<td><? echo $news['data']['headline']; ?></td>
<td>Learn More</td>
</tr>
<? } while($news['data'] = mysql_fetch_assoc($news['object'])); ?>
<? }} ?>
<tr>
<td colspan="2" class="last"></td>
<td class="last"> Back to Top</td>
</tr>
</table>
I agree with Kato's comment. Typically, you get all of the results and iterate over them, printing a new header when the data changes.
Here's how this type of thing is usually handled (pseudocode):
$sql = "SELECT * FROM news_enteries ORDER BY date DESC";
// Prepare statement
$stmt = $pdo->prepare($sql);
// Bind variables
...
// Execute query
$stmt->execute() or die();
$year_header = "";
while ($row = $stmt->fetch()) {
$year = date("Y", strtotime($row['date']));
// Do we need a new year header?
if ($year_header <> $year) {
$year_header = $year;
// Print year header
echo $year_header;
}
// Process rest of the row
...
}
I have a PHP/MySQL News system which displayes the newest news article on the home page and a full list on a news page.
The newest article bit works but, my problem is that whenever i try to echo all the news article on the news page it either repeats the same or outputs one and nothing else.
**MySQL Information**
id INT AUTO_INCREMENT,
author VARCHAR(xxx),
title VARCHAR(xxx),
message TEXT,
date TEXT,
time TEXT,
PRIMARY KEY(id)
This is the insertion page (news_center.php)
<form action='/?module=admin&n=news_center_ac' method='post'>
<table align="center" width="68%">
<tr>
<td>Title</td>
<td><input style="width:100%;" type='text' name='news_title' /></td>
</tr>
<tr>
<td height="57">Message</td>
<td><input style="width:100%; height:100%;" type='text' name='news_message' /></td>
</tr>
<tr>
<td colspan='2'><input type='submit' /></td>
</tr>
</table>
This is news_center_ac.php
<?php
$conn = mysql_connect(*Connection Information*) or die(mysql_error());
$db = mysql_select_db( "db372357229")or die(mysql_error());
$author == $_SESSION['name'];
if(!empty($_POST['news_title']) || !empty($_POST['news_message']))
{
if(!empty($_POST['news_title']) && !empty($_POST['news_message']))
{
$date = date( 'jS F Y' );
$time = date( 'H:i' );
$query = "INSERT INTO news (id, author, title, content, date, time) VALUES('', '".$author."', '".$_POST['news_title']."', '".$_POST['news_message']."', '".$date."', '".$time."')" or die(mysql_error());
$insert = mysql_query($query) or die(mysql_error());
echo '<p>Successful News Update “'.$_POST['news_title'].'”';
}
else
{
echo '<p>Please fill in all fields</p>';
}
}
?>
This is the Output on the news page (/news/index.php)
<?php
$conn = mysql_connect("*CONNECTION INFORMATION*") or die(mysql_error());
$db = mysql_select_db( "db372357229")or die(mysql_error());
$news = mysql_query("SELECT * FROM news ORDER BY date DESC,id DESC");
$output = mysql_fetch_array($news);
?>
*CONTENT*
<?php
foreach ($output as $value) {
echo "<p> “" .$output['content'];
echo "”";
echo "Posted:" .$output['date'];
echo " " .$output['time'];
}
?>
I just want it to output each news article in turn i can sort out the formatting later once it works.
You are misusing mysql_fetch_array(). It needs to be called in a loop, as it only returns one row at a time.
$conn = mysql_connect("*CONNECTION INFORMATION*") or die(mysql_error());
$db = mysql_select_db( "db372357229")or die(mysql_error());
$news = mysql_query("SELECT * FROM news ORDER BY date DESC,id DESC");
EDIT Added htmlentities() calls to convert html special characters
while ($row = mysql_fetch_array($news)) {
echo "<p> “" . htmlentities($row['content']);
echo "”";
echo "Posted:" . htmlentities($row['date']);
echo " " . htmlentities($row['time']);
}
Rewrite it this way:
<?php
while($output = mysql_fetch_array($news)) {
echo "<p> “" .$output['content'];
echo "”";
echo "Posted:" .$output['date'];
echo " " .$output['time'];
}
?>
When you call output first, it is only returning one value, this will loop through all.
try /news/index.php
<?php
$conn = mysql_connect("*CONNECTION INFORMATION*") or die(mysql_error());
$db = mysql_select_db( "db372357229")or die(mysql_error());
$news = mysql_query("SELECT * FROM news ORDER BY date DESC,id DESC");
while($output = mysql_fetch_assoc($news)) {
echo "<p> “" .$output['content'];
echo "”";
echo "Posted:" .$output['date'];
echo " " .$output['time'];
}
?>
Shouldn't the last foreach loop use $value, ie
<?php
foreach ($output as $value) {
echo "<p> “" .$value['content'];
echo "”";
echo "Posted:" .$value['date'];
echo " " .$value['time'];
}
?>