Jump to content

[SOLVED] Formatting the ECHO result


suttercain

Recommended Posts

Hi Everyone,

 

Right now I have a page that ECHO's Everything From a MySQL Database. The format is nice looking (see here: http://www.arb.ca.gov/msprog/ccvl/2007ccvl.htm ) and I want to use that same table format for another page. See Code 1.

 

Only this time the page that will be using that format is based on the results of a drop down menu. Instead of showing all the information it only shows one table.

 

My Problem, I can't get that same format to echo. I have tried and so far I get "bland" results. (see http://www.arb.ca.gov/msprog/ccvl/dropdown.php?year=2007 Run the drop down to see the results). I want the results to look exactly like the first link.

 

CODE 1 - The Nice Looking Tables

<?php
//Connect to the Database via the Include File!
require ('get_connected.php');

// Perform a statndard SQL query:
$res = mysql_query("SELECT UPPER(division) AS division, sub_model, disp, fuel, veh_class, arb_std, test_group, eo FROM 2007ccvl WHERE year = '2007' ORDER BY division, sub_model ASC") or die (mysql_error());

// Convert the Array DIVISION to Full Names
$makes = array('ACUR'=>'ACURA', 
		   'ALP'=>'ALPINA', 
		   'ASMA'=>'ASTON MARTIN LAGONDA',
		   'AUDI'=>'AUDI',
		   'BAF'=>'FORD BAF CNG',
		   'BAYT'=>'GENERAL MOTORS BAYTECH CNG',
		   'BENT'=>'BENTLY',
		   'BMW'=>'BMW',
		   'BUIC'=>'BUICK',
		   'CAD'=>'CADILLAC',
		   'CHEV'=>'CHEVROLET',
		   'CHRS'=>'CHRYSLER',
		   'CUMM'=>'CUMMINS',
		   'DODG'=>'DODGE',
		   'FERI'=>'FERARAI',
		   'FORD'=>'FORD',
		   'GMC'=>'GMC',
		   'HOND'=>'HONDA',
		   'HUMM'=>'HUMMER',
		   'HYND'=>'HYUNDAI',
		   'INFI'=>'INFINITI',
		   'ISUZ'=>'ISUZU',
		   'JAG'=>'JAGUAR',
		   'JEEP'=>'JEEP',
		   'KI'=>'KIA',
		   'LAMB'=>'LABORGHINI',
		   'LAND'=>'LAND ROVER',
		   'LEXS'=>'LEXUS',
		   'LINC'=>'LINCOLN',
		   'LOTU'=>'LOTUS',
		   'MASE'=>'MASERATI',
		   'MAZD'=>'MAZDA',
		   'MBZ'=>'MERCEDES BENZ',
		   'MINI'=>'MINI',
		   'MITS'=>'MITSUBISHI',
		   'MORG'=>'MORGAN',
		   'MRCY'=>'MERCURY',
		   'NISS'=>'NISSAN',
		   'PONT'=>'PONTIAC',
		   'PORS'=>'PORSCHE',
		   'RPP'=>'ROUSCH',
		   'RR'=>'ROLLS ROYCE',
		   'SAAB'=>'SAAB',
		   'SALE'=>'SALEEN',
		   'SATU'=>'SATURN',
		   'SHLB'=>'SHELBY',
		   'SUBA'=>'SUBARU',
		   'SUZU'=>'SUZUKI',
		   'TOTA'=>'TOYOTA',
		   'VOLK'=>'VOLKSWAGEN',
		   'VOLV'=>'VOLVO',
		   );
print $makes[$row['division']];

// Convert the Array FUEL to FUEL TYPE
$fuel_types = array('GN11'=>'GASOLINE', 
		   'NN01'=>'CNG', 
		   'DC03'=>'DIESEL');
print $fuel_types[$row['fuel']];

//CSS for the Alternating Color Rows
$class = 'even';

