Jump to content

Recommended Posts

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.
Link to comment
https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-134292
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-134303
Share on other sites

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]<?php
function 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 so

echo substring($yourtextfield, $length);


Ray
Link to comment
https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-134308
Share on other sites

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)
Link to comment
https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-134354
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-134360
Share on other sites

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();
?>
Link to comment
https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-134395
Share on other sites

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
Link to comment
https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-134811
Share on other sites

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
Link to comment
https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-134841
Share on other sites

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?
Link to comment
https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-134857
Share on other sites

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>&nbsp;&nbsp;";
         echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$pageprev\">PREV</a>&nbsp;&nbsp;&nbsp;";
    }else{
        echo "PREV&nbsp;&nbsp;&nbsp;";
    }
// 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 "&nbsp;&nbsp;&nbsp;<a href=\"".$_SERVER['PHP_SELF']."?page=$pagenext\">NEXT</a>&nbsp;&nbsp;&nbsp;";
          echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$totalpages\">>></a>&nbsp;&nbsp;";
    }else{
        echo("&nbsp;&nbsp;&nbsp;NEXT");
    }
echo "</p>";
mysql_close();
?>[/code]

Try it out

Ray
Link to comment
https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-134869
Share on other sites

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 this

THANK YOU ALL
Link to comment
https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-134879
Share on other sites

[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>&nbsp;&nbsp;";
        echo "<a href=\"".$_SERVER['PHP_SELF']."?page=".$pageprev."\">previa</a>&nbsp;&nbsp;&nbsp;";
    }else{
        echo "&nbsp;&nbsp;&nbsp;";
    }
// 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 "&nbsp;&nbsp;&nbsp;<a href=\"".$_SERVER['PHP_SELF']."?page=".$pagenext."\">siguiente</a>&nbsp;&nbsp;&nbsp;";
          echo "<a href=\"".$_SERVER['PHP_SELF']."?page=".$totalpages."\">última</a>&nbsp;&nbsp;";
    }else{
        echo("&nbsp;&nbsp;&nbsp;");
    }
echo "</p>";
mysql_close();
?>
Link to comment
https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-134912
Share on other sites

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>&nbsp;&nbsp;";
        echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$pageprev\">PREV</a>&nbsp;&nbsp;&nbsp;";
    }else{
        echo "PREV&nbsp;&nbsp;&nbsp;";
    }
// 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 "&nbsp;&nbsp;&nbsp;<a href=\"".$_SERVER['PHP_SELF']."?page=$pagenext\">NEXT</a>&nbsp;&nbsp;&nbsp;";
          echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$totalpages\">>></a>&nbsp;&nbsp;";
    }else{
        echo("&nbsp;&nbsp;&nbsp;NEXT");
    }
echo "</p>";
mysql_close();
?>[/code]

Ray
Link to comment
https://forums.phpfreaks.com/topic/29301-solved-paging-text/#findComment-136138
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.