suttercain Posted February 23, 2007 Share Posted February 23, 2007 I first would like to thank obsidian for helping me to get the alternating row colors! What I am trying to do Right now my page looks like this: PICTURE 1 - The Page Now Where it says DIVISION I would like it to be populated from a a MySQL Column to be the name of the Vehicle" instead. It would look as such: PICTURE 2 - What I Want The Page To Be Further details. In MySQL table I have a column named div (Division) and I would like to fetch "Acura" and use it as a table title. But only once. Since there are numerous Acura I only want it listed the single time with all the Acuras listed below that title (see second picture). Once it is done with Acura it will move to the next vehicle and use that as a title (see second pic). Here is the code I am using to generate my page now (see picture one): <?php //Connect to the Database via the Include File! require ('get_connected.inc'); // Begin your table outside of the array echo "<table width='100%' border='0' cellpadding='1' cellspacing='0'> <tr> <td><b>DIVISION</b></td> <td><b>DISPLACEMENT</b></td> //<----THIS IS WHERE I WAS THE MySQL TITLE <td><b>FUEL</b></td> <td><b>CLASS</b></td> <td><b>EMISSION STD</b></td> <td><b>TEST GROUP</b></td> <td><b>EO NUMBER</b></td> </tr>"; // Perform an statndard SQL query: $res = mysql_query("SELECT sub_model, disp, fuel, veh_class, arb_std, test_group, eo FROM vlist_2007 ORDER BY division ASC") or die (mysql_error()); // Assuming $res holds the results from your query: $class = 'even'; while ($row = mysql_fetch_assoc($res)) { $class = $class == 'even' ? 'odd' : 'even'; echo "<tr class=\"$class\">\n"; echo "<td>$row[sub_model]</td>\n"; echo "<td>$row[disp]</td>\n"; echo "<td>$row[fuel]</td>\n"; echo "<td>$row[veh_class]</td>\n"; echo "<td>$row[arb_std]</td>\n"; echo "<td>$row[test_group]</td>\n"; echo "<td>$row[eo]</td>\n"; echo "</tr>\n"; } ?> Is there an easy way I can go about doing this? Link to comment https://forums.phpfreaks.com/topic/39810-using-a-mysql-field-as-a-table-title-cheesy-picture-included/ Share on other sites More sharing options...
TRI0N Posted February 23, 2007 Share Posted February 23, 2007 Well first thing your going to need to do is add a new row in your table that you will need to set each record to the according manufacture name. Then you can compose tables sorted with that as the key for each results. Link to comment https://forums.phpfreaks.com/topic/39810-using-a-mysql-field-as-a-table-title-cheesy-picture-included/#findComment-192268 Share on other sites More sharing options...
suttercain Posted February 23, 2007 Author Share Posted February 23, 2007 Hi TRI0N, Is there a bit of sample code you can show me? I am assuming you mean in this section: $class = 'even'; while ($row = mysql_fetch_assoc($res)) { $class = $class == 'even' ? 'odd' : 'even'; echo "<tr class=\"$class\">\n"; echo "<td>$row[sub_model]</td>\n"; echo "<td>$row[disp]</td>\n"; echo "<td>$row[fuel]</td>\n"; echo "<td>$row[veh_class]</td>\n"; echo "<td>$row[arb_std]</td>\n"; echo "<td>$row[test_group]</td>\n"; echo "<td>$row[eo]</td>\n"; echo "</tr>\n"; } ?? Thank you for your time. Link to comment https://forums.phpfreaks.com/topic/39810-using-a-mysql-field-as-a-table-title-cheesy-picture-included/#findComment-192269 Share on other sites More sharing options...
suttercain Posted February 23, 2007 Author Share Posted February 23, 2007 Okay I am getting closer but need some help... when I ran this code: <?php //Connect to the Database via the Include File! require ('get_connected.inc'); // Perform an statndard SQL query: $res = mysql_query("SELECT division, sub_model, disp, fuel, veh_class, arb_std, test_group, eo FROM vlist_2007 ORDER BY division ASC") or die (mysql_error()); // Assuming $res holds the results from your query: $class = 'even'; while ($row = mysql_fetch_assoc($res)) { echo "<table width='100%' border='0' cellpadding='1' cellspacing='0'> <tr> <td><b>$row[division]\n</b></td> <td><b>DISPLACEMENT</b></td> <td><b>FUEL</b></td> <td><b>CLASS</b></td> <td><b>EMISSION STD</b></td> <td><b>TEST GROUP</b></td> <td><b>EO NUMBER</b></td> </tr>"; while ($row = mysql_fetch_assoc($res)) { $class = $class == 'even' ? 'odd' : 'even'; echo "<tr class=\"$class\">\n"; echo "<td>$row[sub_model]</td>\n"; echo "<td>$row[disp]</td>\n"; echo "<td>$row[fuel]</td>\n"; echo "<td>$row[veh_class]</td>\n"; echo "<td>$row[arb_std]</td>\n"; echo "<td>$row[test_group]</td>\n"; echo "<td>$row[eo]</td>\n"; echo "</tr>\n"; } } ?> I got this: But it is still not separating the vehicles into new "sections" as I need it to do - SEE Picture 1. I nested the loop within a loop... am I on the right track? Link to comment https://forums.phpfreaks.com/topic/39810-using-a-mysql-field-as-a-table-title-cheesy-picture-included/#findComment-192297 Share on other sites More sharing options...
suttercain Posted February 23, 2007 Author Share Posted February 23, 2007 Should I "GROUP" the code? If so, how? Thanks. Link to comment https://forums.phpfreaks.com/topic/39810-using-a-mysql-field-as-a-table-title-cheesy-picture-included/#findComment-192329 Share on other sites More sharing options...
renj0806 Posted February 23, 2007 Share Posted February 23, 2007 <?php $query = 'SHOW COLUMNS FROM [table name]'; $rs = mysql_query($query); $field = array(); while ($row = mysql_fetch_assoc($rs)) { $field[] = $row['Field']; } ?> That should give you an array of fields from your database. Link to comment https://forums.phpfreaks.com/topic/39810-using-a-mysql-field-as-a-table-title-cheesy-picture-included/#findComment-192333 Share on other sites More sharing options...
suttercain Posted February 23, 2007 Author Share Posted February 23, 2007 Sorry I am way lost. Where in my existing code would I place this or replace other code? Thanks. Link to comment https://forums.phpfreaks.com/topic/39810-using-a-mysql-field-as-a-table-title-cheesy-picture-included/#findComment-192346 Share on other sites More sharing options...
renj0806 Posted February 23, 2007 Share Posted February 23, 2007 <?php echo "<table width='100%' border='0' cellpadding='1' cellspacing='0'> <tr> <td><b>$field[2]\n</b></td> <td><b>DISPLACEMENT</b></td> <td><b>FUEL</b></td> <td><b>CLASS</b></td> <td><b>EMISSION STD</b></td> <td><b>TEST GROUP</b></td> <td><b>EO NUMBER</b></td> </tr>"; ?> Change the Index of the $field[] where the DIVISION FieldName at your Database corresponds. Link to comment https://forums.phpfreaks.com/topic/39810-using-a-mysql-field-as-a-table-title-cheesy-picture-included/#findComment-192354 Share on other sites More sharing options...
renj0806 Posted February 23, 2007 Share Posted February 23, 2007 <?php //Connect to the Database via the Include File! require ('get_connected.inc'); $query = 'SHOW COLUMNS FROM [table name]'; $rs = mysql_query($query); $field = array(); while ($row = mysql_fetch_assoc($rs)) { $field[] = $row['Field']; } // Begin your table outside of the array echo "<table width='100%' border='0' cellpadding='1' cellspacing='0'> <tr> <td><b>DIVISION</b></td> <td><b>$field[1]</b></td> //<----The Index depends where your Displacement field is placed in your table. <td><b>FUEL</b></td> <td><b>CLASS</b></td> <td><b>EMISSION STD</b></td> <td><b>TEST GROUP</b></td> <td><b>EO NUMBER</b></td> </tr>"; // Perform an statndard SQL query: $res = mysql_query("SELECT sub_model, disp, fuel, veh_class, arb_std, test_group, eo FROM vlist_2007 ORDER BY division ASC") or die (mysql_error()); // Assuming $res holds the results from your query: $class = 'even'; while ($row = mysql_fetch_assoc($res)) { $class = $class == 'even' ? 'odd' : 'even'; echo "<tr class=\"$class\">\n"; echo "<td>$row[sub_model]</td>\n"; echo "<td>$row[disp]</td>\n"; echo "<td>$row[fuel]</td>\n"; echo "<td>$row[veh_class]</td>\n"; echo "<td>$row[arb_std]</td>\n"; echo "<td>$row[test_group]</td>\n"; echo "<td>$row[eo]</td>\n"; echo "</tr>\n"; } ?> Try this. This should work. Edit: Not Division, rather Displacement Field. Edit: Bah, Division or Displacement. Just work it out. Link to comment https://forums.phpfreaks.com/topic/39810-using-a-mysql-field-as-a-table-title-cheesy-picture-included/#findComment-192361 Share on other sites More sharing options...
suttercain Posted February 23, 2007 Author Share Posted February 23, 2007 Thank you for spending some time on this. I am pulling my hair out I ran this code: <?php //Connect to the Database via the Include File! require ('get_connected.inc'); $query = 'SHOW COLUMNS FROM [table name]'; $rs = mysql_query($query); $field = array(); while ($row = mysql_fetch_assoc($rs)) { $field[] = $row['Field']; } // Begin your table outside of the array echo "<table width='100%' border='0' cellpadding='1' cellspacing='0'> <tr> <td><b>DIVISION</b></td> <td><b>$field[0]</b></td> //<----The Index depends where your Displacement field is placed in your table. <td><b>FUEL</b></td> <td><b>CLASS</b></td> <td><b>EMISSION STD</b></td> <td><b>TEST GROUP</b></td> <td><b>EO NUMBER</b></td> </tr>"; // Perform an statndard SQL query: $res = mysql_query("SELECT sub_model, disp, fuel, veh_class, arb_std, test_group, eo FROM vlist_2007 ORDER BY division ASC") or die (mysql_error()); // Assuming $res holds the results from your query: $class = 'even'; while ($row = mysql_fetch_assoc($res)) { $class = $class == 'even' ? 'odd' : 'even'; echo "<tr class=\"$class\">\n"; echo "<td>$row[sub_model]</td>\n"; echo "<td>$row[disp]</td>\n"; echo "<td>$row[fuel]</td>\n"; echo "<td>$row[veh_class]</td>\n"; echo "<td>$row[arb_std]</td>\n"; echo "<td>$row[test_group]</td>\n"; echo "<td>$row[eo]</td>\n"; echo "</tr>\n"; } ?> and I got the following: The closet I have come is in the third image I posted. It populated the DIVISION Name with Acura but did not echo AUDI after the acuras and so forth... Link to comment https://forums.phpfreaks.com/topic/39810-using-a-mysql-field-as-a-table-title-cheesy-picture-included/#findComment-192370 Share on other sites More sharing options...
suttercain Posted February 23, 2007 Author Share Posted February 23, 2007 I don't want the table header to read "DIVISION" like in the first image. I want it to be replaced by the "DIVISION" field of MySQL like in the second and third image. I was sucsesfull in doing that using: <?php <td><b>$row[division]\n</b></td> <td><b>DISPLACEMENT</b></td> <td><b>FUEL</b></td> <td><b>CLASS</b></td> <td><b>EMISSION STD</b></td> <td><b>TEST GROUP</b></td> <td><b>EO NUMBER</b></td> ?> and also nesting another while statement: <?php// Assuming $res holds the results from your query: $class = 'even'; while ($row = mysql_fetch_assoc($res)) { echo "<table width='100%' border='0' cellpadding='1' cellspacing='0'> <tr> <td><b>$row[division]\n</b></td> <td><b>DISPLACEMENT</b></td> <td><b>FUEL</b></td> <td><b>CLASS</b></td> <td><b>EMISSION STD</b></td> <td><b>TEST GROUP</b></td> <td><b>EO NUMBER</b></td> </tr>"; while ($row = mysql_fetch_assoc($res)) { $class = $class == 'even' ? 'odd' : 'even'; //CSS Style Sheet Class for Alternating echo "<tr class=\"$class\">\n"; echo "<td>$row[sub_model]</td>\n"; echo "<td>$row[disp]</td>\n"; echo "<td>$row[fuel]</td>\n"; echo "<td>$row[veh_class]</td>\n"; echo "<td>$row[arb_std]</td>\n"; echo "<td>$row[test_group]</td>\n"; echo "<td>$row[eo]</td>\n"; echo "</tr>\n"; } }?> I just cannot get it to format or "group" like how I need it too, as displayed in the second image of the first post. :'( Link to comment https://forums.phpfreaks.com/topic/39810-using-a-mysql-field-as-a-table-title-cheesy-picture-included/#findComment-192376 Share on other sites More sharing options...
suttercain Posted February 23, 2007 Author Share Posted February 23, 2007 Is it something that will require me to re-write the code? Link to comment https://forums.phpfreaks.com/topic/39810-using-a-mysql-field-as-a-table-title-cheesy-picture-included/#findComment-192402 Share on other sites More sharing options...
suttercain Posted February 23, 2007 Author Share Posted February 23, 2007 Should I use: $query = "SELECT DISTINCT div FROM vlist"; ? Link to comment https://forums.phpfreaks.com/topic/39810-using-a-mysql-field-as-a-table-title-cheesy-picture-included/#findComment-192446 Share on other sites More sharing options...
suttercain Posted February 23, 2007 Author Share Posted February 23, 2007 Alright I give up. 5 hours is long enough. Link to comment https://forums.phpfreaks.com/topic/39810-using-a-mysql-field-as-a-table-title-cheesy-picture-included/#findComment-192484 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.