SitePoint Sponsor

User Tag List

Results 1 to 20 of 20
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem Displaying Resultset Across Rows in a Table

    Hello Everyone --

    I am an experienced programmer now learning PHP. I'm trying to figure out how to get my resultset to display in a 3 column table with 3 results displaying in each row.

    I searched here at SitePoint and I found examples of how to do this and I have it basically working. However, I'm finding that my code is skipping the first record in each sunsequent row after the 1st one - and I'm not sure why.

    I'm hoping someone can take a look at my code and provide some insight:


    PHP Code:
    <?
    $from 
    0;
    $max_results=mysql_result($result_count0);
    $resfamilyproducts mysql_query("SELECT * FROM products where parent_cat='$catid' order by product_name ASC LIMIT $from$max_results;");


    while(
    $result_products mysql_fetch_assoc($resfamilyproducts)){

    $str_to_print .= "<tr>";     
    for (
    $i 1$i <= 3$i++) {

    if (
    $result_products mysql_fetch_assoc($resfamilyproducts)) {

    $str_to_print .= '<td>*Some field from MYSQL*</td>'
    ?>

    <? 
    $from
    ++;

    // end if
    else { // No row, I am still inside this row finish with blank data entries             
    $str_to_print .= '<td>&nbsp</td>';         
    // end else
     
    // end for
    // end while 
        
    $str_to_print .= "</tr>"// Add end of row to     
        
    echo($str_to_print); // Print single table row
    ?>
    Thanks for any help you can provide.

    -- Chris

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $max_results=mysql_result($result_count0);
    $resfamilyproducts mysql_query("SELECT * FROM products where parent_cat='$catid' order by product_name ASC LIMIT $from$max_results;");
    $result_products mysql_fetch_assoc$resfamilyproducts ); // Fetch your first row here


    while( $result_products ){ // Do while you have a resultset
        
    $str_to_print .= "<tr>";     
        for (
    $i 1$i <= 3$i++) {
            if ( 
    $result_products ) {
                
    $str_to_print .= '<td>*Some field from MYSQL*</td>';
                
    $result_products mysql_fetch_assoc$resfamilyproducts ); // Fetch your row for the next pass
            
    } else { // No row, I am still inside this row finish with blank data entries
                
    $str_to_print .= '<td>&nbsp</td>';         
            } 
    // end else
        
    // end for
    // end while 

    $str_to_print .= "</tr>"// Add end of row to
    echo($str_to_print); // Print single table row 
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  3. #3
    SitePoint Zealot glowdot's Avatar
    Join Date
    Jun 2007
    Location
    Santa Monica, CA
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are fetching a row in the while conditional, and then fetching rows in your for loop. But you never did anything with the first one you grabbed in the while conditional.

    (Just explaining why byron3's code will work while yours doesn't )
    Advertise on Glowfoto
    banners as low as $25/month, text ads $10/month
    Share 10 million impressions per month!

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, Byron for the reply. As soon as I make those changes, I now get:

    Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 7864412 bytes) in D:\Inetpub\mediapostinc\clients\PBM\productfamily.php on line 70

    Here's my code in that area, including line 70:

    PHP Code:
    if ($result_products) {

    $str_to_print .= '<td><table cellpadding="8"><tr><td><a href="productdetail.php?productid=' $result_products['id'] . '"><img width="140" height="202" alt="' $result_products['product_image'] . '" src="images/products/' $result_products['product_image'] . '"/></a></td></tr><tr><td><div style="text-align:center"><a href="productdetail.php?productid=' $result_products['id'] . '">' $result_products['product_name'] . '</a></td></tr></table></td>'
    ?> 
    70 is where I try to set up the string with the table to display.

    Any other ideas/suggestions?

  5. #5
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if ( $result_products ) {
        
    $str_to_print .= '<td>*Some field from MYSQL*</td>';
        
    $result_products mysql_fetch_assoc$resfamilyproducts ); // Fetch your row for the next pass
    } else { // No row, I am still inside this row finish with blank data entries
        
    $str_to_print .= '<td>&nbsp</td>'
    I am wondering if you have another snippet of code somewhere that you used to start with. In the above snippet I do not think "*Some field from MYSQL*" is going to produce a column from the "$result_products" row.

    Look @ your page source, the table structure has to be there.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  6. #6
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is. I just posted it. *points up* When I make those changes, I get the out of memory error I indicated.

    I tried manually setting the memory to a higher amount on the page and I get the same error - only with higher memory amounts.

    Any thoughts?

  7. #7
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $max_results=mysql_result($result_count0);
    $resfamilyproducts mysql_query("SELECT * FROM products where parent_cat='$catid' order by product_name ASC LIMIT $from$max_results;");
    $result_products mysql_fetch_assoc$resfamilyproducts ); // Fetch your first row here

    echo "<table cellpadding=8>\n";
    while( 
    $result_products ){ // Do while you have a resultset
        
    echo "    <tr>\n";     
        for (
    $i 1$i <= 3$i++) {
            if ( 
    $result_products ) {
                echo 
    "        <td>\n";
                echo 
    "            <a href=\"productdetail.php?productid=" .$result_products["id"]. "\">\n";
                echo 
    "            <img width='140' height='202' alt='" .$result_products['product_image']. "' src='images/products/" .$result_products['product_image']. "' /></a>";
                echo 
    "        </td>\n";
                
    $result_products mysql_fetch_assoc$resfamilyproducts ); // Fetch your row for the next pass
            
    } else { // No row, I am still inside this row finish with blank data entries
                
    echo "        <td>&nbsp</td>\n";         
            } 
    // end else
        
    // end for
        
    echo "    </tr>\n";
    // end while 

    echo "</table>>\n"// Add end of row to 
    I am hoping that "$result_products['product_image']" is the name of the photo and not the actual photo.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  8. #8
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by byron3@earthlink View Post

    I am hoping that "$result_products['product_image']" is the name of the photo and not the actual photo.
    Correct. the files reside on the server. The only thing in the field is the filename itself.

  9. #9
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, using Byron's code sample, I now have it 99% working. Now the only problem is the 1st record in the resultset is repeating itself, then the rest of the records are displaying as I intend them to. So, I end up with one extra, duplicate record at the very beginning of my table display.

    Here's the current code:

    PHP Code:
    <?
    //echo $max_results;


    $max_results=mysql_result($result_count0); 
    $products mysql_query("SELECT * FROM pbm_products where parent_cat='$catid' order by product_name ASC LIMIT $from$max_results;"); 

    $result_products mysql_fetch_assoc($products); // Fetch your first row here   
    //while( $result_products ){ // Do while you have a resultset     
    $str_to_print .= "<tr>";          
        for (
    $i 1$i <= 3$i++) {         
                if ( 
    $result_products ) {             
                    
    $str_to_print .= '<td><table cellpadding="8"><tr><td><a href="productdetail.php?productid=' $result_products['id'] . '"><img width="140" height="202" alt="' $result_products['product_image'] . '" src="images/products/' $result_products['product_image'] . '"/></a></td></tr><tr><td><div style="text-align:center"><a href="moviepage.php?movie_id=' $result_products['id'] . '">' $result_products['product_name'] . '</a></td></tr></table></td>';              
                    
    $result_products mysql_fetch_assoc$resfamilyproducts ); // Fetch your row for the next pass         
                    
                        
    } else { // No row, I am still inside this row finish with blank data entries             
                        
    $str_to_print .= '<td>&nbsp</td>';                  
                        } 
    // end else     
                        
    // end for 
    //                    } // end while  
                        
    $str_to_print .= "</tr>"// Add end of row to 
                        
                        
    echo($str_to_print); // Print single table row  

    }
    ?>
    Assistance on this last issue would be appreciated - and thanks for the other ideas as well!

    -- Chris

  10. #10
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using the code provided you are only going to get a single table row containing 3 tabel cells. Each cell will contain a table.
    If this is the code you have working there is no way the first record is pepeated unless it occurs twice in the resultset.
    Have you looked @ the resultset ?
    var_dump( $products ); ?
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  11. #11
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have direct access to the database - I created the table and inserted the records myself.

    I have verified that it is repeating the first record the outputing the rest. You can see it in action here:

    http://www.mediapostinc.com/clients/PBM/productfamily.php?parent_cat=11

    You can clearly see that the first record is repeated then the rest continue.

    Any thoughts?

    -- Chris

  12. #12
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I do not want to see the results. I want to see your recordset, would you do a variable dump and post the output here ?
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  13. #13
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Putting var_dump( $products ); in my code returns:

    resource(7) of type (mysql result)

    Is this returning what you expect?

  14. #14
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Under the line :
    PHP Code:
    for ($i 1$i <= 3$i++) {         
                if ( 
    $result_products ) { 
    do a variable dump on $result_products
    and ttell me what the results look like
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  15. #15
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's what I get:

    Code:
    array(4) { ["id"]=> string(2) "18" ["product_name"]=> string(32) "Angle Stem Flush Tank Ball Valve" ["product_image"]=> NULL ["parent_cat"]=> string(2) "11" } array(4) { ["id"]=> string(2) "18" ["product_name"]=> string(32) "Angle Stem Flush Tank Ball Valve" ["product_image"]=> NULL ["parent_cat"]=> string(2) "11" } array(4) { ["id"]=> string(1) "2" ["product_name"]=> string(15) "ANSI Ball Valve" ["product_image"]=> string(10) "twoway.jpg" ["parent_cat"]=> string(2) "11" } array(4) { ["id"]=> string(2) "22" ["product_name"]=> string(31) "Cylindrical Radius Flush Valves" ["product_image"]=> NULL ["parent_cat"]=> string(2) "11" } array(4) { ["id"]=> string(2) "19" ["product_name"]=> string(20) "Diverter Port Valves" ["product_image"]=> NULL ["parent_cat"]=> string(2) "11" } array(4) { ["id"]=> string(1) "3" ["product_name"]=> string(21) "Flush Tank Ball Valve" ["product_image"]=> string(0) "" ["parent_cat"]=> string(2) "11" } array(4) { ["id"]=> string(2) "20" ["product_name"]=> string(17) "Multi Port Valves" ["product_image"]=> NULL ["parent_cat"]=> string(2) "11" } array(4) { ["id"]=> string(2) "17" ["product_name"]=> string(30) "Sanitary Angle Stem Flush Tank" ["product_image"]=> NULL ["parent_cat"]=> string(2) "11" } array(4) { ["id"]=> string(2) "21" ["product_name"]=> string(27) "Transmitter Isolation Valve" ["product_image"]=> NULL ["parent_cat"]=> string(2) "11" } array(4) { ["id"]=> string(1) "1" ["product_name"]=> string(18) "Two-Way Ball Valve" ["product_image"]=> string(10) "twoway.jpg" ["parent_cat"]=> string(2) "11" }

  16. #16
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By those results your first two records are identical....
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  17. #17
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't see how that can be. I have direct access to the MYSQL DB - and I manually created the table and entered the records myself. I know for a fact that there are no duplicates. This makes me think its something in the code...

  18. #18
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have gone back and removed from the database all results I was trying to display. I have added one single entry - there are no duplicates - and it is still being displayed twice.

    Any ideas?

  19. #19
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $max_results=mysql_result($result_count0);
    $query "SELECT * FROM products where parent_cat='$catid' order by product_name ASC LIMIT $from$max_results";

    $results mysql_query$query );
    $row mysql_fetch_assoc$results ); // Fetch your first row here

    while( $row ){ // Do while you have a resultset
        
    echo "  <tr>\n";
        
        for (
    $i 0$i 3$i++) { // Three cells across
            
    if ( $row ) {
                echo 
    "<td>\n";
                echo 
    "<table cellpadding='8'>\n";
                echo 
    "<tr><td>\n";
                echo 
    "<a href=\"productdetail.php?productid='" .$row['id'] . "'\">\n";
                echo 
    "<img width='140' height='202' alt='" .$row['product_image']. "'";
                echo 
    "src='images/products/" $row['product_image'] . "' /></a>\n";
                echo 
    "</td></tr>\n";
                echo 
    "<tr><td><div style=\"text-align:center\">\n";
                echo 
    "<a href=\"moviepage.php?movie_id='" .$row['id'] . "'>" .$row['product_name'];
                echo 
    "</a></td></tr></table></td>\n";
                echo 
    "</td>\n";
                
                
    $row mysql_fetch_assoc$result ); // Fetch your row for the next pass
            
    } else { // No row, I am still inside this row finish with blank data entries
                
    echo "        <td>&nbsp</td>\n";         
            } 
    // end else
        
    // end for

        
    echo "    </tr>\n";

    // end while 
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  20. #20
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks that that did the trick...

    Thanks for the help!


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
  •