oz11 Posted January 9 Share Posted January 9 Hey (again :).. This is the code I'm working on but can't figure out to the heck of me how to remove these duplicate "words"/strings... A small sample of the output is added to the upload so you can see what I mean.. HELP! ps: a few comments have been left there for testing. $stmt = $pdo->prepare("select DISTINCT terms from links WHERE terms LIKE ? GROUP BY terms"); $stmt->execute(array("$alpha%")); // fetching rows into array $data = $stmt->fetchAll(); //echo gettype($data); foreach($data as $result_tag) { $one = implode(', ',$result_tag) . ""; $two = strtok($one, ',')."<br>"; //echo gettype($two); //echo strtolower($two); $three = strtolower($two); //print_r($three); $string = implode(" ", array_unique(explode(" ", $three))); //echo gettype($string); echo $string; } exit(); Quote Link to comment https://forums.phpfreaks.com/topic/317610-remove-duplicate-words/ Share on other sites More sharing options...
Olumide Posted January 9 Share Posted January 9 The problem might be with the placement of the array_unique function. Try this modification: $stmt = $pdo->prepare("SELECT DISTINCT terms FROM links WHERE terms LIKE ? GROUP BY terms"); $stmt->execute(array("$alpha%")); $data = $stmt->fetchAll(); foreach ($data as $result_tag) { $one = implode(', ', $result_tag) . ""; $two = strtok($one, ',') . "<br>"; $three = strtolower($two); $words = explode(" ", $three); $unique_words = array_unique($words); $string = implode(" ", $unique_words); echo $string; } exit(); Quote Link to comment https://forums.phpfreaks.com/topic/317610-remove-duplicate-words/#findComment-1614016 Share on other sites More sharing options...
Barand Posted January 9 Share Posted January 9 Once again we have no idea what the data you are processing looks like. Post the output from this code... echo '<pre>' . var_export($data, 1) . '</pre>'; 2 Quote Link to comment https://forums.phpfreaks.com/topic/317610-remove-duplicate-words/#findComment-1614018 Share on other sites More sharing options...
oz11 Posted January 9 Author Share Posted January 9 (edited) Do you also happen to know how to get other words after the first ones to show up with different alphabetical pages? Like "magazine" wont show as it's not the first in the line, thus not showing under 'M'. .. and 9/11 wont show under '9' either.. Code update: echo $_SERVER['REQUEST_URI']; echo "<br><hr>"; $stmt = $pdo->prepare("SELECT DISTINCT terms FROM links WHERE terms LIKE ? OR terms like ? GROUP BY terms"); $stmt->execute(array("$alpha%", ", $alpha%")); $data = $stmt->fetchAll(); $index = 0; echo "<ul>"; foreach ($data as $result_tag) { $one = implode(', ', $result_tag) . ""; $two = strtok($one, ',') . "<br>"; $three = strtolower($two); $words = explode(" ", $three); $unique_words = array_unique($words); echo "<li>".$string = implode(" ", $unique_words)."</li>"; } echo "</ul>"; echo '<pre>' . var_export($data, 1) . '</pre>'; exit(); Edit: I apologize for jumping ahead. One thing at a time i guess.. is it OK to talk about both in the same thread? :/ Edited January 9 by oz11 Quote Link to comment https://forums.phpfreaks.com/topic/317610-remove-duplicate-words/#findComment-1614019 Share on other sites More sharing options...
kicken Posted January 9 Share Posted January 9 Pro tip: When asking for the output of var_export, we don't want an image of it. We want you to copy/paste it into a code block so we can copy/paste it into an editor and use it to help you debug the code. Posting an image is not useful and a great way to get people to ignore your thread. Second tip, if you want to work with individual words, you should probably be storing them individually in a their own table, not as a comma separated list in your links table. Then getting your unique list of words would be a simple select distinct query. 1 Quote Link to comment https://forums.phpfreaks.com/topic/317610-remove-duplicate-words/#findComment-1614021 Share on other sites More sharing options...
Barand Posted January 9 Share Posted January 9 Here's my solution using your (terrible) data structure... giving If you want those beginning with 'z', then giving 1 Quote Link to comment https://forums.phpfreaks.com/topic/317610-remove-duplicate-words/#findComment-1614027 Share on other sites More sharing options...
Solution Barand Posted January 9 Solution Share Posted January 9 PS I am in complete agreement with @kicken - you need to restructure your tables +---------------+ | link | +---------------+ | id (PK) |-----+ | url | | +---------------+ +---------------+ | | term | | +---------------+ | | id (PK) | +-----<| link_id (FK) | | term | +---------------+ So that your data looks like this... TABLE: link TABLE: term +-----+-------------------+ +-----+---------+---------------------+ | id | url | | id | link_id | term | +-----+-------------------+ +-----+---------+---------------------+ | 1 | page1.php | | 1 | 1 | Zeitgeist | | 2 | page2.php | | 2 | 1 | conspiracy theories | +-----+-------------------+ | 3 | 1 | 9/11 | | 4 | 1 | 9 11 | | 5 | 2 | zine | | 6 | 2 | magazine | | 7 | 2 | online | | 8 | 2 | newspaper | | 9 | 2 | press | | 10 | 2 | alternative | | 11 | 2 | coldtype | | 12 | 2 | world news | +-----+---------+---------------------+ Then all you need is a couple of simple queries. (My reply to your previous topic assumed were using a correctly normalized design as above) 1 Quote Link to comment https://forums.phpfreaks.com/topic/317610-remove-duplicate-words/#findComment-1614028 Share on other sites More sharing options...
oz11 Posted January 19 Author Share Posted January 19 (edited) Thanks so much for helping me man. I'll "normalized" the db and it all functions way better now. Thanks. Took me till 6:30 am but its done. I thought originally I was doing ok, but a properly 'lized DB is the way to go. woo Edited January 19 by oz11 Quote Link to comment https://forums.phpfreaks.com/topic/317610-remove-duplicate-words/#findComment-1614215 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.