mol Posted November 4, 2007 Share Posted November 4, 2007 Hi all.. I am new in php and mysql, I have a question: I have an home page, there are three links : news1, new2 and news3. and then I have one page to display the news, display.php. Can I display news1,news2,news3 in display.php. So I dont need to make 3 different page (display1.php, display2.php, display3.php) to display 3 different content (news1, new2 and news3) I want to make it dynamic. many thanks Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted November 5, 2007 Share Posted November 5, 2007 Sure, there's a hundred ways to do it... You can store the news stories in MySQL and call them as needed, or store them in files and INCLUDE them as needed, or even embed them right into your homepage script. Obviously if you have too many stories, then using MySQL really becomes necessary. Anyway, to embed within your homepage script, you can try the example code below. To access these 'news stories', use the following URLs: http://www.yoursite.com/display.php?news=1 http://www.yoursite.com/display.php?news=2 ect. http://www.yoursite.com/display.php or http://www.yoursite.com/display.php?news=foobar (anything but 1, 2 or 3) will generate 'No news selected" (the default case) <?php if ( isset($_GET['news']) && is_numeric($_GET['news']) ) { $news = intval($_GET['news']); } else { $news = ''; } Echo"<p> Hi! Welcome to my page! We hope you find what you're looking for, <br /> and if you selected a news item, it will be listed below!</p>"; switch ($news) { case 1: echo "<p>This is news story #1!</p>"; break; case 2: echo "<p>This is news story #2!</p>"; break; case 3: echo "<p>This is news story #3!</p>"; break; default: echo "<p>No news was selected!</p>"; } ?> PhREEEk Quote Link to comment Share on other sites More sharing options...
mol Posted November 5, 2007 Author Share Posted November 5, 2007 OK guy ,many thx for it , i'll try it Quote Link to comment Share on other sites More sharing options...
bdmovies Posted November 5, 2007 Share Posted November 5, 2007 The MySQL way to do it...... Assuming you have a title, author, date_written, story, storyID fields in thd DB... You can do it a couple of ways, if it's just a couple of news stories, than PHP_PhREEEk's way is probably simplest, but if you've got 50, 60 70 + news stories than that would get really daunting. This is index.php <?php $query = "SELECT * FROM news_story"; $result = mysql_query($query) or die(mysql_error()); // This loops through the DB to give you a link to each story while ($row = mysql_fetch_assoc($result)) { echo "<a href=\"display.php?storyID={$row['storyID']}\">{$row['title']} written by: {$row['author']}</a><br>"; } ?> This is display.php <?php $storyID = $_GET['storyID']; $query = "SELECT * FROM news_story WHERE storyID = '$storyID'"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); echo "{$row['title']} written by: {$row['author']} on {$row['date_written']}.<br> {$row['story']}"; ?> That's the basic idea. That will give you your 2 basic pages. 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.