Reputation: 1391
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>
Upvotes: 0
Views: 78
Reputation: 391
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.
Upvotes: 0
Reputation: 26
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]." ---- ";
}
Upvotes: 1