I have a simple php script that counts the number of pages in a manga script :
$omv_pager = "";
$omv_pager .= "<div class=\"well\">\n";
$omv_pager .= "<span>Manga <select class=\"form-control\" style='margin-bottom:10px;' name=\"manga\" onchange=\"change_manga(this.value)\">";
$omv_pager .= "<option class=\"form-control\" value=\"0\">Selecione Título do Manga...</option>";
for ($i = 0; $i < count($mangas); $i++) {
$m = $mangas[$i];
$omv_pager .= "<option value=\"" . omv_encode($m) . "\"" . (($m == $manga) ? " selected=\"selected\"" : "") . ">" . $m . "</option>";
}
$omv_pager .= "</select></span>\n";
if ($manga) {
if ($chapter) {
$omv_pager .= "<span>Chapter <select name=\"chapter\" onchange=\"change_chapter('$manga_escaped', this.value)\">";
for ($i = 0; $i < count($chapters); $i++) {
$cnumber = $chapters[$i]["number"];
$omv_pager .= "<option value=\"" . omv_encode($cnumber) . "\"" . (($cnumber == $chapter_number) ? " selected=\"selected\"" : "") . ">" . $cnumber . (isset($chapters[$i]["title"]) ? (" - " . $chapters[$i]["title"]) : "") . "</option>";
}
$omv_pager .= "</select></span>\n";
if ($page) {
$prevhtml = "";
if ($page <= 1) {
$prevhtml = "<img src=\"http://www.leitor.tk/themes/default/no-previous.png\" alt=\"\" />";
} else {
$prevhtml = "<img src=\"http://www.leitor.tk/themes/default/previous.png\" alt=\"Previous Page\" title=\"Previous Page\" />";
}
$nexthtml = "";
if ($page >= count($pages)) {
$nexthtml = "<img src=\"http://www.leitor.tk/themes/default/no-next.png\" alt=\"\" />";
} else {
$nexthtml = "<img src=\"http://www.leitor.tk/themes/default/next.png\" alt=\"Next Page\" title=\"Next Page\" />";
}
$omv_pager .= "<span>$prevhtml Page <select name=\"page\" onchange=\"change_page('$manga_escaped', '$chapter_number_escaped', this.value)\">";
for ($p = 1; $p <= count($pages); $p++) {
$omv_pager .= "<option value=\"" . $p . "\"" . (($p == $page) ? " selected=\"selected\"" : "") . ">" . $p . "</option>";
}
$omv_pager .= "</select> of " . count($pages) . " $nexthtml</span>\n";
}
}
}
$omv_pager .= "</div>\n";
echo $omv_pager;
What I want to do is, in the last page change the name of the to "end"
e.g : 1-2-3-4-5-6-7-8-9-10-11-12-end
And thanks to all of you guys.
Try with:
">" . ($p == count($pages) ? "end" : $p) . "</option>"
instead of
">" . $p . "</option>"
For optimization reasons you might want to buffer count($pages) into a variable before the for.
Related
I'm trying to write a simple if statement that should be returning true but is instead returning false. The issue I'm having is that for several items on my list $dataPeriod should be returning true matching '202265' I've tried converting it to a string thinking it didn't like that I was comparing a string to an int and that didn't seem to help.
I'm importing the data from this CSV file - https://www.harpercollege.edu/_resources/php/openclasslistweb.csv
<?php
$next = 0;
$file_to_read = fopen('openclasslistweb.csv', 'r');
if($file_to_read !== FALSE){
echo "<table class = 'open-list-table'>\n";
while(($data = fgetcsv($file_to_read, ',')) !== FALSE){
for($i = 0; $i < count($data); $i++) {
if ($i == 0){
$dataCourse = $data[$i];
}
if ($i == 1){
$dataCRN = $data[$i];
}
if ($i == 2){
$dataTitle = $data[$i];
}
if ($i == 3){
$dataMethod = $data[$i];
}
if ($i == 4){
$dataInstructor = $data[$i];
}
if ($i == 5){
$dataStartDate = $data[$i];
}
if ($i == 6){
$dataEndDate = $data[$i];
}
if ($i == 7){
$dataMonday = $data[$i];
}
if ($i == 8){
$dataTuesday = $data[$i];
}
if ($i == 9){
$dataWednesday = $data[$i];
}
if ($i == 10){
$dataThursday = $data[$i];
}
if ($i == 11){
$dataFriday = $data[$i];
}
if ($i == 12){
$dataSaturday = $data[$i];
}
if ($i == 13){
$dataSunday = $data[$i];
}
if ($i == 14){
$dataStartTime = $data[$i];
}
if ($i == 15){
$dataEndTime = $data[$i];
}
if ($i == 16){
$dataRoom = $data[$i];
}
if ($i == 17){
$dataBuilding = $data[$i];
}
if ($i == 18){
$dataSchedule = $data[$i];
}
if ($i == 19){
$dataDesc = $data[$i];
}
if ($i == 20){
$dataPeriod = $data[$i];
}
}
if (strpos($dataPeriod, "202265") !== false) {
if ($next == 0){
echo "<tr class='open-list-header'>";
}
else {
echo "<tr>";
}
echo "<td>" . $dataCourse . "</td>" . "<td>" . $dataCRN . "</td>" . "<td>" . $dataTitle . "</td>" . "<td>" . $dataMethod . "</td>" . "<td>" . $dataInstructor . "</td>" . "<td>" . $dataStartDate . "</td>" . "<td>" . $dataEndDate . "</td>" . "<td>" . $dataMonday . "</td>" . "<td>" . $dataTuesday . "</td>" . "<td>" . $dataWednesday . "</td>" . "<td>" . $dataThursday . "</td>" . "<td>" . $dataFriday . "</td>" . "<td>" . $dataSaturday . "</td>" . "<td>" . $dataSunday . "</td>" . "<td>" . $dataStartTime . "</td>" . "<td>" . $dataEndTime . "</td>" . "<td>" . $dataRoom . "</td>" . "<td>" . $dataBuilding . "</td>" . "<td>" . $dataSchedule . "</td>" . "<td>" . $dataDesc . "</td>" . "<td>" . $dataPeriod . "<td>";
echo "</tr>\n";
$next = $next + 1;
}
}
echo "</table>\n";
fclose($file_to_read);
}
?>
Your CSV has a lot of invisible characters or encoded characters, so some of your values aren't what you expect. I was able to get all of the unique values for $data[20], and with json_encode it shows this:
{
"0":"\u0000A\u0000c\u0000a\u0000d\u0000e\u0000m\u0000i\u0000c\u0000 \u0000P\u0000e\u0000r\u0000i\u0000o\u0000d\u0000",
"1":"\u00002\u00000\u00002\u00002\u00006\u00005\u0000",
"16":"\u0000O\u0000n\u0000l\u0000i\u0000n\u0000e\u0000 \u0000A\u0000N\u0000Y\u0000T\u0000I\u0000M\u0000E\u0000 \u0000(\u0000A\u0000s\u0000y\u0000n\u0000c\u0000h\u0000r\u0000o\u0000n\u0000o\u0000u\u0000s\u0000)\u0000",
"180":"\u00002\u00000\u00002\u00002\u00009\u00005\u0000",
"982":"\u00002\u00000\u00002\u00003\u00003\u00005\u0000",
"2104":"\u0000L\u0000L\u00001\u0000"
}
I'm trying to get search results to paginate if there are greater than 10 items found in the database. For some reason, even though the code recognises there are more than 10 items and creates links for subsequent pages, all search results are listed on the first page only. Anyone able to help please? Code is below:
for($i = 0; $i < $terms_count; $i++)
{
$search_terms_array[$i] = trim($search_terms_array[$i]);
${"query".$i} = $this->mysqli_link->query("SELECT prod_id, prod_tags FROM table WHERE prod_tags LIKE '%" . $search_terms_array[$i] . "%'");
if(${"query".$i}->num_rows < 1)
{
$zerocount++;
}
else
{
$rows = array();
while($row = ${"query".$i}->fetch_array())
{
$rows[] = $row;
}
foreach($rows as $row)
{
$search_id_results[] = $row['prod_id'];
}
}
}
if($zerocount == $terms_count)
{
echo $this->err_handle->fetch_error_text("search_terms_0_results");
return;
}
else
{
$search_results = array_values(array_unique($search_id_results));
$search_results_count = count($search_results);
$search_page_count = ceil($search_results_count / 10);
$search_page_first_result = ($search_page - 1) * 10;
echo '<p>Go to page: ';
for($i = 1; $i <= $search_page_count; $i++)
{
if($i == $search_page)
{
echo " <strong>" . $i . "</strong>";
}
else
{
echo ' ' . $i . '';
}
}
echo '</p><p> </p><p> </p>';
for($i = 0; $i < $search_results_count; $i++)
{
$query = $this->mysqli_link->query("SELECT * FROM table WHERE prod_id='" . $search_results[$i] . "' LIMIT " . $search_page_first_result . ", 10");
while($row = $query->fetch_array())
{
echo "<h4>" . $row['prod_name'] . "</h4><p><img src=\"includes/images/product_images/" . $row['prod_category'] . "/" . $row['prod_pic_filename'] . "\" alt=\"\" width=\"150\" height=\"200\" /></p><p>Price: £" . $row['prod_price'] . "</p><p>" . $row['prod_code'] . "</p><input type=\"number\" name=\"prod_qty\" maxlength=\"2\" /><input type=\"submit\" name=\"add_to_basket\" value=\"Add To Basket\" /></form></p><p> </p><p> </p>";
}
}
echo '<p>Go to page: ';
for($i = 1; $i <= $search_page_count; $i++)
{
if($i == $search_page)
{
echo " <strong>" . $i . "</strong>";
}
else
{
echo ' ' . $i . '';
}
}
echo '</p><p> </p><p> </p>';
}
Ok so I found a solution to this problem and the full function is now as follows:
public function product_search($search_terms, $search_page, $search_flag_check)
{
if($search_flag_check == "invalid")
{
echo $this->err_handle->fetch_error_text("invalid_ns_term");
return;
}
if($search_terms == "")
{
echo $this->err_handle->fetch_error_text("no_search_string");
return;
}
$search_terms = htmlspecialchars($search_terms);
$search_terms = $this->mysqli_link->real_escape_string(filter_var($search_terms, FILTER_SANITIZE_FULL_SPECIAL_CHARS, FILTER_FLAG_NO_ENCODE_QUOTES));
$search_terms_array = explode(" ", $search_terms);
$terms_count = count($search_terms_array);
$zerocount = 0;
$search_id_results = array();
for($i = 0; $i < $terms_count; $i++)
{
$search_terms_array[$i] = trim($search_terms_array[$i]);
${"query".$i} = $this->mysqli_link->query("SELECT prod_id, prod_tags FROM table WHERE prod_tags LIKE '%" . $search_terms_array[$i] . "%'");
if(${"query".$i}->num_rows < 1)
{
$zerocount++;
}
else
{
$rows = array();
while($row = ${"query".$i}->fetch_array())
{
$rows[] = $row;
}
foreach($rows as $row)
{
$search_id_results[] = $row['prod_id'];
}
}
}
if($zerocount == $terms_count)
{
echo $this->err_handle->fetch_error_text("search_terms_0_results");
return;
}
else
{
$search_results = array_values(array_unique($search_id_results));
$search_results_count = count($search_results);
$search_page_count = ceil($search_results_count / 10);
$search_page_first_result = ($search_page - 1) * 10;
$search_page_results_limit = 10;
if($search_page_first_result < 1)
{
$search_page_first_result = 1;
}
echo '<p>Go to page: ';
for($i = 1; $i <= $search_page_count; $i++)
{
if($i == $search_page)
{
echo " <strong>" . $i . "</strong>";
}
else
{
echo ' ' . $i . '';
}
}
echo '</p><p> </p><p> </p>';
$search_page_upper_limit = $search_page_first_result + 9;
if(array_key_exists($search_page_upper_limit, $search_results))
{
$search_results_limit = $search_page_first_result + $search_page_results_limit;
}
else
{
end($search_results);
$search_results_limit = key($search_results);
reset($search_results);
}
for($i = $search_page_first_result; $i <= $search_results_limit; $i++)
{
$query2 = $this->mysqli_link->query("SELECT * FROM table WHERE prod_id='" . $search_results[$i] . "'");
$row = $query2->fetch_array();
echo "<h4>" . $row['prod_name'] . "</h4><p><img src=\"includes/images/product_images/" . $row['prod_category'] . "/" . $row['prod_pic_filename'] . "\" alt=\"\" width=\"150\" height=\"200\" /></p><p>Price: £" . $row['prod_price'] . "</p><p>" . $row['prod_code'] . "</p><input type=\"number\" name=\"prod_qty\" maxlength=\"2\" /><input type=\"submit\" name=\"add_to_basket\" value=\"Add To Basket\" /></form></p><p> </p><p> </p>";
}
echo '<p>Go to page: ';
for($i = 1; $i <= $search_page_count; $i++)
{
if($i == $search_page)
{
echo " <strong>" . $i . "</strong>";
}
else
{
echo ' ' . $i . '';
}
}
echo '</p><p> </p><p> </p>';
}
}
It took a bit of thinking and I was about to give up, but then I thought about using the array keys to calculate the limits for each search page and after a bit of googling on relevant PHP array functions it all fell into place quite well.
I understand the code may not be very tidy right now and there may be ways to optimize/improve it, however for the time being it does the job.
i am trying to generate a selectbox:
$turn= array(
'attr' => array('nameid' => 'turn',),
'0 - 499',
'500 - 1499',
'1500 - 3499',
'3500 - 12999',
'13000 - 17999',
'18000 - 23999',
'24000 - 40000',
);
function createSelectBox($array) {
$out = "<select id=" . $array['attr']['nameid'] . " name=" .$array['attr']['nameid']">";
$out .= "<option class='choose' >Choose...</option>";
for ($idx = 0; $idx < sizeof($array) - 1; $idx++) {
$out .= "<option value=" . $array[$idx] . ">" . $array[$idx] . "</option>";
}
$out .= '</select>';
return $out;
}
The Problem is, that it will generate the 'value'-attribute in my option-tag like this:
<option value="0" 499>0 - 499</option>
the function cant handle the whitescpace in my array.
Use this
function createSelectBox($array) {
$out = '<select id='.$array['attr']['nameid'].' name='.$array['attr']['nameid'].'>';
$out .= '<option class="choose" >Choose...</option>';
for ($idx = 0; $idx < sizeof($array) - 1; $idx++) {
$out .= '<option value="'.$array[$idx].'">' . $array[$idx] . '</option>';
}
$out .= '</select>';
return $out;
}
Your value are strings you are treating them as integers. try it.
<?php
$turn= array(
'attr' => array('nameid' => 'turn',),
'0 - 499',
'500 - 1499',
'1500 - 3499',
'3500 - 12999',
'13000 - 17999',
'18000 - 23999',
'24000 - 40000',
);
function createSelectBox($array) {
$out = "<select id=" . $array['attr']['nameid'] . " name=" .$array['attr']['nameid'] . ">";
$out .= "<option class='choose' >Choose...</option>";
for ($idx = 0; $idx < sizeof($array) - 1; $idx++) {
$out .= "<option value='" . $array[$idx] . "'>" . $array[$idx] . "</option>";
}
$out .= '</select>';
return $out;
}
echo createSelectBox( $turn );
?>
I have a pagination class with buildTrail function to display pages number.
For searching the db, this class is working great.
If i'm searching for "intel", the second page link will be "http://localhost/search/intel/pg/2".
The problem occures when i try to filter the results .EX: "http://localhost/search/intel/type/ssd" so the links for paging will display : ex"http://localhost/search/intel/type/ssd/intel/type/ssd/pg/2"
function buildTrail($param = ""){
// $cur_page = basename($_SERVER['PHP_SELF']);
$link = $_SERVER['REQUEST_URI'];
$link_array = explode('/', $link);
//$count = count($link_array);
$pagename = $link_array[1];
// echo $magename;
if(is_array($param)){
foreach($param as $a => $b){
if($a != "page"){
$url .= $pagename."/".$b;
}
}
} else {
$url = $param;
}
$trail = "";
if($this->getPages() > 1){
if($this->getFrom() > 1){
$trail .= "<a href='" . WEBSITE . "/". $url . "/pg/" . $this->getPrevious()."'>«</a>\n ";
}
if($this->getFrom() < 10 && $this->getPages() > 10){
for ($i = 1; $i <= 10; $i++){
$trail .= "<a class='". ($i == $this->getFrom() ? "selected" : "links") . "' href='" . WEBSITE . "/".$url."/pg/" . $i ."'>" . $i . "</a>\n ";
}
} elseif($this->getFrom() < 10 && $this->getPages() < 10){
for ($i = 1; $i <= $this->getPages(); $i++){
$trail .= "<a class='". ($i == $this->getFrom() ? "selected" : "links") . "' href='" . WEBSITE . "/".$url."/pg/" . $i ."'>" . $i . "</a>\n ";
}
}elseif ($this->getFrom() >= 10 && $this->getFrom() <= ($this->getPages() - 5) ){
for ($i = ($this->getFrom() - 5); $i <= ($this->getFrom() + 5); $i ++){
$trail .= "<a class='". ($i == $this->getFrom() ? "selected" : "links") . "' href='" . WEBSITE . "/".$url."/pg/" . $i ."'>" . $i . "</a>\n ";
}
} else {
for ($i = ($this->getPages() - 10); $i <= $this->getPages(); $i ++){
$trail .= "<a class='". ($i == $this->getFrom() ? "selected" : "links") . "' href='" . WEBSITE . "/".$url."/pg/" . $i ."'>" . $i . "</a>\n ";
}
}
if($this->getFrom() < $this->getPages()){
$trail .= "<a href='" . WEBSITE . "/".$url."/pg/" . $this->getNext()."'>»</a>\n ";
}
}
return $trail;
}
Solved with htaccess
RewriteEngine On
Options -Indexes
Options +FollowSymLinks
<Files .htaccess>
deny from all
</Files>
RewriteRule ^search/(.*)/pg/([0-9]+)(/)?$ search.php?page=$2&q=$1 [NC,L]
RewriteRule ^search/(.*)/sort/(.*)/$ search.php?q=$1&sort=$2 [NC,L]
RewriteRule ^search/(.*)/sort/(.*)$ search.php?q=$1&sort=$2 [NC,L]
While making a photo gallery I encountered a problem. With every photo I try to show how many comments it has, however if a photo has 0 comments it will give me an 'undefined offset' error. I have no idea what I am doing wrong because it does show that there are 0 comments.
This is the code of what is relevant to the problem:
(The problem occurres in the line: if($reacties[$i]==0){)
if((isset($_GET['vanafFoto'])) AND (intval($_GET['vanafFoto']>=0)) AND (intval($_GET['vanafFoto'] < $countFotos))){
$begin = intval($_GET['vanafFoto']);
if(($begin + $aantalFotos) <= $countFotos){
$eind = ($begin + $aantalFotos);
} // end if
else {
$eind = $countFotos;
} // end else
} // end if
else {
$begin = 0;
$eind = $aantalFotos;
} // end else
$countFotos = count($fotoArray);
// path naar echte foto
} // end else
echo "<table border='0' cellpadding='0' cellspacing='2'><tr><td ><b>" . $pathspatie . "</b> <small>(" . $count . ")</small>
<br><br><center><small>Pictures " . ($begin + 1) . " - " . $eind . "</small></center></td></tr></table>";
if(($begin - $aantalFotos) >= 0){
$navigation = "<a href='" . $_SERVER['PHP_SELF'] . "?page=album&boek=" . $originalPath . "&vanafFoto=" . ($begin - $aantalFotos) . "'><</a> " . $navigation;
} // end if
if(($begin + $aantalFotos) < $count){
$navigation .= " <a href='" . $_SERVER['PHP_SELF'] . "?page=album&boek=" . $originalPath . "&vanafFoto=" . ($begin + $aantalFotos) . "'>></a>";
} // end if
echo $navigation . "<br><br>";
echo "</td></tr><tr>";
$fotonr = 1;
for($i=$begin; $i < $eind; $i++){
$thumb = str_replace($path2, $thumbPath, $fotoArray[$i]);
echo "<td align='center'><a href='" . $_SERVER['PHP_SELF'] . "?page=album&boek=" . $originalPath . "&fotoID=" . $i . "'><img border='0' src='" . $thumb . "' height='100'><br>";
echo "<small>reacties (";
if($reacties[$i]==0){ // error occurres here.
echo "0";
} // end if
else {
echo $reacties[$i];
} // end else
echo ")</small>";
echo "</a></td>";
$fotonr++;
if($fotonr == ($clm + 1)){
echo "</tr>\n<tr>";
$fotonr = 1;
} // end if
} // end for
If anyone can see what the problem is it would be great!
I did not understand you exact goal but maybe it is better to write one more check:
if(!isset($reacties[$i]) || $reacties[$i]==0){
echo "0";
}