
itsrien
New Members-
Posts
6 -
Joined
-
Last visited
Never
Everything posted by itsrien
-
I'am using Adam's pagination script from: http://www.developphp.com/view_lesson.php?v=289 And I adjusted the code just a little, so it fitted my needs. Now I bumb into a problem, and I can't figure it out, how to let the script doing its job right. The adjusted pagination script pulls a list of links, from my database from a certain category. The pagination script is called from a link on another page, so that other categories can be choosen there. In the url of that link are the variables needed for the the pagination script to doing it's work. (see example bellow:) http://localhost/database/links-view.php?ID=73&pid=2&aid=1 The pagination script actually pulls data from my database, but only the first result page is shown, (only the 1e 10 result) when I try to click on the next button, to show the next 10 results, a empty page is shown. And the following error code appears: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/database/links-view.php on line 79 The problem is that the variables ID, pid and aid are not posted/pass on to the rest of the script. My question is. How can I adjust the script so those variables are pass on/posted corectly, so the script can do it's work? Bellow the adjusted code of Adam's pagination script: <?php include "connect.php"; include "var.php"; $sql = mysql_query("SELECT * FROM linkpartners WHERE ID='$ID' AND pid='$pid' AND aid='$aid' ORDER BY lpid ASC"); //////////////////////////////////// Adam's Pagination Logic //////////////////////////////////////////////////////////////////////// $nr = mysql_num_rows($sql); // Get total of Num rows from the database query if (isset($_GET['pn'])) { // Get pn from URL vars if it is present $pn = preg_replace('#[^0-9]#i', '', $_GET['pn']); // filter everything but numbers for security(new) } else { // If the pn URL variable is not present force it to be value of page number 1 $pn = 1; } //This is where we set how many database items to show on each page $itemsPerPage = 10; // Get the value of the last page in the pagination result set $lastPage = ceil($nr / $itemsPerPage); // Be sure URL variable $pn(page number) is no lower than page 1 and no higher than $lastpage if ($pn < 1) { // If it is less than 1 $pn = 1; // force if to be 1 } else if ($pn > $lastPage) { // if it is greater than $lastpage $pn = $lastPage; // force it to be $lastpage's value } // This creates the numbers to click in between the next and back buttons // This section is explained well in the video that accompanies this script $centerPages = ""; $sub1 = $pn - 1; $sub2 = $pn - 2; $add1 = $pn + 1; $add2 = $pn + 2; if ($pn == 1) { $centerPages .= ' <span class="pagNumActive">' . $pn . '</span> '; $centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> '; } else if ($pn == $lastPage) { $centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> '; $centerPages .= ' <span class="pagNumActive">' . $pn . '</span> '; } else if ($pn > 2 && $pn < ($lastPage - 1)) { $centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub2 . '">' . $sub2 . '</a> '; $centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> '; $centerPages .= ' <span class="pagNumActive">' . $pn . '</span> '; $centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> '; $centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add2 . '">' . $add2 . '</a> '; } else if ($pn > 1 && $pn < $lastPage) { $centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> '; $centerPages .= ' <span class="pagNumActive">' . $pn . '</span> '; $centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> '; } // This line sets the "LIMIT" range... the 2 values we place to choose a range of rows from database in our query $limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage; // Now we are going to run the same query as above but this time add $limit onto the end of the SQL syntax // $sql2 is what we will use to fuel our while loop statement below $sql2 = mysql_query("SELECT * FROM linkpartners WHERE ID='$ID' AND pid='$pid' AND aid='$aid' ORDER BY lpid ASC $limit"); //////////////////////////////// END Adam's Pagination Logic //////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////// Adam's Pagination Display Setup ///////////////////////////////////////////////////////////////////// $paginationDisplay = ""; // Initialize the pagination output variable // This code runs only if the last page variable is ot equal to 1, if it is only 1 page we require no paginated links to display if ($lastPage != "1"){ // This shows the user what page they are on, and the total number of pages $paginationDisplay .= 'Page <strong>' . $pn . '</strong> of ' . $lastPage. ' '; // If we are not on page 1 we can place the Back button if ($pn != 1) { $previous = $pn - 1; $paginationDisplay .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $previous . '"> Back</a> '; } // Lay in the clickable numbers display here between the Back and Next links $paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>'; // If we are not on the very last page we can place the Next button if ($pn != $lastPage) { $nextPage = $pn + 1; $paginationDisplay .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $nextPage . '"> Next</a> '; } } ///////////////////////////////////// END Adam's Pagination Display Setup /////////////////////////////////////////////////////////////////////////// // Build the Output Section Here $headerslist .= '<tr><td class="view-ctd">lpid</td> <td class="view-ctd">Url</td> </tr>'; $outputList = ''; while($row = mysql_fetch_array($sql2)){ $ID = $row["ID"]; $pid = $row["pid"]; $aid = $row["aid"]; $lpid = $row["lpid"]; $url = $row["url"]; $title = $row["title"]; $outputList .= '<tr><td class="view-dct">' . $lpid . '</td> <td class="view-dct"><a href="' . $url . '"target="_blank">' . $title . '</a></td> </tr>'; } // close while loop ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Page Title</title> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <link rel="StyleSheet" type="text/css" media="screen" href="styles.css" /> <style type="text/css"> </style> </head> <body> <div style="margin-left:64px; margin-right:64px;"> <h2>Total Items: <?php echo $nr; ?></h2> </div> <div style="margin-left:58px; margin-right:58px; padding:6px; background-color:#FFF; border:#999 1px solid;"><?php echo $paginationDisplay; ?></div> <div style="margin-left:64px; margin-right:64px;"><table><?php print "$headerslist"; ?></div> <div style="margin-left:64px; margin-right:64px;"><?php print "$outputList"; ?></table></div> <div style="margin-left:58px; margin-right:58px; padding:6px; background-color:#FFF; border:#999 1px solid;"><?php echo $paginationDisplay; ?></div> </body> </html>
-
Thanks you NoDog for providing me the solution, and solving my problem. The following code, did do the trick for me. <?php $var = 'volkswagen-golf-gti'; $regexp = array( '/-/' => ' ', '/.*/e' => 'ucwords("$0")', '/\b\S+$/e' => 'strtoupper("$0")' ); $var2 = preg_replace(array_keys($regexp), $regexp, $var); echo $var2; ?>
-
Hello all, I try to accomplish the following. I have a string like: “volkswagen-golf-gti” ** And I want it to change the string into: “Volkswagen Golf GTI” (last part completely uppercase) But I have some difficulties understanding preg_replace. Right now I have the following code: <?php $string = 'volkswagen-golf-gti'; $string2 = ucwords(str_replace('-', ' ', $string)); $string2 = preg_replace("/\s+\S+$/e", "strtoupper('\\1')", $string2); echo $string2; ?> Without line 4 enabled, the result of the echo $string2 is: Volkswagen Golf Gti (is wrong, last part is not completely uppercase) With line 4 enabled, the last part of the $string is removed so echo $string2 gives me : Volkswagen Golf How can I change the code so it select the last part of $string2 and makes that part uppercase? So the result of echo $string2; will be: Volkswagen Golf GTI ** $string can be anything, not only volkswagen-golf-gti
-
HowTo Dynamic Select Menus With PHP, and remembered last option set.
itsrien replied to itsrien's topic in PHP Coding Help
Thank you so much for the answers already, but unfortunately the change didn't bring me the result I had hoped for. The result is still posted correctly to the database, the only thing still not working is that the edit page pull down menu not showing the right value. Every time the page reload, it shows the default value. So for example: I have a link in the database called url: http://sports.com title: sports catname: sport When I submit the page, the data is stored correctly in de database. But when I reload the edit page again, it doesn't load the right value from the database. And only the default value (in this case it is cars) is showed. I can choose the right value, but I would like the correct value is showed. Bellow an example of the tables in the database: Table: links ID url title catparent 1 http:// buddy 3 2 http:// sports 2 3 http:// cars 1 Table: categories ID catname 1 cars 2 sport 3 friends The field catname from de table categories, contained the desired values for the pulldown menu. Bellow I attached the changed code, what do I need to change more to get it work? //SELECT AND EDIT CATEGORY $sql = "SELECT * FROM links"; $link_query = mysql_query($sql); $link = mysql_fetch_array($link_query); $sql = "SELECT * FROM categories ORDER BY catname"; $category_query = mysql_query($sql); echo '<select name="category">'; while( $category = mysql_fetch_array($category_query) ){ if( $link['catparent'] == $category['ID'] ){ $selected = ' selected="selected"'; } echo '<option value="'.$category['ID'].'"'.$selected.'>'.$category['catname'].'</option>'; } echo '</select><br>'; //SELECT AND EDIT CATEGORY Thanks in advance: Rien -
HowTo Dynamic Select Menus With PHP, and remembered last option set.
itsrien replied to itsrien's topic in PHP Coding Help
No body? -
I have a question about Dynamic Select Menus With PHP. De following I like to getting work: I have two tables: one is categories the other is named links. Now I have a edit page so I can edit the links. On that edit page I have a php pull down menu where I can change the category. This already working, the only problem is that it doesn't remember the last option I set. When I reload the page it always shows the first (default) option, from the selection list. How do I change the script, so the pull down menu remembers the last set option or load the right value from the database. Bellow I attached the code: Thanks in advance: Rien //SELECT AND EDIT CATEGORY $sql = "SELECT * FROM tb_links"; $link_query = mysql_query($sql); $link = mysql_fetch_array($link_query); $sql = "SELECT * FROM tb_categories ORDER BY catname"; $category_query = mysql_query($sql); echo '<select name="category">'; while( $category = mysql_fetch_array($category_query) ){ if( $link['catparent'] == $category['ID'] ){ $selected = 'selected="selected"'; } echo '<option value="'.$category['ID'].'"'.$selected.'>'.$category['catname'].'</option>'; } echo '</select><br>'; //SELECT AND EDIT CATEGORY