hoopplaya4 Posted December 8, 2008 Share Posted December 8, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/136098-solved-remove-duplicates-from-an-array/ Share on other sites More sharing options...
hoopplaya4 Posted December 8, 2008 Author Share Posted December 8, 2008 Does anyone have any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/136098-solved-remove-duplicates-from-an-array/#findComment-709942 Share on other sites More sharing options...
genericnumber1 Posted December 9, 2008 Share Posted December 9, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/136098-solved-remove-duplicates-from-an-array/#findComment-710087 Share on other sites More sharing options...
balistic Posted December 9, 2008 Share Posted December 9, 2008 http://www.php.net/array_unique this will do the job nicely Quote Link to comment https://forums.phpfreaks.com/topic/136098-solved-remove-duplicates-from-an-array/#findComment-710092 Share on other sites More sharing options...
genericnumber1 Posted December 9, 2008 Share Posted December 9, 2008 array_unique() would be nice if the values were truly unique - they're not - they're just similar, array_unique() won't work in this instance and that's why I didn't suggest it Quote Link to comment https://forums.phpfreaks.com/topic/136098-solved-remove-duplicates-from-an-array/#findComment-710095 Share on other sites More sharing options...
DarkWater Posted December 9, 2008 Share Posted December 9, 2008 Check out MySQL's COUNT() function and GROUP BY clause. You should be able to piece it together. Quote Link to comment https://forums.phpfreaks.com/topic/136098-solved-remove-duplicates-from-an-array/#findComment-710098 Share on other sites More sharing options...
hoopplaya4 Posted December 9, 2008 Author Share Posted December 9, 2008 Thanks for the replies guys. I'll take a look at these soon, and get back to you. I've got finals today and tomorrow! :-\ Quote Link to comment https://forums.phpfreaks.com/topic/136098-solved-remove-duplicates-from-an-array/#findComment-710231 Share on other sites More sharing options...
genericnumber1 Posted December 9, 2008 Share Posted December 9, 2008 Yeah, good luck, I've got a date with the calculus gods tomorrow as well. Quote Link to comment https://forums.phpfreaks.com/topic/136098-solved-remove-duplicates-from-an-array/#findComment-710295 Share on other sites More sharing options...
hoopplaya4 Posted December 13, 2008 Author Share Posted December 13, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/136098-solved-remove-duplicates-from-an-array/#findComment-714701 Share on other sites More sharing options...
hoopplaya4 Posted December 13, 2008 Author Share Posted December 13, 2008 Any ideas out there? Quote Link to comment https://forums.phpfreaks.com/topic/136098-solved-remove-duplicates-from-an-array/#findComment-714839 Share on other sites More sharing options...
DarkWater Posted December 13, 2008 Share Posted December 13, 2008 If the only reason to do this is to "save room in the table", don't bother. MySQL can handle terabytes of data. Quote Link to comment https://forums.phpfreaks.com/topic/136098-solved-remove-duplicates-from-an-array/#findComment-714840 Share on other sites More sharing options...
hoopplaya4 Posted December 13, 2008 Author Share Posted December 13, 2008 Thanks for the reply Darkwater. Sorry for not being specific. By "room," I mean 'visually speaking.' I don't want it to take up an entire page. That would be a lot of scrolling. Quote Link to comment https://forums.phpfreaks.com/topic/136098-solved-remove-duplicates-from-an-array/#findComment-714843 Share on other sites More sharing options...
DarkWater Posted December 13, 2008 Share Posted December 13, 2008 SELECT *, COUNT(*) AS total FROM tblVideos GROUP BY videoTitle; I think. Quote Link to comment https://forums.phpfreaks.com/topic/136098-solved-remove-duplicates-from-an-array/#findComment-714844 Share on other sites More sharing options...
hoopplaya4 Posted December 13, 2008 Author Share Posted December 13, 2008 Thanks for the reply. That worked terrific! Now, I have a (hopefully simple) question: 1) Is there any way to display that this video was posted to multiple users? Quote Link to comment https://forums.phpfreaks.com/topic/136098-solved-remove-duplicates-from-an-array/#findComment-714873 Share on other sites More sharing options...
DarkWater Posted December 14, 2008 Share Posted December 14, 2008 Thanks for the reply. That worked terrific! Now, I have a (hopefully simple) question: 1) Is there any way to display that this video was posted to multiple users? //pretend we're in the loop: if ($row['total'] > 1) { //multiple } else { //one } Quote Link to comment https://forums.phpfreaks.com/topic/136098-solved-remove-duplicates-from-an-array/#findComment-714944 Share on other sites More sharing options...
hoopplaya4 Posted December 14, 2008 Author Share Posted December 14, 2008 That worked great!! Thank you!! Quote Link to comment https://forums.phpfreaks.com/topic/136098-solved-remove-duplicates-from-an-array/#findComment-715198 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.