myrok24 Posted January 13, 2009 Share Posted January 13, 2009 Hi there, I would like to add nex/previous buttons(links) to my current script and also a number showing wich page is displayed of the total (like: page 6/12). Not with a row of numbers like in many examples on internet. I am showing one record per page. My question: Should/could still use the script/structure like in the examples, to eventualy accomplish my goal? Tricky part is the fact that i want to navigate through a selected category (when selected) OR through all records (all assigments (ID)), depending on wich action the user My code: <? $query = " SELECT id, name, label, tekst, category, klant, DATE_FORMAT(datum, '%d-%m-%Y') AS nl_datum FROM upload "; if(isset($_GET['id'])) { $query .= " WHERE id = '".mysql_real_escape_string($_GET['id'])."'"; } elseif(isset($_GET['category'])) { $query .= " WHERE category = '".mysql_real_escape_string($_GET['category'])."'"; } else { echo 'No id given'; } $result = mysql_query($query) or die('Error : ' . mysql_error()); $myrow = mysql_fetch_assoc($result); ?> <!-- ### SHOW WORK ######--> <div > <div> <h3> <? echo $myrow['klant'] ?></h3> <p class="newsdate">Geplaatst op: <? echo $myrow['nl_datum'] ?></p> <p class="newsdate">Middel: <? echo $myrow['category'] ?></p> <p class="newsdate">Opdracht: <? echo $myrow['label'] ?></p> <? echo '<img class=plaatje src="image/' . $myrow['name'] . '" />' ; ?> <p class="newstext"> <? echo ($myrow['tekst']) ?> </p> </div> </div> ?> I have no clue how to realize this within my two different querys.. thnx in advance for any tips/help..! Link to comment https://forums.phpfreaks.com/topic/140681-solved-nextprevious-links/ Share on other sites More sharing options...
dawsba Posted January 13, 2009 Share Posted January 13, 2009 I've done this a view times before while ($result5 = mysql_fetch_array($result4)) { if(!isset($ijk) || $ijk == 0){$ijk=1;} $_SESSION['reviewarray'][$ijk] = $result5[id]; if($ijk == 1){$_SESSION['reviewarraytitle'] = "< Defined Search Results >";} $ijk++; } $_SESSION[pagecount] = $ijk; This will store your results in a session var then all you have to do is for page is echo $ijk when going previous or next use in your review page script <a href="yourpage.php?yourvar_previouspage=<?php if($currentpage > 1){$previous_page=$_SESSION[pagecount]-1;}else{$previous_page=1;} ?> </a> <a href="yourpage.php?yourvar_nextpage=<?php if($currentpage < $_SESSION[pagecount]){$next_page=$ijk+1;}else{$previous_page=$_SESSION[pagecount];} </a> hope this helps Link to comment https://forums.phpfreaks.com/topic/140681-solved-nextprevious-links/#findComment-736269 Share on other sites More sharing options...
myrok24 Posted January 13, 2009 Author Share Posted January 13, 2009 Looks compact, but i absolutely don't know how to intergrate this with my code.. Is this code enought for my two querys? Link to comment https://forums.phpfreaks.com/topic/140681-solved-nextprevious-links/#findComment-736283 Share on other sites More sharing options...
dawsba Posted January 13, 2009 Share Posted January 13, 2009 create 2 files 1 query.php or alike <? session_start(); $query = "SELECT id, name, label, tekst, category, klant, DATE_FORMAT(datum, '%d-%m-%Y') AS nl_datum FROM upload"; if(isset($_GET['id'])){$query .= " WHERE `id` = '".mysql_real_escape_string($_GET['id'])."'";} elseif(isset($_GET['category'])){$query .= " WHERE `category` = '".mysql_real_escape_string($_GET['category'])."'";} else{echo 'No id given';} //$dbname is your database name and $link is the link to the DB $result = mysql_db_query($dbname,$query,$link) or die("Invalid Query<br>". mysql_error()); while ($results = mysql_fetch_array($result)) { if(!isset($ijk) || $ijk == 0){$ijk=1;} foreach($results as $k=>$v){$_SESSION['reviewarray'][$ijk][$k] = $v;} $ijk++; } $_SESSION[pagecount] = $ijk; if($_SESSION[pagecount]>=1) { $currentpage = 1; include_once 'review.php'; } else { echo "No Results"; } ?> and a reviw page ie review.php <? session_start(): ?> <div > <div> <h3> <?= $_SESSION['reviewarray'][$currentpage]['klant'] ?></h3> <p class="newsdate">Geplaatst op: <?= $_SESSION['reviewarray'][$currentpage]['nl_datum'] ?></p> <p class="newsdate">Middel: <?= $_SESSION['reviewarray'][$currentpage]['category'] ?></p> <p class="newsdate">Opdracht: <?= $_SESSION['reviewarray'][$currentpage]['label'] ?></p> <? echo '<img class=plaatje src="image/' . $_SESSION['reviewarray'][$currentpage]['name'] . '" />' ; ?> <p class="newstext"> <?= $_SESSION['reviewarray'][$currentpage]['tekst'] ?> </p> </div> </div> <div> <a href="review.php?currentpage=<? if($currentpage > 1){echo $_SESSION[pagecount]-1;}else{echo 1;} ?>"> </a> <a href="review.php?currentpage=<? if($currentpage < $_SESSION[pagecount]){echo $currentpage+1;}else{echo $_SESSION[pagecount];}?> "> </a> </div> you'll need to put in your db details and set up your link, but should do it. Link to comment https://forums.phpfreaks.com/topic/140681-solved-nextprevious-links/#findComment-736304 Share on other sites More sharing options...
myrok24 Posted January 13, 2009 Author Share Posted January 13, 2009 i found this example code: <? if(!isset($_GET['page'])){ $page = 1; } else { $page = $_GET['page']; } $max_results = 1; $from = (($page * $max_results) - $max_results); // Perform MySQL query on only the current page number's results $sql = mysql_query("SELECT * FROM pages LIMIT $from, $max_results"); while($row = mysql_fetch_array($sql)){ // Build your formatted results here. echo $row['title']."<br />"; } // Figure out the total number of results in DB: $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM pages"),0); // Figure out the total number of pages. Always round up using ceil() $total_pages = ceil($total_results / $max_results); // then the paging links?> but is don't know if i must change the if(!isset($_GET['page'])){ i get ID. (or category).. Link to comment https://forums.phpfreaks.com/topic/140681-solved-nextprevious-links/#findComment-736479 Share on other sites More sharing options...
myrok24 Posted January 19, 2009 Author Share Posted January 19, 2009 More detailed, for a complete image. In another page i get the category and client list.: grouped by category: <? <a href="index.php?pagina=portfolio&category=<?php echo $category;?>"><? echo $category ?></a> ?> and grouped by client (id): <? <a href="index.php?pagina=portfolio&id=<?php echo $id;?>"><? echo $klant ?></a> ?> Wich i then get in another php page, with this code: <? $query = " SELECT id, name, label, tekst, category, klant, DATE_FORMAT(datum, '%d-%m-%Y') AS nl_datum FROM upload "; if(isset($_GET['id'])) { $query .= " WHERE id = '".mysql_real_escape_string($_GET['id'])."'"; } elseif(isset($_GET['category'])) { $query .= " WHERE category = '".mysql_real_escape_string($_GET['category'])."'"; } else { echo 'Geen id of categorie opgegeven'; } $result = mysql_query($query) or die('Error : ' . mysql_error()); $myrow = mysql_fetch_assoc($result); ?> <!-- ### SHOW WORK ######--> <div > <div> <h3> <? echo $myrow['klant'] ?></h3> <p class="newsdate">Geplaatst op: <? echo $myrow['nl_datum'] ?></p> <p class="newsdate">Middel: <? echo $myrow['category'] ?></p> <p class="newsdate">Opdracht: <? echo $myrow['label'] ?></p> <? echo '<img class=plaatje src="image/' . $myrow['name'] . '" />' ; ?> <p class="newstext"> <? echo ($myrow['tekst']) ?> </p> </div> </div> ?> And now i want to "skip" within category OR within all "id's". Based on the user choice in the other page (first codes) But i don't understand how i should do this with pagination.. wich is mostly based on one choice. preview: http://blikvoer.com/temp/samtest/index.php?pagina=portfolio Link to comment https://forums.phpfreaks.com/topic/140681-solved-nextprevious-links/#findComment-740305 Share on other sites More sharing options...
myrok24 Posted January 20, 2009 Author Share Posted January 20, 2009 ok am back. I splitted my previous code to two different ones (to keep it a bit more orchanized for me) But still i have this "page" question (in the url..) My code now (for one of the pages (klanten.php ->clients) <? if (!isset($_GET['klant'])) { // kijken of klant is verkregen echo "klant niet gedefineerd"; } else { // category pakken $query = " SELECT id, name, label, tekst, category, klant, DATE_FORMAT(datum, '%d-%m-%Y') AS nl_datum FROM upload WHERE klant = '".mysql_real_escape_string($_GET['klant'])."' ORDER BY datum DESC "; $result = mysql_query($query) or die('Error : ' . mysql_error()); while ($myrow = mysql_fetch_assoc($result)) { ?> <!-- ### SHOW WORK ######--> <div style="width:500px;background-color:#0CF; float:right"> <div > <div> <h3> <? echo $myrow['klant'] ?></h3> <p class="newsdate">Geplaatst op: <? echo $myrow['nl_datum'] ?></p> <p class="newsdate">Middel: <? echo $myrow['category'] ?></p> <p class="newsdate">Opdracht: <? echo $myrow['label'] ?></p> <? echo '<img class=plaatje src="image/' . $myrow['name'] . '" />' ; ?> <p class="newstext"> <? echo ($myrow['tekst']) ?> </p> </div> </div> </div> <?php } // while afsluiten. } // (else..) aflsuiten.. ?> I've tried several examples concerning pagination. But i'm stuck at this part: if(!isset($_GET['page'])){ $page = 1; } else { $page = $_GET['page']; } Because i'm already sending a variable (if (!isset($_GET['klant']))... ) in the page before: <a href="index.php?pagina=klanten&klant=<?php echo $klant;?>"><? echo $klant ?> I just don't get (understand) how to rebuilt this for my code.. example: http://blikvoer.com/temp/samtest/index.php?pagina=klanten&klant=Anwb Link to comment https://forums.phpfreaks.com/topic/140681-solved-nextprevious-links/#findComment-740995 Share on other sites More sharing options...
myrok24 Posted January 20, 2009 Author Share Posted January 20, 2009 It's almost working now. Only problem is the next (previous ed..) button. It's not giving the right url output. whole code: <? if (isset($_GET['pagina'])) { $pagina = $_GET['pagina']; } else { $pagina = 1; } $query = "SELECT count(*) FROM upload WHERE klant = '".mysql_real_escape_string($_GET['klant'])."' "; $result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR); $query_data = mysql_fetch_row($result); $numrows = $query_data[0]; $rows_per_page = 1; $lastpage = ceil($numrows/$rows_per_page); $pagina = (int)$pagina; if ($pagina > $lastpage) { $pagina = $lastpage; } if ($pagina < 1) { $pagina = 1; } $limit = 'LIMIT ' .($pagina - 1) * $rows_per_page .',' .$rows_per_page; $query = " SELECT id, name, label, tekst, category, klant, DATE_FORMAT(datum, '%d-%m-%Y') AS nl_datum FROM upload WHERE klant = '".mysql_real_escape_string($_GET['klant'])."' $limit"; $result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR); while ($myrow = mysql_fetch_assoc($result)) { ?> <!-- ### SHOW WORK ######--> <div style="width:500px;background-color:#0CF; float:right"> <div > <div> <h3> <? echo $myrow['klant'] ?></h3> <p class="newsdate">Geplaatst op: <? echo $myrow['nl_datum'] ?></p> <p class="newsdate">Middel: <? echo $myrow['category'] ?></p> <p class="newsdate">Opdracht: <? echo $myrow['label'] ?></p> <? echo '<img class=plaatje src="image/' . $myrow['name'] . '" />' ; ?> <p class="newstext"> <? echo ($myrow['tekst']) ?> </p> </div> </div> </div> <?php } // (else..) close.. ?> <? if ($pagina == 1) { echo " EERSTE VORIGE"; } else { echo " <a href='{$_SERVER['PHP_SELF']}?pagina=1'>EERSTE</a> "; $prevpage = $pagina-1; echo " <a href='{$_SERVER['PHP_SELF']}?pagina=$prevpage'>VORIGE</a> "; } echo " ( pagina $pagina / $lastpage ) "; if ($pagina == $lastpage) { echo " VOLGENDE LAATSTE "; } else { $nextpage = $pagina+1; echo " <a href='{$_SERVER['PHP_SELF']}?pagina=$nextpage'>VOLGENDE</a> "; echo " <a href='{$_SERVER['PHP_SELF']}?pagina=$lastpage'>LAATSTE</a> "; } ?> I think the problem is in {$_SERVER['PHP_SELF']}, because the url is: ./index.php?pagina=2 instead of index.php?pagina=klanten&klant=Anwb?pagina=2 but i cannot find the exact problem.. Any idea? Link to comment https://forums.phpfreaks.com/topic/140681-solved-nextprevious-links/#findComment-741116 Share on other sites More sharing options...
myrok24 Posted January 20, 2009 Author Share Posted January 20, 2009 solved: <? if (isset($_GET['selectie'])) { $selectie = $_GET['selectie']; } else { $selectie = 1; } $query = "SELECT count(*) FROM upload WHERE klant = '".mysql_real_escape_string($_GET['klant'])."' "; $result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR); $query_data = mysql_fetch_row($result); $numrows = $query_data[0]; $rows_per_page = 1; $lastpage = ceil($numrows/$rows_per_page); $selectie = (int)$selectie; if ($selectie > $lastpage) { $selectie = $lastpage; } if ($selectie < 1) { $selectie = 1; } $limit = 'LIMIT ' .($selectie - 1) * $rows_per_page .',' .$rows_per_page; $query = " SELECT id, name, label, tekst, category, klant, DATE_FORMAT(datum, '%d-%m-%Y') AS nl_datum FROM upload WHERE klant = '".mysql_real_escape_string($_GET['klant'])."' $limit "; $result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR); while ($myrow = mysql_fetch_assoc($result)) { ?> <!-- ### WERK TONEN ######--> <div style="width:500px;background-color:#0CF; float:right"> <div > <div> <h3> <? echo $myrow['klant'] ?></h3> <p class="newsdate">Geplaatst op: <? echo $myrow['nl_datum'] ?></p> <p class="newsdate">Middel: <? echo $myrow['category'] ?></p> <p class="newsdate">Opdracht: <? echo $myrow['label'] ?></p> <? echo '<img class=plaatje src="image/' . $myrow['name'] . '" />' ; ?> <p class="newstext"> <? echo ($myrow['tekst']) ?> </p> </div> </div> </div> <!-- ### PAGINERING: volgende/totaal/volgende ######--> <div style="background-color:yellow"> <? if ($selectie == 1) { echo "<FONT color=grey size=1>VORIGE</FONT>"; //tekst als er geen vorige of eerste is } else { // echo " <a href='index.php?pagina=klanten&klant={$_GET['klant']}&selectie=1'> <FONT size=1>EERSTE</FONT></a> "; //eerste optie $prevpage = $selectie-1; echo " <a href='index.php?pagina=klanten&klant={$_GET['klant']}&selectie=$prevpage'><FONT size=1>VORIGE</FONT></a> "; //vorige optie } echo " <FONT size=1> || OPDRACHT <strong> $selectie </strong> / $lastpage || </FONT> "; if ($selectie == $lastpage) { echo "<FONT color=grey size=1>VOLGENDE</FONT>"; //tekst als er geen volgende of laatste is } else { $nextpage = $selectie+1; echo " <a href='index.php?pagina=klanten&klant={$_GET['klant']}&selectie=$nextpage'><FONT size=1>VOLGENDE</FONT></a> "; //volgende optie // echo " <a href='index.php?pagina=klanten&klant={$_GET['klant']}&selectie=$lastpage'><FONT size=1>LAATSTE</FONT></a> "; //laatste optie } ?></br>KLANTEN </div> <!-- ### PAGINERING EINDE ####--> <?php } // (while..) aflsuiten.. ?> Link to comment https://forums.phpfreaks.com/topic/140681-solved-nextprevious-links/#findComment-741483 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.