PHP dynamic previous/next links - php

I have a table called "product".
I'm displaying all the data from PHP product view page by using the id parameter.
With my code:
if(isset($_GET['id'])) {
$id = $_GET['id'];
}
$query = "SELECT * FROM product WHERE id = $id ";
$select_product = $db->query($query);
while($row = $db->fetch_object($select_product)) {
$status = $row->status;
if($status == 'published') {
$title = $row->title;
echo $title;
}
}
I can pull all the data I need. In this example I can echo the title.
But how do I display a dynamic next page link? That goes same for previous link.
When I try with this code:
$query = "SELECT id FROM product";
$select_ids = $db->query($query);
while($row = $db->fetch_object($select_ids)) {
$ids = $row->id;
echo $ids;
}
I can see all the pages from that table. In my case they are 20,21,22...28
Next, I've tried like this:
$query = "SELECT id from product";
$select_ids = $db->query($query);
if($select_ids->num_rows > 0) {
$next = $id + 1;
echo 'Next';
} else {
}
With this code, my "next" link successfully goes to the next page. But, when it finishes with the result, in this case when I'm on page 28, the next dynamic link goes to empty page (29). Also this is not a good approach, because if some page, let's say 25 is deleted, the next link won't skip that page.
How can I improve that dynamic "next" link?

So first of all, you need two queries one for getting the data to display and another for counting the number of rows, also we'll need some additional variables.
Here comes the code:
$page = 1;
if(isset($_GET['page'])) {
$page = $_GET['page'];
}
$numberOfResultsPerPage = 10;
$offset = $numberOfResultsPerPage * ($page - 1)
$count = $db->query("SELECT COUNT(id) as num FROM product");
while($row = $db->fetch_object($count)) {
$numberOfRows = $row->num;
}
$query = "SELECT id FROM product LIMIT ". $offset.", ".$numberOfResultsPerPage;
$select_ids = $db->query($query);
while($row = $db->fetch_object($select_ids)) {
$ids = $row->id;
echo $ids;
}
if($page > 1) {
echo 'Prev';
}
if($page < ceil($numberOfRows / $numberOfResultsPerPage)) {
echo 'Next';
}
UPDATE:
In case you need to just find the next and previous product of the product this is the solution. It is not good if you'll have lots of products but in case the number of products is under 1000 there should not be any problems:
if(isset($_GET['id'])) {
$id = $_GET['id'];
}
$query = "SELECT id from product";
$select_ids = $db->query($query);
$ids = array();
$i = 0;
while($row = $db->fetch_object($select_ids)) {
$ids[$i] = $row->id;
if($row->id == $id) {
$index = $i;
}
$i++;
}
if(isset($ids[$index-1])) {
echo 'Prev';
}
if(isset($ids[$index+1])) {
echo 'Next';
}

Related

Multiple Loops running together

$a_forms = array("a_GG", "a_FF");
$sql = "SELECT name, field FROM categories WHERE enabled = '1' ";
$result = mysqli_query($con,$sql);
$Others = array();
while($row = mysqli_fetch_array($result)) {
$Other_names[] = $row['name'];
$Other_fields[] = $row['db_field'];
}
for ($i=0;$i<count($a_forms);$i++) {
if ($a_forms[$i] == "a_FF") {
$dforms_sql = "SELECT *
FROM a_FF
where id=".$id;
$dforms_result = mysqli_query($con,$dforms_sql);
while ($forms_row = mysqli_fetch_array($dforms_result)) {
for ($i=0; $i<count($Other_fields); $i++) {
echo "<tr><td colspan='3'>".$Other_names[$i]."</td></tr>";
}
}
} elseif ($a_forms[$i] == "a_GG") {
$dforms_sql = "SELECT *
FROM a_GG where id=".$id;
$dforms_result = mysqli_query($con,$dforms_sql);
while($forms_row = mysqli_fetch_array($dforms_result)) {
echo 'Other cool stuff';
}
}
} //END (first) For Loop
So, for some reason, if that second for loop, $Other_field is there it will only show the IF statement for a_FF. But if the array = a_FF and a_GG and the for loop for $other_field is NOT there it displays them both. So it's obviously that for loop that is breaking something, I have no idea what though. Anyone have any thoughts?

PHP if statements being ignored

