help.......php and mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

help.......php and mysql

Post by Nay »

okay, i've decided to build my own blog system after i got fed up of the ones out there. i've got things all planned and good to go. now my problem is building the archives system.

okay, there will be archive.php and viewarchive.php.

archive.php will list out the months to click on. so it'll be something like:

SELECT * FROM 'entries' order by 'month' asc

so that'll return like:

Code: Select all

id    title      time      month
1     blog 1     11:35    august
2     blog 2     00:15    august
3     blog 3     01:50      july
now how would i only select ONE out of all the augusts or junes in the entries?

thanks......

-Nay
User avatar
greenhorn666
Forum Commoner
Posts: 87
Joined: Thu Aug 14, 2003 7:14 am
Location: Brussels, Belgium

Post by greenhorn666 »

Randomly? or the latest?

I think this would require subqueries, that aren't supported by MySQL I think...
Otherwise, you could

Code: Select all

SELECT month, MAX(id) 
    FROM entries
    GROUP BY month
this would select the highest ID of each month's entry
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

randomly or latest? i'd say nethier :-). i want to select all the months. like if i did:

SELECT * FROM 'entries' order by 'month' asc

then i'd come out as:

april
april
april
august
august
july
july
june

and so on. is there a way to strip off the rest so there's only be ONE of EACH. so:

april
august
july
june

then only i will be able to do something like:

Code: Select all

while($arch=mysql_fetch_array($result)) {
echo "<a href="viewarchives.php?month=$arch['month']">$arch['month']</a>";
}
-Nay

ps: does anyone have an idea of archiving the entries better?
will
Forum Contributor
Posts: 120
Joined: Fri Jun 21, 2002 9:38 am
Location: Memphis, TN

Post by will »

use the SQL command 'group by'... greenhorn included an example in his post.
User avatar
greenhorn666
Forum Commoner
Posts: 87
Joined: Thu Aug 14, 2003 7:14 am
Location: Brussels, Belgium

Post by greenhorn666 »

Code: Select all

SELECT DISTINCT month 
    FROM entries
Is actually what you want, isn't it?

Or using that "group by" you could also get how many post you did that month

Code: Select all

SELECT month, COUNT(id)
    FROM entries
    GROUP BY month
will
Forum Contributor
Posts: 120
Joined: Fri Jun 21, 2002 9:38 am
Location: Memphis, TN

Post by will »

yes yes... distinct. that's actually what you want. i wasn't thinking :)
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

ohhh........okay......i was a bit O_o at first........so is this code correct?

Code: Select all

<?

require_once("config.php");

$c = mysql_connect("$host","$user","$pass");
$db = mysql_select_db($db, $c);

$q = "SELECT DISTINCT 'month' FROM 'entries' order by 'month' asc";
$result = mysql_query($q,$c) or die (mysql_error());

while($r=mysql_fetch_array($result) {
$month = $r['month'];
echo "<a href="viewarchive.php?month=$month">\n";
echo "- $month";
echo "</a>";
}

?>
thanks a lot ^_______^

-Nay
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

okay, now i'm up to my templates system:

index.php // the main page

Code: Select all

<?php
$filename= "content.php"; // html for the template
$fd = fopen ($filename , "r") or die ("Uhoh! The PHP script cant open $filename");
$fstring = fread ($fd , filesize ($filename));
fclose($fd);

$news= "stuff.php"; // script that gets news data from sql
$open = fopen ($news , "r") or die ("Uhoh! The PHP script cant open $news");
$replace = fread ($open , filesize ($news));
fclose($open);

$news = str_replace("[blog]", "$replace", $fstring);

echo $news;
?>
okay in stuff.php:

Code: Select all

echo "the news";
i get echo "the news"; on the page, shouldn't it be just the news.

thanks...

-Nay
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

another problem :-(....

include()
require()

won't work. i mean i want to have a seperate config.php file and include it in the functions.php. it works for the first time. but when i recall the functions.php for another function in there at the second time, i get errors popping up all over the place. if all the config variables are set locally at functions.php, it works O_o....

but then that creates a big mess.......

thanks...

-Nay
Post Reply