Quantcast

Maximum PC

It is currently Thu Apr 17, 2014 4:39 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Urgent: Need some help figuring out PHP for a class.
PostPosted: Wed Dec 14, 2011 11:52 am 
Team Member Top 500
Team Member Top 500
User avatar

Joined: Sun Aug 13, 2006 8:47 pm
Posts: 154
Location: Goldsboro, NC
I've been trying to figure out this bit of code for 2 days and I can't figure it out. As written by the instructor:
Code:
Since the index page only shows the latest posts, we will need some organized means to
display all of the posts in the system. One way to do this is through the posts summary
page.
 The posts summary should organize posts by year, then by month.
 For each post, simply display the title of the post. Clicking on the post will take the
user to a page that shows that post and only that post. (See the reference blog web
site for a hint of how this is being done using $ GET).
 Near the top of the page, don't forget to display the total number of posts that the
blog has so far.
This page is probably the most dicult page of the project, and it is tricky for several
reasons.
Remember that you can get just the year or just the month out of a date eld by performing
the following:
$post_year = date("Y", strtotime($row['post_date']));
$post_month = date("F", strtotime($row['post_date']));
This might, for example, put 2011 in the variable $post year and the string January in
$post month.
One approach to solving this problem in general is to return a set of results that are sorted by
post date in descending order. Keep some temporary variables, perhaps $year and $month
and initialize them to NULL.
Then, iterate (using a while) through each of the records, comparing $post year with $year
and $post month with $month. If they are di erent, output (using echo) the new year or
month and set $post year and $post month to the last seen $year and $month.
In this way, every time a new year or month is encountered a new heading will be output as
HTML to the screen.


I managed to get the links to the individual post working in 30 mins, bit the loops to output the everthing else is giving me problems. It's supposed to be structured like:

Code:
Year
---Month
------Post
Year
---Month
------Post


My code is posted below. Any help would be greatly appreciated. The site is accessible at http://dragonfall.ath.cx/final/.


Last edited by Smokex365 on Wed Dec 14, 2011 7:14 pm, edited 1 time in total.

Top
  Profile  
 
 Post subject: Re: Urgent: Need some help figuring out PHP for a class.
PostPosted: Wed Dec 14, 2011 2:57 pm 
Team Member Top 50
Team Member Top 50

Joined: Sat Jun 25, 2005 11:04 am
Posts: 1026
What are you needing help with? Understanding what you're suppose to do? Figuring out how to do it?

I would suggest you re-read his description of how to do it (loop through the posts, then every time the year/month changes output the year/month) and then make an effort to implement it. If you get stuck on the code, post it. We will provide limited help, but we won't do anything that approaches doing your assignment for you.


Top
  Profile  
 
 Post subject: Re: Urgent: Need some help figuring out PHP for a class.
PostPosted: Wed Dec 14, 2011 3:22 pm 
Team Member Top 500
Team Member Top 500
User avatar

Joined: Sun Aug 13, 2006 8:47 pm
Posts: 154
Location: Goldsboro, NC
mag wrote:
What are you needing help with? Understanding what you're suppose to do? Figuring out how to do it?

I would suggest you re-read his description of how to do it (loop through the posts, then every time the year/month changes output the year/month) and then make an effort to implement it. If you get stuck on the code, post it. We will provide limited help, but we won't do anything that approaches doing your assignment for you.

Figuring out how to do it. The most I've been able to do is get it to post the links for the articles.

Code:
while ($row  = mysql_fetch_assoc($posts)) {
   // Creates link to individual post
   echo "<b><a href='posts.php?id=" . $row['id'] . "'>" . $row['post_title']. "</a></b></br>";
}

I expect it's a easy solution I'm not seeing but I can't figure out how to get the while loops to output in the format I need. I've tried a dozen variations but the most I've gotten to display so far is from the vaiables $post_year and $post_month. There's something I'm missing. I know it but I can't find it. It's the only trouble I've had with the project.


Top
  Profile  
 
 Post subject: Re: Urgent: Need some help figuring out PHP for a class.
PostPosted: Wed Dec 14, 2011 5:34 pm 
Team Member Top 500
Team Member Top 500
User avatar

Joined: Sun Aug 13, 2006 8:47 pm
Posts: 154
Location: Goldsboro, NC
After some more hair pulling I have it semi-working but it's still not seperating the posts. It does the first post in December but it doesn't create additional headings for November or the other months.
Code:
   while ($row = mysql_fetch_assoc($posts)) {   
      $year = NULL;
      while ($row = mysql_fetch_assoc($posts)) {
         $year = $post_year;
         echo "test year: " . $year . "</br>";
         $month = NULL;
         while ($row = mysql_fetch_assoc($posts)) {
            $month = $post_month;
            echo "test month: " . $month . "</br>";
            while ($row = mysql_fetch_assoc($posts)) {
               // Creates link to individual post
               echo "<b><a href='posts.php?id=" . $row['id'] . "'>" . $row['post_title']. "</a></b></br>";
            }
         }
      }
   }


page is at http://dragonfall.ath.cx/final/psummary.php


Top
  Profile  
 
 Post subject: Re: Urgent: Need some help figuring out PHP for a class.
PostPosted: Thu Dec 15, 2011 10:49 am 
Team Member Top 50
Team Member Top 50

Joined: Sat Jun 25, 2005 11:04 am
Posts: 1026
In your loop, you aren't doing any checks to see if the year or month has changed.

A hint: you are using too many nested loops (look at the documentation for mysql-fetch-assoc).


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group