//Convert DIVISION Column to out put from the MySQL instead of HTML
$current = '';
while($row = mysql_fetch_array($res)) {
  if($current != $row['division']) {
    $current = $row['division'];
    echo "<table width='100%' border='0' bgcolor=#CCCCCC cellpadding='2' cellspacing='1'>
<tr>
        <td width='135'><font size=1.5><b>$makes[$current]</b></font></td> 
	<td width='55'><font size=1.5><b>DISP</b></font></td>
	<td width='60'><font size=1.5><b>FUEL</b></font></td>
	<td width='55'><font size=1.5><b>CLASS</b></font></td>
	<td width='110'><font size=1.5><b>EMISSION STD</b></font></td>
	<td width='90'><font size=1.5><b>TEST GROUP</b></font></td>
	<td width='90'><font size=1.5><b>EO NUMBER</b></font></td>
</tr>
<br>";
  }
  
  //Continuation of CSS for the Alternating Color Rows
  $class = $class == 'even' ? 'odd' : 'even';
  
  //Trying to make it Uppercase
  
  
  //Populate the Tables from the Database
  echo "<tr class=\"$class\">\n";
  echo strtoupper("<td width='135'><font size=1.5>$row[sub_model]</font></td>\n");
  echo "<td width='55'><font size=1.5>".sprintf( '%01.1f',$row[disp])." L</font></td>\n";
  echo "<td width='60'><font size=1.5>".$fuel_types[$row['fuel']]."</font></td>\n";
  echo "<td width='55'><font size=1.5>$row[veh_class]</font></td>\n";
  echo "<td width='110'><font size=1.5>$row[arb_std]</font></td>\n";
  echo "<td width='90'><font size=1.5>$row[test_group]</font></td>\n";
  echo "<td width='90'><font size=1.5>$row[eo]</font></td>\n";
  echo "</tr>\n";
}
?>

 

CODE 2 - Current Query Results

<?php
                
         $getit = $_POST['searchtype'];  //Determines which search button is pushed
         
         //Populate needed variables
         $year = $_REQUEST[year];
         $manuf = $_REQUEST[division];
	     $model = $_REQUEST[model];
	     $efn = $_REQUEST[efn];
	     
	     $year_arr = array("2007");
         $arr_size = count($year_arr);
               
              
        //DROP DOWN SEARCH
        if ($getit == "BROWSE")
           {
              //SQL Statement
              $sql="SELECT * FROM " .$year. "ccvl WHERE division = '$manuf'";
           
//Connect to the Database via the Include File!
require ('get_connected.php');
           
              //count the number of records
              $result = mysql_query("SELECT * FROM " .$year. "ccvl WHERE division = '$manuf'");
           
              $rows = mysql_num_rows($result); 
           
              //Check to see if there are any records matching search criteria
              if ($rows == 0)
                echo "Sorry, no records were found matching your search criteria.";
              else
              {   
              
              //Print Results for Drop-Down
              for ($res = 0; $res < $rows; $res++)
                {
                echo "<TD WIDTH='41%' BGCOLOR='white'><P ALIGN='LEFT'>";    
			$tempsub_model = mysql_result($sql_result,$res,'sub_model');
			$temptest_group = mysql_result($sql_result,$res,'test_group');
			$tempeo = mysql_result($sql_result,$res,'EO');
			$tempvdn = mysql_result($sql_result,$res,'VDN');
                $templink = mysql_result($sql_result,$res,'LINK');
                echo strtoupper("<b><a href='$templink'>$tempvdn</a></b>");
			echo "<BR>";
			echo "<b>Engine Family Number:</b> $tempsub_model";
			echo "<BR>";
			echo "<b>Engine Family Number:</b> $temptest_group";
                echo "<BR>";
			echo "<b>Executive Order Number:</b> " . strtoupper("$tempeo");
			echo "<BR>";
                }  			  
              }
             }
  
        //Close the database connection
         mysql_close();
?>

 

 