I'm new to PHP and SQL. I'm trying to make a rule so that it will only show certain information for certain pages. The code I'm using is
include 'dbh-login.php';
$id = $_GET['id'];
$i = 1;
while ($i != 100) {
$sql = "SELECT * FROM ui_off WHERE id='$i'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
if ($row['link'] = $id) {
echo $row['title']."<br>";
}
$i++;
}
The if statement seems to have no effect on weather the script echoes the title or not.
You are missing == assignment. Here is the working code.
$id = $_GET['id'];
$i = 1;
while ($i != 100) {
$sql = "SELECT * FROM ui_off WHERE id='$i'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
if ($row['link'] == $id) {
echo $row['title']."<br>";
}
$i++;
}
Your code does not make any sense.
You are using a while loop and looping in it 100 times just to check if 1 row have the given id.
Why don't you search directly for the id? Your code will be cleaner and you will free some memory on the server by deducting 100 queries each time the page is opened.
$id = $_GET['id'];
$sql = "SELECT * FROM ui_off WHERE id!='100' AND link='$id'" ;
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
if ($row['link'] != '') {
echo $row['title']."<br>";
}

Php code returns empty page. tried all other solutions

I am trying to learn php. I wrote code to insert post to database and get posts to display on the home page. I wrote pagination code as well and tried to run it. but as a result php is returning an empty page with no code and no error.
Tried every thing. Went to through most of the answers here. I still can figure out the error.
<?php
$con = mysqli_connect("localhost","root","password","social") or die ("Connection was not established");
//function for getting topics
function getTopics() {
global $con;
$get_topics = "select * from topics";
$run_topics = mysqli_query($con,$get_topics);
while($row=mysqli_fetch_array($run_topics)) {
$topic_id = $row['topic_id'];
$topic_title = $row['topic_title'];
echo "<option value='$topic_id'>$topic_title</option>";
}
}
//function for insert posts to database
function insertPost() {
if(isset($_POST['sub'])) {
global $con;
$title = $_POST['title'];
$content = $_POST['content'];
$topic = $_POST['topic'];
$user = $_SESSION['user_email'];
$get_user = "SELECT * FROM users WHERE user_email = '$user'";
$run_user = mysqli_query($con,$get_user);
$row=mysqli_fetch_array($run_user);
$user_id = $row['user_id'];
$insert = "INSERT INTO posts (user_id,topic_id,post_title,post_content,post_date) VALUES ('$user_id','$topic','$title','$content',NOW())";
$run = mysqli_query($con,$insert);
if($run) {
echo "<h3>Posted to timeline. Looks great.</h3>";
$update = "Update users set posts='yes' where user_id='$user_id'";
$run_update = mysqli_query($con,$update);
}
}
}
//function to get posts
function get_posts() {
global $con;
$per_page=5;
if (isset($_GET['page'])) {
$page = $_GET['page'];
}
else {
$page=1;
}
$start_from = ($page-1)*$per_page;
$get_posts = "SELECT * FROM posts ORDER by 1 DESC LIMIT $start_from, $per_page";
$run_posts = mysqli_query($con,$get_posts);
while($row_posts=mysqli_fetch_array($run_posts)) {
$post_id = $row_posts['post_id'];
$user_id = $row_posts['user_id'];
$post_title = $row_posts['post_title'];
$content = $row_posts['post_content'];
$post_date = $row_posts['post_date'];
//gettin the user who posted the thread
$user = "SELECT * FROM users WHERE user_id='$user_id' AND posts='yes'";
$run_user = mysqli_query($con,$user);
$row_user = mysqli_fetch_array($run_user);
$user_name = $row_user['user_name'];
$user_image = $row_user['user_image'];
//displaying all at once.
echo "<div id='posts'>
<p><img src='user_images/$user_image' width='50' height='50'</p>
<h3><a href='user_profile.php?luser_id=$user_id'>$user_name</h3>
<h3>$post_title</h3>
<p>$post_date</p>
<a href='single.php?post_id=$post_id' style='float:right'><button>See replies</button></a>
</div>";
}
include("pagination.php");
}
?>
pagination code goes like this.
<?php
$query = "SELECT * FROM posts";
$result = mysqli_query($con,$query);
//count the total records
$total_posts = mysqli_num_rows($result);
//using ceil function to divide the total records per page
$total_pages = ceil($total_posts / $per_page);
//going to first page
echo "
<center>
<div id='pagination'>
<a href='welcome.php?page=1'>First Page</a>
";
for ($i=1; $i=$total_pages; $i++) {
echo "<a href='welcome.php?page=$i'>$i</a>";
}
//going to last page
echo "<a href='welcome.php?page=$total_pages'>Last Page</a></center>";
?>

new badge on category list php

