tags:

views:

169

answers:

5

Hello,

I have the following code:

<?php

if (isset($_GET["cmd"]))

  $cmd = $_GET["cmd"];

else

  die("You should have a 'cmd' parameter in your URL");

 $pk = $_GET["pk"];

$con = mysql_connect("localhost","x","geheim");

if(!$con)

{

die('Connection failed because of' .mysql_error());

}

mysql_select_db("ebay",$con);

if($cmd=="GetAuctionData")

{

$sql="SELECT * FROM Auctions WHERE ARTICLE_NO ='$pk'";

$sql2="SELECT ARTICLE_DESC FROM Auctions WHERE ARTICLE_NO ='$pk'";

$htmlset = mysql_query($sql2);

$row2 = mysql_fetch_array($htmlset);



$result = mysql_query($sql);

while ($row = mysql_fetch_array($result))

{

if ($row['COUNTRYCODE'] == "77"){

$country = "Germany";

else if ($row['LOCATION'] == "1")

$country = "USA";

else if ($row['LOCATION'] == "16")

$country = "Austria";

else if ($row['LOCATION'] == "122")

$country = "Switzerland";

else if ($row['LOCATION'] == "123")

$country = "Holland";

else

$country = "Not Apliccable";

}

if ($row['PAYPAL_ACCEPT'] == "1"){

$paypal = "Yes";

else

$paypal = "No";

}

echo "<div id='leftlayer'>

  <strong>Article Number</strong> ".$row['ARTICLE_NO']."

    <p><strong>Article Name</strong></p> ".$row['ARTICLE_NAME']."

    <p><strong>Subtitle</strong></p> ".$row['SUBTITLE']."

    <p><strong>Username</strong></p> ".$row['USERNAME']."

    <p><strong>Total Selling</strong></p> ".$row['QUANT_TOTAL']."

    <p><strong>Total Sold</strong></p> ".$row['QUANT_SOLD']."



    <p><strong>Highest Bidder</strong></p> ".$row['BEST_BIDDER_ID']."

  </div>

<div class='leftlayer2'>

  <strong>Current Bid</strong> ".$row['CURRENT_BID']."

  <p><strong>Start Price</strong></p> ".$row['START_PRICE']."

  <p><strong>Buyitnow Price</strong></p> ".$row['BUYITNOW_PRICE']."

  <p><strong>Bid Count</strong></p> ".$row['BID_COUNT']."

  <p><strong>Start Date</strong></p> ".$row['ACCESSSTARTS']."

  <p><strong>End Date</strong></p> ".$row['ACCESSENDS']."

  <p><strong>Original End</strong></p> ".$row['ACCESSORIGIN_END']."

  <p><strong>Auction Type</strong></p> ".$row['AUCTION_TYPE']."

</div>

<div class='leftlayer2'>

  <strong>Private Auction</strong></p> ".$row['PRIVATE_AUCTION']."

  <p><strong>Paypal Accepted</strong></p> ".$paypal."

  <p><strong>Auction Watched</strong></p> ".$row['WATCH']."

  <p><strong>Finished</strong></p> ".$row['FINISHED']."

  <p><strong>Category</strong></p> ".$row['CAT_DESC']."

  <p><strong>Category</strong></p> ".$row['CAT_PATH']."  

  <p><strong>Location</strong></p> ".$country."

  <p><strong>Conditions</strong></p> ".$row['LOCATION']."

  <p><strong>Conditions</strong></p> ".$row['CONDITIONS']."

</div>

<div class='leftlayer2'>

  <strong>Auction Revised</strong></p> ".$row['REVISED']."

  <p><strong>Cancelled</strong></p> ".$row['PRE_TERMINATED']."

  <p><strong>Shipping to</strong></p> ".$row['SHIPPING_TO']."

  <p><strong>Fee Insertion</strong></p> ".$row['FEE_INSERTION']."

  <p><strong>Fee Final</strong></p> ".$row['FEE_FINAL']."

  <p><strong>Fee Listing</strong></p> ".$row['FEE_LISTING']."

  <p><a href='#' onclick='makewindows(); return false;'>Click for full description </a></p>

</div>";



$lastImg = $row['PIC_URL'];

echo "<div id='rightlayer'>Picture Picture

<img src='".$lastImg."'>

</div>";



}



}

mysql_free_result($result);

I am wondering if there is a better way to do the conditional logic for country code, and to avoid having an if else statement for every boolean field in the database. Is this the accepted way to do things, or is there a better way.

edit: the problem is I do not have control of the database, and the country names are not included in the databse..

+4  A: 

Use an associative array to map the country codes into the names, and then look up the values in that:

$codes = array(77 => 'Germany', 1 => 'USA', etc... );
Alnitak
+6  A: 

I'd put the countries into another table in the database and then select the country name that way using a join. (table with id, country_name as the columns)

$sql="SELECT * FROM Auctions a INNER JOIN countries c ON a.LOCATION=c.ID WHERE ARTICLE_NO ='$pk'";

Your result row will then have a key called country_name: $row['country_name']

Or if not, then make an array like $countries = array(1=>'UK', 2=>'USA', 3=>'France') etc which you can then reference as $country = $countries[$row['LOCATION']]

benlumley
+1  A: 

You can use a secondary table (not worth it if you handle only this few countries).
You can use a switch (much better than successive ifs: faster, more readable/maintainable).
I would just make a small associative array and pull the country name from the code key...

PhiLho
+3  A: 

Following Alnitak:

$ccodes = array(
     "1" => "USA",
    "77" => "Germany",
    "16" => "Austria",
    // ... etc
);

$country = @$ccodes[$row['COUNTRYCODE']];

if ( $country == "" ) $country = "Not applicable";

Just note that you used both COUNTRYCODE and LOCATION above, you have to discern between them (i.e., have two associative arrays).

The @ symbol is used to suppress warning messages for undefined indices. You could use the function array_key_exists() instead, for testing if the given key exists.

For things like the paypal-variables, I'd simply do something like this:

$paypal = ( $row['PAYPAL_ACCEPT'] == "1" ) ? "Yes" : "No";
csl
Perfect answer := Location is a text field so its fine. What would you say for the things like paypal_accept etc, a different conditional for each one?
Joshxtothe4
Added note about paypal_accept.
csl
+2  A: 
$map = array(  
     array( 'COUNTRYCODE' , 
              array( 77 => "Germany" )
     ),
     array( 'LOCATION' ,
              array( 1 => 'USA', 
                     16 => 'Austria', 
                     122 => 'Switzerland'.
                     123 => 'Holland' 
              )
     )
); 
function resolveRowLocation( $row )
{
   foreach( $map as $maprec )
   {
    foreach( $maprec[1] as $code => $result ) 
    {
      if( $row[ $maprec[0] ] == $code )
      {
         return $result;  
      }
    }
   }
   return 'Not Applicable'; 
}



$country = resolveRowLocation( $row );

More functions tends towards better maintainability, less redundant code, and more self-documenting code :)

Kent Fredric