Any help would be great. Thank you in advance.

 

 

Shannon

Link to comment
https://forums.phpfreaks.com/topic/44526-solved-formatting-the-echo-result/
Share on other sites

the first example echo's into a table. you need to echo your results into a table as well. and then possibly style it with css.

echo "<table width='100%' border='0' bgcolor=#CCCCCC cellpadding='2' cellspacing='1'>
<tr>
        <td width='135'><font size=1.5><b>$makes[$current]</b></font></td> 
	<td width='55'><font size=1.5><b>DISP</b></font></td>
	<td width='60'><font size=1.5><b>FUEL</b></font></td>
	<td width='55'><font size=1.5><b>CLASS</b></font></td>
	<td width='110'><font size=1.5><b>EMISSION STD</b></font></td>
	<td width='90'><font size=1.5><b>TEST GROUP</b></font></td>
	<td width='90'><font size=1.5><b>EO NUMBER</b></font></td>
</tr>
<br>";

 

in the firs tone is what does this. you only have br's (line breaks) in yours.

I'd take this part out of the 'while' loop:

 

<?php
echo "<table width='100%' border='0' bgcolor=#CCCCCC cellpadding='2' cellspacing='1'>
<tr>
        <td width='135'><font size=1.5><b>$makes[$current]</b></font></td> 
	<td width='55'><font size=1.5><b>DISP</b></font></td>
	<td width='60'><font size=1.5><b>FUEL</b></font></td>
	<td width='55'><font size=1.5><b>CLASS</b></font></td>
	<td width='110'><font size=1.5><b>EMISSION STD</b></font></td>
	<td width='90'><font size=1.5><b>TEST GROUP</b></font></td>
	<td width='90'><font size=1.5><b>EO NUMBER</b></font></td>
</tr>
?>

 

Otherwise it will repeat in the while loop. I'm assuming you just want those to be the titles at the top of the fields/columns. Then this part:

 

<?
//Populate the Tables from the Database
  echo "<tr class=\"$class\">\n";
  echo strtoupper("<td width='135'><font size=1.5>$row['sub_model']</font></td>\n");
  echo "<td width='55'><font size=1.5>".sprintf( '%01.1f',$row['disp'])." L</font></td>\n";
  echo "<td width='60'><font size=1.5>".$fuel_types[$row['fuel']]."</font></td>\n";
  echo "<td width='55'><font size=1.5>$row['veh_class']</font></td>\n";
  echo "<td width='110'><font size=1.5>$row['arb_std']</font></td>\n";
  echo "<td width='90'><font size=1.5>$row['test_group']</font></td>\n";
  echo "<td width='90'><font size=1.5>$row['eo']</font></td>\n";
  echo "</tr>\n";
?>

 

would be in the while loop so it displays as many rows as needed for the results in your query.

 

Also, I didn't notice the closing </table> tag. You need to echo that after your closing } of the while loop.

 

echo "</table><br />\n";

 

 

Hey guys thanks for the help. I got a little further due to it.

 

Here is where I am at now. I got the table showing up, but instead of having the same format it appears as just a solid color table. Not like the first link in my first post. I thought this was due to the css file, so I included it and the alternating row code. Still no luck...

 

Here is the updated code for the query result page:

