-1

I am doing news site and fetching news from database like this

<div class="left-content nine-column" style="font-size:13px; line-height:25px; font-family:open sans;">
    <?php
        $qry = "Select * from  tbl_news";
        $result = null;
            if(!$result = mysqli_query($con,$qry)) 
                Die("Error in database");
            else
            {
                if(mysqli_num_rows($result) >= 1)
                {
                    while(($rec = mysqli_fetch_assoc($result)) != null) 
                    {
                        echo '<div class="post">
                        <h1>'. $rec['news_heading'] .'</h1>
                        <ul class="post-meta">
                        <li><a href="" title=""><i class="icon-calendar-empty"></i><span>'. $rec['news_date'] .'</span></a></li>
                        <li><a href="" title=""><i class="icon-user"></i>By'.$rec['postedby'].'/a></li>
                        <li><a href="" title=""><i class="icon-map-marker"></i>'.$rec['news_location'].'</a></li>
                        </ul>
                        <div class="post-desc">
                        <p>'.$rec['news_ldesc'].'                                     

                        <!-- here i want read more link -->
                        </p>                        
                        <br/>
                        </div>
                        </div> '; 

                    }
                }
            }           

            ?>
        </div>

Now what I want to do is I want read more link against each news item them redirects to news_details.php and that single news will be shown there , how do i get it , pleas help me with it

Shikiryu
  • 10,180
  • 8
  • 49
  • 75
  • [`This Should do the trick....`](http://stackoverflow.com/questions/4258557/limit-text-length-in-php-and-provide-read-more-link) – Darren Nov 12 '13 at 08:47

4 Answers4

1

First of all, you need to build the news_detail.php, to handle single post viewing. There, you need to decide which news detail to bring, usually people do it with id. Then, you need to pass the id from your links like href='news_details.php?id=your_id_here'

Kuzgun
  • 4,649
  • 4
  • 34
  • 48
0

You should have news id in database. When you retrieve all news and print in loop you should use the following code. I do not know what is name of news id column name in database

echo '<a href="news_details.php?id='.$rec['news_id'].' ">Read More</a>'; // news_id should be matched with database column name

Now you should write code for news details. You should query from the database receive the news id using get method and print into the loop.

UPDATE: in news_details.php you should receive the value of news id by GET method $_GET["id"], then write code get row of database table. i.e.

$newsId = $_GET["id"];
$qry = "Select * from  tbl_news where id = $newsId ";

$result = mysqli_query($con,$qry);

while($row = mysqli_fetch_array($result))
{
  echo $row['news_ldesc'] ;
}

Try this.

StreetCoder
  • 9,871
  • 9
  • 44
  • 62
0

you can set the separate link for particular news like

href='news_description.php?id='.base64_encode($id)
Nikul
  • 1,025
  • 1
  • 13
  • 33
0

Use the following steps :

1) You can use php substr to truncate your news contents to a number of words. and then add Read More link below that.

Your code will be like below :

<div class="left-content nine-column" style="font-size:13px; line-height:25px; font-family:open sans;">
         <?php
            $qry = "Select * from  tbl_news";
            $result = null;
            if(!$result = mysqli_query($con,$qry)) 
            Die("Error in database");
            else
            {
            if(mysqli_num_rows($result) >= 1)
            {
            while(($rec = mysqli_fetch_assoc($result)) != null) 
            {
            echo '<div class="post">
            <h1>'. $rec['news_heading'] .'</h1>
            <ul class="post-meta">
            <li><a href="" title=""><i class="icon-calendar-empty"></i><span>'. $rec['news_date'] .'</span></a></li>
            <li><a href="" title=""><i class="icon-user"></i>By'.$rec['postedby'].'/a></li>
                                            <li><a href="" title=""><i class="icon-map-marker"></i>'.$rec['news_location'].'</a></li>
         </ul>
        <div class="post-desc">
        <p>'.substr($rec['news_ldesc'], 0, 100).'                                     

           <!-- here i want read more link -->
            </p>                        
        <br/>
        </div>
        </div> '; 

                        }

                    }
                }           

            ?>
        </div>

if you see, i have used substr($rec['news_ldesc'], 0 , 100) which truncate your news contents upto 100 words. 0 start and 100 is end.

2) Use answer of Kuzgun

Also read this :

How can I truncate a string to the first 20 words in PHP?

Community
  • 1
  • 1
Altaf Hussain
  • 5,166
  • 4
  • 30
  • 47