popcop Posted July 30, 2011 Share Posted July 30, 2011 Hi guys im new to PHP and im trying to pick up as much as i can as quickly as possible ive used a form to submit data to the database and now i want to pull that data and display it on a page. Ive attached an image of what the final page will look like. i just need to know how i will go about getttin that information out of the DB and onto the page in the way that i want it i also want to display it in rows which have alternate colours, and also be able to click the buttons on left hand side to split the results into the appropriate months hope you can help Quote Link to comment https://forums.phpfreaks.com/topic/243315-pull-data-from-the-database-and-displaying-in-table-rows/ Share on other sites More sharing options...
AyKay47 Posted July 30, 2011 Share Posted July 30, 2011 can you post the code that you have please Quote Link to comment https://forums.phpfreaks.com/topic/243315-pull-data-from-the-database-and-displaying-in-table-rows/#findComment-1249548 Share on other sites More sharing options...
popcop Posted July 30, 2011 Author Share Posted July 30, 2011 the html of the screen shot i posted? Quote Link to comment https://forums.phpfreaks.com/topic/243315-pull-data-from-the-database-and-displaying-in-table-rows/#findComment-1249567 Share on other sites More sharing options...
popcop Posted July 31, 2011 Author Share Posted July 31, 2011 anyone able to help, id really appreciate it Quote Link to comment https://forums.phpfreaks.com/topic/243315-pull-data-from-the-database-and-displaying-in-table-rows/#findComment-1249767 Share on other sites More sharing options...
trq Posted July 31, 2011 Share Posted July 31, 2011 There has got to be thousands of tutorials around for querying a database with php. Have you read any of those? Quote Link to comment https://forums.phpfreaks.com/topic/243315-pull-data-from-the-database-and-displaying-in-table-rows/#findComment-1249768 Share on other sites More sharing options...
popcop Posted July 31, 2011 Author Share Posted July 31, 2011 im not sure on how to display the results in alternate coloured table rows would u be able to help? Quote Link to comment https://forums.phpfreaks.com/topic/243315-pull-data-from-the-database-and-displaying-in-table-rows/#findComment-1249805 Share on other sites More sharing options...
popcop Posted July 31, 2011 Author Share Posted July 31, 2011 hi guys sorry if im being annoying but i am new and im trying to learn php really quickly to get page done but im definatley gettin confused now im trying to display the information the way it is on the jpeg in the post above with alternate coloured cells i was lookin online for a tutorial but im not gettin anywhere fast.. i did find one and did it but i doesnt quite fit with what i want to do, heres the code <?php $con = mysql_connect("localhost", "gezzamon_voting", "mrtickle"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("gezzamon_competition", $con); $result = mysql_query("SELECT * FROM allymccoist"); echo "<table border='1'> <tr> <th>Firstname</th> <th>Lastname</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['firstname'] . "</td>"; echo "<td>" . $row['lastname'] . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($con); ?> fingers crossed one of you guys can help Quote Link to comment https://forums.phpfreaks.com/topic/243315-pull-data-from-the-database-and-displaying-in-table-rows/#findComment-1249847 Share on other sites More sharing options...
popcop Posted July 31, 2011 Author Share Posted July 31, 2011 anyone? Quote Link to comment https://forums.phpfreaks.com/topic/243315-pull-data-from-the-database-and-displaying-in-table-rows/#findComment-1249891 Share on other sites More sharing options...
wildteen88 Posted July 31, 2011 Share Posted July 31, 2011 This is how you alternate colors $i = 0; // initiate counter variable // set the colors $color1 = '#e1e1e1'; // first color $color2 = '#cccccc'; // secound color while($row = mysql_fetch_array($result)) { // alternate row colors $bgcolor = ($i % 2 == 0) ? $color1 : $color2; echo "<tr style=\"background-color: $bgcolor\">"; // apply the color to the row echo "<td>" . $row['firstname'] . "</td>"; echo "<td>" . $row['lastname'] . "</td>"; echo "</tr>"; $i++; // increment counter } Quote Link to comment https://forums.phpfreaks.com/topic/243315-pull-data-from-the-database-and-displaying-in-table-rows/#findComment-1249893 Share on other sites More sharing options...
popcop Posted July 31, 2011 Author Share Posted July 31, 2011 ahhhh but what if ive made 2 classes in the css to style the cells? as they should have a border bottom and also padding? also, how would i display the full line of text including varibles in the one cell? in the code i posted it only is 2 varibles posted into 2 cells. I need the full sentence like the images i attached thanks for ur help Quote Link to comment https://forums.phpfreaks.com/topic/243315-pull-data-from-the-database-and-displaying-in-table-rows/#findComment-1249897 Share on other sites More sharing options...
wildteen88 Posted July 31, 2011 Share Posted July 31, 2011 ahhhh but what if ive made 2 classes in the css to style the cells? as they should have a border bottom and also padding? Then change echo "<tr style=\"background-color: $bgcolor\">"; // apply the color to the row to echo "<tr class=\"$bgcolor\">"; // apply the class to the row Now set the variables $color1 and $color2 to your two classes. also, how would i display the full line of text including varibles in the one cell? in the code i posted it only is 2 varibles posted into 2 cells. I need the full sentence like the images i attached thanks for ur help Just place the text after your variables. Quote Link to comment https://forums.phpfreaks.com/topic/243315-pull-data-from-the-database-and-displaying-in-table-rows/#findComment-1249905 Share on other sites More sharing options...
popcop Posted July 31, 2011 Author Share Posted July 31, 2011 sorry im gettin totally confused now... how do i assign the class instead of the $color1 & $color2 also i tried just adding thr text after the vaiables but it returned an error Quote Link to comment https://forums.phpfreaks.com/topic/243315-pull-data-from-the-database-and-displaying-in-table-rows/#findComment-1249917 Share on other sites More sharing options...
gizmola Posted July 31, 2011 Share Posted July 31, 2011 You know how manually to assign a class to a tr right? The trick that was shown to you is to use the modulus operator dividing by 2. Do you know what a modulus does in math? Based on that trick, as you loop through the output of rows, you need to set the class either to be the even color or the odd color. The modulus with the ternary operator that wildteen showed is how most people accomplish this. The ternary operator is equivalent to an if-then-else, so based on the result of the modulus the variable is either going to be set to the 1st value if true, or the 2nd value if false. All you need to do is adapt the small piece of code that does the modulus and uses that in the ternary operator ( (bool) ? value if true : value if false ) so that it sets the variable $bgcolor to be the name of the odd or even class. All I can say at this point, is that if you have a specific question, or something specifically that you don't understand, then spell that out, preferably with code snippet included. We understand that you are new, but we also expect people to try things out and read, and do their own explorations, otherwise we're just writing code for people, and we already do enough of that in our day jobs Quote Link to comment https://forums.phpfreaks.com/topic/243315-pull-data-from-the-database-and-displaying-in-table-rows/#findComment-1249930 Share on other sites More sharing options...
voip03 Posted July 31, 2011 Share Posted July 31, 2011 <style type="text/css"> <!-- .style1 { background:#CCCCCC;} .style2 { background:#cccccc;} --> </style> <? $con = mysql_connect("localhost", "gezzamon_voting", "mrtickle"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("gezzamon_competition", $con); $result = mysql_query("SELECT * FROM allymccoist"); // set the class $style1 = 'style1'; // first class 1 $style2 = 'style2'; // secound class 2 echo "<table border='1'><tr><th>Firstname</th><th>Lastname</th></tr>"; while($row = mysql_fetch_array($result)) { // alternate row style $bgstyle = ($i % 2 == 0) ? $style1 : $style2; echo "<tr class=\"$bgstyle\">"; // apply the class to the row echo "<td>" . $row['firstname'] . "</td>"; cho "<td>" . $row['lastname'] . "</td>"; echo "</tr>"; $ i++; } echo "</table>"; mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/243315-pull-data-from-the-database-and-displaying-in-table-rows/#findComment-1249966 Share on other sites More sharing options...
gizmola Posted August 1, 2011 Share Posted August 1, 2011 Looks good. You don't have to make string variables ($style1, $style2) just to use them in the ternary, you could just specify those same strings in the ternary. $bgstyle = ($i % 2 == 0) ? 'style1' : 'style2'; Quote Link to comment https://forums.phpfreaks.com/topic/243315-pull-data-from-the-database-and-displaying-in-table-rows/#findComment-1250050 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.