Jump to content

Website "newspage" PHP, each item openable separately?


boogiewoogie

Recommended Posts

I have a website with a newspage that shows "news" from a mysql database. I would want to make it possible to click the headline of each item, so you can see it on it's own, and that you could add a Facebook LIKE button for each separate piece of news. Any suggestions on how I can do it? I suppose it somehow has to generate a subpage for each news item?

 

As it is now, if I make a LIKE button, it just likes the main newspage.

 

Kind of like a blog page, where you can LIKE each entry separately.

Now I simply show the new like this

 

// get the info from the db

$sql = "SELECT newsid, publishtime, newstitle, newstext FROM news ORDER BY `newsid` DESC LIMIT $offset, $rowsperpage";

$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

 

// while there are rows to be fetched...

while ($list = mysql_fetch_assoc($result)) {

    // echo data

    //  echo $list['id'] . " : " . $list['number'] . "<br />";

 

    echo '<center>';

    echo '<b>';

    echo $list['newstitle'];

    echo '</b>';

    echo '</center>';

    echo '<br />';

    echo 'Posted at: ';

    echo $list['publishtime'];

    echo '<hr />';

    echo nl2br($list['newstext']);

    echo '<br /><br />';

 

} //end while

 

 

So basically, I would like to have a link after each item that you can click to open the news article on a subpage, or what it's called.

I can't really understand where to put what, how to make the page readable by using the "?id=1" etc.

 

Could I also make the headlines clickable then by making a hyperlink pointing to that link?

For example

<a href="news.php?id=1">First piece of news</a>
<a href="news.php?id=2">Second piece of news</a>

Thanks for the info. I am still quite lost though. Could someone give the basics of how to get the necessary stuff into the page? I only now read about "query strings" and I suppose that's how it works, but how to get it all to work.

 

I will post here the full basic news page I have, with the sections not relevant here removed

 

<?php


require 'config.inc.php';
require 'functions.php';


///////////
// Head: //
///////////

REMOVED

///////////
// Body: //
///////////

REMOVED


// Center division of page:

//Main text:
echo "\n\t<div id=\"main-div\" class=\"news\">";

// database connection info
$conn = connect_db() or error_message();


// get the info from the db
$sql = "SELECT newsid, publishtime, newstitle, newstext FROM news ORDER BY `newsid` DESC";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

// while there are rows to be fetched...
while ($list = mysql_fetch_assoc($result)) {
    // echo data
    //  echo $list['id'] . " : " . $list['number'] . "<br />";

    echo '<center>';
    echo '<b>';
    echo $list['newstitle'];
    echo '</b>';
    echo '</center>';
    echo '<br />';
    echo 'Posted at: ';
    echo $list['publishtime'];
    echo '<hr />';
    echo nl2br($list['newstext']);
    echo '<br /><br />';


} //end while


//////////////////////////
// Close body and html: //
//////////////////////////

echo "\n</body>";
echo "\n</html>";


////////////////
// Functions: //
////////////////

// Here you can keep functions you only need for this page




?>

 

Could someone show where to add what?

// You would add this line to inside the loop u echo all the news info:
echo '<a href="somepage.php?news_id=' . $list['newsid'] .'"> Click to do somethin to this particular news </a>';

// Then in somepage.php OR in the same page or any url u want to define, you would check against
// also validate with intval that the id will be int and bigger than zero
if (isset($_GET['news_id']) && intval($_GET['news_id']) > 0)
{
	$clickedId = $_GET['news_id']
	// then do something with the chosen news id ...
}

You're a real life-saver :) I got it working preliminary!

 

One more thing, any clues on how to make the "newstitle" row clickable to lead to the article?

 

// while there are rows to be fetched...

while ($list = mysql_fetch_assoc($result)) {

    // echo data

    //  echo $list['id'] . " : " . $list['number'] . "<br />";

 

    echo '<center>';

    echo '<b>';

    echo $list['newstitle'];

    echo '</b>';

    echo '</center>';

    echo '<br />';

    echo 'Posted at: ';

    echo $list['publishtime'];

    echo '<hr />';

    echo nl2br($list['newstext']);

    echo '<br /><br />';

 

    if ($list['youtube'] != '') {

 

        echo $list['youtube'];

    }

 

    if ($list['youtube2'] != '') {

        echo '<br /><br />';

        echo $list['youtube2'];

    }

 

echo '<a href="topnews.php?newsid=' . $list['newsid'] .'"> Read More </a>';

 

 

} //end while

Oh, and one more thing

 

Right now I have the

Code:

    echo nl2br($list['newstext']);

 

 

to show the new text on the main page. How can I limit it to a certain number of rows? I want to display the full news article on the subpage, but just a snippet on the main news page.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.