Page 1 of 1

Paging big text with images

Posted: Wed Jul 11, 2007 9:30 pm
by Copo
Hello everybody!
I need help/solution to page big text with images from MySQL.I tried many things,but I can't do it :(

Here is the code:

Code: Select all

<?php
if(isset($_GET['page']) && (int)$_GET['page'] >= 1 && (int)$_GET['page'] < 100 ) {
      $page=(int)$_GET['page'];
} else {
      $page=1;
}

$show=1000; //Letters per page

$from = (($page * $show) - $show + 1);
$q = mysql_query("SELECT SUBSTRING(text,$from,$show) AS Page FROM test") or die(mysql_error());
$q2 = mysql_fetch_array(mysql_query("SELECT LENGTH(text) AS Len FROM test")) or die(mysql_error());
$total_results = (int)$q2['Len'];

if (mysql_num_rows($q) == 0) {
      echo "empty!";
} else {
      while ($fet = mysql_fetch_array($q)) {
            $the = $fet['Page'];
      }
}

echo $the;
echo "<br/>all:$total_results<br/>";
$total_pages = ceil($total_results / $show);
if($page > 1) {
      $prev = ($page - 1);
      echo "<a href='?page=$prev'><prev</a>&nbsp;";
}

for($i = 1; $i <= $total_pages; $i++) {
      if(($page) == $i) {
            echo "<b>[$i]</b>";
      } else {
            echo "&nbsp;<a href='?page=$i'>$i</a>&nbsp;";
      }
}

if($page < $total_pages) {
      $next = ($page + 1);
      echo "&nbsp;<a href='?page=$next'>next></a>";
}
?>
Problem is that sometimes the SQL cuts the image tag like this
<a href="test.html"><img src="test.png....
and the text after this goes like link (because the <a tag is unclosed). :(
Please someone to write me the solution/code.

p.p.Sorry for my bad english!

Greetings to all!

Posted: Thu Jul 12, 2007 12:54 am
by Christopher
You obviously need to remove partial HTML tags from the end of your buffer. The question is how to detect them. You could start with a quick count the <'s and >'s and in the buffer. If they match you at least have all complete tags. Then you would need to check to see if you had complete blocks that have an end tag. You could search from the end for opening tags and then search from that point forward to find a closing tag. Give it a try and post some code.

Posted: Thu Jul 12, 2007 1:45 am
by Copo
I tried with strripos and stripos,but the effect isn't so good. :(

Can you give me the code for exampe ? :roll:

Posted: Thu Jul 12, 2007 5:43 pm
by Copo
Up :)

Posted: Thu Jul 12, 2007 5:47 pm
by Christopher
Copo wrote:I tried with strripos and stripos,but the effect isn't so good. :(
Post your code and let's see what "isn't so good" means...

Posted: Fri Jul 13, 2007 7:37 am
by Copo
Here is the beta code:

Code: Select all

if(isset($_GET['page']) && (int)$_GET['page'] >= 1 && (int)$_GET['page'] < 100 ) {
$page=(int)$_GET['page'];
} else {
$page=1;
}
////////////////////////////////////
$q3 = mysql_fetch_array(mysql_query("SELECT text FROM test WHERE id = '$id'")) or die(mysql_error());
$q4 = mysql_fetch_array(mysql_query("SELECT LENGTH(text) AS Len FROM test WHERE id = '$id'")) or die(mysql_error());
$word2 = "</a>";
$word = "<a href=";
$checking = strripos($q3['text'], $word);
$checking2 = stripos($q3['text'], $word2);
if($checking !== false and $checking2 !== false) {
$show = ($checking + $checking2);
}
else {
$x = (int)$q4['Len'];
$show = $x;
}
////////////////////////////////////
$from = (($page * $show) - $show + 1);
$q = mysql_query("SELECT SUBSTRING(text,$from,$show) AS Page FROM test WHERE id = '$id'") or die(mysql_error());
$total_results = (int)$q4['Len'];
function limit($data, $limit )
{
if( strlen($data)>$limit )
{

$data = substr( $data,0,$limit );
$data = substr( $data,0,-(strlen(strrchr($data,'</a>'))) ).' ...';
}
return $data;
}

if (mysql_num_rows($q) == 0) {
echo "empty!";
} else {
while ($fet = mysql_fetch_array($q)) {
$the = $fet['Page'];
}
}
$the = str_replace('[b]', '<strong>', $the);
$the = str_replace('[/b]', '</strong>', $the);
$the = str_replace('[i]', '<i>', $the);
$the = str_replace('[/i]', '</i>', $the);
$the = str_replace('[u]', '<u>', $the);
$the = str_replace('[/u]', '</u>', $the);
$the = str_replace("''", "'", $the);
$the = nl2br($the);


$total_pages = ceil($total_results / $show);

if($page == 1) {
echo "".limit($the,$show)."";
}
else if($page > 1 and $page < $total_pages) {
echo "...".limit($the,$show)."";
}
else if($page == $total_pages) {
echo "...".limit($the,$show)."";
}
else {
}
if($page > 1) {
$prev = ($page - 1);
echo "<a href='?page=$prev&id=$id'>< prev</a>&nbsp;";
}

for($i = 1; $i <= $total_pages; $i++) {
if(($page) == $i) {
echo "<b>[$i]</b>";
} else {
echo "&nbsp;<a href='?page=$i&id=$id'>$i</a>&nbsp;";
}
}

if($page < $total_pages) {
$next = ($page + 1);
echo "&nbsp;<a href='?page=$next&id=$id'>next ></a>";
}

Posted: Sat Jul 14, 2007 5:23 pm
by Copo
Please someone to help me to find the right solution. :cry:

Posted: Sun Jul 15, 2007 6:29 am
by superdezign
The simplest way would be to remove ALL tags for the preview. To keep track of all tags being opened and closed,you'd need to deal with tokenization and matching, which is no simple task.

I haven't read your code at all, but what you should do is remove the tags, THEN chop the text.

Code: Select all

// Removing the tags...
$content = preg_replace('#<.*?>#', '', $content);

Posted: Sun Jul 15, 2007 5:36 pm
by Copo
Okey,but I don't want to remove the <img and <a tags.I want users to view everything.