Jump to content

generate dynamic content in one page


mol

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.