Hi everyone. I am having a problem with array_search(). My php script
gets a list of books from the database, and then compares them with a
list of books which have been obtained from a textarea in a form, and
formatted into single books, each one stored in a non-associative
array.
I have a function that compares the books in the $user_books array, to
those in the array derived from the books that are in the database.
If a book exists in the database, then it needs to be added to the user
table, for this I need the book id. If it doesn't the user will be
given a list of such books, displaying the names of the books that
aren't in the database.
If I created the list my making an array manually of both user books,
and books that are in the database, everything works fine, however, as
things are, array_search() won't return the key, if the book title has
a number in it.
I have posted my script below. I know it looks big and messy, but I
just wanted to know if anyone could spot any errors that may be leading
to the problem I am experiencing. I generally try to keep the script
simple, and then once it's doing what I want, I then work on optimizing
it.
<?php
$input = (isset($_POST['input'])) ? $_POST['input'] : '';
function validateBooks(& $booklist) {
global $db;
if ($booklist) {
$query =
"SELECT `book_name` AS name, `book_id` AS bid FROM
`pp_books`;";
$dbbooks_res = $db->sql_query($que ry);
$newbooklist = array();
$dbbooks = array();
while ($book = $db->sql_fetchrow($ dbbooks_res)) {
$book_name = $book['name'];
$dbbooks[$book['bid']] = $book_name;
}
foreach ($booklist as $book_name) {
$book_name;
$key = array_search("$ book_name", $dbbooks);
echo "key:$key<b r />";
if ($key) {
$newbooklist['valid'][] = $key;
}
else {
$newbooklist['invalid'][] = $book_name;
}
}
echo "Add<br />";
echo "<pre>",print_r ($newbooklist['valid']),"</pre>";
echo "Don't add<br />";
echo "<pre>",print_r ($newbooklist['invalid']),"</pre>";
}
}
function process($input= '') {
if ($input) {
$input = preg_replace("/([\n\r]|.+) Special Books \][\n\r]/s",
"", $input);
$input = preg_replace("/\[ .+/s", "", $input);
$input = preg_replace("/[\r\n]+/", "\n", $input);
$input_lines=pr eg_split("/\n/", $input);
$sizeof_input_l ines = sizeof($input_l ines);
$book_list = array();
for ($i=0; $i<$sizeof_inpu t_lines; $i++) {
if (strlen($input_ lines[$i])>1) {
$book_list[] = $input_lines[$i];
}
}
$books = sizeof($book_li st);
$not_added = validateBooks($ book_list);
}
}
echo "<center><subti tle>Import Books</subtitle></center><p>  ;</p>";
echo "<table>";
if ($input) {
process($input) ;
}
echo "<tr><td>".show ImportBox()."</td></tr>";
echo "</table>";
function showImportBox() {
global $module_name;
echo <<<EOT
<form method="post" action="index.p hp?name=$module _name">
<center><textar ea name="input" cols="80"
rows="30"></textarea></center><br />
<center><inpu t type="submit" name="submit_im port" value="Import
Books"></center>
<input type="hidden" name="do" value="Import My Books">
<input type="hidden" name="Import My Books" value="1">
</form>
EOT;
}
?>
gets a list of books from the database, and then compares them with a
list of books which have been obtained from a textarea in a form, and
formatted into single books, each one stored in a non-associative
array.
I have a function that compares the books in the $user_books array, to
those in the array derived from the books that are in the database.
If a book exists in the database, then it needs to be added to the user
table, for this I need the book id. If it doesn't the user will be
given a list of such books, displaying the names of the books that
aren't in the database.
If I created the list my making an array manually of both user books,
and books that are in the database, everything works fine, however, as
things are, array_search() won't return the key, if the book title has
a number in it.
I have posted my script below. I know it looks big and messy, but I
just wanted to know if anyone could spot any errors that may be leading
to the problem I am experiencing. I generally try to keep the script
simple, and then once it's doing what I want, I then work on optimizing
it.
<?php
$input = (isset($_POST['input'])) ? $_POST['input'] : '';
function validateBooks(& $booklist) {
global $db;
if ($booklist) {
$query =
"SELECT `book_name` AS name, `book_id` AS bid FROM
`pp_books`;";
$dbbooks_res = $db->sql_query($que ry);
$newbooklist = array();
$dbbooks = array();
while ($book = $db->sql_fetchrow($ dbbooks_res)) {
$book_name = $book['name'];
$dbbooks[$book['bid']] = $book_name;
}
foreach ($booklist as $book_name) {
$book_name;
$key = array_search("$ book_name", $dbbooks);
echo "key:$key<b r />";
if ($key) {
$newbooklist['valid'][] = $key;
}
else {
$newbooklist['invalid'][] = $book_name;
}
}
echo "Add<br />";
echo "<pre>",print_r ($newbooklist['valid']),"</pre>";
echo "Don't add<br />";
echo "<pre>",print_r ($newbooklist['invalid']),"</pre>";
}
}
function process($input= '') {
if ($input) {
$input = preg_replace("/([\n\r]|.+) Special Books \][\n\r]/s",
"", $input);
$input = preg_replace("/\[ .+/s", "", $input);
$input = preg_replace("/[\r\n]+/", "\n", $input);
$input_lines=pr eg_split("/\n/", $input);
$sizeof_input_l ines = sizeof($input_l ines);
$book_list = array();
for ($i=0; $i<$sizeof_inpu t_lines; $i++) {
if (strlen($input_ lines[$i])>1) {
$book_list[] = $input_lines[$i];
}
}
$books = sizeof($book_li st);
$not_added = validateBooks($ book_list);
}
}
echo "<center><subti tle>Import Books</subtitle></center><p>  ;</p>";
echo "<table>";
if ($input) {
process($input) ;
}
echo "<tr><td>".show ImportBox()."</td></tr>";
echo "</table>";
function showImportBox() {
global $module_name;
echo <<<EOT
<form method="post" action="index.p hp?name=$module _name">
<center><textar ea name="input" cols="80"
rows="30"></textarea></center><br />
<center><inpu t type="submit" name="submit_im port" value="Import
Books"></center>
<input type="hidden" name="do" value="Import My Books">
<input type="hidden" name="Import My Books" value="1">
</form>
EOT;
}
?>
Comment