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;
}
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'm trying to match results from this PHP code into this Bootstrap HTML structure.
What I'm able to achieve so far is printing items from database in 3-column layout, but I don't known how to combine this results with/ info my bootstrap html "structure/ table". I think I need to work with "id" and increment it after every "echo" (h4)? But I'm very new to PHP. Thank you for any practical advices.
PHP CODE:
// Run query on connection
$query = "SELECT
product.id,
product.name as product_name,
product.price,
product.type
FROM pizza_project.product
INNER JOIN pizza_project.product_type on product.type = product_type.id
where product.type in (1,2)
order by product.type ASC, product.price ASC";
$result = $con->query($query);
?>
<table>
<tr>
<?php
$split = 0;
$id = $row['id']; // just an idea I think I need to start with
if (mysqli_num_rows($result) > 0) {
foreach ($result as $row) {
echo '<td>' . $row['product_name'] . ' ' . $row['id'] . '</td>';
$split++;
if ($split%3 == 0) {
echo '</tr> </tr>';
}
}
}
?>
</tr>
</table>
<?php
$itemsInRow = 0;
foreach ($result as $row) {
if ($itemsInRow === 0)
echo "<div class=\"row text-center\">";
echo "<div class=\"col-md-4\">";
echo "<span class=\"fa-stack fa-4x\">";
echo "<img class=\"product-image\" src=\"" . $row['img'] . "\">";
echo "</span>";
echo "<h4 class=\"service-heading\">" . $row['headline'] . "</h4>";
echo "<p class=\"text-muted\">" . $row['text'] . "</p>";
echo "</div>";
if ($itemsInRow === 0)
echo "</div>";
if ($itemsInRow % 3 === 0) {
$itemsInRow = 0;
continue;
}
$itemsInRow++;
}
Is this what you are trying to achieve? I will improve my answers if its the right way. This is not tested.
I have the code below and am trying to get the next 25 results from my sql table to appear on page. However, whenever I click the next button, no information is displayed. I have my offset = ($page - 1) * $items_per_page......I'm struggling to figure this out as it seems so simple compared the other code I've written, but is proving to be very elusive to me....any assistance would be greatly appreciated. My primary issue is that the next link does not provide the next 25 results and I'm unable to determine why and how to correct.
echo "<h3 style='text-align:center;'>Welcome to the Exchange Portal, " . $row['name'] . "! </h3>";
$items_per_page = 25;
$sql_count = "SELECT pin, title, title2, email, phone FROM crown_acura";
$result_cnt = mysqli_query($conn, $sql_count);
if(false === $result_cnt) {
throw new Exception('Query failed with: ' . mysqli_error());
} else {
$row_count = mysqli_num_rows($result_cnt);
// free the result set as you don't need it anymore
//mysqli_free_result($result_cnt);
}
echo $row_count;
echo " ";
if (!isset($_GET['Page'])) {
$Page = 1;
} else {
$Page = $_GET['Page'];
}
echo $page;
echo " ";
$page_count = 0;
if (0 === $row_count) {
// maybe show some error since there is nothing in your table
} else {
// determine page_count
$page_count = (int)ceil($row_count / $items_per_page);
// double check that request page is in range
if($page > $page_count) {
// error to user, maybe set page to 1
$page = 1;
}
}
echo " ";
echo $page_count;
echo " ";
echo $items_per_page;
$offset = ($page-1)*$items_per_page;
//echo $paging_info;
//echo " ";
echo "<br />";
//Query for displaying results
$list_sql = "SELECT pin, title, title2, email, phone FROM crown_acura LIMIT $offset, $items_per_page";
$result_query = $conn->query($list_sql);
//Table for displaying query results
echo "<table class='verify'>";
echo "<tr >";
echo "<td><h3>Name</h3></td><td> </td><td><h3>E-mail</h3></td><td><h3>Phone</h3></td>";
echo "</tr>";
for($i = 1; $i<= $page_count; $i++) {
if ($result_query->num_rows > 0) {
// output data of each row
while($row3 = mysqli_fetch_array($result_query)) {
echo "<tr>";
echo "<td class='dltd2 dlcl'>" . $row3["title"] . "</td><td>" . $row3["title2"] . "</td><td><a href='mailto:" . $row3['email'] . "'>" . $row3["email"] . "</a> </td><td>" . $row3["phone"] . " </td>";
echo "</tr>";
}
} else {
echo "0 results";
}
}
echo "<tr></tr>";
$next_page = $page + 1;
$last_page = $page - 1;
if($paging_info['curr_page'] <= 1) {
echo "<tr>";
echo "<td></td><td colspan='2'><a class='loadlink' href='" . $_PHP_SELF . "'>Next 25</a></td><td></td>";
echo "</tr>";
} elseif ($paging_info['curr_page'] < $page_count) {
echo "<tr>";
echo "<td></td><td><a href='" . $_PHP_SELF . "?page=" . $last_page . "'>Prev 25</a></td><td><a href='" . $_PHP_SELF . "?page=" . $next_page . "'>Next 25</a></td><td></td>";
echo "</tr>";
} elseif ($paging_info['curr_page'] === $page_count) {
echo "<tr>";
echo "<td></td><td colspan='2'><a href='" . $_PHP_SELF . "?page=" . $last_page . "'>Prev 25</a></td><td></td>";
echo "</tr>";
}
echo "</table>";
}
}
}
Have you tried to run the rendered SQL.
Output to browser:
"SELECT pin, title, title2, email, phone FROM crown_acura LIMIT $offset, $items_per_page"
try this... and change $page for different values (2,3,...,etc)
<?php
$items_per_page = 25;
$sql_count = "SELECT pin, title, title2, email, phone FROM crown_acura";
$result_cnt = mysqli_query($conn, $sql_count);
if (false === $result_cnt) {
throw new Exception('Query failed with: ' . mysqli_error());
} else {
$row_count = mysqli_num_rows($result_cnt);
// free the result set as you don't need it anymore
//mysqli_free_result($result_cnt);
}
echo $row_count;
echo " ";
if (!isset($_GET['Page'])) {
$Page = 1;
} else {
$Page = $_GET['Page'];
}
echo $page;
echo " ";
$page_count = 0;
if (0 === $row_count) {
// maybe show some error since there is nothing in your table
} else {
// determine page_count
$page_count = (int)ceil($row_count / $items_per_page);
// double check that request page is in range
if ($page > $page_count) {
// error to user, maybe set page to 1
$page = 1;
}
}
echo " ";
echo $page_count;
echo " ";
echo $items_per_page;
$offset = ($page - 1) * $items_per_page;
//echo $paging_info;
//echo " ";
echo "<br />";
//Query for displaying results
$list_sql = "SELECT pin, title, title2, email, phone FROM crown_acura LIMIT $offset, $items_per_page";
$result_query = $conn->query($list_sql);
echo ("RESULTS: ".$result_query->num_rows());
?>
I have a mysql table in which contains video titles, video embed html, video description, and video thumbnails. I want it to output the first entry as;
$result = mysqli_query($con,"SELECT * FROM entries");
while($row = mysqli_fetch_array($result)) {
echo $row['title'];
echo $row['html'];
echo $row['desc'];
}
and after that, I would like it to output the next five entries as
$result = mysqli_query($con,"SELECT * FROM entries");
while($row = mysqli_fetch_array($result)) {
echo $row['title'];
echo "<a href='". $row['id'] . "'><img src='" . $row['thu'] . "'></a>";
}
however I have no idea how to do this. I am kind of a newb when it comes to mysql. Any help would be appreciated.
I have looked at other similar questions but none of them really fit the bill.
Poor man's solution would be something like:
$result = mysqli_query($con,"SELECT * FROM entries");
$first = true;
while($row = mysqli_fetch_array($result)) {
if ($first) {
echo $row['title'], $row['html'], $row['desc'];
$first = false;
continue 2;
}
echo $row['title'], "<a href='{$row[id]}'><img src='{$row[thu]}'></a>";
}
(As explained in PHP chatroom)
Since mysqli_fetch_array() gets you a row, you can use it before the while() without a problem like this
$result = mysqli_query($con,"SELECT * FROM entries");
$row = mysqli_fetch_array($result);
echo $row['title'];
//etc.
while($row = mysqli_fetch_array($result)) {
echo $row['title'];
echo "<a href='". $row['id'] . "'><img src='" . $row['thu'] . "'></a>";
}
Track it with a sentinel value
$i = 0;
while($row = mysqli_fetch_array($result)) {
if($i == 0){
//first iteration
echo $row['title'];
echo $row['html'];
echo $row['desc'];
}else{
echo $row['title'];
echo "<a href='". $row['id'] . "'><img src='" . $row['thu'] . "'></a>";
}
$i++;//increment value
}
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