News Script Advice

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Lorddraco98
    Senior Member
    • Nov 2001
    • 301
    • 3.5.x

    News Script Advice

    I need some advice here.
    I just made a news script that allows one to post news articles, edit and delete them, and a page that displays them. All works great.

    Now, I want to have it so the user can input a number, say 10, and after 10 news stories, the script stops displaying news stories, there is a link to an archive page, and then the arhcive page displays all the news after the 10 already displayed on the main page.

    So basically, the 10 newest news stories on the main page, then the rest show on an archive page. What is the best way to do this?

    my DB has a news table tat i'm working off of with this structure:

    newsid int(11) No auto_increment primary
    subject text No
    body longtext No
    username text No
    userid int(11) No 0
    date datetime No 0000-00-00 00:00:00
    email text No
    they are all not null, and the userid has a default value of 0 and date field has the default value lsited.

    my current code to display all news articles:
    PHP Code:
    $query1"Select newsid from news";
    $result1mysql_query($query1)
    or die(
    "couldn't execute query");
    $num1mysql_num_rows($result1);
    if (
    $num1==0)
    {
    echo 
    "There are currently no news articles to display. Please check back later!";
    }
    for (
    $i=1$i<=$num1$i++)
    {
    $query2"select * from news where newsid= '$i'";
    $result2mysql_query($query2)
    or die(
    "couldn't execute query");
    while (
    $rmysql_fetch_array($result2))
     {
     
    $subject$r["subject"];
     
    $post$r["body"];
     
    $name$r["username"];
     
    $date$r["date"];
     
    $email=$r["email"];
     
    $datedate("F jS, Y, G:i a"strtotime($date));
     echo 
    "<font size='+1'><b>$subject </b></font><br><br>";
     echo 
    "$post <br><br>";
     echo 
    "<font size='-1'><i>posted by <a href='mailto:$email'>$name</a> on $date</i></font><br><br><br>";
     }

    Anyone have any ideas?
    -Draco
    The Digital Sector Forums
  • Floris
    Senior Member
    • Dec 2001
    • 37767

    #2
    $query1= "Select newsid from news";

    $query1= "Select newsid from news LIMIT 10";
    might do the trick

    Comment

    • nsr81
      Member
      • Mar 2002
      • 39

      #3
      You do not need to get the number of rows first and the run the queries in a loop.

      Now back to your question. Get a number from the user somehow, either store in a cookie or in user profile etc. Let's say if the number is stored in variable "$maxnews" then the following code should do the trick:

      PHP Code:

      $maxnews 
      10// get the max. news stories number.

      // this query will choose last 10 article, i.e. the latest 10.
      $query1"Select * from news ORDER BY newsid DESC LIMIT $maxnews";

      $result1mysql_query($query1) or die("couldn't execute query");
      $num1mysql_num_rows($result1);

      if (!
      $result1// check if mysql returned NULL
      {
      echo 
      "There are currently no news articles to display. Please check back later!";
      }
      else
      {
       while (
      $rmysql_fetch_array($result1))
       {
       
      $subject$r["subject"];
       
      $post$r["body"];
       
      $name$r["username"];
       
      $date$r["date"];
       
      $email=$r["email"];
       
      $datedate("F jS, Y, G:i a"strtotime($date));
       echo 
      "<font size='+1'><b>$subject </b></font><br><br>";
       echo 
      "$post <br><br>";
       echo 
      "<font size='-1'><i>posted by <a href='mailto:$email'>$name</a> on $date</i></font><br><br><br>";
       }

      hope that helps.

      Comment

      • Lorddraco98
        Senior Member
        • Nov 2001
        • 301
        • 3.5.x

        #4
        Originally posted by TNasir
        You do not need to get the number of rows first and the run the queries in a loop.

        Now back to your question. Get a number from the user somehow, either store in a cookie or in user profile etc. Let's say if the number is stored in variable "$maxnews" then the following code should do the trick:

        PHP Code:

        $maxnews 
        10// get the max. news stories number.

        // this query will choose last 10 article, i.e. the latest 10.
        $query1"Select * from news ORDER BY newsid DESC LIMIT $maxnews";

        $result1mysql_query($query1) or die("couldn't execute query");
        $num1mysql_num_rows($result1);

        if (!
        $result1// check if mysql returned NULL
        {
        echo 
        "There are currently no news articles to display. Please check back later!";
        }
        else
        {
         while (
        $rmysql_fetch_array($result1))
         {
         
        $subject$r["subject"];
         
        $post$r["body"];
         
        $name$r["username"];
         
        $date$r["date"];
         
        $email=$r["email"];
         
        $datedate("F jS, Y, G:i a"strtotime($date));
         echo 
        "<font size='+1'><b>$subject </b></font><br><br>";
         echo 
        "$post <br><br>";
         echo 
        "<font size='-1'><i>posted by <a href='mailto:$email'>$name</a> on $date</i></font><br><br><br>";
         }

        hope that helps.
        ok, awesome man. That works.
        Now for the archive page.
        How would I make a new page(say newsarchive.php)
        display all news articles after the ones already displayed with the $maxnews variable.

        So say $maxnews=4
        I want the news.php to display the 4 newest posts.

        Then newsarchive.php to display 5-x(x being the first news post ever made).
        How would I code that??
        Thanks!
        -Draco
        The Digital Sector Forums

        Comment

        • Lorddraco98
          Senior Member
          • Nov 2001
          • 301
          • 3.5.x

          #5
          I got the news.php program working to display a limited amount of news posts, but I'm having trouble with an archive. I'm not sure what my query should be to limit the results.

          ANy help on making an archive page?
          -Draco
          The Digital Sector Forums

          Comment

          • nsr81
            Member
            • Mar 2002
            • 39

            #6
            Sorry, had a couple of busy days at work and school that's why couldn't respond earlier.

            Here is an example of what you might use on archive page.

            PHP Code:

            //expecpting a variable $page containing the current page number.

            if(!isset($page) || $page == "")
            {
              
            $page 0;  // $page isn't set or is empty, show first page.
            }

            // same $maxnews from previouse script.

            $maxnews 10;

            // calculate the limits of the query.

            $from $page $maxnews;

            $query1 "Select * from news LIMIT $from$maxnews";

            $result1mysql_query($query1) or die("couldn't execute query");
            $num1mysql_num_rows($result1);

            if (!
            $result1// check if mysql returned NULL
            {
            echo 
            "There are currently no news articles to display. Please check back later!";
            }
            else
            {
             while (
            $rmysql_fetch_array($result1))
             {
             
            $subject$r["subject"];
             
            $post$r["body"];
             
            $name$r["username"];
             
            $date$r["date"];
             
            $email=$r["email"];
             
            $datedate("F jS, Y, G:i a"strtotime($date));
             echo 
            "<font size='+1'><b>$subject </b></font><br><br>";
             echo 
            "$post <br><br>";
             echo 
            "<font size='-1'><i>posted by <a href='mailto:$email'>$name</a> on $date</i></font><br><br><br>";
             }

            I've use $page=0 to be the first page, you can play around with that.

            Comment

            • Lorddraco98
              Senior Member
              • Nov 2001
              • 301
              • 3.5.x

              #7
              Originally posted by TNasir
              Sorry, had a couple of busy days at work and school that's why couldn't respond earlier.

              Here is an example of what you might use on archive page.

              PHP Code:

              //expecpting a variable $page containing the current page number.

              if(!isset($page) || $page == "")
              {
                
              $page 0;  // $page isn't set or is empty, show first page.
              }

              // same $maxnews from previouse script.

              $maxnews 10;

              // calculate the limits of the query.

              $from $page $maxnews;

              $query1 "Select * from news LIMIT $from$maxnews";

              $result1mysql_query($query1) or die("couldn't execute query");
              $num1mysql_num_rows($result1);

              if (!
              $result1// check if mysql returned NULL
              {
              echo 
              "There are currently no news articles to display. Please check back later!";
              }
              else
              {
               while (
              $rmysql_fetch_array($result1))
               {
               
              $subject$r["subject"];
               
              $post$r["body"];
               
              $name$r["username"];
               
              $date$r["date"];
               
              $email=$r["email"];
               
              $datedate("F jS, Y, G:i a"strtotime($date));
               echo 
              "<font size='+1'><b>$subject </b></font><br><br>";
               echo 
              "$post <br><br>";
               echo 
              "<font size='-1'><i>posted by <a href='mailto:$email'>$name</a> on $date</i></font><br><br><br>";
               }

              I've use $page=0 to be the first page, you can play around with that.
              no problems man. I understand hehe
              Sorry for all the questions, this situation is just really playing with my mind hehe. Your example code is good, but I want to try something else. What if I wanted the value in $maxnews to display on the main page, like it is now, and then, on the archive.php page to display the rest, and not split that into pages like the code you gave does. Is there a way to do that?
              -Draco
              The Digital Sector Forums

              Comment

              • nsr81
                Member
                • Mar 2002
                • 39

                #8
                I meant the $maxnews on archive is same concept as $maxnews on main page. You can set them differently because they are not the same variable.

                Comment

                • Lorddraco98
                  Senior Member
                  • Nov 2001
                  • 301
                  • 3.5.x

                  #9
                  Originally posted by TNasir
                  I meant the $maxnews on archive is same concept as $maxnews on main page. You can set them differently because they are not the same variable.
                  k, but i guess i dunno if I really should have that if I wanna display all news posts after the ones I've already displayed on the main page
                  -Draco
                  The Digital Sector Forums

                  Comment

                  • Lorddraco98
                    Senior Member
                    • Nov 2001
                    • 301
                    • 3.5.x

                    #10
                    I think I got it
                    TNasir, thank you SO much for your help! I appreciate you taking time to code all that
                    I really appreciate it! I'll let you know if I have anymore problems hehe
                    -Draco
                    The Digital Sector Forums

                    Comment

                    widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
                    Working...