Jump to content

Using a MySQL Field as a Table Title (Cheesy Picture Included)


suttercain

Recommended Posts

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:

row1.gif

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:

row2.gif

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
Share on other sites

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
Share on other sites

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:

row3.gif

 

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
Share on other sites

<?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
Share on other sites

<?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.  :D

Edit: Bah, Division or Displacement. Just work it out.

Link to comment
Share on other sites

Thank you for spending some time on this. I am pulling my hair out  ;D

 

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:

row5.gif

 

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.