PDA

Click to See Complete Forum and Search --> : paging an array of records


gilgalbiblewhee
Aug 6th, 2008, 02:29 PM
The original script is found on:
http://www.sitepoint.com/article/perfect-php-pagination

<?php
require_once "Paginated.php";
require_once "DoubleBarLayout.php";
?>
<html>
<head>
<title>Pagination</title>

<!-- Just a little style formatting. Has no bearing on example -->
<style type="text/css">
body {
font-family: Verdana;
font-size: 13px;
}

a {
text-decoration: none;
}

a:hover {
text-decoration: underline;
}
</style>
<!-- End style formatting -->
</head>

<body>

<?php
//create an array of names in alphabetic order. A database call could have retrieved these items
$names = array("Andrew", "Bernard", "Castello", "Dennis", "Ernie", "Frank", "Greg", "Henry", "Isac", "Jax", "Kester", "Leonard", "Matthew", "Nigel", "Oscar");

$page = $_GET['page'];

//constructor takes three parameters
//1. array to be paged
//2. number of results per page (optional parameter. Default is 10)
//3. the current page (optional parameter. Default is 1)
$pagedResults = new Paginated($names, 10, $page);

echo "<ul>";

while($row = $pagedResults->fetchPagedRow()) { //when $row is false loop terminates
echo "<li>{$row}</li>";
}

echo "</ul>";

//important to set the strategy to be used before a call to fetchPagedNavigation
$pagedResults->setLayout(new DoubleBarLayout());
echo $pagedResults->fetchPagedNavigation();
?>
</body>
</html>
It consists an array of 15 names.
WHat I needed to do is have a query and set the result into an array and replace the $names above. I replaced it with a function. And within the function called getQuery($bigWords) I placed both the query and the while loop of $results.
See next posting.

gilgalbiblewhee
Aug 6th, 2008, 02:54 PM
$bigWords is an array of words > 4 letters. The rest of the words have previously sorted out.
function getQuery($bigWords){

$query="SELECT * FROM bible WHERE 1=1 AND";

//to sort out all words with length less than 4 like AND, OR, BUT...
for ($i=0; $i < count($bigWords); $i++){
if($bigWords[$i]!=""){
$query.=" CASE WHEN text_data LIKE '%" .$bigWords[$i]. "%' THEN 1 ELSE 0 END\n";
if($i!=count($bigWords)-1){
$query.=" +";
}else{
//removes the OR from the last line and replaces with the following
//for results of words > 3
$query.= " > 3";
}
}
}
$query .= " ORDER BY id";
$theresult = array();
//$theresult[] = "";
//$theresult[] = $query;
$result = mysql_query($query);

while($row = mysql_fetch_assoc($result)){
$strText = $row['text_data'];
$COLORS = array('red','Teal','blue','Magenta','green','PaleGreen','orange','purple','Pink','YellowGreen','Sien na','aqua','Gray','LightBlue','MediumTurquoise','DarkRed');
for($m=0; $m < count($bigWords); $m++){
$strText = preg_replace("/(".$bigWords[$m].")/i", "<span class=\"\" id=\"\" style=\"color:".$COLORS[$m]."; font-weight:bold;\">$1</span>", $strText);
}
$theresult[] = "<br />\n<span class=\"goToBookChapter\" style=\"font-weight: bold;\">".$row['book_title']." ".$row['chapter'].":".$row['verse']."</span><br />\n".$strText;
}
return $theresult;
echo "<br /><br />".$query."<br />";
}

gilgalbiblewhee
Aug 6th, 2008, 03:05 PM
I forgot to mention the problem. The pagination of 10 records per page is not happening.
I've been having problems in this for WEEKS!!! With no one to help me.
I don't understand how the while loop for paging should be set. This is what I have so far:
while($row = $pagedResults->fetchPagedRow()){
echo "<span style='color: red;'>".$c."</span><br />";
//echo "{$row}";
print(getQuery($bigWords));
//echo getQuery($bigWords);
//echo "hi";
$c++;
}
I get:
1
Array2
Array3
Array4
Array5
Array6
Array7
Array8
Array9
Array10
Array

HEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEELP!!!!!