-1

I am so close but need some help please. On my site I have tag words (visit here) -- As you can see I have 2 words named America then it says Persecution then Christians. Here is where I need help.

  1. I need only one word for America not two words.
  2. In the first row on my website (please go there to view) it says Abortion America America Persecution Christians then Animals... As you can see this is not in alphabetical order. I need for this to be in alphabetical order with the rest of the words.

Right now I am testing one row with three words inside and all the rest only have one word inside. The test row has the words America, Persecution, Christians. Until I learn how to achieve what I need then all the rows will have more than one word inside.

enter image description here

Here is my code below ----

<?php
          include('connect.php');
          //now execut Select statement on table
$sql = mysqli_query($db_xxx, "SELECT word FROM articles GROUP BY word ORDER BY word ASC");

while ($row = mysqli_fetch_array($sql)) { 
$link = $row['link'];
$word_array = explode(", ", $row['word']); $unique_word_array = array_unique($word_array); 

foreach($unique_word_array as $key) 
{ 
        echo "<div class='cloudbox tag'><a href = '../../articlestagresults.php?word=" . $key . "'> " . $key . "</a></div>"; }

}

   // Close your database connection
mysqli_close($db_xxx);
?>

Again I need help to combine the words America and put all those tag words in alphabetical order, thank you. If you need further information please ask. I really need help on this, again thank you.

Jenn
  • 49
  • 7
  • What I think you have to do here is to separate the word column into its own table, that would be easier to work with and it will be better as a design, or you will have to work with these values inside word column by applying some stored procedures like in this Stackoverflow question: https://stackoverflow.com/questions/17942508/sql-split-values-to-multiple-rows the problem is that you have to deal with each value separately and order them by alphabetical order, which is harder by your current database design. – ROOT Aug 05 '17 at 18:43
  • Okay I do have mutiple columns named word, word2, word3, word4 and word5 however I do not know how to combine these columns into one using the code above. So word row will have America then word2 row can have Persecution and word3 can have Christians. How do I combine all these rows into one, thank you. – Jenn Aug 05 '17 at 18:59
  • a quick solution for your question is to sort using php asort() might be a good way to go, just apply it to $unique_word_array variable and then you will have alphabetically sorted array. – ROOT Aug 05 '17 at 20:14
  • Okay here is my new code with 2 columns selected how to do I GROUP THEM and put them in ALPHABETICAL ORDER. Any help would be great, can you please show me the code for I am new at this, thank you. $sql = mysqli_query($db_xxx, "SELECT word, word2 FROM post"); while ($row = mysqli_fetch_array($sql)) { $link = $row['link']; $word_array = explode(", ", $row['word']); $unique_word_array = array_unique($word_array); foreach($unique_word_array as $key) { echo ""; } } – Jenn Aug 06 '17 at 17:12
  • I'm not going to your website. Provide the necessary information here. See: [Why should I provide an MCVE for what seems to me to be a very simple SQL query?](https://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query) – Strawberry Aug 07 '17 at 09:03

1 Answers1

1

I used asort() on $unique_array_word variable, the following is your code with the added sort function call:

$sql = mysqli_query($db_xxx, "SELECT word, word2 FROM post");  
while ($row = mysqli_fetch_array($sql)) { $link = $row['link']; 
  $word_array = explode(", ", $row['word']);
  $unique_word_array = array_unique($word_array);
  asort(unique_word_array);
  foreach($unique_word_array as $key) {
    echo "<div class='cloudbox tag'><a href = '../../articlestagresults.php?word=" . $key . "'> " . $key . "</a></div>"; 
  }
}

I hope this would help u achieving what you want.

ROOT
  • 11,363
  • 5
  • 30
  • 45