Jump to content

[SOLVED] Dynamic URLs


john010117
 Share

Recommended Posts

Ok, I have the following.
[code]
<?php
// Connection information
$db_host = "localhost";
$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");

$result = mysql_query("SELECT * FROM news WHERE Date");

if ($result && mysql_num_rows($result)) {
      $numrows = mysql_num_rows($result);
      $rowcount = 1;
       
      while ($row = mysql_fetch_assoc($result)) {
           
        while(list($var, $val) = each($row)) {
            print "<B>$var</B>: $val<br />";
        }
 
        print "<br />";
        ++$rowcount;
      }
  }
?>
[/code]

I have a table named "news", and 3 fields: "News", "Posted_by", "Date" (in DATE format). I can retrieve the data from the database.

All I need is a way to only view the news posted for that day using dynamic URL's (index.php?Date=2007-01-07) should only show news posted on Jan. 7, 2007. Can anybody point me the right way?
Link to comment
Share on other sites

[code]<?php
// Connection information
$db_host = "localhost";
$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");

// Change is here added [ = '{$_GET['date']}' ] to end. Which get ?date=____ from url.
$result = mysql_query("SELECT * FROM news WHERE Date = '{$_GET['date']}'");

if ($result && mysql_num_rows($result)) {
      $numrows = mysql_num_rows($result);
      $rowcount = 1;
       
      while ($row = mysql_fetch_assoc($result)) {
           
        while(list($var, $val) = each($row)) {
            print "<B>$var</B>: $val<br />";
        }
 
        print "<br />";
        ++$rowcount;
      }
  }
?>[/code]
Link to comment
Share on other sites

Okay i think i misread what you want, you want. let me repost.


This would go in a new page.
[code]<?php
// Connection information
$db_host = "localhost";
$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");

// Change is here added [ = '{$_GET['date']}' ] to end. Which get ?date=____ from url.
$result = mysql_query("SELECT news, Posted_by, date FROM news WHERE Date = '{$_GET['date']}'");

if ($result && mysql_num_rows($result)) {
      $numrows = mysql_num_rows($result);
      $rowcount = 1;
       
      while ($row = mysql_fetch_assoc($result)) {
           
        while(list($new, $by, $date) = each($row)) {
            print "<span align='right' style='font-size:13px'><B>$date</B></span><br />";
            print "<span align='right' style='font-size:9px'>$by</span><br />";
            print "$news<br />";
        }
 
        print "<br />";
        ++$rowcount;
      }
  }
?>[/code]
Link to comment
Share on other sites

ummm... I have read most of these posts and I hate to be a pain an all but... is it not a sensible idea to check that john knows how to CONNECT to a database before going any further with this topic?

John, do you know how to connect to a database, or what a connection string is?

RC
Link to comment
Share on other sites

You need a table (call it "newsentries", for sake of argument), with columns "news" (text, 255 chars), "timeadded" (text, 15 chars) and "dateadded" (long integer) (do NOT use "date" or "time" as a field name as they are reserved words in SQL). You need to connect to the database through php. Lets say the connection string is called $conn. Use something similar to the following...

Set the dateadded column to the day, month and year the news was added all in a row, for instance today would be 08012007. The timeadded column should be formatted as 12:53:45 (hour:min:sec). I dont bother with setting the field to date format - I just use code to "segment" it if need be.

The following code is php, followed by SQL.

$date = date("dmY");
for($selectnews = $conn->Execute("SELECT * FROM newsentries WHERE dateadded = $date"); !$selectnews->EOF; $selectnews->MoveNext())
{
   $newstext = $selectnews->Fields("news")->Value; //text - needs a "->Value" at the end
   $timeadded = $selectnews->Fields("timeadded")->Value; //although its mainly number, it is a text field in the table so still need a "->Value" at the end
   echo $newstext."<br>".$timeadded."<br><br>";
}

This will print all news entries for the current day, with a linebreak straight afterwards, then the time it was added, then 2 line breaks.

Hope this helps. Some things I left out so it would not be ALL there for you to just use - such as submitting into the table in the first place.

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