i am having category sidebar below code
Working Code:
//Showing categories function
function getCats(){
global $db;
$q_cats = "select * from cats ORDER BY cat_name ASC";
$run_cats = mysqli_query($db, $q_cats);
while($row_cat = mysqli_fetch_array($run_cats)){
$cat_id = $row_cat['cat_id'];
$cat_name = $row_cat['cat_name'];
$q_count = "select * from messages where cat_id='$cat_id'";
$run_count = mysqli_query($db, $q_count);
$cat_sms_count = mysqli_num_rows($run_count);
if($cat_sms_count < 1){
$cat_sms_count = 0;
}
echo "<a href='category.php?category=$cat_id' class='list-group-item'>$cat_name<span class='badge'>$cat_sms_count</span></a>";
}//while row END here
$q_tmsgs = "select * from messages";
$run_tcount = mysqli_query($db, $q_tmsgs);
$tsms_count = mysqli_num_rows($run_tcount);
echo "<a href='allindb.php' class='list-group-item t-list'>Total Messages in DB<span class='badge'>$tsms_count</span></a>";
}//Function getCats END here
this working fine but what i want, i want to add a new badge image next to the category name. i tried a lot but failed to do, what i think is the most relevant code.
Please check this code:
//Showing categories function
function getCats(){
global $db;
$q_cats = "select * from cats ORDER BY cat_name ASC";
$run_cats = mysqli_query($db, $q_cats);
while($row_cat = mysqli_fetch_array($run_cats)){
$cat_id = $row_cat['cat_id'];
$cat_name = $row_cat['cat_name'];
$q_count = "select * from messages where cat_id='$cat_id'";
$run_count = mysqli_query($db, $q_count);
$cat_sms_count = mysqli_num_rows($run_count);
//problem area----------------------------------
$q_new = "select * from messages where cat_id='$cat_id' ORDER BY date DESC LIMIT 20";
$run_new = mysqli_query($db, $q_new);
while($new = mysqli_num_rows($run_new)){
$newbadge = $new['cat_id'];
}
if($cat_id==$newbadge){
$newbadge1 = "<img src='images/mynew.gif'>";
}
//--------------------------------------------
if($cat_sms_count < 1){
$cat_sms_count = 0;
}
echo "<a href='category.php?category=$cat_id' class='list-group-item'>$cat_name $newbadge1<span class='badge'>$cat_sms_count</span></a>";
}//while row END here
$q_tmsgs = "select * from messages";
$run_tcount = mysqli_query($db, $q_tmsgs);
$tsms_count = mysqli_num_rows($run_tcount);
echo "<a href='allindb.php' class='list-group-item t-list'>Total Messages in DB<span class='badge'>$tsms_count</span></a>";
}//Function getCats END here
If i understand you correctly you want to show "new badge" on those topics that doesnt have any messages in them. if so you got $q_count
Just replace the while{} inside with an if{}else{}like so:
if($q_count == 0) {
$newbadge1 = "<img src='images/mynew.gif'>";
} else {
$newbadge1 = '';
}
The problem is that you are doing another query that is returning empty array, and therefore you are getting badges on topics that has got messages. So either remove the while{} and replace with the if-else or you can just do a check like:
if(empty($new)) {
$newbadge1 = "<img src='images/mynew.gif'>";
} else {
$newbadge1 = '';
}

How to run a function inside a function if a condition is met

Please I've been trying to create a function that would query a DB, select from the table, and if the row count is not equal to 6, then a row from the table and repeat (or maybe duplicate) until the row count is equal to 6. I've search for this here in StackOverflow, but didn't get anything close. Please if you have a similar link to this, please post it here, and I'll give it a go.
Here's my code:
//List All active adverts
function showActiveAdverts()
{
$status = 1;
//Build final queries.
$query = mysql_query("SELECT * FROM table WHERE
status = '".mysql_real_escape_string($status)."' ORDER BY rand() LIMIT 6") or die(mysql_error());
$count = mysql_num_rows($query);
$row = mysql_fetch_assoc($query);
# My question here, check if the $count >= 1 && $count != 6, then do getDefaultBannner() and repeat it until it runs for 6 times.
if($count >= 1){
do{
$list[] = $row['id'];
}while($row = mysql_fetch_assoc($query));
return $list;
}
else{ return FALSE; }
}
Here's the code for getDefaultBannner()
function getDefaultBannner()
{
$status = 6;
$query = mysql_query("SELECT id FROM table WHERE status = '".mysql_real_escape_string($status)."' ")
or die(mysql_error());
$count = mysql_num_rows($query);
$row = mysql_fetch_assoc($query);
if($count >= 1){
do{
$list[] = $row['id'];
}while($row = mysql_fetch_assoc($query));
return $list;
}
else{ return FALSE; }
}
Thanks in advance!
You can rewrite your code as this
//List All active adverts
$query = mysql_query("SELECT * FROM table WHERE
status = '".mysql_real_escape_string($status)."' ORDER BY rand() LIMIT 6") or die(mysql_error());
$count = mysql_num_rows($query);
if($count >= 1 && $count != 6)
$list = getDefaultBannner();
else
$list = showActiveAdverts($query);
function showActiveAdverts($query)
{
$status = 1;
//Build final queries.
$row = mysql_fetch_assoc($query);
if($count >= 1){
do{
$list[] = $row['id'];
}while($row = mysql_fetch_assoc($query));
return $list;
}
else{ return FALSE; }
}

Categories