dannybrazil Posted March 8, 2010 Share Posted March 8, 2010 Hello I was using this pagination script for a while in a site in English Now I need to use it in HEBREW (direction:rtl) the things is I cant find hoe to make the numbers of the pagination to change direction as well...I did change the direction of the NEXT and LAST page buttons by adding to the DIV style="direction:rtl;" Originally its like this: << Last 1 2 3 4 5 ... 11 12 13 Next>> I want/need it like this: << Next 13 12 11 ... 3 2 1 Last>> CODE: $tbl_name="class"; //your table name // How many adjacent pages should be shown on each side? $adjacents = 1; /* First get total number of rows in data table. If you have a WHERE clause in your query, make sure you mirror it here. */ $query = "SELECT COUNT(*) as num FROM class WHERE $category_sql AND $type_sql AND $area_sql AND $rooms_sql AND $preco_between AND $day_sql"; $total_pages = mysql_fetch_array(mysql_query($query)); $total_pages = $total_pages[num]; /* Setup vars for query. */ $targetpage = "result.php"; //your file name (the name of this file) $limit = 1; //how many items to show per page $page =$_GET['page']; if($page) $start = ($page - 1) * $limit; //first item to display on this page else $start = 0; //if no page var is given, set start to 0 /* Get data. */ $sql = "SELECT * FROM class WHERE $category_sql AND $type_sql AND $area_sql AND $rooms_sql AND $preco_between AND $day_sql LIMIT $start, $limit"; $result = mysql_query($sql); /* Setup page vars for display. */ if ($page == 0) $page = 1; //if no page var is given, default to 1. $prev = $page - 1; //previous page is page - 1 $next = $page + 1; //next page is page + 1 $lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up. $lpm1 = $lastpage - 1; //last page minus 1 /* Now we apply our rules and draw the pagination object. We're actually saving the code to a variable in case we want to draw it more than once. */ $pagination_link='category='.$category.'&type='.$type.'&area='.$area.'&rooms='.$rooms.'&preco1='.$preco1.'&preco2='.$preco2.'$day='.$day.' '; if($lastpage > 1) { $pagination.= "<div class=\"pagination\" style=\"direction:rtl; float:right;\">"; //previous button if ($page > 1) $pagination.= "<a href=\"$targetpage?$pagination_link&page=$prev\" method=\"post\">« דף קודם</a>"; else $pagination.= "<span class=\"disabled\">« דף קודם</span>"; //pages if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up { for ($counter = 1; $counter <= $lastpage; $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?$pagination_link&page=$counter\" method=\"post\">$counter</a>"; } } elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some { //close to beginning; only hide later pages if($page < 1 + ($adjacents * 2)) { for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?$pagination_link&page=$counter\" method=\"post\">$counter</a>"; } $pagination.= "..."; $pagination.= "<a href=\"$targetpage?$pagination_link&page=$lastpage\" method=\"post\">$lastpage</a>"; $pagination.= "<a href=\"$targetpage?$pagination_link&page=$lpm1\" method=\"post\">$lpm1</a>"; } //in middle; hide some front and some back elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) { $pagination.= "<a href=\"$targetpage?$pagination_link&page=2\" method=\"post\">2</a>"; $pagination.= "<a href=\"$targetpage?$pagination_link&page=1\" method=\"post\">1</a>"; $pagination.= "..."; for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?$pagination_link&page=$counter\" method=\"post\">$counter</a>"; } $pagination.= "..."; $pagination.= "<a href=\"$targetpage?$pagination_link&page=$lastpage\" method=\"post\">$lastpage</a>"; $pagination.= "<a href=\"$targetpage?$pagination_link&page=$lpm1\" method=\"post\">$lpm1</a>"; } //close to end; only hide early pages else { $pagination.= "<a href=\"$targetpage?$pagination_link&page=2\" method=\"post\">2</a>"; $pagination.= "<a href=\"$targetpage?$pagination_link&page=1\" method=\"post\">1</a>"; $pagination.= "..."; for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?$pagination_link&page=$counter\" method=\"post\">$counter</a>"; } } } //next button if ($page < $counter - 1) $pagination.= "<a href=\"$targetpage?$pagination_link&page=$next\" method=\"post\">דף הבא »</a>"; else $pagination.= "<span class=\"disabled\">דף הבא »</span>"; $pagination.= "</div>\n"; } Quote Link to comment Share on other sites More sharing options...
sasa Posted March 8, 2010 Share Posted March 8, 2010 try to add <style type="text/css"> a, span { float:right; margin-right:8; } </style> to your script Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 8, 2010 Share Posted March 8, 2010 Hmm... the style property for direction:rtl appeared to foul up the Next/Prev links. The double arrows had a different page number than the word. Very odd. I even checked the HTML output and it made no sense. I had to take it out. Anyway, that script is much, much more complex than it needs to be. I have rewritten it to be much more elegant. All the configuration variables are at the top. There is now one for direction. Change it from a 1 to a -1 to make it go from right to left. <?php //Configuration variables $tbl_name="class"; //your table name $targetpage = "result.php"; //your file name (the name of this file) $limit = 1; //how many items to show per page $adjacents = 2; // How many adjacent pages should be shown on each side? $beginend = 2; //Number of pages to always show at beginning & end $direction = -1; //Direction: 1 = forward, -1 = reverse $next_text = "Next »"; $prev_text = "« Prev"; //Default query $default_query = "SELECT [FIELD_LIST] as num FROM class WHERE $category_sql AND $type_sql AND $area_sql AND $rooms_sql AND $preco_between AND $day_sql"; //Get total number of records $query .= str_replace("[FIELD_LIST]", "COUNT(*) as num", $default_query); $result = mysql_query($query) $total_records = mysql_fetch_assoc($result); $total_records = $result['num']; // Determine total pages $total_pages = ceil($total_records/$limit); //total pages = total pages / items per page, rounded up. //Determine current page $page = (int) $_GET['page']; if ($page==0 || $page>$total_pages) { $page = 1; //if no page is given or > total pages, default to 1. } // Get data for current page $limit_start = ($page - 1) * $limit; //first item to display on this page $query .= str_replace("[FIELD_LIST]", "*", $default_query) . " LIMIT $limit_start, $limit"; $result = mysql_query($query); //Now we apply our rules and draw the pagination object. //We're actually saving the code to a variable in case we want to draw it more than once. function createPageLink($baseLink, $linkText, $pageNum=false) { if($pageNum) { $link = "<a href=\"$baseLink&page=$pageNum\">{$linkText}</a>\n"; } else { $link = "<span class=\"disabled\">{$linkText}</span>\n"; } return $link; } $pagination_link="{$targetpage}?category={$category}&type={$type}&area={$area}&rooms={$rooms}&preco1={$preco1}&preco2={$preco2}$day={$day}"; if($total_pages > 1) { //pages $loopStart = ($direction==1) ? 1 : $total_pages; for($counter=$loopStart; $counter>0 && $counter<=$total_pages; $counter=$counter+$direction) { //Add ellipses at beginning if needed if ( ($counter > $beginend) && ($counter < ($page-$adjacents)) ) { $page_links .= createPageLink('', "...\n", false); $counter = ($direction==1) ? ($page-$adjacents) : $beginend; } //Add ellipses at end if needed if ( ($counter > ($page+$adjacents)) && ($counter < ($total_pages-$beginend+1)) ) { $page_links .= createPageLink('', "...\n", false); $counter = ($direction==1) ? ($total_pages-$beginend+1) : ($page+$adjacents); } //Add page link $page_links .= createPageLink($pagination_link, $counter, (($counter!=$page)?$counter:false)); } //previous/next buttons $prev_bttn = createPageLink($pagination_link, $prev_text, (($page>1)?($page-1):false)); $next_bttn = createPageLink($pagination_link, $next_text, (($page<$total_pages)?$page+1:false)); //Create pagination div $pagination.= "<div class=\"pagination\" style=\"direction:; float:right;\">\n"; //Add prev/next buttons and page links if($direction==1) { $pagination .= $prev_bttn . $page_links . $next_bttn; } else { $pagination = $next_bttn . $page_links . $prev_bttn; } //End pagination div $pagination.= "</div>\n"; } echo $pagination; ?> Quote Link to comment Share on other sites More sharing options...
dannybrazil Posted March 9, 2010 Author Share Posted March 9, 2010 Wow good people do excist...thanks guys, i'll try that and will get back to you ASAP with results Thanks again Danny Quote Link to comment Share on other sites More sharing options...
dannybrazil Posted March 9, 2010 Author Share Posted March 9, 2010 mjdamato , thanks again for the new script BUT it doesnt work for a reason which I cant discover. First : It was : //Default query $default_query = "SELECT [FIELD_LIST] as num FROM class WHERE $category_sql AND $type_sql AND $area_sql AND $rooms_sql AND $preco_between AND $day_sql"; I guess in the real query it wont need to use the "as num" so I removed it //Default query $default_query = "SELECT [FIELD_LIST] FROM class WHERE $category_sql AND $type_sql AND $area_sql AND $rooms_sql AND $preco_between AND $day_sql"; but for the rest I get this error : <br /> <b>Warning</b>: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>/home/rgajsgjb/public_html/classimoveisrn/hebrew/result.php</b> on line <b>304</b><br /> </table> The full code: //category $category_sql="category='$category' "; //tipo $type_sql="type='$type' "; //area $area_sql="area='$area' "; //quartos $rooms_sql="(rooms='$rooms' or '$rooms' = '0')"; //preco between $preco_between="(price_between BETWEEN '$preco1' AND '$preco2')"; $day_sql="date >= '$day' "; //Configuration variables $tbl_name="class"; //your table name $targetpage = "result.php"; //your file name (the name of this file) $limit = 1; //how many items to show per page $adjacents = 2; // How many adjacent pages should be shown on each side? $beginend = 2; //Number of pages to always show at beginning & end $direction = -1; //Direction: 1 = forward, -1 = reverse $next_text = "Next »"; $prev_text = "« Prev"; //Default query $default_query = "SELECT [FIELD_LIST] FROM class WHERE $category_sql AND $type_sql AND $area_sql AND $rooms_sql AND $preco_between AND $day_sql"; //Get total number of records $query .= str_replace("[FIELD_LIST]", "COUNT(*) as num", $default_query); $result = mysql_query($query); $total_records = mysql_fetch_assoc($result); $total_records = $result['num']; // Determine total pages $total_pages = ceil($total_records/$limit); //total pages = total pages / items per page, rounded up. //Determine current page $page = (int) $_GET['page']; if ($page==0 || $page>$total_pages) { $page = 1; //if no page is given or > total pages, default to 1. } // Get data for current page $limit_start = ($page - 1) * $limit; //first item to display on this page $query .= str_replace("[FIELD_LIST]", "*", $default_query) . " LIMIT $limit_start, $limit"; $result = mysql_query($query); //Now we apply our rules and draw the pagination object. //We're actually saving the code to a variable in case we want to draw it more than once. function createPageLink($baseLink, $linkText, $pageNum=false) { if($pageNum) { $link = "<a href=\"$baseLink&page=$pageNum\">{$linkText}</a>\n"; } else { $link = "<span class=\"disabled\">{$linkText}</span>\n"; } return $link; } $pagination_link="{$targetpage}?category={$category}&type={$type}&area={$area}&rooms={$rooms}&preco1={$preco1}&preco2={$preco2}$day={$day}"; if($total_pages > 1) { //pages $loopStart = ($direction==1) ? 1 : $total_pages; for($counter=$loopStart; $counter>0 && $counter<=$total_pages; $counter=$counter+$direction) { //Add ellipses at beginning if needed if ( ($counter > $beginend) && ($counter < ($page-$adjacents)) ) { $page_links .= createPageLink('', "...\n", false); $counter = ($direction==1) ? ($page-$adjacents) : $beginend; } //Add ellipses at end if needed if ( ($counter > ($page+$adjacents)) && ($counter < ($total_pages-$beginend+1)) ) { $page_links .= createPageLink('', "...\n", false); $counter = ($direction==1) ? ($total_pages-$beginend+1) : ($page+$adjacents); } //Add page link $page_links .= createPageLink($pagination_link, $counter, (($counter!=$page)?$counter:false)); } //previous/next buttons $prev_bttn = createPageLink($pagination_link, $prev_text, (($page>1)?($page-1):false)); $next_bttn = createPageLink($pagination_link, $next_text, (($page<$total_pages)?$page+1:false)); //Create pagination div $pagination.= "<div class=\"pagination\" style=\"direction:; float:right;\">\n"; //Add prev/next buttons and page links if($direction==1) { $pagination .= $prev_bttn . $page_links . $next_bttn; } else { $pagination = $next_bttn . $page_links . $prev_bttn; } //End pagination div $pagination.= "</div>\n"; } //====================== showing the entries here...start============================================ while($row = mysql_fetch_array($result)) { $id=$row['id']; $type=$row['type']; $category=$row['category']; $area=$row['area']; $city=$row['city']; $street=$row['street']; $street_nm=$row['street_nm']; $bairro=$row['bairro']; $date_in=$row['date_in']; $rooms=$row['rooms']; $floor=$row['floor']; $floor_t=$row['floor_total']; $size=$row['size']; echo '<table border="0" width="800px" style="direction:rtl; margin-left:auto; margin-right:auto; background-color:#0188A6; margin-top:20px;text-align:right;">'; echo '<tr>'; echo '<td>'; $pic_1=$row['upload_1']; echo '<a href="#" class="img_brdr" style=" float:right;"><img border="0" src="imgsize.php?h=100&img='.$pic_1.'"/></a> <div style="direction:rtl; float:right; margin-right:20px; margin-top:10px; font-size:12px;"> <b><u>'. $type_array["type"][$type].' - '.$category_array["category"][$category].' - '.$area_array["area"][$area].'</u></b> <br> עיר: <span style="color:#FFCC66; font-weight:bold; ">'.$city.'</span> <br> שכונה: <span style="color:#FFCC66; font-weight:bold; ">'.$bairro.'</span> <br> רחוב: <span style="color:#FFCC66; font-weight:bold; ">'.$street.' '.$street_nm.'</span> <br> תאריך כניסה: <span style="color:#FFCC66; font-weight:bold; ">'.$date_in.'</span> </div> <div style="direction:rtl; float:right; margin-right:20px; margin-top:10px; font-size:12px;"> <b><u>פרטי הנכס</u></b><br> קומה: <span style="color:#FFCC66; font-weight:bold; ">'.$floor.'</span> מתוך <span style="color:#FFCC66; font-weight:bold; ">'.$floor_t.'</span> קומות <br> גודל הנכס: <span style="color:#FFCC66; font-weight:bold; ">'.$size.'</span> מ"ר <br> חדרים: <span style="color:#FFCC66; font-weight:bold; ">'.$rooms.'</span> <div> '; echo '</td>'; echo '</tr>'; } echo '</table>'; ?> <div style=" margin-top:20px; text-align:right;"> <?=$pagination?> </div> </div><!--end of blue box div--> Any thing wrong ? Thanks again Danny Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 9, 2010 Share Posted March 9, 2010 OK, I did not have your database so some of the coding had to be done without testing. I tried to make the query be "flexible" but must have created an error when doing so. One of the queries is failing. The code above is apparently being included in another file, correct? Because the line number in the error (line 304) is greater than the line numbers above. You need to figure out which query is failing and why. The code doesn't have any error handling for query failures and it should - but that was outside the context fo this post. There are two lines where you actually run queries. Change both of them as indicated. die() is not recommended for "normal" debugging in a production script, but is fine for on-the-fly debugging such as this. Change this: $result = mysql_query($query); To this: or die ("Query:<br />{$query}<br />Error:<br />".mysql_error()); Also, I see another error. Change this: $total_records = mysql_fetch_assoc($result); $total_records = $result['num']; To this: $result_row = mysql_fetch_assoc($result); $total_records = $result_row['num']; Quote Link to comment Share on other sites More sharing options...
dannybrazil Posted March 9, 2010 Author Share Posted March 9, 2010 Hey there Now its showing the PAGINATION ok BUT not the info it self that I want to show, it shows just the pagination div //category $category_sql="category='$category' "; //tipo $type_sql="type='$type' "; //area $area_sql="area='$area' "; //quartos $rooms_sql="(rooms='$rooms' or '$rooms' = '0')"; //preco between $preco_between="(price_between BETWEEN '$preco1' AND '$preco2')"; $day_sql="date >= '$day' "; //Configuration variables $tbl_name="class"; //your table name $targetpage = "result.php"; //your file name (the name of this file) $limit = 1; //how many items to show per page $adjacents = 2; // How many adjacent pages should be shown on each side? $beginend = 2; //Number of pages to always show at beginning & end $direction = -1; //Direction: 1 = forward, -1 = reverse $next_text = "Next »"; $prev_text = "« Prev"; //Default query $default_query = "SELECT [FIELD_LIST] FROM class WHERE $category_sql AND $type_sql AND $area_sql AND $rooms_sql AND $preco_between AND $day_sql"; //Get total number of records $query .= str_replace("[FIELD_LIST]", "COUNT(*) as num", $default_query); $result = mysql_query($query); $result_row = mysql_fetch_assoc($result); $total_records = $result_row['num']; // Determine total pages $total_pages = ceil($total_records/$limit); //total pages = total pages / items per page, rounded up. //Determine current page $page = (int) $_GET['page']; if ($page==0 || $page>$total_pages) { $page = 1; //if no page is given or > total pages, default to 1. } // Get data for current page $limit_start = ($page - 1) * $limit; //first item to display on this page $query .= str_replace("[FIELD_LIST]", "*", $default_query) . " LIMIT $limit_start, $limit"; $result = mysql_query($query); //Now we apply our rules and draw the pagination object. //We're actually saving the code to a variable in case we want to draw it more than once. function createPageLink($baseLink, $linkText, $pageNum=false) { if($pageNum) { $link = "<a href=\"$baseLink&page=$pageNum\">{$linkText}</a>\n"; } else { $link = "<span class=\"disabled\">{$linkText}</span>\n"; } return $link; } $pagination_link="{$targetpage}?category={$category}&type={$type}&area={$area}&rooms={$rooms}&preco1={$preco1}&preco2={$preco2}$day={$day}"; if($total_pages > 1) { //pages $loopStart = ($direction==1) ? 1 : $total_pages; for($counter=$loopStart; $counter>0 && $counter<=$total_pages; $counter=$counter+$direction) { //Add ellipses at beginning if needed if ( ($counter > $beginend) && ($counter < ($page-$adjacents)) ) { $page_links .= createPageLink('', "...\n", false); $counter = ($direction==1) ? ($page-$adjacents) : $beginend; } //Add ellipses at end if needed if ( ($counter > ($page+$adjacents)) && ($counter < ($total_pages-$beginend+1)) ) { $page_links .= createPageLink('', "...\n", false); $counter = ($direction==1) ? ($total_pages-$beginend+1) : ($page+$adjacents); } //Add page link $page_links .= createPageLink($pagination_link, $counter, (($counter!=$page)?$counter:false)); } //previous/next buttons $prev_bttn = createPageLink($pagination_link, $prev_text, (($page>1)?($page-1):false)); $next_bttn = createPageLink($pagination_link, $next_text, (($page<$total_pages)?$page+1:false)); //Create pagination div $pagination.= "<div class=\"pagination\" style=\"direction:; float:right;\">\n"; //Add prev/next buttons and page links if($direction==1) { $pagination .= $prev_bttn . $page_links . $next_bttn; } else { $pagination = $next_bttn . $page_links . $prev_bttn; } //End pagination div $pagination.= "</div>\n"; } //====================== showing the entries here...start============================================ while($row = mysql_fetch_array($result)) { $id=$row['id']; $type=$row['type']; $category=$row['category']; $area=$row['area']; $city=$row['city']; $street=$row['street']; $street_nm=$row['street_nm']; $bairro=$row['bairro']; $date_in=$row['date_in']; $rooms=$row['rooms']; $floor=$row['floor']; $floor_t=$row['floor_total']; $size=$row['size']; $link_for_show=$site.'show.php?id='.$id; echo '<table border="0" width="800px" style="direction:rtl; margin-left:auto; margin-right:auto; background-color:#0188A6; margin-top:20px;text-align:right;">'; echo '<tr>'; echo '<td>'; $pic_1=$row['upload_1']; echo '<a href="'.$link_for_show.'" class="img_brdr" target="_self" style=" float:right;"><img border="0" src="imgsize.php?h=100&img='.$pic_1.'"/></a>'; echo '<table> <tr> <td> <div style="direction:rtl; float:right; margin-right:20px; margin-top:10px; font-size:16px; height:100px;"> <b><u>'. $type_array["type"][$type].' - '.$category_array["category"][$category].' - '.$area_array["area"][$area].'</u></b> <br> עיר: <span style="color:#FFCC66; font-weight:bold; font-size:14px; ">'.$city.'</span> <br> שכונה: <span style="color:#FFCC66; font-weight:bold;font-size:14px; ">'.$bairro.'</span> <br> רחוב: <span style="color:#FFCC66; font-weight:bold;font-size:14px; ">'.$street.' '.$street_nm.'</span> <br> תאריך כניסה: <span style="color:#FFCC66; font-weight:bold; font-size:14px;">'.$date_in.'</span> </div> </td> <td> <div style="direction:rtl; margin-right:20px; margin-top:10px; font-size:16px;height:100px;"> <b><u>פרטי הנכס</u></b><br> קומה: <span style="color:#FFCC66; font-weight:bold;font-size:14px; ">'.$floor.'</span> מתוך <span style="color:#FFCC66; font-weight:bold;font-size:14px; ">'.$floor_t.'</span> קומות <br> גודל הנכס: <span style="color:#FFCC66; font-weight:bold;font-size:14px; ">'.$size.'</span> מ"ר <br> חדרים: <span style="color:#FFCC66; font-weight:bold;font-size:14px; ">'.$rooms.'</span> <div> </td> <td> <div style=" margin-top:10px; margin-right:50px; font-size:12px;height:100px;"> <a href="'.$link_for_show.'" target="_self" ><img src="images/select.png" border="0" /></a> <br> <a href="'.$link_for_show.'" target="_self" ><img src="images/details.png" border="0" /></a> </div> </td> <tr> </table> '; echo '</td>'; echo '</tr>'; } echo '</table>'; ?> <div style=" margin-top:20px; text-align:right;"> <?=$pagination?> </div> </div><!--end of blue box div--> It seems that this part isnt receiving what is should in order to loop through the entries to exibit while($row = mysql_fetch_array($result)) { Any help ? Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 9, 2010 Share Posted March 9, 2010 So, do some basic debugging to find the problem: - Echo the query to the page to see if it looks correct. - Test the query in PHP admin to ensure it returns results or echo mysql_num_rows() to see how many records are displayed. See what those return and then go from there. Quote Link to comment Share on other sites More sharing options...
dannybrazil Posted March 9, 2010 Author Share Posted March 9, 2010 Thanks I will Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.