PHP - Paging System (very Noob Question)
Hello everyone!
I'm completely new to PHP and Databases. I've managed to do some nice work by reading, but now I've encountered a problem I can't solve. I made a site with trailers and got tierd of adding them manually. So I created a form which sends the trailer info to my database. There's now trailers in specific organized genres. But the list is beginning to get really big so I wanted to show just 10 trailers per page. And so I found out I could use a paging system which I ALMOST got working. Just one last thing that needs to be fixed and that is when I click on next page "http://www.xxxxxxxx.com/test.php&page=2" it doesn't show the content. While "http://www.xxxxxxxx.com/test.php" does. So I just need that next button to work, I don't know if theres something missing or what it is... Note: I'm new to all this so please excuse my very very bad coding. Here is my code: test.php <link rel="stylesheet" type="text/css" media="all" href="http://www.xxxxxxxxxxxxx.n.nu/createdfiles/trailers/style-twentyten.css" /> <link rel="stylesheet" type="text/css" media="all" href="http://www.xxxxxxxxxxxxx.com/paginate.css" /> <?php $rpp = 10; // results per page $adjacents = 4; $page = intval($_GET["page"]); if(!$page) $page = 1; $reload = $_SERVER['PHP_SELF']; // connect to your DB: $link_id = mysql_connect("server","username","my password"); mysql_select_db("database"); // select appropriate results from DB: $sql = "SELECT * FROM blablabla WHERE Genre LIKE '%Drama%' ORDER BY `blablabla`.`sort` DESC LIMIT 0, 9999"; $result = mysql_query($sql, $link_id); // count total number of appropriate listings: $tcount = mysql_num_rows($result); // count number of pages: $tpages = ($tcount) ? ceil($tcount/$rpp) : 1; // total pages, last page number $count = 0; $i = ($page-1)*$rpp; while(($count<$rpp) && ($i<$tcount)) { mysql_data_seek($result,$i); $query = mysql_fetch_array($result); // output each row: echo "<br />--------------------------------------------------------------------------------------------------------<br /><br />"; ?><strong> <img src=" <?php echo $query['Poster'];?>" /><?php echo "<br /> <br />"; ////////////////////////////// //SHOW TITLE, YEAR AND GENRE// ////////////////////////////// echo "<H1>"; echo $query['Title']; echo "<br /> <br />"; echo "</h1>"; ?></strong> <?php echo "<H2>"; echo $query['Year']; echo "</H2>"; echo "<br />"; echo "<H3>"; echo $query['Genre']; echo "</H3>"; ?> <a href=" <?php ////////////////////////////// //SHOW IMDB, ACTORS AND PLOT// ////////////////////////////// echo $query['IMDB']; ?> " target="_self">Link to IMDb</a> <?php echo "<br /> <br />"; echo "<H2>Stars:</H2>"; echo "<br />"; echo "<H3>"; echo $query['Actors']; echo "</H3>"; echo "<br />"; echo "<H2>Plot:</H2>"; echo "<br />"; echo "<H3>"; echo $query['Plot']; echo "</H3>"; echo "<br /> <br />"; echo "<H2>"; //////////////// //SHOW TRAILER// //////////////// ?> <div id="<?php echo $query['ID'];?>" style="display: none;"> <?php echo $query['Youtube']; echo "</div>"; ?> <a href="javascript:ReverseDisplay('<?php echo $query['ID'];?>')">Click to show/hide trailer.</a> <?php echo "</H2>"; echo "<br /> <br />"; $i++; $count++; } // call pagination function: include("paginate_one.php"); echo paginate_one($reload, $page, $tpages); ?> <?php //////////////////////////////// //SCRIPT FOR SHOW/HIDE TRAILER// //////////////////////////////// ?> <script type="text/javascript" language="JavaScript"><!-- function HideContent(d) { document.getElementById(d).style.display = "none"; } function ShowContent(d) { document.getElementById(d).style.display = "block"; } function ReverseDisplay(d) { if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = "block"; } else { document.getElementById(d).style.display = "none"; } } //--></script> paginate_one.php <?php /************************************************************************* php easy :: pagination scripts set - Version One ========================================================================== Author: php easy code, www.phpeasycode.com Web Site: http://www.phpeasycode.com Contact: webmaster@phpeasycode.com *************************************************************************/ function paginate_one($reload, $page, $tpages) { $firstlabel = "First"; $prevlabel = "Prev"; $nextlabel = "Next"; $lastlabel = "Last"; $out = "<div class=\"pagin\">\n"; // first if($page>1) { $out.= "<a href=\"" . $reload . "\">" . $firstlabel . "</a>\n"; } else { $out.= "<span>" . $firstlabel . "</span>\n"; } // previous if($page==1) { $out.= "<span>" . $prevlabel . "</span>\n"; } elseif($page==2) { $out.= "<a href=\"" . $reload . "\">" . $prevlabel . "</a>\n"; } else { $out.= "<a href=\"" . $reload . "&page=" . ($page-1) . "\">" . $prevlabel . "</a>\n"; } // current $out.= "<span class=\"current\">Page " . $page . " of " . $tpages . "</span>\n"; // next if($page<$tpages) { $out.= "<a href=\"" . $reload . "&page=" .($page+1) . "\">" . $nextlabel . "</a>\n"; } else { $out.= "<span>" . $nextlabel . "</span>\n"; } // last if($page<$tpages) { $out.= "<a href=\"" . $reload . "&page=" . $tpages . "\">" . $lastlabel . "</a>\n"; } else { $out.= "<span>" . $lastlabel . "</span>\n"; } $out.= "</div>"; return $out; } ?> Here you can view a test of it all in action: http://www.mansgullberg.com/test.php please dont hack my site I'm thankfull for all of your answers! Really nice forum this! /kvickan Similar TutorialsAny one no a simple way to create a numbering system for a basic comment system. Code: [Select] <body> <?php $commentsTable = "comments"; $comments = "SELECT * FROM $commentsTable"; $commentResults = mysql_query($comments); $commentRows = mysql_fetch_array($commentResults); ?> <div id="BodyWrapper"> <?php while($commentRows = mysql_fetch_array($commentResults)){?> <div id="comments"> <div id="CommentWrapper"> <div id="comment"> <div id="UserName"><? echo $commentRows['name'];?></div> <div id="UserComment"><? echo $commentRows['comment'];?></div> <div id="UserEmail"><? echo $commentRows['email'];?></div> <div id="PostDateTime"><? echo $commentRows['datatime'];?></div> </div> </div> </div> <?php } mysql_close(); ?> </div> </body> This is what i have, i want to create a limit of 5 comment but with nummber so you can view older posts. hello dear PHP-Fans - greetings to you - and a happy new year!! i set up a WAMP-System on my openSuse 11.4 system. In order to learn as much as i can bout PHP i want to do some tests and write some scripts. Well the WAMP is allready up and running. Now i try to give the writing access to the folder mkdir /srv/www/ where the php-scripts should go in... i want to give write permission to all to all files in /srv/www As root I generally: mkdir /srv/www/ chown <webmaster usrername> /srv/www/ /srv/www/ should be readable and traversable by all, but only writeable by it's owner (the user designated as the webmaster.) can i do this like mentioned above,... Love to hear from you greetings db1 Hi all i have a silly noob question.... When i directly query my database with the following i get the exact result i want....... Code: [Select] SELECT * FROM posts WHERE postTopic=3 AND postSubject=business; However when i try this using the following PHP....... $sql = "SELECT * FROM posts WHERE postTopic =" . mysql_real_escape_string($_GET['id']) . "AND postSubject='" . $_SESSION[forum] . "'"; I get no result I also get the correct results from $sql = "SELECT * FROM posts WHERE postTopic =" . mysql_real_escape_string($_GET['id']) . "; and $sql = "SELECT * FROM posts WHERE postSubject='" . $_SESSION[forum] . "'"; mysql_real_escape_string($_GET['id']) is an integer and $_SESSION[forum] is a string. I know this has something to do with the manner in which the query is passed with the AND and the string but i can't for the life of my work out where i am going wrong. Thanks for any help. OK, so I have a question about databases and how they sort things being loaded into them. I have a database that I have images uploaded to (some people dont like to do it this way but this is what is going to work for my project). I have it set up with an id column that auto-increments. When I upload the files for some reason it is putting the highest id number first and the second one is the lowest then it goes in order from there. Why would this be happening and how do I correct it? But how do I check input to make sure that the input has to be seperated by commas and has to be a integer like "1,2,3,4,5" is correct if a user puts "1,:25,,l2@" or something funky, I can error out Thanks also is there a php function that does this already? and is this way safe? im trying to store comma seperated id's I'm really sorry if this has been answered (I assume it has) I'm unfamiliar with search terms of what I am trying to do. I have this web page Code: [Select] <html> <body> <?php $con = mysql_connect("localhost","user","pass"); if (!$con) { echo "Fail"; die('Could not connect: ' . mysql_error()); } echo "Triumph"; ?> </body> </html> Which I got from a snippet. I load the webpage expecting to see fail or triumph, but I see nothing. My question is how to run the php script so I see something on the page (hopefully triumph. I apoligise for my noobness, I'm a PHP know nothing at the moment. OK so sorry if my understandings a bit off, but if a I want to introduce a class into my doc is the best way to fo this with the include() function? Long story short i'm paginating database results. So pages = results / res-per-page-limit but 28 / 10 will be 2.8 should i use round() or ceil()? I hope no one rolls their eyes at me, lol, but I literally, just made my first PHP script, but I ran into a snag.
Okay so the code I did was
<!DOCTYPE html> <html> <body> <?php echo <h1>"my First PHP script"</h1>; ?> </body> </html> but in firefox the outcome is always "my first PHP script?> I had added the h1 tags because it wouldn't print at all before I did. I'm trying to figure out what I did wrong. I'm almost embarrassed to ask such a simple question but I just cannot get this to work. I'm trying to figure out how to use sessions that takes the information from a form on the first page and displays it on the last. I have this on page 1: <?php session_start(); ?> <form action="test2.php" method="post"> <p>Page 1</p> <p>First Name <input type="text" name="first" /></p> <p><input type="submit" value="Next page 2" /></p> And this for page 2: <?php SESSION_START(); $_SESSION['first'] = $_POST['first']; echo $_SESSION['first'] = $_POST['first']; ?> <form action="test3.php" method="post"> <p>Page 2</p> <input type="submit" value="Next page 3" /> </form> And this for page 3: <?php SESSION_START(); $_SESSION['first'] = $_POST['first']; echo $_SESSION['first'] = $_POST['first']; ?> <p>Page 3</p> <p>You're done</p> I can get it to display on page 2 but not on page 3. Can anyone tell me why? I've been at this for a long time and it should be simple but I cannot get it to work. Thanks in advance. hey guys! I just got started with php (yesterday) and im currently trying to write a script that cuts email signatures off. The first filter ($sig[0]) works fine, the string is cut and posted- and, well, that's it. I think i did sth wrong within the for- but i cant find it. So please help me out. A hint would be fine already. Thanks in advance- and here is some code: Code: [Select] <?php $string="TESTSTRING!! I AM AN EMAIL TEXT!!! SO RANDOM! --- I AM A SIGNATURE!! HERP DERP CUT ME OFF"; //define possible sig starts (=filter) $sig[0]=("#=="); $sig[1]=("---"); $sig[2]=("the name of an enterprise"); $sig[3]=("___"); for ($i=0; $i<=3; $i++) { /* or count($sig) ? */ $sigpos=strpos($string,$sig[$i]); if($sigpos>0) { $string= substr($string,0,$sigpos); break; echo $string; } else { echo $string; break; } } ?> <br> <? //show where the sig is cut off echo $sigpos; ?> Hi everyone Well I've started learning php yesterday and now I encountered my first problem, I looked around but I don't really know what to search for if I want my answer so here I come Code: [Select] <html> <body> <?php $names = array('Peter','Quagmire','Joe'); $ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34); $who = rand(0,2); $who2 = $names[$who]; echo "$names[$who] is $ages[$who2] years old"; ?> </body> </html> This is the script I have, and there is the kind of script I expected: Code: [Select] <html> <body> <?php $names = array('Peter','Quagmire','Joe'); $ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34); $who = rand('Peter','Quagmire','Joe'); echo "$who is $ages[$who] years old"; ?> </body> </html> But after some search I concluded that rand is only used for random numbers So, my question is, which function should I use to achieve my goal? Is there any randstring('string1','string2',etc) command? Tnx for your time So I'm using simplexml_load_file($url) to load up a client's XML file so I can use it with a jQuery slide show right. My problem is that some of the strings within the xml files are often times sentences and when I echo, for example... Code: [Select] echo $xml->images->categories->category[$gallery]->images->image[0]->description[0]; I should see Dream as if you'll live forever, Live as if you'll die today but all I'm getting is Dream as if you It appears as though php is getting confused when it sees those little single quotes. Anyone? Hello there. I used this code so many times long ago, but I still don't remeber and don't know what I'm doing wrong. http://circus.ka-blooey.net/ If I try to click on any link below the post, I got the error: Code: [Select] Warning: include(.php) [function.include]: failed to open stream: No such file or directory in /home/kablooey/public_html/circus/index.php on line 5 Warning: include(.php) [function.include]: failed to open stream: No such file or directory in /home/kablooey/public_html/circus/index.php on line 5 Warning: include() [function.include]: Failed opening '.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/kablooey/public_html/circus/index.php on line 5 -----------------------x My index is: Code: [Select] <?php include("header.php"); ?> <? if (empty($_GET["boo"])) {$boo="news/show_news";} include "$boo.php"; ?> <?php include("footer.php"); ?> and the footer, where the links are is: Code: [Select] <br /> <div id="menu"><center><a href="http://circus.ka-blooey.net">clear</a> ---x--- <a href="?boo=out">links out</a> ---x--- <a href="?boo=credits">credits</a></center></div><br /> <br /> <br /> <div id="footer"><img src="http://circus.ka-blooey.net/layout/circus_footer.jpg" /></div> </div> </div> </div> </body> </html> Help please? =/ Hi guys, Sorry - I know this is a basic question but I've been out of the game for a year now and need to jog my memory. I am trying to count the number of rows per group. e.g: $sql = "SELECT *, COUNT(id) FROM image GROUP BY city"; $result = mysql_query($sql); $row = mysql_fetch_assoc($result); echo $row['COUNT(id)']; So if there are, for example, 5 New York's in the table, 3 London's and 2 Sydney's I want the echo to display 5, 3, 2 Should I use a loop for this? Cheers hey guys! Im trying to get hashtags out of a string. The function works so far- but i cant transfer the insides of the preg_match_all array into the string. A hint would be fine already. Thanks in advance- and here is some code Code: [Select] <?php //example string $strcontent = "ima string... #wat #taggy #taggytag im in your stringz, stealing your charz!"; //find hashtags preg_match_all("/(#\w+)/", $strcontent, $matches); echo $matches; //the output is just "array" -> why? foreach ($matches as $match) { // $tempmatch=$match[1]; #####like this? //hiding the hashtags via span $strtemp="<br>ima span<br>" . $match . "<br>ima /span<br>" . $strcontent; $strcontent = $strtemp; echo $strcontent; } #echo $strcontent; # <span style="display:none;"></span> ?> I have just a general question about a friends system here with regards to the database design....... lets say you have a website where users can rent books. The database design would be MEMBER BOOK LINk memberid isbn memberid name name isbn Or something to that extent Now with a friends system like facebook has that "LINK" table would be absolutley huge and I cant see it being good design practise. I mean how do you suppose facebook does this. Do they have ...... FRIENDS myID friendID and just millions upon millions of repeated data? The only way I can see would be in the member table have a field called friends and then the ID's of each friend like so Friends 001, 002, 003, 004, 005, 006, 007, Now is my logic correct here or am I thinking of this totally the wrong way? Would the other way be a better option or would it take the system too long to get to the 1000,000 th record in the database? I believe the first option would be correct but I cant see how the server would handle the request if say you had 1000,000 users. It just seems to me that you would create too many records. In my website people can sign up to mulitple events but if every user had 100 events the link table would just be massive!!!!!!!!!!!!!!!!!!!!!! Im just after peoples input really on the situation. Thank you Hi Everyone, I need help in paging, i have a form with a lot of text boxes and drop down boxes, i need to use paging "Next..1..2..3", Please someone help me with it, i tried some codes but didn't work out. Thanks RAM I see several great PHP/MySQL paging systems on google, but how can I make a paging system when I'm just showing all images in a directory, not showing all images from a database? I have no idea how I could convert one of those mysql systems to work for my needs. Here is my code: <?php $columns = "4"; $count = "0"; echo '<div style="overflow:auto; width:500px; height:266px;"><table cellpadding="0" cellspacing="10">'; if ($handle = opendir('.')) { while (false !== ($image = readdir($handle))) { if ($image != "." && $image != ".." && $image != "index.php") { if (strlen($image) > 11) { $short = substr($image,0,11); echo '<td align="center"><a href="'.$image.'" title="'.$image.'"><img src="'.$image.'" width="100" height="100"><br><div style="overflow:auto; width:100px;">'.$short.'...</div></a></td>'; } else { echo '<td align="center"><a href="'.$image.'" title="'.$image.'"><img src="'.$image.'" width="100" height="100"><br><div style="overflow:auto; width:100px;">'.$image.'</div></a></td>'; } $count++; if ($count == 4) { echo "</tr><tr>"; $count = 0; } } } closedir($handle); } echo "</table></div>"; ?> This code shows all images in a directory, 4 per row. I'd really appreciate some help with adding a paging feature to this. Thank you so much for the help! so my database has a page number next to each field. What I need to know how to do is make it so that the paging number, ie being able to select the page, if greater than one, will display 1-whatever the highest page number is. does this make sense? any help is appreciated. |