Paging big text with images

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Copo
Forum Newbie
Posts: 6
Joined: Wed Jul 11, 2007 9:20 pm

Paging big text with images

Post 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!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
Copo
Forum Newbie
Posts: 6
Joined: Wed Jul 11, 2007 9:20 pm

Post by Copo »

I tried with strripos and stripos,but the effect isn't so good. :(

Can you give me the code for exampe ? :roll:
Copo
Forum Newbie
Posts: 6
Joined: Wed Jul 11, 2007 9:20 pm

Post by Copo »

Up :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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...
(#10850)
Copo
Forum Newbie
Posts: 6
Joined: Wed Jul 11, 2007 9:20 pm

Post 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>";
}
Copo
Forum Newbie
Posts: 6
Joined: Wed Jul 11, 2007 9:20 pm

Post by Copo »

Please someone to help me to find the right solution. :cry:
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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);
Copo
Forum Newbie
Posts: 6
Joined: Wed Jul 11, 2007 9:20 pm

Post by Copo »

Okey,but I don't want to remove the <img and <a tags.I want users to view everything.
Post Reply