Jump to content

Remove duplicate words


Go to solution Solved by Barand,

Recommended Posts

 

 

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();

Screenshot from 2024-01-09 00-44-47.png

Link to comment
https://forums.phpfreaks.com/topic/317610-remove-duplicate-words/
Share on other sites

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();

 

Posted (edited)

Screenshotfrom2024-01-0901-55-03.png.37349b9592fe36afee0fe7850b7651b0.png

 

 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'.:shrug: ..  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 by oz11

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.

 

  • Like 1
  • Solution

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)

  • Thanks 1
  • 2 weeks later...

:birthday: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 by oz11

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.