Jump to content

Formatting retrieved data from database


dstar101

Recommended Posts

How do I add Text Formating to the Data retrieved from the Database and How do I limit the text retrieved from the DB I mean I want to Display Only 900Kbytes of text and Display link if user want to read more

Here is my code:

<?php
session_start();
$state = $_SESSION['auth_state'];
$login = '';
if($state == 1){
$login = '<a href="../login/logout.php">Logout</a>';
}
elseif($state =! 1){
$login = '<a href="../login/login.php">Login</a>';
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Full Story</title>
<link href="../_css.css" rel="stylesheet" type="text/css" />
</head>
<?php require("../connectvars.php"); ?>
<?php
$author = $_GET['author'];
$id = $_GET['id'];
if(!empty($author) AND !empty($id)){
    
					  
$query = "SELECT * FROM info WHERE author='{$author}' AND id={$id} ;";
$cxn = mysql_connect(server, user, pass) or die("USer or password incorrect");
mysql_select_db("books") or die ("DB books not found");
$res = mysql_query($query,$cxn)or die ("Query Failed");
while($row = mysql_fetch_array($res)){
    $author = $row['author'];
    $synopsis = $row['story'];
    $article = $row['reading'];
    $title = $row['title'];
    }
   
?>

<body>
<table border="0" align="center">
  <tr>
    <td class="nav">Author:<?php echo $author ; ?></td>
    <td class="dingbat">Title: <?php echo $title ; ?></td>
  </tr>
  <tr>
    <td><a href="../index.php">Main</a></td>
    <td class="nav">Summary<?php echo $synopsis ; ?></td>
  </tr>
  <tr>
    <td><?php echo $login; ?></td>
    <td class="sidebarHeader"><?php echo $article; ?></td>
  </tr>
</table>
<?php
				}
else{
    echo "Either ID or Author is Not Specified";
} ?>
</body>
</html>

Link to comment
Share on other sites

Well.. I know how to display a certain amount of characters.

 

For example - This will display 50 characters.

$synopsis = substr($row['story'],0,50);

 

Keeping it simple, you could do something like... - This will display 50 characters, with a Read More after story snippet.

$synopsis = substr($row['story'],0,50) .'..<a href="/my-page-to-display-news.php">Read More</a>';

 

As for only displaying Read More if there is more than 50 characters exceeded, you could do..

$synopsis = substr($row['story'],0,50);
if($row['story'] > 50){ $synopsis .= '..<a href="/my-page-to-display-news.php">Read More</a>';

 

However this may display a resut if there is anumber bigger than 50, so you'll have to change it slightly to do it with characters.

Could be either..

 if(substr($row['story']) > 50) /* OR */ if(substr($row['story'] > 50)) 

Not sure though.. was an option I never tried.

Link to comment
Share on other sites

$synopsis = substr($row['story'],0,50) .'..<a href="/my-page-to-display-news.php">Read More</a>';

Thanks this code gets the work done never thought it was so simple  :o

 

I still want to format the text retrieved from the database using BBcodes (like one use in forums).

And How can I filter PHP and Html Tags before entering the Text into Database

Link to comment
Share on other sites

LOL I laughed at:

 

elseif($state =! 1){

 

$state = !1 <=> $state = 0 which means that the body (between { and }) of elseif($state = !1) never executes :)

 

$synopsis = substr($row['story'],0,50);

 

Is not a good idea you could get:

 

He kissed my ass...
while the long text would have been
He kissed my assistant

 

Use

$synopsis = substr($row['story'], 0, strpos($row['story'], ' ', 50));

It also looks nicer :)

 

 if(substr($row['story']) > 50) /* OR */ if(substr($row['story'] > 50)) 

 

'hello world' > 50 <=> 0 > 50 = always false in both above cases. You probably meant:

 

 if(strlen($row['story']) > 50)

Link to comment
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.