Results 1 to 8 of 8

Thread: Display Results in rows

  1. #1
    Join Date
    Jul 2007
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Display Results in rows

    I need a code to display a search result from mysql.

    If I'm using a table for example, display the first 4 results in the first row - <tr> tag, the next 4 on the next row and the next 4 on the next one etc.

    I also need to set a limit as to how many results can be displayed, say 16 - so 4 rows, before I have a "next page" link to continue displaying the remaining results in a similar fashion.

    can someone help? i'm hopeless at if statements which is what i feel this requires. lol

  2. #2
    Join Date
    Jul 2007
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    no-one... please...

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    <table>
    <?php
      $rowwidth = 4;
    
      for($i = @$_GET['start'] or 0; $row = mysql_fetch_array($rs) && $i < @$_GET['num'] or 16; ++$i) {
        if($i &#37; $rowwidth === 0) {
          if($i !== @$_GET['start'] or 0) {
    ?>
      </tr>
          <?php } ?>
      <tr>
        <?php } ?>
        <td><?php echo $row['result']; ?></td>
      <?php } ?>
      </tr>
    </table>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Jul 2007
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    that certainly explains it, but i can't seem to alter it to fit in with the following code:

    <td>

    $ID = $_GET['ID'];

    $result = mysql_query("SELECT * FROM fragrances WHERE ID=$ID ORDER BY PRO")
    or die(mysql_error());

    echo "<table border='0' cellpadding='5' cellspacing='0' width='500'>";
    while($row = mysql_fetch_array( $result )) {
    // Print out the contents of each row into a table
    echo "<tr><td align='center' bgcolor='#ffffff' width='125'><p class='brand_div'>";
    echo $row['ima_link'];
    echo "</p></td></tr><tr><td align='center' bgcolor='#ffffff' width='125'><p class='brand_head'>";
    echo $row['PRO'];
    echo "</p></td></tr><tr><td align='center' bgcolor='#ffffff' width='125'><p class='brand_cli'>";
    echo $row['cli_link'];
    echo "</p></td></tr><tr><td align='center' bgcolor='#ffffff' width='500'><hr color='#cccccc' width='125'></td></tr>";
    }

    echo "</table>";
    ?>

    </td>

    can someone help me. i know it's more complex but i really do appreciate all this help, you're lifesavers - honestly!

  5. #5
    Join Date
    Jul 2007
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    ooh, i really hope someone can. i'm very confused and can't apply the code to my template without getting error messages...

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    <table>
    <?php
      $rowwidth = 4;
      if(!is_numeric($_GET['id']))
        die('Invalid ID specified.');
      $rs = mysql_query('SELECT * FROM fragrances WHERE ID=' . $_GET['id'] . ' ORDER BY PRO');
    
      for($i = @$_GET['start'] or 0; $row = mysql_fetch_array($rs) && $i < @$_GET['num'] or 16; ++$i) {
        if($i &#37; $rowwidth === 0) {
          if($i !== @$_GET['start'] or 0) {
    ?>
      </tr>
          <?php } ?>
      <tr>
        <?php } ?>
        <td><?php echo $row['result']; ?></td>
      <?php } ?>
      </tr>
    </table>
    A lot of your markup is deprecated. Try to validate with HTML 4.01 Strict.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey View Post
    PHP Code:
    for($i = @$_GET['start'] or 0$row mysql_fetch_array($rs) && $i < @$_GET['num'] or 16; ++$i) {
        if(
    $i $rowwidth === 0) {
          if(
    $i !== @$_GET['start'] or 0) { 
    Rather than these ors, which will need to be re-evaluated on subsequent iterations, why not just use separate variables?

    PHP Code:
    $start = isset($_GET['start']) ? $_GET['start'] : 0;
    $count = isset($_GET['num']) ? $_GET['num'] : 16;
    $i $start;
    while ((
    $row mysql_fetch_array($rs)) && ($i $count)) {
        if ((
    $i $rowwidth) == 0) {
            if (
    $i != $start) {
                
    /* ... */
            
    }
            
    /* ... */
        
    }
        ++
    i;

    Quote Originally Posted by e1seix View Post
    that certainly explains it, but i can't seem to alter it to fit in with the following code:
    It would seem that you've missed the opening <?php delimiter. However, I would also make some further changes. The most important I will address specifically:

    PHP Code:
    $ID $_GET['ID'];

    $result mysql_query("SELECT * FROM fragrances WHERE ID=$ID ORDER BY PRO"
    or die(
    mysql_error()); 
    Never, ever include externally obtained data in a database query without validating and sanitising it - the latter may not always be necessary. This opens oneself to SQL injection attacks. Typically, database error messages shouldn't be displayed either for the same reason; it also looks very unprofessional.

    If $ID is meant to be a number, either coerce it to one or use pattern matching. Either way, errors need to be handled gracefully.

    PHP Code:
    <td>
    <?php
    // The ID parameter should be tested for existence using the isset function, with
    // either a default value or an error-handling mechanism in place if missing.
    //
    // Assuming this is just an integer, sanitation is not necessary as a number will
    // never be harmful (though it might be an invalid or unexpected value).
    $ID = (int) $_GET['ID'];


    $result mysql_query("SELECT * FROM fragrances WHERE ID=$ID ORDER BY PRO");
    if (!
    $result) {
        
    // Redirect to an error page that does something nice, such as apologise for
        // the problem - don't get technical.
        // One could even take the opportunity to send an e-mail to notify someone
        // of a problem, though obviously this would need to be logged to prevent it
        // from being sent many times.
    }
    ?>
    <table border="0" cellpadding="5" cellspacing="0" width="500">
    <?php
    while (($row mysql_fetch_array($result))) {
    ?>
        <tr>
            <td align="center" bgcolor="#ffffff" width="125">
                <p class="brand_div"><?php echo $row['ima_link']; ?></p>
            </td>
        </tr>
        <tr>
            <td align="center" bgcolor="#ffffff" width="125">
                <p class="brand_head"><?php echo $row['PRO']; ?></p>
            </td>
        </tr>
        <tr>
            <td align="center" bgcolor="#ffffff" width="125">
                <p class="brand_cli"><?php echo $row['cli_link']; ?></p>
            </td>
        </tr>
        <tr>
            <td align="center" bgcolor="#ffffff" width="500">
                <hr color="#cccccc" width="125">
            </td>
        </tr>
    <?php

    ?>
    </table>
    </td>
    If your data is really tabular, use CSS instead of presentational attributes. Using tbody elements may be useful in grouping each block, perhaps making the hr element obsolete. If the data isn't tabular, drop the tables: table-based layouts are an abomination.

    Hope that sets you on the right track.
    Mike

  8. #8
    Join Date
    Jul 2007
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    dudes, i really appreciate all you've done for me... but i'm just not getting it. call me stupid or a novice...

    this might give you some idea as to what i'm looking to achieve. the two lots of code you have given me only return error messages. hundreds of repetitive ones!

    http://www.dogfightuk.com/byBrand.php?ID=4511

    the idea is to display all the "products" four in a row in a cascading fashion

    bear in mind i am still only at the very initial construction stage so of course i will have a redirection page and i am aware of the opening <?php. i just pasted the basic php code to see if you could make any sense.

    i'm beginning to wonder if this is achievable, by me at least.

    try and talk me through this... please!
    Last edited by e1seix; 07-23-2007 at 12:43 AM.

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
  •