Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/38007-solved-read-more-in-a-news-script/
Share on other sites

$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>";
?>

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 ???

<?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.

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.

 

 

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...
    }

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

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>

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.

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>

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?

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."

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 ;) )

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.