felipeebs Posted February 11, 2007 Share Posted February 11, 2007 hi, i have devceloped a PHP and MySQL based news script but it is ready to display only the full article the TABLE is too simple: ID, TITLE, BODY, DATE and I want to keep it simplicity. Now, I need help to make a link to force the page to show only some words or letters and put a reticence and a link to a page that will show the full article after the little introduction If someone can help PS: My english is not good and I am using a translator PS no 2: I am a PHP beginner Quote Link to comment Share on other sites More sharing options...
almightyegg Posted February 11, 2007 Share Posted February 11, 2007 $articleshort = join(' ', array_slice (explode(' ', $articlequery), 0, 40)); that will show the first 40 words. notice the '40' change that to however many you'd like. <? echo "$articleshort ... <br><a href='page.php'>Read more...</a>"; ?> Quote Link to comment Share on other sites More sharing options...
felipeebs Posted February 11, 2007 Author Share Posted February 11, 2007 I couldn't understand the code (some tries and nothing good)... as I said, am a PHP beginner :-\ The code I use to show the "full" news article is: <?php include('news_conf.php'); $sql = "select * from news order by id DESC limit $limit"; // $limit is the no. of news that the script will show <change news_conf.php> $conn = mysql_connect("$dbhost","$dbuser","$dbpass"); // the variables must be changed in news_conf.php mysql_select_db("$dbname"); $query = mysql_query($sql); while($line = mysql_fetch_array($query)) { echo "<fieldset><legend><p><b>" . $line["title"] . "</b></legend> <br>" . $line["body"] . " <br><i>in: " . $line["date"] . " - " . $line["time"] . "</i></p>\n</fieldset>"; } ?> How to implement the almightyegg's code? Thanks ??? Quote Link to comment Share on other sites More sharing options...
felipeebs Posted February 13, 2007 Author Share Posted February 13, 2007 someone? (up) (sorry for the double posting... but I'm needing this stuff) Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted February 13, 2007 Share Posted February 13, 2007 <?php include('news_conf.php'); $sql = "select * from news order by id DESC limit $limit"; // $limit is the no. of news that the script will show <change news_conf.php> $conn = mysql_connect("$dbhost","$dbuser","$dbpass"); // the variables must be changed in news_conf.php mysql_select_db("$dbname"); $query = mysql_query($sql); while($line = mysql_fetch_array($query)) { $articleshort = join(' ', array_slice (explode(' ', $line["body"]), 0, 40)); echo "<fieldset><legend><p><b>" . $line["title"] . "</b></legend> <br>" . $articleshort . " <br><i>in: " . $line["date"] . " - " . $line["time"] . "</i></p>\n</fieldset>"; } ?> You must have a native language similar to english, only a few of your sentences are structured oddly. Quote Link to comment Share on other sites More sharing options...
suttercain Posted February 13, 2007 Share Posted February 13, 2007 I am just reading the code to try and learn as much php as possible. In the code listed by genericnumber1, how does it know which table contains the id, title, date and body? I can see it connects to the mysql database but how does it know where to fetch the news information from? Thanks. Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted February 13, 2007 Share Posted February 13, 2007 after it's connected you can retrieve information like he did with $sql = "select * from news order by id DESC limit $limit"; $query = mysql_query($sql); and then loop through the rows that were returned with while($line = mysql_fetch_array($query)) { // code... } Quote Link to comment Share on other sites More sharing options...
felipeebs Posted February 13, 2007 Author Share Posted February 13, 2007 Thanks genericnumber1 and almightyegg The code is fully functional Quote Link to comment Share on other sites More sharing options...
suttercain Posted February 13, 2007 Share Posted February 13, 2007 Hey guys, So I wanted to try this out since I already have a mysql news table that contains a similar setup just to learn more about PHP. I ran the following code: <?php //Database Information $host = 'localhost'; $user = 'user'; $password = 'password'; $dbName = 'dbname'; //Connect and Select Database $conn = mysql_connect ($host, $user, $password) or die (mysql_error()); $db = mysql_select_db ($dbName, $conn) or die (mysql_error()); $sql = "select * from nuke_stories order by id DESC limit $limit"; // $limit is the no. of news that the script will show <change news_conf.php> $query = mysql_query($sql); while($line = mysql_fetch_array($query)) { $articleshort = join(' ', array_slice (explode(' ', $line["bodytext"]), 0, 40)); echo "<fieldset><legend><p><b>" . $line["title"] . "</b></legend> <br>" . $articleshort . " <br><i>in: " . $line["time"] . " - " . $line["time"] . "</i></p>\n</fieldset>"; } ?> When I ran the script I got the following error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/superman/public_html/require.php on line 17 I am running PHP 4.4.4 so I am unsure why I am getting that error. Any suggestions? Thanks. Shannon Quote Link to comment Share on other sites More sharing options...
JJohnsenDK Posted February 13, 2007 Share Posted February 13, 2007 You have entered a colum or table that doesnt exits in your database... If not try to put $input in '' like this: $sql = "select * from nuke_stories order by id DESC limit '$limit'"; // $limit is the no. of news that the script will show <change news_conf.php> Quote Link to comment Share on other sites More sharing options...
suttercain Posted February 13, 2007 Share Posted February 13, 2007 Now I am determined to get this. JJohnsen, thanks for the reply. I did notice that id should be sid to correspond to my column name. I changed that and also did the single quotes around $limit. I am still getting the same error. I am trying to pull the information from the table nuke_stories and the fields sid, title, bodytext and time. I made the changes suggested above and still got the same error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/superman/public_html/require.php on line 17 Here is the slightly altered code: <?php //Database Information $host = 'localhost'; $user = 'username'; $password = 'pw'; $dbName = 'dbname'; //Connect and Select Database $conn = mysql_connect ($host, $user, $password) or die (mysql_error()); $db = mysql_select_db ($dbName, $conn) or die (mysql_error()); $sql = "select * from nuke_stories order by sid DESC limit '$limit'"; // $limit is the no. of news that the script will show <change news_conf.php> $query = mysql_query($sql); while($line = mysql_fetch_array($query)) { $articleshort = join(' ', array_slice (explode(' ', $line["bodytext"]), 0, 40)); echo "<fieldset><legend><p><b>" . $line["title"] . "</b></legend> <br>" . $articleshort . " <br><i>in: " . $line["time"] . " - " . $line["time"] . "</i></p>\n</fieldset>"; } ?> Thanks in advance for any help. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 13, 2007 Share Posted February 13, 2007 You need the or die(mysql_error()) part after your mysql_query() too. Quote Link to comment Share on other sites More sharing options...
JJohnsenDK Posted February 13, 2007 Share Posted February 13, 2007 Try what jesirose wrote and also put in the fields you want to use in your query insted of the * tag. And try removing the $limit variable and write a number insted. It shouldnt be the problem, but try it anyway, migth solv your problem Im guessing that you have defined $limit somewhere in the script? $sql = "select sid, title, bodytext, time from nuke_stories order by sid DESC limit 7"; // $limit is the no. of news that the script will show <change news_conf.php> Quote Link to comment Share on other sites More sharing options...
suttercain Posted February 13, 2007 Share Posted February 13, 2007 Thanks Jesirose and JJohnsenDK. That did it. Quote Link to comment Share on other sites More sharing options...
JJohnsenDK Posted February 13, 2007 Share Posted February 13, 2007 You are welcome... remember to mark the thread RESOLVED Quote Link to comment Share on other sites More sharing options...
suttercain Posted February 13, 2007 Share Posted February 13, 2007 Can I do that if I didn't start it? Also, this is the page I get, thanks to you guys: http://supermandatabase.com/require.php It looks nice and I can add some css later, but how do you incorporate the "read more" into it so the bodytext can be expanded? Quote Link to comment Share on other sites More sharing options...
felipeebs Posted February 13, 2007 Author Share Posted February 13, 2007 $articleshort = join(' ', array_slice (explode(' ', $line["bodytext"]), 0, 40)); the "40" is the number of words I think that's it Quote Link to comment Share on other sites More sharing options...
suttercain Posted February 14, 2007 Share Posted February 14, 2007 Hi Felipeebs, I understand that if it exceeds 40 it should say "read more" but it doesn't for some reason. This is the code I am using <?php //Database Information $host = 'localhost'; $user = 'user'; $password = 'pass'; $dbName = 'database'; //Connect and Select Database $conn = mysql_connect ($host, $user, $password) or die (mysql_error()); $db = mysql_select_db ($dbName, $conn) or die (mysql_error()); $sql = "select sid, title, bodytext, time from nuke_stories order by sid DESC limit 7"; // $limit is the no. of news that the script will show <change news_conf.php> $query = mysql_query($sql) or die (mysql_error()); while($line = mysql_fetch_array($query)) { $articleshort = join(' ', array_slice (explode(' ', $line["bodytext"]), 0, 40)); echo "<fieldset><legend><p><b>" . $line["title"] . "</b></legend> <br>" . $articleshort . " <br><i>in: " . $line["time"] . " - " . $line["time"] . "</i></p>\n</fieldset>"; } ?> But when you visit the page: it lays out the 7 news items, but does not allow the user to "read more." Quote Link to comment Share on other sites More sharing options...
felipeebs Posted February 14, 2007 Author Share Posted February 14, 2007 suttercain, after <br>" . $articleshort . " add ... <a href=\"fullnews.php?sid=" . $line["sid"] . "\">Read more</a> an example of the fullnews.php is here: <?php //Database Information $host = 'localhost'; $user = 'user'; $password = 'pass'; $dbName = 'database'; //Connect and Select Database $conn = mysql_connect ($host, $user, $password) or die (mysql_error()); $db = mysql_select_db ($dbName, $conn) or die (mysql_error()); $sql = "select sid, title, bodytext, time from nuke_stories WHERE sid = " . $_GET["sid"] . ""; $query = mysql_query($sql) or die (mysql_error()); while($line = mysql_fetch_array($query)) { echo "<fieldset><legend><p><b>" . $line["title"] . "</b></legend> <br>" . $line["body"] . " <br><i>" . $line["time"] . " - " . $line["time"] . "</i></p>\n</fieldset>"; } ?> or something like that (im having no time... try this ) Quote Link to comment Share on other sites More sharing options...
suttercain Posted February 14, 2007 Share Posted February 14, 2007 It worked, I just changed the link to match my module. Thanks guys and gals! Quote Link to comment Share on other sites More sharing options...
felipeebs Posted February 14, 2007 Author Share Posted February 14, 2007 I Hope it means... "Topic solved" If someone wants the class to add/edit/delete values for the news script please send-me a PM or e-mail //FelipeEBS Quote Link to comment Share on other sites More sharing options...
sspoke Posted February 14, 2007 Share Posted February 14, 2007 articleshort = implode(' ', array_slice (explode(' ', $line["bodytext"]), 0, 40)); Quote Link to comment Share on other sites More sharing options...
felipeebs Posted February 14, 2007 Author Share Posted February 14, 2007 articleshort = implode(' ', array_slice (explode(' ', $line["bodytext"]), 0, 40)); as i said in the first message of this thread... I am a beginner now: why implode? what it does? :-\ thx Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.