Jump to content

[SOLVED] Dynamic URLs


john010117
 Share

Recommended Posts

I have read the stickies/announcements, and they [i]did[/i] say how to do this. But they didn't explain it further, and the link they've posted for further reading doesn't work. So, here's my problem.

On my index page, I have a news section. No php needed for that. But I want the website to automatically archive old news when the next day comes. So, if I type in index.php, it should automatically get the news for today (but I'm typing up the news). Sorry if this doesn't make sense. I want to have something [url=http://halo.bungie.org]like this[/url] (see the date at the top and the arrows?). How do you do that?
Link to comment
Share on other sites

You will need to have a database field for the date.

Then, you simple SELECT only the news items in the database on that day. In the URL you have something like www.mysite.com/index.php?newsdate=123




[code=php:0]
$date = $_GET['newsdate'];
// This looks at the URL for the value "?newsdate="
$sql  = "SELECT * ";
$sql .= "FROM mynews ";
$sql .= "WHERE datecreated = '$date' ";
// Select information out of database for that day
$result = mysql_query($sql) or die (mysql_error());

if(mysql_num_rows($result) == 1) {
// ...........go on to display it
[/code]




etc, hope that helps you understand it better.
Link to comment
Share on other sites

In your table which has the news, add a field for DATE. Not a new table, just a field to your existing table.

Post your database structure and we can explain more if need be.

If you don't have a database yet, do more research and start actually making something ;)
Link to comment
Share on other sites

I'm guessing that you do have some knowledge of PHP basics, and MySQL is your new point here...

Now you need a form to easily post all this, if you've made it to PHP basics you know about HTML, just an assumption but you really should.

This form should have, well from what your structure suggests just 1 field for all your news post content.

Using the information posted from the form we want to insert it into your database. this function is called 'INSERT'.

[code=php:0]<?
$date = date("m.d.Y")." at ".date("h:i:s a");
mysql_query("INSERT INTO news (text, date) VALUES ('{$_POST['text']}', $date)");
?>[/code]

Now what this code does is first create a date, and assigns it to $date to be used later. (This date will be MM/DD/YYYY at HH:MM:SS am/pm) It then takes the information posted from the form and inserts it.

This is a basic basic basic version, you will need to add to it, and create the other bits to make it function to the best of its ability.

Another suggestion would be to add fields to your table such as Title and Poster.
Link to comment
Share on other sites

Even though it didn't say if it worked or not, it didn't show any errors, either (using your code).

Here's my form code:
[code]
<form action="test10.php">
<input name="news" size="30">
<input type="submit" value="submit">
</form>
[/code]

...and here's my test10.php code:
[code]
<?php
$db_host = "&*<?php
$db_host = "****";
$db_user = "****";
$db_pass = "****";
$db_name = "****";
$dbac = mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db ($db_name) or die ("Cannot connect to database");

$date = date("m.d.Y")." at ".date("h:i:s a");
mysql_query("INSERT INTO news (text, date) VALUES ('{$_POST['text']}', $date)");
?>
<?php
$db_host = "****";
$db_user = "****";
$db_pass = "****";
$db_name = "****";
$dbac = mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db ($db_name) or die ("Cannot connect to database");

$date = date("m.d.Y")." at ".date("h:i:s a");
mysql_query("INSERT INTO news (text, date) VALUES ('{$_POST['text']}', $date)");
?>
";
$db_user = "allaoorg_admin1";
$db_pass = "9055084308";
$db_name = "****";
$dbac = mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db ($db_name) or die ("Cannot connect to database");

$date = date("m.d.Y")." at ".date("h:i:s a");
mysql_query("INSERT INTO news (text, date) VALUES ('{$_POST['text']}', $date)");
?>
[/code]

Am I doing it right so far?
Link to comment
Share on other sites

Well test it out and let us know if it adds stuff.

Note that this is insecure and anyone can post.

And clean up your code! Heres a cleaned version that can go into test10.php

[code]<?php

// Connection information
$db_host = "***";
$db_user = "***";
$db_pass = "***";
$db_name = "***";

// Connect to server
$dbac = mysql_connect($db_host,$db_user,$db_pass);

// Select database
mysql_select_db ($db_name) or die ("Cannot connect to database");

// Check if news has been posted.
if ($_POST['news']) { // *Start*

  // Get current date.
  $date = date("m.d.Y")." at ".date("h:i:s a");

  // Insert information to table
  mysql_query("INSERT INTO news (text, date) VALUES ('{$_POST['news']}', $date)");

/*END*/ }
?>

<form action="test10.php">
<input name="news" size="30">
<input type="submit" value="submit">
</form>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

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.

 Share

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