Jump to content

[SOLVED] MySQL Out Put Data Name Change Using PHP


suttercain

Recommended Posts

Good Saturday Morning to you all,

 

I am gathering data using the mysql_query and echoing it to the browser. The thing is in one of my MySQL columns the data entered is "ACR" or "TOY or "AUD". These should read "Acura," "Toyota" and "Audi".

 

How would I code it so ACR = Acura, TOY = Toyota and AUD = Audi. This would be echoed to the browser so the end user does not get the abbreviated version of the data?

 

This is the code I am working with now which out puts my data. The 'vehicles' is the column I need the name changes to be performed:

 

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



// Perform an statndard SQL query:
$res = mysql_query("SELECT UPPER(vehicle) AS vehicle, sub_class, disp, fuel FROM cars ORDER BY vehicle ASC") or die (mysql_error());

// Assuming $res holds the results from your query:
$class = 'even';
$current = '';
while($row = mysql_fetch_array($res)) {
  if($current != $row['vehicle']) {
    $current = $row['vehicle'];
    echo "<table width='100%' border='0' bgcolor=#CCCCCC cellpadding='1' cellspacing='1'>
<tr>
        <td width='110'><b>$current</b></td>
	<td width='110'><b>FUEL</b></td>
	<td width='110'><b>DISPLACEMENT</b></td>
</tr>";
  }
  $class = $class == 'even' ? 'odd' : 'even';
  echo "<tr class=\"$class\">\n";
  echo "<td>$row[sub_class]</td>\n";
  echo "<td>$row[disp]</td>\n";
  echo "<td>$row[fuel]</td>\n";
  echo "</tr>\n";
}
?>

 

Thank you in advance.

 

Link to comment
Share on other sites

I'm assuming $row['vehicle'] is where the ARC TOY etc is...

 

if($row['vehicle'] == "ACR") {
$make = "Accura" ;
} else if {
$row['vehicle'] == "TOY") {
$make = "Toyota" ;
} else {
$make = "Audi" ;
}
}

 

Place that right before your $class then echo $make where you want it to show the name.

 

Link to comment
Share on other sites

In addition, you could also use a switch or an array

//Switch
switch($row['vehicle']){
  case 'ACR':
     print 'Acura';
     break;
  default:
     break;
}
//Array
$makes = array('ACR'=>'Acura', 'TOY'=>'Toyota');
print $makes[$row['vehicle']];

 

I prefer the array out of the three.

Link to comment
Share on other sites

Thank to both of you. I too think the array is the way to go. I gave it a shot and it stays the same.

 

I think the porblem may be because I am assigning the 'vehicle' to the $current variable and the echoing the $current variable in the table headers:

 

$class = 'even';
$current = '';
while($row = mysql_fetch_array($res)) {
  if($current != $row['vehicle']) {
    $current = $row['vehicle'];
    echo "<table width='100%' border='0' bgcolor=#CCCCCC cellpadding='1' cellspacing='1'>
<tr>
        <td width='110'><b>$current</b></td>
	<td width='110'><b>FUEL</b></td>
	<td width='110'><b>DISPLACEMENT</b></td>
</tr>";
  }
  $class = $class == 'even' ? 'odd' : 'even';
  echo "<tr class=\"$class\">\n";
  echo "<td>$row[sub_class]</td>\n";
  echo "<td>$row[disp]</td>\n";
  echo "<td>$row[fuel]</td>\n";
  echo "</tr>\n";
}

 

Should I still use the $makes variable in the :

$makes = array('ACR'=>'Acura', 'TOY'=>'Toyota');
print $makes[$row['vehicle']];

or should I change that to $current?

 

I see what the code is trying to do I am just unsure of how to lay it out and where to place it. I tried before the first $class and the output was the same.

 

Shannon

Link to comment
Share on other sites

I gave that a shot. I know I am doing something wrong, just not sure of what.

 

I changed the previous code (see above) to use the array and echo it out:

 

// Assuming $res holds the results from your query:
$makes = array('ACR'=>'Acura', 'TOY'=>'Toyota');
print $makes[$row['vehicle']];
$class = 'even';
$current = '';
while($row = mysql_fetch_array($res)) {
  if($current != $row['vehicle']) {
    $current = $row['vehicle'];
    echo "<table width='100%' border='0' bgcolor=#CCCCCC cellpadding='1' cellspacing='1'>
<tr>
        <td width='110'><b>$makes[$current]</b></td>
	<td width='110'><b>FUEL</b></td>
	<td width='110'><b>DISPLACEMENT</b></td>
</tr>";
  }
  $class = $class == 'even' ? 'odd' : 'even';
  echo "<tr class=\"$class\">\n";
  echo "<td>$row[sub_class]</td>\n";
  echo "<td>$row[disp]</td>\n";
  echo "<td>$row[fuel]</td>\n";
  echo "</tr>\n";
}
?>

 

Instead of changing the ACR to ACURA, etc. The ACR dissapeared and left en empty cell where it was.

 

I also tried placing the

$makes = array('ACR'=>'Acura', 'TOY'=>'Toyota');
print $makes[$row['vehicle']];

Within the while loop and got the same results.

 

I am sure it's right there in front of me, I just can't see it.

 

 

Link to comment
Share on other sites

$current takes the 'vehcile' section of the MySQL column and echos it only once even if there are 10 accuras, which is the first column in the header (dark grey) that is echoed anytime a new instance of the vehicle appears.

 

Example:

row.jpg

 

The code listed in Post #1 of this topic echos the above table. The 'vehicle' is placed in the $current. But istead of the $current echoing only "ACR" I need it to echo "ACURA", same for the other vehicles.

 

Hope this explains it.....

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.