0

I'm trying to copy from one array to another but only unique values. I use for loop and when I debug all 10 blues are shown but after for loop the last 2 values are gone.

I debug each array element during the "for loop" and it seems fine coz I see all 10 elements. The problem starts after the for loop. E.g. If I want to print the 9th element it doesn't print it, i.e. it shows emty.

What can be the proble?

P.S> I've tried array_unique(), same output so its not it.

Here is my code:

    <?php

session_start();
error_reporting(E_ALL ^ E_NOTICE); //to remove annoying notices

include 'connectDB.php';

$queryISBN = mysql_query("SELECT * FROM booksread");
$numrows = mysql_num_rows($queryISBN);
if($numrows!=0)
{   
    $isbn_array = array();
    while($row = mysql_fetch_assoc($queryISBN))
    {
        $isbn_array[]=$row['ISBN'];
    }

    $isbn_unique = array();

    for ($i=0;$i<count($isbn_array );$i++)
    {
        if(!in_array($isbn_array[$i],$isbn_unique ))
        {
            $isbn_unique[$i]=$isbn_array[$i];
            echo $isbn_unique[$i]."  ---- ";
        }
    }
}
echo "<h1>Select books you would like to view</h1>";
$submit = $_POST['submit'];

if(isset($_POST['selectedbooks']))
    {
        $checked = $_POST['selectedbooks'];
    }

if($submit)
    {
        if(!isset($checked))
        {
            echo "You must check at least one book.";
        }
        else 
        {
            $_SESSION['checked'] = $checked;
            header("location: view_entries_results.php");
        }
    }

?>

<html>
<body>

<form action="view_entries.php" method="POST">
<table>
<table border="1">
<th>ISBN<th>Title<th>Author<th>Select</th>

<?php
    echo "Arr size: ".count($isbn_unique )." </br>";
    echo "8: ".$isbn_unique[7]."</br>";
    for ($j=0;$j<count($isbn_unique );$j++)
    {
        $curElement = $isbn_unique[$j];
        echo "Cur el: ".$curElement;
        $queryBook = mysql_query("SELECT * from book WHERE ISBN='$curElement'");

    while ($row = mysql_fetch_assoc($queryBook))
        {   
            $ISBN = $row['ISBN'];
            $title = $row['Title'];
            $author = $row['Authorname'];
        }
        ?>

    <tr>
        <td><?php echo $ISBN; ?></td>
        <td><?php echo $title; ?></td>
        <td><?php echo $author; ?></td>
        <td><input type="checkbox" name="selectedbooks[]" value="<?php echo $ISBN; ?>"/>
    </tr>

   <?php }  ?>

</table>
<p><input type="submit" name="submit" value="Add Entry" /></p>
</form>


</body>
</html>
1
  • PROBLEM SOLVED THANKS TO helloxk! Commented May 24, 2013 at 5:22

2 Answers 2

1

I think

if(!in_array($isbn_array[$i],$isbn_unique ))
{
    $isbn_unique[$i] = $isbn_array[$i];
    echo $isbn_unique[$i]."  ---- ";
}

should be

if(!in_array($isbn_array[$i],$isbn_unique ))
{
    $isbn_unique[] = $isbn_array[$i];
    echo $isbn_array[$i]."  ---- ";
}
Sign up to request clarification or add additional context in comments.

Comments

0

I think you have duplicate values in $isbn_array, so its skip keys as its already in array and hence size of $isbn_unique is different then then the $isbn_array. Try to debug on that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.