This is the project I need to finish. I am fairly new to php and this project has got me confused. Any help would be greatly appreciated
The project ask for to validate ISBN 10 Number. There would be 10 digits with 1-9 being Numbers, but Number 10 is an x.
This is What I have so far:
This is the input page or html page:

<form method="POST" action="Process10ISBN.php">

<p>Enter ISBN 10: <input type="text" name="isbn10"
/></p>

<input type="submit" name="Submit" value="Submit" />

</form>

This is what I have so far for the Procssing page to validate ISBN 10:

if (empty($_POST['isbn10']))

    echo "<p>Please Fill in the input fields!</p>\n";      
else {
    $ISBN10 = addslashes($_POST['isbn10']);

        echo "<p>Thank you for your input!</p>\n";    
    } 

$chk_sum_num=10;

for($i=0;$i<=9;$i++
    {
    $chk_sum+=$ISBN_Arr[$i] * $chk_sum_num;
    print"$ISBN_Arr[$i] | $chk_sum_num<br/>";

    $chk_sum_num--;
    }//end for loop

//this goes at top, then the loop goes inside it

If $ISBNARR[9]!=is numeric{{!= 'X'!!='x'
    print"Not ISBN 10 Number<br/>";

    Else If $ISBNARR[9]=='x' or "X" $ISBNARR[9] = 10

?><!--End PHP Script-->

I don't know how to organize my processing page. All I know is that this code has to be included in the processing page: I know my processing code is set up wrong. I couldn't figure out how I would put the codes in and what else I needed to add! Can someone take few minutes and tell me how do I set up my processing page.

Member Avatar for iamthwee

...make sure the first nine characters are numbers 0-9

something like

$str1 = "0123456789";

$str2 = "383422343X";

if strlen($str2) == 10
 then
  for $i = 0; $i <9; $i++
  {
     for($j = 0; $j <9; $j++)
     {

      //process
     }

  }

then make sure the last character is an x.

Member Avatar for diafol

To check an ISBN10, from what I can see, 10 digits or 9 digits with an X at the end.

Wikipedia shows the usage of dashes to group-publisher-title-checkDigit

You're not interested in the dashes I take it?

So, validate with preg...

$pattern = '/(\d{10})|(\d{9}X)/';

$isbn = '748495606X';

preg_match($pattern, $isbn, $matches);

print_r($matches);

If isset $matches[1] - good!

That's not quite all that's needed. I read the same Wikipedia article, and the tenth digit is actually a %11 checksum, which is why the OP had those loops. But the preg_match() you posted is certainly a good first step.

Member Avatar for diafol

That's not quite all that's needed. I read the same Wikipedia article, and the tenth digit is actually a %11 checksum, which is why the OP had those loops

OK, but...

the International ISBN Agency says that the ISBN-10 check digit[24] – which is the last digit of the ten-digit ISBN – must range from 0 to 10 (the symbol X is used for 10)

Am I missing something?

//edit OK, I get it - you need to calculate the check - not just the pattern. Apologies.

$isbn = 'ISBN0-306-40615-2';

function checkISBN($isbn)
{
    $isbn = preg_replace('/[ISBN-]/i', '', $isbn);

    $len = strlen ((string) $isbn); 
    $pattern = '/(\d{'.$len.'})|(\d{'.($len-1).'}X)/i';
    $isbnConv = str_split($isbn);
    if(strtoupper($isbnConv[$len-1]) == 'X') $isbnConv[$len-1] = 10;

    if($len === 10)
    {
        $total = array_map(function($x, $y) { return $x * $y; },
                   $isbnConv, range(1,10));
        return (array_sum($total)%11 === 0) ? true : false;
    }elseif($len === 13){
        $total = array_map(function($x, $y) { return $x * $y; },
                   $isbnConv, array(1,3,1,3,1,3,1,3,1,3,1,3,1));
        return (array_sum($total)%10 === 0) ? true : false;
    }else{
        return false;
    }
}


echo (checkISBN($isbn)) ? "ISBN $isbn is TRUE" : "ISBN $isbn is FALSE";

Not tested thoroughly. Just a few ideas.

//edit again - forgot to test the pattern -oh well.

Member Avatar for diafol

I posted a demo here...

http://demos.diafol.org/isbn-checker.php

I think the main difference between my solution and the ones in the SO link are that I used array functions and they used loops. I'll be posting a jquery validation today or tomorrow too.

//Edit - jQuery posted too.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.