jefepwnzer Posted November 2, 2011 Share Posted November 2, 2011 Hello all, I've run into a problem with what seems to be a basic script. I want to display two different tables of data, side by side. Each table is built with a while loop, pulling data from an MySQL query. When I try to wrap the first table in a div the result is a div that appears empty when there is really a table with 60 rows of data I expect it to wrap around. I can't get this to work, I've tried to insert the div tag in many places to no avail. Also, I wanted to add some blank space at the bottom of the page so that there was room between the end of the table and the actual bottom of the page. I tried adding some <br />'s at the end of the code but that didn't work. I guess I'm confused because the code is acting as if the 60 rows of table data isn't there. When I tried to add my page layout around the script (as includes at the top and bottom of the script) the layout acts as if the table isn't there, either. That is to say that my "header" and "footer" appear bunched up at the top of the page as if there wasn't content on the screen. Hopefully someone can help! Thanks. <?php echo "<br /> <a href='index.php'> Back Home </a> <br /> <br />"; // -------------------------- Connect to DB include ('connect_script.php'); // ------------------------------ Color variable for alternating table colors $color = 1; // ------------------------- Query Parameters $select = "SELECT (number) AS id, (first_name) AS fn, (last_name) AS ln, (position) AS position FROM table WHERE position = 'X' "; $get = @mysqli_query ($dbc, $select); // ------------------------- Run Query if ($get) { // ------------------------Start table echo " <div style='border-style: solid;'> <table align='left' border='1'> <tr> <td>People</td> </tr> <tr> <td>ID</td><td>First Name</td><td>Last Name</td><td>Position</td> </tr>"; // ------------------------ Retrieve People while ($row = mysqli_fetch_array($get, MYSQLI_ASSOC)) if ($color==1) {echo ' <tr bgcolor= #47EA7D> <td>' . $row['id'] . '</td><td>' . $row['fn'] . '</td><td>' . $row['ln'] . '</td><td>' . $row['position'] . '</td> </tr>'; $color = '2';} else {echo ' <tr bgcolor= #A4C8B0> <td>' . $row['id'] . '</td><td>' . $row['fn'] . '</td><td>' . $row['ln'] . '</td><td>' . $row['position'] . '</td> </tr> '; $color= '1';} // ----------------------- Close table echo "</div> </table>"; mysqli_free_result ($get); } // -------------------- IF ERROR WITH QUERY else {echo "Didn't connect";} // ---------------------- Spaces --> This is the code that doesn't appear to affect the space at the bottom of the table echo "<br /> <br /> <br />"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/250273-issues-with-table-built-with-php-and-mysql/ Share on other sites More sharing options...
xtopolis Posted November 2, 2011 Share Posted November 2, 2011 So, there were a few things missing with you code. Mainly, the while loop didn't have {} braces, and also it was freeing the mysqli result after the first iteration due to that. Here's your code cleanup and formatted in a way I prefer (I had trouble looking at your formatting, but that's just a personal thing). <br /> <a href='index.php'> Back Home </a> <br /> <br /> <?php // -------------------------- Connect to DB include ('connect_script.php'); // ------------------------------ Color variable for alternating table colors $color = 1; // ------------------------- Query Parameters $select = "SELECT (number) AS id, (first_name) AS fn, (last_name) AS ln, (position) AS position FROM table WHERE position = 'X' "; $get = @mysqli_query ($dbc, $select); // ------------------------- Run Query if($get) { // ------------------------Start table echo " <div style='border-style: solid;'> <table align='left' border='1'> <tr> <td>People</td> </tr> <tr> <td>ID</td><td>First Name</td><td>Last Name</td><td>Position</td> </tr>"; // ------------------------ Retrieve People while ($row = mysqli_fetch_array($get, MYSQLI_ASSOC)) { if($color==1) { echo ' <tr bgcolor= #47EA7D> <td>' . $row['id'] . '</td><td>' . $row['fn'] . '</td><td>' . $row['ln'] . '</td><td>' . $row['position'] . '</td> </tr>'; $color = '2'; }else{ echo ' <tr bgcolor= #A4C8B0> <td>' . $row['id'] . '</td><td>' . $row['fn'] . '</td><td>' . $row['ln'] . '</td><td>' . $row['position'] . '</td> </tr> '; $color= '1'; } }//end while loop // ----------------------- Close table echo "</div> </table>"; mysqli_free_result ($get); }//"get" (mysql query) if statement // -------------------- IF ERROR WITH QUERY else { echo "Didn't connect"; } // ---------------------- Spaces --> This is the code that doesn't appear to affect the space at the bottom of the table echo "<br /> <br /> <br />"; ?> ^^--- will this code work? I don't know. If an error exists after you add the while {} braces, then it's with the mysql connection/query. I made a sample array and it "worked" as far as I could tell what you were trying to do. I didn't see anything in here about a second table though. Here's the code I tested with. <?php error_reporting(E_ALL); echo "<br /> <a href='index.php'> Back Home </a> <br /> <br />"; // -------------------------- Connect to DB //include ('connect_script.php'); // ------------------------------ Color variable for alternating table colors $color = 1; // ------------------------- Query Parameters //$select = "SELECT (number) AS id, (first_name) AS fn, (last_name) AS ln, (position) AS position FROM table WHERE position = 'X' "; //$get = @mysqli_query ($dbc, $select); $row = array( "id" => range(0,25), "fn" => range("A","Z"), "ln" => range("z","a"), "position" => range(50,75) ); $get = true; // ------------------------- Run Query if ($get) { // ------------------------Start table echo " <div style='border-style: solid;'> <table align='left' border='1'> <tr> <td>People</td> </tr> <tr> <td>ID</td><td>First Name</td><td>Last Name</td><td>Position</td> </tr>"; // ------------------------ Retrieve People $counter = 0; while ($counter < 26) {//$row = mysqli_fetch_array($get, MYSQLI_ASSOC)) if ($color==1) {echo ' <tr bgcolor= #47EA7D> <td>' . $row['id'][$counter] . '</td><td>' . $row['fn'][$counter] . '</td><td>' . $row['ln'][$counter] . '</td><td>' . $row['position'][$counter] . '</td> </tr>'; $color = '2';} else {echo ' <tr bgcolor= #A4C8B0> <td>' . $row['id'][$counter] . '</td><td>' . $row['fn'][$counter] . '</td><td>' . $row['ln'][$counter] . '</td><td>' . $row['position'][$counter] . '</td> </tr> '; $color= '1';} // ----------------------- Close table $counter++; } echo "</div> </table>"; //mysqli_free_result ($get); } // -------------------- IF ERROR WITH QUERY else {echo "Didn't connect";} // ---------------------- Spaces --> This is the code that doesn't appear to affect the space at the bottom of the table echo "<br /> <br /> <br />"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/250273-issues-with-table-built-with-php-and-mysql/#findComment-1284167 Share on other sites More sharing options...
jefepwnzer Posted November 2, 2011 Author Share Posted November 2, 2011 Your help is much appreciated, Xtop, however, I copy and pasted your code and am getting the same problem that I experienced. The div does not wrap around the table itself. This problem occurs in IE, Firefox and Chrome. Quote Link to comment https://forums.phpfreaks.com/topic/250273-issues-with-table-built-with-php-and-mysql/#findComment-1284378 Share on other sites More sharing options...
PaulRyan Posted November 2, 2011 Share Posted November 2, 2011 echo "</div> </table>"; Should be... echo "</table> </div>"; You need to first close the table before you close the div. Regards, PaulRyan. Quote Link to comment https://forums.phpfreaks.com/topic/250273-issues-with-table-built-with-php-and-mysql/#findComment-1284381 Share on other sites More sharing options...
jefepwnzer Posted November 2, 2011 Author Share Posted November 2, 2011 Thanks for the help PaulRyan, however that didn't correct the issue. The div box remains at the top, and not wrapped around the table. Quote Link to comment https://forums.phpfreaks.com/topic/250273-issues-with-table-built-with-php-and-mysql/#findComment-1284397 Share on other sites More sharing options...
Drummin Posted November 2, 2011 Share Posted November 2, 2011 You might need to add some floats. <div style='border-style: solid; float:left'> <table align='left' border='1' style='float:left'> Quote Link to comment https://forums.phpfreaks.com/topic/250273-issues-with-table-built-with-php-and-mysql/#findComment-1284406 Share on other sites More sharing options...
PaulRyan Posted November 2, 2011 Share Posted November 2, 2011 My fault for not looking properly, change the following: echo "</div> </table>"; To... echo "</table><div style='clear:both;'></div></div>"; Try that? Tell me if it works, I got it working for me and it still aligns left. Regards, PaulRyan, Quote Link to comment https://forums.phpfreaks.com/topic/250273-issues-with-table-built-with-php-and-mysql/#findComment-1284414 Share on other sites More sharing options...
jefepwnzer Posted November 2, 2011 Author Share Posted November 2, 2011 Drummin: That worked, the div now wraps around the table and I can position it as needed, thank you very much! PaulRyan: I added in your code, it didn't seem to have any affect on the table after I added in Drummin's code, not sure if any affect should be seen. I still have one problem remaining: I can't get anything to align below the table. "This text should be at the bottom" has been placed at the end of the code and yet it still appears at the top (see image below). Is this proper and I'm just not understanding something or should it be appearing at the bottom? Right now the code is echo "<br /> This text should be at the bottom"; but I've also tried simply putting it after the PHP code, in HTML, but I get the same result. Like I said before, I'm trying to add some space in between the end of the table and the page, and eventually there will be a footer that needs to sit below the table. <html> <head> </head> <body> <?php // PHP code for the table ?> This text should be at the bottom </body> </html> Thanks again for everyone's help and quick replies, I really appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/250273-issues-with-table-built-with-php-and-mysql/#findComment-1284423 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.