SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2002
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy while & loop functions with MySQL not working correctly. Please help!

    When I use the code below in a file to publish news on my website, it goes all weird.

    Basically, the latest newsid post is displayed at the top as it should be. However, underneath that, instead of more newsposts being displayed, the entire page is repeated again (such as the entire header, logo and page start etc) with the latest posts displayed at the top down to the first post.

    Then under that it continues again, for as many news posts as there have been. For exmaple, 4 news posts and the page is looped for times. 3 news posts and it is looped 3 times.

    Like this:

    -- Header --
    - News (ID: latest entry)
    -- Footer --

    -- Header --
    - News (ID latest entry)
    - News (ID 2nd from latest entry)
    - News (ID first entry)
    -- Footer --

    -- Header --
    - News (ID latest entry)
    - News (ID 2nd from latest entry)
    - News (ID first entry)
    -- Footer --

    Since there are 3 posts, the page is looped like this. if there are four posts it is looped like below:

    -- Header --
    - News (ID: latest entry)
    -- Footer --

    -- Header --
    - News (ID latest entry)
    - News (ID 2nd from latest entry)
    - News (ID first entry)
    -- Footer --

    -- Header --
    - News (ID latest entry)
    - News (ID 2nd from latest entry)
    - News (ID first entry)
    -- Footer --

    -- Header --
    - News (ID latest entry)
    - News (ID 2nd from latest entry)
    - News (ID first entry)
    -- Footer --




    *** I am using the script below. ***

    PHP Code:
    <?php


    $db_name 
    "dbname";

    $table_name "news2";

    $connection mysql_connect("localhost""username""password") or die(mysql_error());

    $db mysql_select_db($db_name$connection) or die(mysql_error());

    if (
    $show == "all")

    {

    $sql "SELECT * FROM $table_name ORDER BY title";

    $result mysql_query($sql$connection) or die(mysql_error());

    while (
    $row mysql_fetch_array($result)) {

    $newsid $row['newsid'];
    $title stripslashes($row['title']);
    $postedby stripslashes($row['postedby']);
    $date stripslashes($row['date']);
    $contact stripslashes($row['contact']);
    $description stripslashes($row['description']);





    $display_block .= "<tr><td width=\"20%\" valign=\"top\"><b>$postedby</b><br><a href=\"mailto:$contact\">$contact</a></td><td width=\"80%\"><b>$title - Posted on: $date - <a href=\"mailto:$contact\">Email Poster</a> - <a href=\"edit.php?action=edit&id=$newsid\">Edit</a> - <a href=\"delete.php\">Delete</a></b><hr width=\"93%\" align=\"center\"><br><b><a href=?action=showmore&newsid=$newsid>$title</a><br><br>$description<br><br></td></tr>";

    echo 
    "<html><head><title>Huh</title><body><table width=\"700\" align=\"center\">$display_block</table></body></html>";

    }

    }



    ?>
    Anyone know whats going on with this script. I have a feeling its the WHILE and LOOP playing up, but I haven't the faintest clue.

    Can anyone help?

    For anyone wondering I removed the &action=showmore&newsid=$newsid code section becuase it is irellevant to the post.
    Last edited by noodle; Dec 21, 2002 at 15:35.

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2002
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can anyone help?

  3. #3
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Get all that <html><head><body><table> </html></head></body></table> stuff out of the While loop. You are creating a whole ne page for ever db entry called. While loops usually only use <tr><td> </td></tr>. This makes a new row for each entry. Everythin else goes outside the loop so that it only displayed once.

  4. #4
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You would have gotten a faster reply in the php forum.

  5. #5
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is how I would do it.

    PHP Code:
    <?php
    $db_name 
    "dbname";
    $table_name "news2";
    $connection mysql_connect("localhost""username""password") or die(mysql_error());
    $db mysql_select_db($db_name$connection) or die(mysql_error());
    if (
    $show == "all")
    {
    $sql "SELECT * FROM $table_name ORDER BY title";
    $result mysql_query($sql$connection) or die(mysql_error());
    ?>
       <html>
         <head>
          <title>Huh</title>
            <body>
              <table 
               width="700" 
               align="center">
    <?
    while ($row mysql_fetch_array($result)) {
    $newsid $row['newsid'];
    $title stripslashes($row['title']);
    $postedby stripslashes($row['postedby']);
    $date stripslashes($row['date']);
    $contact stripslashes($row['contact']);
    $description stripslashes($row['description']);
    $display_block .= "<tr><td width=\"20%\" valign=\"top\"><b>$postedby</b><br><a href=\"mailto:$contact\">$contact</a></td><td width=\"80%\"><b>$title - Posted on: $date - <a href=\"mailto:$contact\">Email Poster</a> - <a href=\"edit.php?action=edit&id=$newsid\">Edit</a> - <a href=\"delete.php\">Delete</a></b><hr width=\"93%\" align=\"center\"><br><b><a href=?action=showmore&newsid=$newsid>$title</a><br><br>$description<br><br></td></tr>";
    echo 
    $display_block;
    }
    ?>
              </table>
            </body>
       </html>
    <?
    }
    ?>

  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2002
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks! Its working just as intended now.

    Do you have any idea how to make this log the posters IP address?


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
  •