0

Hi, I'm trying to use the array_unique method in php to get the unique values of the array but what i get is just one value and the other values deleted by the method , the values in the array are (Pro CSharp 2008 and the NET 3.5 Platform , Beginning C Sharp 2008 and Beginning C Sharp 2008 ) and the value only i got was Beginning C Sharp 2008 but Pro CSharp 2008 and the NET 3.5 Platform doesn't exist in the returned array , so what is the problem and who to solve it ?

4
Contributors
10
Replies
11
Views
6 Years
Discussion Span
Last Post by pritaeas
Featured Replies
  • 1

    [QUOTE]can i use the mysql_fetch_array twice in different places in my code [/QUOTE] Yes [QUOTE]Probably the easiest solution is to build a complete unique array in the first loop, and use that[/QUOTE] My suggestion would be to reset the pointer to the query result. IMMEDIATELY BEFORE the SECOND while put … Read More

0

Your code is right.. But surely it will display as Array([0]=>Pro CSharp 2008, [1]=> NET 3.5 Platform, [2]=>Beginning C Sharp 2008)

This function only omits duplicate values only.. Kindly post your full code first.. We will definitely help you...

0

Thank u all , there was a mistake in my code i didn't notice , but there is another problem that can i use the mysql_fetch_array twice in different places in my code i tried but it doesn't work with me and this the code :

while($co_book = mysql_fetch_array($rs))
 {
   $book_names_arr[$i] = $co_book["book_name"];
   $i++;
 }

$book_names_arr2 = array_unique($book_names_arr);

for($i = 0 ; $i < $num_rows ; $i++)
 {
	 
	 
   echo "<tr><td>".htmlspecialchars(stripslashes($book_names_arr2[$i]))."</td>";

   echo "<td>";
while($co_books = mysql_fetch_array($rs))
    {
		echo "hello";
		//echo htmlspecialchars(stripslashes($co_books["book_name"]));
      if($book_names_arr2[$i] == $co_books["book_name"])
       {
         echo htmlspecialchars(stripslashes($co_books["aut_name"]))."<br>";
       }
    }
	
	echo "</td></tr>";
 }

the second while that inside the for loop doesn't run i don't know the reason ?

0

Thank u all , there was a mistake in my code i didn't notice , but there is another problem that can i use the mysql_fetch_array twice in different places in my code i tried but it doesn't work with me and this the code :

while($co_book = mysql_fetch_array($rs))
 {
   $book_names_arr[$i] = $co_book["book_name"];
   $i++;
 }

$book_names_arr2 = array_unique($book_names_arr);

for($i = 0 ; $i < $num_rows ; $i++)
 {
	 
	 
   echo "<tr><td>".htmlspecialchars(stripslashes($book_names_arr2[$i]))."</td>";

   echo "<td>";
while($co_books = mysql_fetch_array($rs))
    {
		echo "hello";
		//echo htmlspecialchars(stripslashes($co_books["book_name"]));
      if($book_names_arr2[$i] == $co_books["book_name"])
       {
         echo htmlspecialchars(stripslashes($co_books["aut_name"]))."<br>";
       }
    }
	
	echo "</td></tr>";
 }

this code check if the book name in the $book_names_arr2[$i] is equal to the book name in the result set of the query to get the authors names of the book but the second while that inside the for loop doesn't run i don't know the reason ?

Edited by sedalnas: n/a

0

The first while loop already passed all results, so the second time it does not find any. Probably the easiest solution is to build a complete unique array in the first loop, and use that. I think you're looking for something like this:

$books = array ();
while ($co_book = mysql_fetch_array($rs)) {
  if (!isset($books[$co_book['book_name']]))
    $books[$co_book['book_name']] = $co_book['aut_name'];
}

foreach ($books as $book_name => $author_name) {
  echo "<tr><td>$book_name</td><td>$author_name</td></tr>";
}
1

can i use the mysql_fetch_array twice in different places in my code

Yes

Probably the easiest solution is to build a complete unique array in the first loop, and use that

My suggestion would be to reset the pointer to the query result. IMMEDIATELY BEFORE the SECOND while put mysql_data_seek($rs, 0); http://us2.php.net/manual/en/function.mysql-data-seek.php

0

Thanks pritaeas for your help and thanks hielo your suggestion solve the problem .

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.