Jump to content

Using an array


tully87

Recommended Posts

Apologies but I'm quite new at using PHP and need some help  :-[

 

From an Access Database I want to search a field for "keywords". However one record can have multiple "keywords" so I want to search this field, select the distinct values, split them wherever a space occurs, put this list of values (a string) into an array and then select the distinct values again. The code I have so far is:

 

<?php

 

$items = odbc_exec($odbc, "SELECT DISTINCT keywords FROM faq WHERE visible = 'true'") or die (odbc_errormsg());

 

echo "<span class=titles'>FAQ Categories</span><br /><br />";

 

while ($detail = odbc_fetch_array($items)){

 

$category = $detail['keywords];

$splitcategory = preg_split('/ /', $category);

foreach($splitcategory as $categories) {

  echo "$categories";

}

 

echo ("<a href='index.php?l1=support&l2=faq&l3=catdetail&cat=".urlencode($detail['keywords'])."'>".$detail['keywords']."</a><br />");

 

}

?>

 

So when I "echo $categories" I get:

 

ArchiveCalendarsICQMacroMailboxP&VP&VTestPrintingSignatureWorkbook  - (P&V Test) are in the same record

 

So what I need to do (I think) is put these in an array and then select the distinct values but I can't figure out how to create an array and populate it with $category. Any ideas?

 

Please note that the database only contains a sample of data at the minute and will continue to be updated regularly so the number of values in $category will never have a set amount.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/238181-using-an-array/
Share on other sites

Thanks, I do want to remove duplicates from that array as I want to then link each single "keyword" to any record that contains that word.

 

I tried $distinct = array_unique($splitcategory); but unfortunately it didnt work. If I use:

 

$splitcategory = preg_split('/ /', $category);

print_r($splitcategory)

 

I get:

 

Array ( [0] => Archive ) Array ( [0] => Calendars ) Array ( [0] => P&V) Array ( [0] => P&V[1] => Test ) Array ( [0] => ICQ ) Array ( [0] => Macro ) Array ( [0] => Mailbox ) Array ( [0] => Printing ) Array ( [0] => Signature ) Array ( [0] => Workbook )

 

 

Link to comment
https://forums.phpfreaks.com/topic/238181-using-an-array/#findComment-1223977
Share on other sites

let me see if I understand... You want to:

 

1. get the unique keywords

2. link each keyword to a different url (this is easier if you link them all to the same page but pass the keyword with it)

 

how about this: (assuming $keywords is just a string with words : "ICQ JOOMLA fancy word facebook etc");

 

// split string into an array whenever a space is found:

$words = explode(" ",$keywords);

// remove duplicates

$unique = array_unique($words);

// output keywords and link them to another page

foreach($unique as $word){

echo '<a href="pageToLinkTo.php?keyword='.$word.'">'.$word.'</a> ';

}

 

then on pageToLinkTo.php you just need something like

if(isset($_GET['keyword']) && $_GET['keyword']!=''){

  //send to appropriate page

}else{

  // cant determine page. Send to default page or 404.html

}

 

hope this helps

Link to comment
https://forums.phpfreaks.com/topic/238181-using-an-array/#findComment-1223982
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.