SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Thread: {PHP} Repeat

Hybrid View

  1. #1
    SitePoint Enthusiast NetworkTown.Net's Avatar
    Join Date
    Jan 2006
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    {PHP} Repeat

    Hi

    I am having some problems atm, i am trying to get the following php code to go 4 across and then i want to use $i - $i++ to make it loop down. But when i try to make it go 4 across i get a problem the same result is shown 4 across and then another result which is the same across.

    Here is my code:

    PHP Code:
    <?php

    l_display 
    '3';

    $result mysql_query("SELECT * FROM media ORDER BY Id DESC, RAND() LIMIT $l_display");

    while(
    $row mysql_fetch_array($result))
      {

    echo 
    '<a href="view.php?id='.$row['Id'].'">';
    echo 
    '<img src="img/'.$row['Img'].'" border="0" width="142" height="100"><br /><b><strong>';
    echo 
    $row['Title'];
    echo 
    "</a><br /></strong></b>";
    echo 
    $row['Description'];
    echo 
    "<br /><small>Submitted: ";
    echo 
    $row['Time'];
    echo 
    "</small><br />";
    echo 
    '<td>';

      }

    ?>
    How would i make it go 4 across which will get diffrent results instead of the same?

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i think this is what you want, im having a hard time understanding you.

    im cutting and pasting some code so you will need to adapt it
    PHP Code:
    // how many columns we want our table to have
    $columns 4;



    echo 
    "<table>\n";

    for (
    $i 0$row mysql_fetch_assoc($result); $i++) {

        if ((
    $i $columns) == 0) {
            echo 
    " <tr>\n";
        }

        echo 
    "  <td>" $row['fieldname'] . "</td>\n";

        if ((
    $i $columns) == ($columns 1)) {
            echo 
    " </tr>\n";
        }

    }

    // find out how many empty <td></td> we need to output now
    // to make the html correct and well formed
    $remainder = ($columns - ($i $columns)) % $columns;

    // do we have remainders?
    if ($remainder 0) {
        echo 
    str_repeat("  <td></td>\n"$remainder);
        
    // or alternatively
        // echo "  <td colspan=\"$remainder\"></td>\n";
        
    echo " </tr>\n";
    }
    echo 
    "</table>\n"
    and then get rid of the limit in your query.

  3. #3
    SitePoint Enthusiast NetworkTown.Net's Avatar
    Join Date
    Jan 2006
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So the bit where it says
    echo " <td>" . $row['fieldname'] . "</td>\n";
    Is where i put all the content like this:
    echo '<a href="view.php?id='.$row['Id'].'">';
    echo '<img src="img/'.$row['Img'].'" border="0" width="142" height="100"><br /><b><strong>';
    echo $row['Title'];
    echo "</a><br /></strong></b>";
    echo $row['Description'];
    echo "<br /><small>Submitted: ";
    echo $row['Time'];
    echo "</small><br />";
    echo '<td>';

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yup

  5. #5
    SitePoint Enthusiast NetworkTown.Net's Avatar
    Join Date
    Jan 2006
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well i one result with that, i did this:

    // how many columns we want our table to have
    $columns = 4;



    echo "<table>\n";

    for ($i = 0; $row = mysql_fetch_assoc($result); $i++) {

    if (($i % $columns) == 0) {
    echo " <tr>\n";
    }

    echo '<td><a href="view.php?id='.$row['Id'].'">';
    echo '<img src="img/'.$row['Img'].'" border="0" width="142" height="100"><br /><b><strong>';
    echo $row['Title'];
    echo "</a><br /></strong></b>";
    echo $row['Description'];
    echo "<br /><small>Submitted: ";
    echo $row['Time'];
    echo "</small><br />";
    echo '</td>';

    if (($i % $columns) == ($columns - 1)) {
    echo " </tr>\n";
    }

    }

    // find out how many empty <td></td> we need to output now
    // to make the html correct and well formed
    $remainder = ($columns - ($i % $columns)) % $columns;

    // do we have remainders?
    if ($remainder > 0) {
    echo str_repeat(" <td></td>\n", $remainder);
    // or alternatively
    // echo " <td colspan=\"$remainder\"></td>\n";
    echo " </tr>\n";
    }
    echo "</table>\n";

    }

  6. #6
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry i dont understand?

  7. #7
    SitePoint Enthusiast NetworkTown.Net's Avatar
    Join Date
    Jan 2006
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well when i used the code above i got only one result shown from the database and not 4 across

  8. #8
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $result = mysql_query("SELECT * FROM media ORDER BY Id DESC, RAND() LIMIT 8");

  9. #9
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wait...


    Code:
    // do you just need 4 across like
    1 2 3 4
    
    // or do you need
    1 2 3 4
    5 6 7 8
    etc...

  10. #10
    SitePoint Enthusiast NetworkTown.Net's Avatar
    Join Date
    Jan 2006
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i need it like the second one:
    1234
    5678

  11. #11
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thats what it currently does. you need to feed at least 5 records into that code for it to wrap it to the next table row.

  12. #12
    SitePoint Enthusiast NetworkTown.Net's Avatar
    Join Date
    Jan 2006
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh i currently got 3 into the database ill add around 15 and then get back to you.


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
  •