Jump to content

[SOLVED] Remove Duplicates from An Array


hoopplaya4

Recommended Posts

Hey All,

 

I have a form where users can post an item to several users at once.  For example, let's say they want to add a video to several users profiles.  The following information would be POSTd and then looped:

 

 

   
<?php   
      <form class="uniForm" enctype="multipart/form-data" action="addvideo.php" method="POST">
      <select multiple style="width:200px" name="name[]" id="name">

      $sql = "SELECT usrID, usrFirst, usrLast";
      $sql .= " FROM tblUsers";
      $sql .= " WHERE usrActive = '1'";
      $sql .= " ORDER BY usrFirst";

	require("../connection.php");

   		$rs=mysql_db_query($DBname,$sql,$link);

    	if ($rs) {

     	 while ($row=mysql_fetch_array($rs)){

          print("<option value='" . $row["usrID"] . "'>");
  		  print("" . $row["usrFirst"] . " " . $row["usrLast"] . "</option>\n");
  		  } // end while

   	     }
      	else {  // No Events found

 	 print("No Active players");

     mysql_close($link);  } // end else ($rs) ?>
	</select>

 

And here's the PHP that adds the info to the database:

 

 

<?php
require("../connection.php");
$rs = mysql_db_query($DBname,$sql,$link);




$title=$_POST['title'];


$name=$_POST['name'];
foreach ($name as $nam) {

mysql_query("INSERT INTO `tblVideos` (usrID, videoTitle) VALUES ('$nam', '$title')" );

if ($rs) {print" <script>
  window.location=\"video.php?action=add&msg=2\"
  </script> ";
  
  }

else {print" <script>
  window.location=\"video.php?action=add&msg=1\"
  </script> ";
  
  }
  
  }

mysql_close($link);

 

And here is the code to display all the videos that were added:

 

<?php
$sql = "SELECT * FROM tblVideos, tblUsers WHERE tblVideos.usrID = tblUsers.usrID ORDER BY videoID DESC";

require("../connection.php");

$rs = mysql_db_query($DBname,$sql,$link) or die(mysql_error()); 

if ($rs) {

  while ($row=mysql_fetch_array($rs)){
     print("<tr>\n" );
     print("<td>".$row['videoTitle'] ."</td>\n" );
 print("<td>". $row['usrFirst'] ." ". $row['usrLast'] ."</td>");
 print("</tr>\n");	

   } //end while
   
} // end if

 

However, this outputs something like this:

 

Really Cool Video Title for John Doe

Really Cool Video Title for Jane Doe

Really Cool Video Title for John Smith

Really Cool Video Title for Jane Smith (etc.)

 

How would I remove the duplicates and have just one instance of:

 

Really Cool Video Title for Multiple Users

 

Please let me know if I'm not making sense!  Thanks!

 

Link to comment
Share on other sites

How many different types of messages do you have? if it's just this one, it would be trivial to just count the number of entries, but if there are multiple messages that will be in this array, you might want to pass around some type of a message type constant so that you don't have to do any string parsing that may be slow.

 

For instance...

<?php
define('MSG_TYPE_COOL_VIDEO', 0);
define('MSG_TYPE_COOL_MUSIC', 1);

$array = array(
   array('msg' => 'Really Cool Video Title for John Doe', 'type' => MSG_TYPE_COOL_VIDEO),
   array('msg' => 'Really Cool Video Title for Jane Doe', 'type' => MSG_TYPE_COOL_VIDEO),
   array('msg' => 'Really Cool Music for John Doe', 'type' => MSG_TYPE_COOL_MUSIC)
);

// then see if there is more than one message with MSG_TYPE_COOL_VIDEO for its type, if so, combine them on display
// ask me to show you an example of this if you want, just wanted to show you the concept.
?>

 

of course you could analyze the strings to see if they're similar and avoid passing around a constant, but I like to think memory is cheaper than CPU cycles.

Link to comment
Share on other sites

Now that I've finally had some time to look into this issue a bit more, I'm thinking that I haven't explained the issue well enough.  So, I'll attempt to re-explain:

 

I have a 'form' where an admin can post a video to a user's account; thus the video is unique to that user.  However, the admins may want to post a video to all users (or multiple users) at once. 

 

So, for example, let's say that the admin wants to post a video for 4 users: (John, Jane, Jack, & Jill).

 

When he submits the form, the following fields are passed:

 

usrID (this is the usrID of the user who is getting video, e.g., John, Jane, etc).

videoURL (this is the URL of the YouTube video)

videoTitle (this is the title that the admin gives it, so it is dynamic)

videoComments (these are the comments given by the admin, which is also dynamic).

videoID (this is an auto-incremented number).

 

Then, the mysql query loops, and does the same thing for the next person, until it finished.

 

Now, when I want to list these videos, it currently would show the following in a table:

 

 

   

usrID

   

videoURL

   

videoTitle

   

videoComments

   

videoID

 

 

   

John

   

www.youtube.com/abc

   

Funny Clip

   

You will like this clip.

   

01

 

 

   

Jane

   

www.youtube.com/abc

   

Funny Clip

   

You will like this clip.

   

02

 

 

   

Jack

   

www.youtube.com/abc

   

Funny Clip

   

You will like this clip.

   

03

 

 

   

Jill

   

www.youtube.com/abc

   

Funny Clip

   

You will like this clip.

   

04

 

 

However, so that we're not being redundant, I'd like the table to display something like this:

 

 

   

usrID

   

videoURL

   

videoTitle

   

videoComments

   

videoID

 

 

   

Multiple Users

   

www.youtube.com/abc

   

Funny Clip

   

You will like this clip.

   

01

 

 

It saves a lot of room in the table.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.