Jump to content

[SOLVED] Determining cc type based on first digit - code not working


jaxdevil

Recommended Posts

Here is the code I am trying, it is all working except displaying the correct cc type. Any idea?

 

<?php
$number = "$cc_num";
$last_four = substr($number, -4);
$first_one = substr($number, 1);
if($first_one=3){$cc_type = 'American Express';}
elseif($first_one==4){$cc_type = 'Visa';}
elseif($first_one==5){$cc_type = 'MasterCard';}
elseif($first_one==6){$cc_type = 'Discover';}
else {$cc_type = 'Unknown';}
?>
<center>
<table width="800" height="50" cellspacing="0" cellpadding="0" border="1" bordercolor="black">
<tr valign="top">
<td width="200" height="10" align="left" valign="top">
<font face="Tahoma" size="2">CARD TYPE</font>
</td>
<td width="200" height="10" align="left" valign="top">
<font face="Tahoma" size="2">LAST 4</font>
</td>
<td width="200" height="10" align="left" valign="top">
<font face="Tahoma" size="2">AUTH CODE</font>
</td>
<td width="200" height="10" align="left" valign="top">
<font face="Tahoma" size="2">AMOUNT</font>
</td>
</tr>
<tr valign="top">
<td width="200" height="40" align="left" valign="top">
<font face="Tahoma" size="2"><?=$cc_type?></font>
</td>
<td width="200" height="40" align="left" valign="top">
<font face="Tahoma" size="2"><?=$last_four?></font>
</td>
<td width="200" height="40" align="left" valign="top">
<font face="Tahoma" size="2"><?=$authxyz?></font>
</td>
<td width="200" height="40" align="left" valign="top">
<font face="Tahoma" size="2"><?=$amount?></font>
</td>
</tr>
</table>
</center>

try something like this...

 

 

<?php

 

//test number

$cc_num = "123312312";

 

$number = "$cc_num";

$last_four = substr($number, -4);

$first_one = substr($number, 1);

if($first_one=3){$cc_type = "American Express";}

elseif($first_one==4){$cc_type = "Visa";}

elseif($first_one==5){$cc_type = "MasterCard";}

elseif($first_one==6){$cc_type = "Discover";}

else {$cc_type = "Unknown";}

 

//bla data

$authxyz = "234";

$amount = "123.44";

 

Print "

<center>

<table width='800' height='50' cellspacing='0' cellpadding='0' border='1' bordercolor='black'>

<tr valign='top'>

<td width='200' height='10' align='left' valign='top'>

<font face='Tahoma' size='2'>CARD TYPE</font>

</td>

<td width='200' height='10' align='left' valign='top'>

<font face='Tahoma' size='2'>LAST 4</font>

</td>

<td width='200' height='10' align='left' valign='top'>

<font face='Tahoma' size='2'>AUTH CODE</font>

</td>

<td width='200' height='10' align='left' valign='top'>

<font face='Tahoma' size='2'>AMOUNT</font>

</td>

</tr>

<tr valign='top'>

<td width='200' height='40' align='left' valign='top'>

<font face='Tahoma' size='2'>$cc_type</font>

</td>

<td width='200' height='40' align='left' valign='top'>

<font face='Tahoma' size='2'>$last_four</font>

</td>

<td width='200' height='40' align='left' valign='top'>

<font face='Tahoma' size='2'>$authxyz</font>

</td>

<td width='200' height='40' align='left' valign='top'>

<font face='Tahoma' size='2'>$amount</font>

</td>

</tr>

</table>

</center>

 

";

 

?>

<?php

  $number = "$cc_num";
  $last_four = substr($number, -4);
  switch($number{1}) {
    case '3':
      $cc_type = 'American Express';
      break;
    case '4':
      $cc_type = 'Visa';
      break;
    case '5':
      $cc_type = 'MasterCard';
      break;
    case '6':
      $cc_type = 'Discover';
      break;
    default:
      $cc_type = 'Unknown';
  }

?>

Thanks for the help guys...it looks like I wasn't properly limiting the results. I was using substr($number, 1) when I should have been using substr($number,0,1) It works now. Here is the working code..

 

<?php
$number = "$cc_num";
$last_four = substr($number, -4);
$first_one = substr($number,0,1);
echo $first_one;
if($first_one=='3'){$cc_type = 'American Express';}
elseif($first_one=='4'){$cc_type = 'Visa';}
elseif($first_one=='5'){$cc_type = 'MasterCard';}
elseif($first_one=='6'){$cc_type = 'Discover';}
else {$cc_type = 'Unknown';}
?>

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.