SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Addict
    Join Date
    Apr 2003
    Location
    canada
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post PHP Looping problems

    I'm having a problem with getting a loop to display all records from a MySql db right now it's only displaying 1 record.
    The code for this problem is in the attachment
    Thanks
    Attached Files Attached Files
    Last edited by capples3; Jun 12, 2003 at 11:59.

  2. #2
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The attachment has not been approved yet, but I'm going to attempt to guess the problem. :-)

    My bet is that you've used mysql_fetch_array (or similar function) but you've only used it once rather than running it through a loop like this:

    PHP Code:
    $sql 'SELECT * FROM whatever';
    $result mysql_query($sql);
    while (
    $row mysql_fetch_array($result))
    {
       echo 
    '<pre>';
       
    print_r($row);
       echo 
    '</pre>

    Was I right? Wrong? We'll find out in a few minutes!
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  3. #3
    SitePoint Addict
    Join Date
    Apr 2003
    Location
    canada
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php looping problem

    My code looks a little diffrent I'll copy and and paste it so you can take a look at it,

    <?

    // general mysql access and vars
    include("./admin/inc/connectdb.inc.php3");

    // the document
    include("./menu.html");
    //include("./bottom.html");
    //include("./inc/css1.php");
    //include("./inc/top_banner2.php");
    //include("./inc/top_nav1.php");

    $query = "SELECT * FROM important_dates";
    //print("$query");
    $result = mysql_query($query, $mysql_link);
    while($row = mysql_fetch_row($result)) {
    $id = $row[0];
    $imp_dates = $row[1];
    $message = $row[2];
    $active = $row[3];

    }

    $message = str_replace("\n\r","",$message);
    $message = str_replace("\r\n","",$message);

    $message = str_replace("\r","",$message);
    $message = str_replace("\n","",$message);

    print("

    <p><b><font color=\"990000\" size=\"4\" align=\"left\">Justice Dept. Important Dates</font></b></p>
    <TABLE WIDTH=\"100%\" BORDER=\"0\" bordercolor=\"#000000\" bgcolor=\"#ffffff\" CELLSPACING=\"0\" CELLPADDING=\"2\" ALIGN=\"center\">
    <tr>
    <TH align=\"left\" width=\"5%\"><b>Date</TH>
    <TH align=\"left\" width=\"5%\"><b>Message</TH>
    </tr>
    </tr>
    <td align=\"left\" width=\"5%\">$imp_dates</td>
    <td align=\"left\" width=\"5%\">$message</td>


    </tr>
    ");


    print("

    </TABLE>
    <p>&nbsp</p>
    </BODY>
    </HTML>
    ");

    include("./bottom.html");
    ?>

  4. #4
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?

    // general mysql access and vars
    include("./admin/inc/connectdb.inc.php3" );

    // the document
    include("./menu.html" );
    //include("./bottom.html" );
    //include("./inc/css1.php" );
    //include("./inc/top_banner2.php" );
    //include("./inc/top_nav1.php" );
    echo ('
          <p><b><font color="990000" size="4" align="left">Justice Dept. Important Dates</font></b></p>
          <TABLE WIDTH="100%" BORDER="0" bordercolor="#000000" bgcolor="#ffffff" CELLSPACING="0" CELLPADDING="2" ALIGN="center">
          <tr>
                <TH align="left" width="5%"><b>Date</TH>
                <TH align="left" width="5%"><b>Message</TH>    
            </tr>
    '
    );
    $query 'SELECT * FROM important_dates';
    //print("$query" );
    $result mysql_query($query$mysql_link);
    while (
    $row mysql_fetch_row($result)) {
      
    $id $row[0];
      
    $imp_dates $row[1];
      
    $message $row[2];
      
    $active $row[3];

      
    $message str_replace("\r","",$message);
      
    $message str_replace("\n","",$message);

      echo(
    '  
            </tr>
                <td align="left" width="5%">' 
    $imp_dates '</td>            
                <td align="left" width="5%">' 
    $message '</td>
            </tr>
        '
    );          

    }

                    
                    
    echo(
    '          
    </TABLE>
    <p>&nbsp</p>
    </BODY>
    </HTML>
    '
    );

    include(
    "./bottom.html");
    ?>
    You need to place the print() inside the while () {} loop, if you don't (as in your example) they (the variables) are just constantly over written and only one row will be shown, hope this helps

    Edit:

    Made some non important changes

    Off Topic:

    Off topic, you should use echo() instead of print() in almost all cases and the usage of single quotes instead of double quotes is also generally better
    - website

  5. #5
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to have the printing/echoing within the loop, not after.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  6. #6
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You closed the loop to early.

    PHP Code:
    $query "SELECT * FROM important_dates";
    //print("$query" );
    $result mysql_query($query$mysql_link);
    while(
    $row mysql_fetch_row($result)) {
    $id $row[0];
    $imp_dates $row[1];
    $message $row[2];
    $active $row[3];

    $message str_replace("\n\r","",$message);
    $message str_replace("\r\n","",$message);

    $message str_replace("\r","",$message);
    $message str_replace("\n","",$message);

    print(
    "

    <p><b><font color=\"990000\" size=\"4\" align=\"left\">Justice Dept. Important Dates</font></b></p>
    <TABLE WIDTH=\"100%\" BORDER=\"0\" bordercolor=\"#000000\" bgcolor=\"#ffffff\" CELLSPACING=\"0\" CELLPADDING=\"2\" ALIGN=\"center\">
    <tr>
    <TH align=\"left\" width=\"5%\"><b>Date</TH>
    <TH align=\"left\" width=\"5%\"><b>Message</TH>
    </tr>
    </tr>
    <td align=\"left\" width=\"5%\">
    $imp_dates</td>
    <td align=\"left\" width=\"5%\">
    $message</td>


    </tr>
    ); 


    prints all records

  7. #7
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    beat ya!
    - website

  8. #8
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    dang... that was really fast.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  9. #9
    SitePoint Addict
    Join Date
    Apr 2003
    Location
    canada
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes it now displays all records the only problem I'm have now is the Head Justice Dept. Important Dates is looping as well and the heading DATES and MESSage is looping as well now. The data is properly looping I just don't want the headings to loop as well

  10. #10
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I fixed that in my above post, take a look...
    - website

  11. #11
    SitePoint Addict
    Join Date
    Apr 2003
    Location
    canada
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    never mind i just took it above the loop all is good now Thanks all for your help

  12. #12
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Curses! I must train harder to be able to accurately spot problems based purely upon rhetorical evidence.

    Glad it's working, those were some fast replies up there!
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?


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
  •