array_search() not working correctly?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daz

    array_search() not working correctly?

    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>&nbsp ;</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;
    }

    ?>

  • maven22

    #2
    Re: array_search() not working correctly?

    Can you give us examples as to what titles work and what titles don't?
    It only doesn't work when there's a number in the book title? What does
    array_search return?

    a good debug idea would be to print the value of $book_name each time
    you are about to run an array_search().


    Daz wrote:
    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>&nbsp ;</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

    • Daz

      #3
      Re: array_search() not working correctly?


      maven22 wrote:
      Can you give us examples as to what titles work and what titles don't?
      It only doesn't work when there's a number in the book title? What does
      array_search return?
      >
      a good debug idea would be to print the value of $book_name each time
      you are about to run an array_search().
      >
      >
      Daz wrote:
      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>&nbsp ;</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;
      }

      ?>
      I don't believe it! I have figured out why it's not working, and man,
      do I feel silly?

      The books aren't in the database. It just happened to be that not a
      single one of these 6 books were in there and they happened to be the
      only 6 in my query containing numbers...

      Thanks for your input, and sorry for wasting any of your time.

      Sincerest regards.

      Daz.

      Comment

      Working...