masgas Posted December 3, 2006 Share Posted December 3, 2006 Hello!I managed to page results from a mysql DB. Now the question is... I have a page where text is going to be loaded, is there a way to paging this text if it is very long...What is the rough idea to start with...thank you in advance! Quote Link to comment https://forums.phpfreaks.com/topic/29301-solved-paging-text/ Share on other sites More sharing options...
chiprivers Posted December 3, 2006 Share Posted December 3, 2006 Do you mean that the whole text will be in one database column? If so, how about counting number of characters in returned string, then just displaying the first x number of characters. Then put a link to goto next page and create a rule so that it would show the next x number of characters. Quote Link to comment https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-134292 Share on other sites More sharing options...
masgas Posted December 3, 2006 Author Share Posted December 3, 2006 that sound just like what I need... which function counts characters?I guess, the rule for the different pages would be similar to a "paging results" from any DB...but I'm a bit lost in the count characters, where to start... Quote Link to comment https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-134296 Share on other sites More sharing options...
chiprivers Posted December 3, 2006 Share Posted December 3, 2006 strlen($string); Quote Link to comment https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-134300 Share on other sites More sharing options...
chiprivers Posted December 3, 2006 Share Posted December 3, 2006 Actually that will return the number of characters and I guess to prevent splitting any words, really you will want the number of words. Not sure if there is a function to count words in a string but you could explode string using the space as the delimiter and then count the number of words in the new array. You could then display the first x number of values in the array remembering to put a space between each value. Quote Link to comment https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-134303 Share on other sites More sharing options...
craygo Posted December 3, 2006 Share Posted December 3, 2006 substr is the function or the combination of both. You could check the leng of the string and if it too long cut it down[code]<?php$string = "The fox jumped over the lazy dog";$maxlength = 10;$strlength = strlen($string);if($strlength > $maxlength){$newstring = substr($string, 0, $maxlength)."...";} else {$newstring = $string;}?>[/code]Now what ever you put in for $maxlength, the string will be cut off there. Also substitute my fox statement with your string field. :)You could also make a function from this[code]<?phpfunction substring($string, $maxlength){$strlength = strlen($string);if($strlength > $maxlength){$newstring = substr($string, 0, $maxlength)."...";} else {$newstring = $string;}return $newstring;}?>[/code]now you could call your field like soecho substring($yourtextfield, $length);Ray Quote Link to comment https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-134308 Share on other sites More sharing options...
masgas Posted December 3, 2006 Author Share Posted December 3, 2006 thank you...I will start working on this and let you know how it goes!!! Quote Link to comment https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-134309 Share on other sites More sharing options...
masgas Posted December 3, 2006 Author Share Posted December 3, 2006 right!I worked on it and it seems that strlen() function gives me a number of characters... the question is... how do I get the text printed... cause if I print the variable that contains the strlen () just appears the number of characters that the message in the DB has...if (!isset($_GET['page'])){ $page = 1; } else {$page = $_GET['page']; } $max_results = 1;$from = (($page * $max_results) - $max_results);mysql_connect("$host", "$username", "$password")or die("no conn a BD"); mysql_select_db("$db_name")or die("no puede seleccionar BD");$sql="SELECT * FROM $tbl_name ORDER BY id DESC LIMIT $from, $max_results";$result=mysql_query($sql);while ($row = mysql_fetch_array($result)) { $id = $row["id"]; $string = $row["topic"]; }$message = strlen($string);echo $message;(after this I have the "page select" part of the script only) Quote Link to comment https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-134354 Share on other sites More sharing options...
chiprivers Posted December 3, 2006 Share Posted December 3, 2006 OK, what you need to do is then get a sub string from the original if the original string is too long#1 get string length using strlen()#2 check against maximum length allowable#3 if too long take first x number of characters from original string using substr()#4 display new string Quote Link to comment https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-134360 Share on other sites More sharing options...
masgas Posted December 3, 2006 Author Share Posted December 3, 2006 I got this code now... but I can't get it to print a regular interval of text, it keeps on adding 25 characters to the ones I already printed... I want to print only the next 25 characters in the message!!any ideas?if (!isset($_GET['page'])){ $page = 1; } else {$page = $_GET['page']; } $max_results = 5;$from = (($page * $max_results) - $max_results);mysql_connect("$host", "$username", "$password")or die("no conn DB"); mysql_select_db("$db_name")or die("no DB");$sql="SELECT * FROM $tbl_name ORDER BY id DESC LIMIT 1";$result=mysql_query($sql);while ($row = mysql_fetch_assoc($result)) { $id = $row["id"]; $string = $row["detail"]; }$message = strlen($string);if ($message > 25){$partX=$page*25;$part=($partX)-25;echo "<table width='300' height='455'><tr><td>".substr($string, $part, $partX)."</td></tr></table>";}//echo $string;?><?php$total_results= strlen($string);$total_pages = ceil ($total_results / $max_results);echo "<center>Page<br />";if ($page > 1){$prev = ($page - 1);echo "<a href='".$_SERVER['PHP_SELF']."?page=".$prev."'><<prev</a>";}for ($i = 1; $i <= $total_pages; $i++){if (($page) == $i){echo "$i ";} else {"<a href='".$_SERVER['PHP_SELF']."?page=".$i.">".$i."</a>";}}if ($page < $total_pages){$next = ($page + 1);echo "<a href='".$_SERVER['PHP_SELF']."?page=".$next."'>+>></a>";}echo "</center>";mysql_close();?> Quote Link to comment https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-134395 Share on other sites More sharing options...
masgas Posted December 3, 2006 Author Share Posted December 3, 2006 any ideas, I'm lost right now!???? Quote Link to comment https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-134418 Share on other sites More sharing options...
masgas Posted December 4, 2006 Author Share Posted December 4, 2006 any kind help :( Quote Link to comment https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-134773 Share on other sites More sharing options...
craygo Posted December 4, 2006 Share Posted December 4, 2006 So let me get this right, you are looping through all the rows in your DBtable and you want to print them all to a table in your browser while limiting the $string to 25 charactors. Correct??or are you looking to break the text apart???Ray Quote Link to comment https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-134786 Share on other sites More sharing options...
craygo Posted December 4, 2006 Share Posted December 4, 2006 OK try this out[code]<?php/****** Break function ******/function substring($string, $maxlength){$strlength = strlen($string);// divide the length by the limit$trows = ceil($strlength / $maxlength);$start = 0;$line = "";for($i=0; $i<$trows; $i++){$line .= substr($string, $start, $maxlength)."<br>";$start = $start + $maxlength;}return $line;}/******* End Function *******/$maxlength = 25;if (!isset($_GET['page'])){ $page = 1; } else {$page = $_GET['page']; } $max_results = 5;$from = (($page * $max_results) - $max_results);echo "<table width='300' height='455' border=1>" ;mysql_connect("$host", "$username", "$password")or die("no conn DB");mysql_select_db("$db_name")or die("no DB");$sql="SELECT * FROM $tbl_name ORDER BY id DESC LIMIT 1";$result=mysql_query($sql) or die(mysql_error());while ($row = mysql_fetch_assoc($result)) { $id = $row["id"]; $string = $row["detail"]; echo "<tr><td>".$id."</td><td>".substring($string, $maxlength)."</td></tr>";}echo "</table>";?><?php$total_results= strlen($string);$total_pages = ceil ($total_results / $max_results);echo "<center>Page";if ($page > 1){$prev = ($page - 1);echo "<a href='".$_SERVER['PHP_SELF']."?page=".$prev."'><<prev>>";}for ($i = 1; $i <= $total_pages; $i++){if (($page) == $i){echo "$i ";} else {"<a href='".$_SERVER['PHP_SELF']."?page=".$i.">".$i."[/url]";}}if ($page < $total_pages){$next = ($page + 1);echo "<a href='".$_SERVER['PHP_SELF']."?page=".$next."'>+>>";}echo "</center>";mysql_close();?>[/code]Ray Quote Link to comment https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-134811 Share on other sites More sharing options...
masgas Posted December 4, 2006 Author Share Posted December 4, 2006 I'll explain myself a little better... I have a text, let's say 600 chars, and I want to page it because is too long for the space in te iframe I'm using...so I'd like to page the text... when I use the script from your last post it prints the whole message... how could I limit the text to 250 chars for example?thank you very much for your help Quote Link to comment https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-134841 Share on other sites More sharing options...
chiprivers Posted December 4, 2006 Share Posted December 4, 2006 So you will have a list of articles returned with a title and then a snippet of the text displayed for each? When you click on the text or a link, it will take you to the full body of text for that article? Quote Link to comment https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-134845 Share on other sites More sharing options...
masgas Posted December 4, 2006 Author Share Posted December 4, 2006 aha! something like that!I've got a table with links on one side, and when I click on that link an article appears on the iframe... but this articles can have up to 20 paragraphs which is a lot for a 300 by 400 pixel box.. so the idea is page the text in the box...does it help? Quote Link to comment https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-134857 Share on other sites More sharing options...
craygo Posted December 4, 2006 Share Posted December 4, 2006 OK I think I got it for ya[code]<?php/*********** Start function *********************/function substring($string, $start, $maxlength){$strlength = strlen($string);// divide the length by the limit$trows = ceil($strlength / $maxlength);for($i=0; $i<$trows; $i++){$line = substr($string, $start, $maxlength)."<br>";}return $line;}/************** End Function ******************/$maxlength = 25;if (!isset($_GET['page'])){ $page = 1; $start = 0; } else { $page = $_GET['page']; $start = ($page * $maxlength) - $maxlength; }echo "<table width='300' height='455'>" ;mysql_connect("$host", "$username", "$password")or die("no conn DB");mysql_select_db("$db_name")or die("no DB");$sql="SELECT * FROM $tbl_name ORDER BY id DESC LIMIT 1";$result=mysql_query($sql) or die(mysql_error());while ($row = mysql_fetch_assoc($result)) { $id = $row["id"]; $string = $row["detail"]; echo "<tr><td>".substring($string, $start, $maxlength)."</td></tr>";}echo "</table>";?><?php$stringlength = strlen($string);echo "<p align=center>";// Sets link for previous 25 and return to page 1 if($page != 1){ $pageprev = ($page - 1); echo "<a href=\"".$_SERVER['PHP_SELF']."?page=1\"><<</a> "; echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$pageprev\">PREV</a> "; }else{ echo "PREV "; }// Find out the total number of pages depending on the limit set$numofpages = ($stringlength / $maxlength);$totalpages = round($numofpages);// Loop thru all the pages and echo out the links for($i = 1; $i <= $numofpages; $i++){ if($i == $page){ echo $i." "; }else{ echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> "; } }// Check for straglers after the limit blocks if(($stringlength % $maxlength) != 0){ if($i == $page){ echo $i." "; }else{ echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> "; } }// Print out the Next 25 and Goto Last page links if(($stringlength - ($maxlength * $page)) > 0){ $pagenext = $page++; echo " <a href=\"".$_SERVER['PHP_SELF']."?page=$pagenext\">NEXT</a> "; echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$totalpages\">>></a> "; }else{ echo(" NEXT"); }echo "</p>";mysql_close();?>[/code]Try it outRay Quote Link to comment https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-134869 Share on other sites More sharing options...
masgas Posted December 4, 2006 Author Share Posted December 4, 2006 that did the trick!!!!only the next link is not working correctly! but the job was to get it printing right!I'll try to fix this next link and then post the full code for the rest of begginers like me... yo never know, someone may need something like thisTHANK YOU ALL Quote Link to comment https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-134879 Share on other sites More sharing options...
masgas Posted December 4, 2006 Author Share Posted December 4, 2006 [color=orange]//This code selects an article from a DB and prints 250 chars at a time[/color]<?php$host="l"; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name=""; // Database name $tbl_name=""; // Table name /*********** Start function *********************/function substring($string, $start, $maxlength){$strlength = strlen($string);// divide the length by the limit$trows = ceil($strlength / $maxlength);for($i=0; $i<$trows; $i++){$lineB = substr($string, $start, $maxlength)."";$lineA = explode (" ", $lineB, 60);array_pop($lineA);$line = implode (" ", $lineA);}return $line;}/************** End Function ******************/$maxlength = 250;if (!isset($_GET['page'])){ $page = 1; $start = 0; } else { $page = $_GET['page']; $start = ($page * $maxlength) - $maxlength; }echo "<table width='300' height='455'>" ;mysql_connect("$host", "$username", "$password")or die("no conn DB");mysql_select_db("$db_name")or die("no DB");$sql="SELECT * FROM $tbl_name ORDER BY id DESC LIMIT 1";$result=mysql_query($sql) or die(mysql_error());while ($row = mysql_fetch_assoc($result)) { $id = $row["id"]; $string = $row["detail"]; echo "<tr><td>".substring($string, $start, $maxlength)."...</td></tr>";}echo "</table>";?><?php$stringlength = strlen($string);echo "<p align=center>";// Sets link for previous 25 and return to page 1 if($page != 1){ $pageprev = ($page - 1); echo "<a href=\"".$_SERVER['PHP_SELF']."?page=1\">primera</a> "; echo "<a href=\"".$_SERVER['PHP_SELF']."?page=".$pageprev."\">previa</a> "; }else{ echo " "; }// Find out the total number of pages depending on the limit set$numofpages = ($stringlength / $maxlength);$totalpages = round($numofpages);// Loop thru all the pages and echo out the links for($i = 1; $i <= $numofpages; $i++){ if($i == $page){ echo $i." "; }else{ echo "<a href=\"".$_SERVER['PHP_SELF']."?page=".$i."\">".$i."</a> "; } }// Check for straglers after the limit blocks if(($stringlength % $maxlength) != 0){ if($i == $page){ echo $i." "; }else{ echo "<a href=\"".$_SERVER['PHP_SELF']."?page=".$i."\">".$i."</a> "; } }// Print out the Next 25 and Goto Last page links if(($stringlength - ($maxlength * $page)) > 0){ $pagenext = ++$page; echo " <a href=\"".$_SERVER['PHP_SELF']."?page=".$pagenext."\">siguiente</a> "; echo "<a href=\"".$_SERVER['PHP_SELF']."?page=".$totalpages."\">última</a> "; }else{ echo(" "); }echo "</p>";mysql_close();?> Quote Link to comment https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-134912 Share on other sites More sharing options...
craygo Posted December 6, 2006 Share Posted December 6, 2006 This will break up the text by whole words for you[code]<?php/*********** Start function *********************/function substring($string, $maxlength, $page){$strlength = strlen($string);// divide the length by the limit and add 1 incase$trows = ceil($strlength / $maxlength) +1;$start = 0;$data = array();for($i=1; $i<=$trows; $i++){$line = substr($string, $start, $maxlength);$pos = strrpos($line, ' ');$data[$i] = substr($string, $start, $pos);$string = substr($string, $pos);}return $data[$page];}/************** End Function ******************/$tbl_name = "htmlpage";$maxlength = 25;if (!isset($_GET['page'])){ $page = 1; } else { $page = $_GET['page']; }echo "<table width='300' height='455' border=1>" ;mysql_connect("$host", "$username", "$password")or die("no conn DB");mysql_select_db("$db_name")or die("no DB");$sql="SELECT * FROM $tbl_name ORDER BY id DESC LIMIT 1";$result=mysql_query($sql) or die(mysql_error());while ($row = mysql_fetch_assoc($result)) { $string = $row["text1"]; echo "<tr><td>".substring($string, $maxlength, $page)."</td></tr>";}echo "</table>";?><?php$stringlength = strlen($string);echo "<p align=center>";// Sets link for previous 25 and return to page 1 if($page != 1){ $pageprev = ($page - 1); echo "<a href=\"".$_SERVER['PHP_SELF']."?page=1\"><<</a> "; echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$pageprev\">PREV</a> "; }else{ echo "PREV "; }// Find out the total number of pages depending on the limit set$numofpages = ($stringlength / $maxlength)+1;$totalpages = round($numofpages);// Loop thru all the pages and echo out the links for($i = 1; $i <= $numofpages; $i++){ if($i == $page){ echo $i." "; }else{ echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> "; } }// Check for straglers after the limit blocks if(($stringlength % $maxlength) != 0){ if($i == $page){ echo $i." "; }else{ echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> "; } }// Print out the Next 25 and Goto Last page links if(($stringlength - ($maxlength * $page)) > 0){ $pagenext = $page+1; echo " <a href=\"".$_SERVER['PHP_SELF']."?page=$pagenext\">NEXT</a> "; echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$totalpages\">>></a> "; }else{ echo(" NEXT"); }echo "</p>";mysql_close();?>[/code]Ray Quote Link to comment https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-136138 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.