|
Posted by Daz on 10/17/06 17:05
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($query);
$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<br />";
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=preg_split("/\n/", $input);
$sizeof_input_lines = sizeof($input_lines);
$book_list = array();
for ($i=0; $i<$sizeof_input_lines; $i++) {
if (strlen($input_lines[$i])>1) {
$book_list[] = $input_lines[$i];
}
}
$books = sizeof($book_list);
$not_added = validateBooks($book_list);
}
}
echo "<center><subtitle>Import Books</subtitle></center><p> </p>";
echo "<table>";
if ($input) {
process($input);
}
echo "<tr><td>".showImportBox()."</td></tr>";
echo "</table>";
function showImportBox() {
global $module_name;
echo <<<EOT
<form method="post" action="index.php?name=$module_name">
<center><textarea name="input" cols="80"
rows="30"></textarea></center><br />
<center><input type="submit" name="submit_import" value="Import
Books"></center>
<input type="hidden" name="do" value="Import My Books">
<input type="hidden" name="Import My Books" value="1">
</form>
EOT;
}
?>
Navigation:
[Reply to this message]
|