suttercain Posted February 24, 2007 Share Posted February 24, 2007 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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 24, 2007 Share Posted February 24, 2007 Do you want to change the actual database, or just when it prints out? Quote Link to comment Share on other sites More sharing options...
suttercain Posted February 24, 2007 Author Share Posted February 24, 2007 Just the print outs. The data that was provided is messy and I want to get educated on how I can have something like ACR be echoed as ACURA. This would be a time saver. Thanks Jesi. Quote Link to comment Share on other sites More sharing options...
TRI0N Posted February 24, 2007 Share Posted February 24, 2007 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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 24, 2007 Share Posted February 24, 2007 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. Quote Link to comment Share on other sites More sharing options...
suttercain Posted February 24, 2007 Author Share Posted February 24, 2007 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 Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 24, 2007 Share Posted February 24, 2007 <td width='110'><b>$makes[$current];</b></td> Quote Link to comment Share on other sites More sharing options...
suttercain Posted February 24, 2007 Author Share Posted February 24, 2007 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. Quote Link to comment Share on other sites More sharing options...
suttercain Posted February 24, 2007 Author Share Posted February 24, 2007 This a no go? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 24, 2007 Share Posted February 24, 2007 And $current is supposed to be 'ACR'? Quote Link to comment Share on other sites More sharing options...
suttercain Posted February 24, 2007 Author Share Posted February 24, 2007 $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: 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..... Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 24, 2007 Share Posted February 24, 2007 and $makes[$current] doesn't show acura? Quote Link to comment Share on other sites More sharing options...
suttercain Posted February 24, 2007 Author Share Posted February 24, 2007 That was weird. I ran the code earlier and it just made the cell empty. I tried it again before replying and it worked. I must have done something wrong. It seems to be working now! Once again Jesi, Thanks. You are a huge help. Shannon Quote Link to comment Share on other sites More sharing options...
suttercain Posted February 24, 2007 Author Share Posted February 24, 2007 How do I mark this topic as solved since I can't "Modify" the topic title? Quote Link to comment Share on other sites More sharing options...
magic2goodil Posted February 24, 2007 Share Posted February 24, 2007 Should be at bottom left.. Quote Link to comment 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.