<?php include("style.inc"); ?>
<?php
                
         $getit = $_POST['searchtype'];  //Determines which search button is pushed
         
         //Populate needed variables
         $year = $_REQUEST[year];
         $manuf = $_REQUEST[division];
	     $model = $_REQUEST[model];
	     $efn = $_REQUEST[efn];
	     
	     $year_arr = array("2007");
         $arr_size = count($year_arr);
               
              
        //DROP DOWN SEARCH
        if ($getit == "BROWSE")
           {
              //SQL Statement
              $sql="SELECT * FROM " .$year. "ccvl WHERE division = '$manuf'";
           
//Connect to the Database via the Include File!
require ('get_connected.php');
           
              //count the number of records
              $result = mysql_query("SELECT * FROM " .$year. "ccvl WHERE division = '$manuf'");
           
              $rows = mysql_num_rows($result); 
		  
//CSS for the Alternating Color Rows
$class = 'even';      
	   
	   echo "<table width='100%' border='0' bgcolor=#CCCCCC cellpadding='2' cellspacing='1'>
<tr>
        <td width='135'><font size=1.5><b>MANU</b></font></td> 
	<td width='55'><font size=1.5><b>DISP</b></font></td>
	<td width='60'><font size=1.5><b>FUEL</b></font></td>
	<td width='55'><font size=1.5><b>CLASS</b></font></td>
	<td width='110'><font size=1.5><b>EMISSION STD</b></font></td>
	<td width='90'><font size=1.5><b>TEST GROUP</b></font></td>
	<td width='90'><font size=1.5><b>EO NUMBER</b></font></td>
</tr>
<br>";
	   
              //Check to see if there are any records matching search criteria
              if ($rows == 0)
                echo "Sorry, no records were found matching your search criteria.";
              else
              {   
  
    //Continuation of CSS for the Alternating Color Rows
  	$class = $class == 'even' ? 'odd' : 'even';        
    
              //Print Results for Drop-Down
              for ($res = 0; $res < $rows; $res++)
                {  
			$tempsub_model = mysql_result($sql_result,$res,'sub_model');
			$tempdisp = mysql_result($sql_result,$res,'disp');
			$tempfuel = mysql_result($sql_result,$res,'fuel');
			$tempclass = mysql_result($sql_result,$res,'veh_class');
                $tempstd = mysql_result($sql_result,$res,'standard');
			$temptg = mysql_result($sql_result,$res,'test_group');
			$tempeo = mysql_result($sql_result,$res,'eo');
                echo "<tr class=\"$class\">\n";
  				echo "<td width='135'><font size=1.5>$tempsub_model</font></td>\n";
			echo "<td width='55'><font size=1.5>$tempdisp</font></td>\n";
			echo "<td width='60'><font size=1.5>$tempfuel</font></td>\n";
			echo "<td width='55'><font size=1.5>$tempclass</font></td>\n";
			echo "<td width='110'><font size=1.5>$tempstd</font></td>\n";
			echo "<td width='110'><font size=1.5>$temptg</font></td>\n";
			echo "<td width='110'><font size=1.5>$tempeo</font></td>\n";
  				echo "</tr>\n";
                }  			  
              }
             }
  
        //Close the database connection
         mysql_close();
?>

 

Any suggestions? Thanks!

 

 

 

I Tried putting it in the loop and still got the same results.

 

for ($res = 0; $res < $rows; $res++)
                {  
			$class = $class == 'even' ? 'odd' : 'even'; 
			$tempsub_model = mysql_result($sql_result,$res,'sub_model');
			$tempdisp = mysql_result($sql_result,$res,'disp');
			$tempfuel = mysql_result($sql_result,$res,'fuel');
			$tempclass = mysql_result($sql_result,$res,'veh_class');
                $tempstd = mysql_result($sql_result,$res,'standard');
			$temptg = mysql_result($sql_result,$res,'test_group');
			$tempeo = mysql_result($sql_result,$res,'eo');
                echo "<tr class=\"$class\">\n";
  				echo "<td width='135'><font size=1.5>$tempsub_model</font></td>\n";
			echo "<td width='55'><font size=1.5>$tempdisp</font></td>\n";
			echo "<td width='60'><font size=1.5>$tempfuel</font></td>\n";
			echo "<td width='55'><font size=1.5>$tempclass</font></td>\n";
			echo "<td width='110'><font size=1.5>$tempstd</font></td>\n";
			echo "<td width='110'><font size=1.5>$temptg</font></td>\n";
			echo "<td width='110'><font size=1.5>$tempeo</font></td>\n";
  				echo "</tr>\n";
                }  	

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.