Dying to get this done: Link to articles

Discussion in 'PHP' started by toronto905, Nov 17, 2008.

  1. #1
    Here is what I am trying to do, and I given my little PHP knowledge, I have already gotten so much done.

    1-I run PHPmyfaq on my website (a script to crate and display FAQs on your website)

    2-I have created a simple script that randomly displays a different FAQ record, everytime you load the home page.

    3-Problem is, the whole FAQ article is displayed, and I don't want that. I simply want to dislpay the title of the record, then first 10 words of the article, and then a link for people to click and read the entire article. Like the following:

    Digitalpoint: A webmasters and Developers forum, where you can discuss.... (click to read more)

    That is it. No more, no less.

    Now for the code, this is what I have, but unfortunately, I am missing the code that will limit how many words are displayed, as well as the link to the entire article.



    <?php

    // Create the connection and select the DB
    $link = mysql_connect("host","user","password");

    if ($link) {
    mysql_selectdb("db",$link);

    // Select records from the DB
    $query = "SELECT content FROM pmf_faqdata ORDER BY Rand() LIMIT 1";
    $result = mysql_query($query);


    // Display records from the table
    echo "<table border='1'>";
    while ($row = mysql_fetch_array($result, MYSQL_NUM))


    {
    echo "<tr><td>$row[0]</td><td>$row[1]</td><td>$row[1]</td></tr>";
    }
    echo "</table>";

    } else {
    echo "Can't connect to the database!";
    }

    ?>




    I would really appreciate if someone can help me in achieving this? :(:cool:
     
    toronto905, Nov 17, 2008 IP
  2. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Limit the word using this:
    
    //replace $theword with the variable name that hold the faq content
    $length_limit = 20;
    
    if (strlen($theword) > $length_limit) {
    $theword = substr($theword , 0 , $length_limit).'...';
    }
    
    PHP:
    Or just simply

    
    substr($YOUR_VARIABLE , 0 , 20).'...';
    
    PHP:
     
    ads2help, Nov 17, 2008 IP
  3. toronto905

    toronto905 Active Member

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    Articles:
    1
    #3
    Really appreciate the quick reply.

    Where exactly in my code would I put this: substr($result , 0 , 20).'...';

    ?
     
    toronto905, Nov 17, 2008 IP
  4. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #4
    you can put it in your query

    select LEFT(content, 50) from table where condition = 'satisfied';

    where 50 represents the string count of the content from left to right
     
    bartolay13, Nov 17, 2008 IP
  5. toronto905

    toronto905 Active Member

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    Articles:
    1
    #5
    Thanks

    So far I tried plugging the following in many places in my script, but it is not working. Not even sure if I got the right query that holds the data?

    //replace $theword with the variable name that hold the faq content
    $length_limit = 20;

    if (strlen($query) > $length_limit) {
    $query = substr($query , 0 , $length_limit).'...';
    }
     
    toronto905, Nov 17, 2008 IP
  6. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #6
    try this query..

     
    bartolay13, Nov 17, 2008 IP
  7. toronto905

    toronto905 Active Member

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    Articles:
    1
    #7
    Bartolay, excuse my ignorance, I am newbie to PHP. Please let me know:

    1-where in the code do I put that?
    2-which one would be my variable name that I have to replace?

    Thanks a lot
     
    toronto905, Nov 17, 2008 IP
  8. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #8
    Make sure you replace the tags for the query
     
    bartolay13, Nov 17, 2008 IP
  9. toronto905

    toronto905 Active Member

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    Articles:
    1
    #9
    unfortunately it is not working for me. I have to keep trying. thanks for all the help guys
     
    toronto905, Nov 17, 2008 IP
  10. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #10
    if $row[0] hold the title. it will be:

    echo "<tr><td>".substr($row[0],0,20)."...</td><td>$row[1]</td><td>$row[1]</td></tr>";
    PHP:
    if $row[1] hold the title. it will be:

    echo "<tr><td>$row[0]</td><td>".substr($row[1],0,20)."..</td><td>".substr($row[1],0,20)."..</td></tr>";
    PHP:
    because i don't know whether $row[1] or $row[0] hold the title ...
     
    ads2help, Nov 17, 2008 IP
  11. toronto905

    toronto905 Active Member

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    Articles:
    1
    #11
    Thanks a lot guys,

    I got the first thing done.

    The only thing left, is to hyperlink it, so people can click on the topic and read it in full

    Article: This is an article about something...(click here to read more)

    I would appreciate if someone can finish this for me please?

    // Create the connection and select the DB
    $link = mysql_connect("host","name","password");

    if ($link) {
    mysql_selectdb("password",$link);

    // Select records from the DB
    $query = "SELECT content,thema FROM pmf_faqdata ORDER BY Rand() LIMIT 1";
    $result = mysql_query($query);



    // Display records from the table
    echo "<table border='1'>";
    while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    echo "<tr><td>".substr($row[1],0,50).substr($row[0],0,75)."...</td></tr>";
    }
    echo "</table>";
    } else {
    echo "Can't connect to the database!";
    }
     
    toronto905, Nov 18, 2008 IP
  12. juust

    juust Peon

    Messages:
    214
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #12
    another quick way :

    echo first_ten("Problem is, the whole FAQ article is displayed, and I don't want that. I simply want to dislpay the title of the record, then first 10 words of the article, and then a link for people to click and read the");
    
    function first_ten($basetext) {
    
      $words=split(' ', $basetext);
    
      for($i=0;$i<10;$i++) {
    	 $text .= ' '.$words[$i];
      }
    
      return $text.'...';
    
    }
    
    PHP:
    returns
    Problem is, the whole FAQ article is displayed, and I don't...
     
    juust, Nov 18, 2008 IP
  13. juust

    juust Peon

    Messages:
    214
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #13
    for the link :
    
    echo "<tr>
    <td>".substr($row[1],0,50).substr($row[0],0,75)."...</td>
    <td><a href=\"".$row[2]."\">more...</a></td>
    </tr>";
    PHP:
    assuming you retrieve content, thema, link with your query
    url would be $row[2]
    (arrays are 0-based, first field is 0)
     
    juust, Nov 18, 2008 IP
  14. toronto905

    toronto905 Active Member

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    Articles:
    1
    #14
    Thanks a lot. That didn't work for some reason.

    A typical FAQ record on this section looks like this:


    http://www.SiteAddress.com/faq/index.php?action=artikel&cat=3&id=5&artlang=en


    How can we integrate that? Thanks a lot
     
    toronto905, Nov 26, 2008 IP
  15. juust

    juust Peon

    Messages:
    214
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #15
    juust, Nov 26, 2008 IP
  16. toronto905

    toronto905 Active Member

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    Articles:
    1
    #16
    I have gotten all this figured out. The only thing I am stuck with, is to display the first 100 characters , up to the last character in a word.

    <?php

    // Create the connection and select the DB
    $link = mysql_connect("URL","db","password");

    if ($link) {
    mysql_selectdb("db",$link);

    // Select records from the DB
    $query = "SELECT content,thema,id FROM pmf_faqdata ORDER BY Rand() LIMIT 1";
    $result = mysql_query($query);

    // Display records from the table
    while ($row = mysql_fetch_array($result, MYSQL_NUM)) {

    echo "<div style=\"font-weight:bold\">" . substr($row[1],0,50) . "</div><div>" . substr($row[0],0,75) . "...</div>" .
    "<div><a href=\"http://www.example.net/faq/index.php?action=artikel&artlang=en&id=".$row[2]."\">Click to read more...</a></div>";

    }
    } else {
    echo "Can't connect to the database!";
    }

    ?>




    I was also given the following solution by someone, but I am not sure where to insert it in my script above?

    $variable['abbr'] = $this->cutText($variable['description'], 175);
    function cutText($string, $length) {

    if($length<strlen($string)){

    while ($string{$length} != " ") {

    $length--;
    }

    return substr($string, 0, $length);

    }else return $string;
    }



    Please advise?

    Thanks a lot
     
    toronto905, Dec 27, 2008 IP
  17. juust

    juust Peon

    Messages:
    214
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #17
    you can paste the function anywhere in the source, and use
    .cutText($row[1],50).
    where it says
    .substr($row[1],0,50).

    i'd omit the ... in the html code and have the function add it if the textstring is bigger than the cut:
    
    function cutText($string, $length) {
    if($length>=strlen($string)) return $string;
    while ($string{$length} != " ") $length--;
    return substr($string, 0, $length).'...';
    } 
    
    PHP:
    The function fails if the first word is longer than the cut. If you don't intend to use a cut smaller than 20 characters it should not give any problems. Otherwise you'd have to decide to cut the word (or allow a longer word than the cut).
    
    function cutText($string, $length) {
    if($length>=strlen($string)) return $string;
    $newlength=$length;
    while ($string{$newlength} != " ") $newlength--;
    if($newlength>0) return substr($string, 0, $newlength).'...';
    return substr($string, 0, $length).'...';
    } 
    
    PHP:
     
    juust, Dec 30, 2008 IP