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?
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:
Really appreciate the quick reply. Where exactly in my code would I put this: substr($result , 0 , 20).'...'; ?
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
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).'...'; }
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
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 ...
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!"; }
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...
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)
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
Are you trying to mask affiliate links ? I lack all experience with that stuff so I can't help you there, but this one sounds like a viable solution : http://www.ragepank.com/articles/hide-affiliate-links/
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
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: