SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2001
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to fix my code that keeps picking up the same data in a while loop

    Hello, I've created a code to create a table according to Mysql data.

    This is the code:

    PHP Code:

    $sql 
    = @mysql_query ("SELECT * FROM frontpage_banners");
                if (!
    $sql) { echo ("Can't request data"); }            
                
    $num_collums mysql_num_rows($sql);
                
                while (
    $banners_array mysql_fetch_array($sql)){
                
    $title $banners_array ["title"];    
                
    $image_name $banners_array ["image_name"];
                
                while (
    $num_collums >= 3){
                echo (
    "<tr>");

                for (
    $counter 0$counter 3$counter++){
                echo (
    "<td>");
                echo (
    "<table>");
                echo (
    "<tr>");
                echo (
    "<td>");
                echo (
    "<img src=\"update/images_upload/$image_name\" width=\"120\" height=\"60\">");
                echo (
    "</td>");
                echo (
    "</tr>");
                echo (
    "</table>");
                }

                echo (
    "</tr>");
                
    $num_collums $num_collums 3;

                }

                if (
    $num_collums 3){
                
    $collum_span $num_collums;
                echo (
    "<tr>");

                while (
    $num_collums 0){
                echo (
    "<td>");
                echo (
    "<table>");
                echo (
    "<tr>");
                echo (
    "<td>");
                echo (
    "<img src=\"update/images_upload/$image_name\" width=\"120\" height=\"60\">");
                echo (
    "</td>");
                echo (
    "</tr>");
                echo (
    "</table>");
                
    $num_collums-- ;
                }

                echo (
    "<td colspan=\".$collum_span.\"></td>");
                echo (
    "</tr>");

                }
                } 
    The creating of the rows + collums works fine but it only displays the first entry in my mysql database..

    How to solve this?
    Thnx in advance.

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Germany
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You made one basic mistake: Your while-loop for reading the result of your query (your $banners_array) can create visible output for only one time.

    You cascaded several while- and for-loops into the first and most important while-loop - PHP processes them until $num_collums reaches 0 (the first for-loop creates the same banner-picture 3 times in a table-row, doesn't it?). When 0 is reached, the second array of $banners_array is read. But since the following while-loops depend on $num_collums to be greater than 0, they don't produce any output.

    Try to work with if-structures instead of while-loops.

    Baerbirke

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2001
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think "if" stuff will solve it?
    Someone told me I have to do it with array's but I don't know how my code would have to look like..anyone know?

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Germany
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    if-Structured

    OK - let's see... You want to display banners in a table, three per row.
    This should work (haven't tested it in real life since I don't have your database):

    PHP Code:
    <?php
    $sql 
    = @mysql_query ("SELECT * FROM frontpage_banners");

    if (!
    $sql):
        echo (
    "Can't request data");
    else:    
        echo(
    "<tr><td><table>");

        
    $tdcounter=0;

        while (
    $banners_array mysql_fetch_array($sql)){
                
    $title $banners_array ["title"];    
                
    $image_name $banners_array ["image_name"];
                
            if(
    $tdcounter==0):
                echo(
    "<tr><td>");
                echo(
    "<img src=\"update/images_upload/$image_name\" width=\"120\" height="\60\">);
                echo("
    </td>");
                
    $tdcounter++;
            else:
                echo("
    <td>");
                echo("
    <img src=\"update/images_upload/$image_name\" width=\"120\" height="\60\">);
                echo("
    </td>");
                
    $tdcounter++;
            endif;

            if(
    $tdcounter==3):
                echo("
    </tr>");
                
    $tdcounter = 0;
            endif;
        };
        if(
    $tdcounter<3)
            echo("
    </tr>");
        endif;
        echo("
    </table></td></tr>");
    endif;
    ?>
    Baerbirke

  5. #5
    SitePoint Zealot
    Join Date
    Dec 2001
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I get a parse error with that code.

  6. #6
    SitePoint Evangelist cyngon's Avatar
    Join Date
    Aug 2001
    Location
    Livonia, MI, USA
    Posts
    513
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What does the parse error say, exactly?

    When posting that you have a parse error, always be sure to include the text of the error.

    Also, it helps to tell us what line of code is the one the parse error's line number corresponds with. Since we usually aren't looking at a full script (and even if we are, no one likes to count line numbers ), a parse error on line 123 isn't much help unless we know what line 123 is.

  7. #7
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Germany
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I reviewed my script - the parse error should have come from unescaped quotes (my fault) in the lines where the img are referenced.

    This should be better now:
    PHP Code:
    <?php
    $sql 
    = @mysql_query ("SELECT * FROM frontpage_banners");

    if (!
    $sql):
        echo (
    "Can't request data");
    else:    
        echo(
    "<tr><td><table>");

        
    $tdcounter=0;

        while (
    $banners_array mysql_fetch_array($sql)){
                
    $title $banners_array ["title"];    
                
    $image_name $banners_array ["image_name"];
                
            if(
    $tdcounter==0):
                echo(
    "<tr><td>");
                echo(
    "<img src=update/images_upload/$image_name width=120 height=60>");
                echo(
    "</td>");
                
    $tdcounter++;
            else:
                echo(
    "<td>");
                echo(
    "<img src=update/images_upload/$image_name width=120 height=60>");
                echo(
    "</td>");
                
    $tdcounter++;
            endif;

            if(
    $tdcounter==3):
                echo(
    "</tr>");
                
    $tdcounter 0;
            endif;
        };
        if(
    $tdcounter<3)
            echo(
    "</tr>");
        endif;
        echo(
    "</table></td></tr>");
    endif;
    ?>

  8. #8
    SitePoint Zealot
    Join Date
    Dec 2001
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    still a parse error

  9. #9
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    Canada
    Posts
    174
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by cyngon
    What does the parse error say, exactly?

    When posting that you have a parse error, always be sure to include the text of the error.

    Also, it helps to tell us what line of code is the one the parse error's line number corresponds with. Since we usually aren't looking at a full script (and even if we are, no one likes to count line numbers ), a parse error on line 123 isn't much help unless we know what line 123 is.
    ....

  10. #10
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Germany
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Again - to find the parse error, I reviewed it once more and even tried it (without the databases part) to get the number of the line the parse error occurs. And - tada! - a simple colon ":" is missing and the end of the script.

    So try this:

    PHP Code:
    <?php
    $sql 
    = @mysql_query ("SELECT * FROM frontpage_banners");

    if (!
    $sql):
        echo (
    "Can't request data");
    else:    
        echo(
    "<tr><td><table>");

        
    $tdcounter=0;

        while (
    $banners_array mysql_fetch_array($sql)){
                
    $title $banners_array ["title"];    
                
    $image_name $banners_array ["image_name"];
                
            if(
    $tdcounter==0):
                echo(
    "<tr><td>");
                echo(
    "<img src=update/images_upload/$image_name width=120 height=60>");
                echo(
    "</td>");
                
    $tdcounter++;
            else:
                echo(
    "<td>");
                echo(
    "<img src=update/images_upload/$image_name width=120 height=60>");
                echo(
    "</td>");
                
    $tdcounter++;
            endif;

            if(
    $tdcounter==3):
                echo(
    "</tr>");
                
    $tdcounter 0;
            endif;
        };
        
    // In the following line the colon was missing
        
    if($tdcounter<3):
            echo(
    "</tr>");
        endif;
        echo(
    "</table></td></tr>");
    endif;
    ?>
    And by reviewing it - it could have been done shorter, like this:

    PHP Code:
    <?php
    $sql 
    = @mysql_query ("SELECT * FROM frontpage_banners");

    if (!
    $sql):
        echo (
    "Can't request data");
    else:    
        echo(
    "<tr><td><table>");

        
    $tdcounter=0;

        while (
    $banners_array mysql_fetch_array($sql)){
                
    $title $banners_array ["title"];    
                
    $image_name $banners_array ["image_name"];
                
            if(
    $tdcounter==0):
                echo(
    "<tr>");
            endif;

            echo(
    "<td>");
            echo(
    "<img src=update/images_upload/$image_name width=120 height=60 alt=$title>");
            echo(
    "</td>");
            
    $tdcounter++;

            if(
    $tdcounter==3):
                echo(
    "</tr>");
                
    $tdcounter 0;
            endif;
        };
        if(
    $tdcounter<3):
            echo(
    "</tr>");
        endif;
        echo(
    "</table></td></tr>");
    endif;
    ?>
    Baerbirke


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
  •