rscott
06-18-2012, 06:19 PM
I have gotten my pagination working but can someone offer a suggestion how I can modify my code so that only the news entry area is affected? I don't want the main section to keep refreshing if the user has gone to other pages. Thanks for your time.
Can be seen in action here in studio news area:http://www.rscottsculpture.com/index.php
And the code for the news.inc where paging is going on:
<?php
//make connection to database
$con = mysql_connect("localhost", "thameu_visitor", "visitor159") or die('Sorry, could not connect to database server');
mysql_select_db("thameu_sculpture", $con) or die('Sorry, could not connect to database');
$query = "SELECT count(title) from news";
$result = mysql_query($query);
$row=mysql_fetch_array($result);
//if there are no articles say so
if ($row[0] == 0)
{
echo "No news posted yet. \n";
//but if there are, get total records
} else
{
$totrecords = $row[0];
//set the page as 1
if (!isset($_GET['page']))
$thispage = 1;
else
$thispage = $_GET['page'];
//how many articles to post per page
$recordsperpage = 3;
$offset = ($thispage - 1) * $recordsperpage;
$totpages = ceil($totrecords / $recordsperpage);
$query = "SELECT title,date,article from news order by date desc limit $offset,$recordsperpage";
$result = mysql_query($query) or die('Could not retrieve any news: ' . mysql_error());
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
//display the articles
{
$date = $row['date'];
$title = $row['title'];
$article = $row['article'];
echo"<div style=\"width:95%;-webkit-border-radius: 20px;-moz-border-radius: 20px;border-radius: 20px;border:2px solid '#'802828;background:rgba(74,120,58,0.5);text-align:center;padding-left:4px;padding-right:4px;\">";
echo "<br>$date - $title<p>$article<br><br>";
echo"</div>";
}
if ($thispage > 1)
{
$page = $thispage - 1;
$prevpage = "<a href=\"index.php?newscontent=news.inc&page=$page\">Previous</a>";
} else
{
$prevpage = "";
}
$bar = '';
if ($totpages > 1)
{
for($page = 1; $page <= $totpages; $page++)
{
if ($page == $thispage)
{
$bar .= " $page ";
} else
{
$bar .= " <a href=\"index.php?newscontent=news.inc&page=$page\">$page</a> ";
}
}
}
if ($thispage < $totpages)
{
$page = $thispage + 1;
$nextpage = " <a href=\"index.php?newscontent=news.inc&page=$page\">Next</a>";
} else
{
$nextpage = "";
}
echo "Go to: " . $prevpage . $bar . $nextpage;
}
?>
</div>
Can be seen in action here in studio news area:http://www.rscottsculpture.com/index.php
And the code for the news.inc where paging is going on:
<?php
//make connection to database
$con = mysql_connect("localhost", "thameu_visitor", "visitor159") or die('Sorry, could not connect to database server');
mysql_select_db("thameu_sculpture", $con) or die('Sorry, could not connect to database');
$query = "SELECT count(title) from news";
$result = mysql_query($query);
$row=mysql_fetch_array($result);
//if there are no articles say so
if ($row[0] == 0)
{
echo "No news posted yet. \n";
//but if there are, get total records
} else
{
$totrecords = $row[0];
//set the page as 1
if (!isset($_GET['page']))
$thispage = 1;
else
$thispage = $_GET['page'];
//how many articles to post per page
$recordsperpage = 3;
$offset = ($thispage - 1) * $recordsperpage;
$totpages = ceil($totrecords / $recordsperpage);
$query = "SELECT title,date,article from news order by date desc limit $offset,$recordsperpage";
$result = mysql_query($query) or die('Could not retrieve any news: ' . mysql_error());
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
//display the articles
{
$date = $row['date'];
$title = $row['title'];
$article = $row['article'];
echo"<div style=\"width:95%;-webkit-border-radius: 20px;-moz-border-radius: 20px;border-radius: 20px;border:2px solid '#'802828;background:rgba(74,120,58,0.5);text-align:center;padding-left:4px;padding-right:4px;\">";
echo "<br>$date - $title<p>$article<br><br>";
echo"</div>";
}
if ($thispage > 1)
{
$page = $thispage - 1;
$prevpage = "<a href=\"index.php?newscontent=news.inc&page=$page\">Previous</a>";
} else
{
$prevpage = "";
}
$bar = '';
if ($totpages > 1)
{
for($page = 1; $page <= $totpages; $page++)
{
if ($page == $thispage)
{
$bar .= " $page ";
} else
{
$bar .= " <a href=\"index.php?newscontent=news.inc&page=$page\">$page</a> ";
}
}
}
if ($thispage < $totpages)
{
$page = $thispage + 1;
$nextpage = " <a href=\"index.php?newscontent=news.inc&page=$page\">Next</a>";
} else
{
$nextpage = "";
}
echo "Go to: " . $prevpage . $bar . $nextpage;
}
?>
</div>