SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru GamerZ's Avatar
    Join Date
    Nov 2001
    Location
    Singapore
    Posts
    617
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    News Script OOP Question

    Hi all, just beginning to learn about oop for php. I am currently doing a news script. I would like to ask whether the method below is apporiate.

    For Example
    PHP Code:
        $results $db_sql->query("SELECT post_date, post_title, category_name, user_name FROM $tableposts INNER JOIN $tablecategory ON $tableposts.post_category = $tablecategory.category_id INNER JOIN $tableusers ON $tableposts.post_userid = $tableusers.user_id LIMIT 5");
        while(
    $results $db_sql->fetchArray($results)) {
                      
    $postinfo[$results[post_id]] = new News;
                      
    $postinfo[$results[post_id]]-> displayTime();
                      
    $postinfo[$results[post_id]]-> displayDate();
                      
    $postinfo[$results[post_id]]-> displayAuthor();
                      
    $postinfo[$results[post_id]]-> displayContent();
        } 

  2. #2
    SitePoint Guru GamerZ's Avatar
    Join Date
    Nov 2001
    Location
    Singapore
    Posts
    617
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone can help pls?

  3. #3
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Some thoughts;

    I'm not sure about the way you're using $results['post_id'] (note the quotes!) as the index for the $postinfo array. If think you'd be better off letting $postinfo define it's own index.

    Also what are you trying to do here;

    PHP Code:
                      $postinfo[$results[post_id]]-> displayTime();
                      
    $postinfo[$results[post_id]]-> displayDate();
                      
    $postinfo[$results[post_id]]-> displayAuthor();
                      
    $postinfo[$results[post_id]]-> displayContent(); 
    ?

    Here's how I'd do what you're doing (using procedural code);

    PHP Code:
         // Does this query really need to be so complex?
         
    $sql "SELECT
                     post_date, post_title, category_name, user_name
                   FROM
                     
    $tableposts
                   INNER JOIN
                     
    $tablecategory
                   ON
                     
    $tableposts.post_category = $tablecategory.category_id
                   INNER JOIN
                     
    $tableusers
                   ON
                     
    $tableposts.post_userid = $tableusers.user_id
                   LIMIT 5"
    ;

         
    $results $db_sql->query($sql);

         
    $postinfo=array(); // Initialize

         // Note "$result =" NOT "$results = "
         
    while($result $db_sql->fetchArray($results)) {
               
    $postinfo[] = new News($result);
         }

         
    // Output here
         
    foreach ( $postinfo as $post ) {
              echo ( 
    'Posted at '.$post->time() );
         } 
    Have a read of this and this for some more ideas.

  4. #4
    SitePoint Guru GamerZ's Avatar
    Join Date
    Nov 2001
    Location
    Singapore
    Posts
    617
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the help =D. The SQL code will join my post, category and user tables together, so that I can get the username and category name. Hehe


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •