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>

Link to comment
Share on other sites

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>

 

";

 

?>

Link to comment
Share on other sites

<?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';
  }

?>

Link to comment
Share on other sites

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';}
?>

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.