开发者

How can I add a 'read more' button below each article?

开发者 https://www.devze.com 2023-03-11 08:36 出处:网络
I am doing a project in college, using MySQL and PHP. Here is my code for getting data from the database:

I am doing a project in college, using MySQL and PHP. Here is my code for getting data from the database:

$sql="INSERT INTO articles (artical,article_heding,date,categories ) VALUES ('$artical','$heading','$datetime','$categories')";
mysql_query($sql,$con);

$sql3="select * from articles";
$rs2=mysql_query($sql3);
while($row2=mysql_fetch_array($rs2))
{
$rating=$row2['article_heding'];
$artical=$r开发者_运维问答ow2['artical'];

echo $row2['article_heding']."<br>".substr($row2['artical'], 0,10)."<br><br>";}}

Currently I am just showing a summary of each article (substr($row2['artical'], 0,10)), but I would like to put a read more button under each article to show the full text. How can I do this?


I just wrote a fiddle to illustrate this, oddly enough. It does use jQuery, but that is easy enough to include.

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>

You are also welcome to use the "$" command instead of "jQuery". I wrote it using "jQuery" because of specific requirements not pertaining to your situation (probably).

Also not that the "more" text in my example will always be hidden if the user has javascript disabled. To fix this remove the .css and add:

jQuery(document).ready(function(){
  jQuery("#more").hide();
});


as far as I know (since this is handled client-side (you don' wanna reload the page right?)) you have to use javascript to do this as php is pure server-based


When you don't want to reload the page, you defenitely need JavaScript, respecitvely a JS Framework.

Here is what you can achieve with jQuery and a Plugin: jQuery Expander Plugin

and here: Quick and Easy jQuery Read More Script Tutorial

also, someone else has already asked a similar question: Read More Feature with JQuery slideToggle Method


Finally I found answer to this!! Here it is : First I add these lines in next echo line:

echo "<form action='welcome.php' method='get'>
       <input name='".$articalid."' type='submit' value='Read More' />
       <input name='buttonId' type='hidden' value='".$articalid."' />
       <input name='buttonIdd' type='hidden' value='".$rating."' />
      </form>";

After that i made "welcome.php" file look like this:

$artical=$_REQUEST['buttonId'];
$heading=$_REQUEST['buttonIdd'];    
echo $heading."</br></br>";

$strSQL = "SELECT * FROM articles where id='$artical'";  
$rs = mysql_query($strSQL); 

while($row=mysql_fetch_array($rs))
{
echo $row['artical'];
}

Done!!! it Works!!!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号