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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.