Page 1 of 1

help.......php and mysql

Posted: Fri Aug 22, 2003 8:30 am
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

Posted: Fri Aug 22, 2003 8:34 am
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

Posted: Fri Aug 22, 2003 8:43 am
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?

Posted: Fri Aug 22, 2003 9:02 am
by will
use the SQL command 'group by'... greenhorn included an example in his post.

Posted: Fri Aug 22, 2003 9:12 am
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

Posted: Fri Aug 22, 2003 9:30 am
by will
yes yes... distinct. that's actually what you want. i wasn't thinking :)

Posted: Fri Aug 22, 2003 9:30 am
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

Posted: Tue Aug 26, 2003 6:40 am
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

Posted: Tue Aug 26, 2003 8:12 am
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