-1

hi everyone,
can anyone tell me why am i getting two outputs when i am only asking for one, well here is the code it will be more self explanatory.
it is simply a dictionary

<?php
// Fill up array with names
$a[]="Hello-common greeting";
$a[]="phone-a comunication device ";
$a[]="pc-personal computer";
$a[]="plane-flying object created to tarnsport objects through air";
$a[]="abba-60's music girls band";
$a[]="brother-close releative";
$a[]="car-transporting device";
$a[]="doctor-person that helps people when they are ill";
$a[]="elephant-huge animal";
$a[]="a-is the first letter in the alphabet";
$a[]="kite-fling object controled by a person on the ground";
$a[]="police-protects people from crimes and danger";
$a[]="pocket-a small holding space atached to the close";
$a[]="abc123-a famous michael jackson song";

//get the q parameter from URL
$q=$_GET["q"];

//lookup all hints from array if length of q>0
if (strlen($q) > 0)
  {
  $def="";
  for($i=0; $i<count($a); $i++)
    {//if what i enter is eaqual to one of the elements in the array
    if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
     {
      if ($def=="")
        {//value of array is placed in the def variable
        $def=$a[$i];
        }
        else
        {//else it is placed below def variable
        $def=$def."<br>".$a[$i];
        }
      }
    }
  }

echo $q;
echo ("<br>");
if ($def == "")
  {
  echo("Definition not found");
  }
else
  {//split the string where "-"  and placed in two different variables
  list($word, $definition)= split('[-]',$def); 
  echo $definition;//should only return definition for all possible words
  }

?>

well for example if i type in "po" it should give me all possible definitions such as police and pocket but instead it gives me definition for police and just word pocket where it shouldn't even return "word" it should only return definition.
thanks for your help

3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by gedas
1

This seems to work:

$a[]=array("Hello","common greeting");
$a[]=array("phone","a comunication device");
$a[]=array("pc","personal computer");
$a[]=array("plane","flying object created to tarnsport objects through air");
$a[]=array("abba","60's music girls band");
$a[]=array("brother","close releative");
$a[]=array("car","transporting device");
$a[]=array("doctor","person that helps people when they are ill");
$a[]=array("elephant","huge animal");
$a[]=array("a","is the first letter in the alphabet");
$a[]=array("kite","fling object controled by a person on the ground");
$a[]=array("police","protects people from crimes and danger");
$a[]=array("pocket","a small holding space atached to the close");
$a[]=array("abc123","a famous michael jackson song");

$q=$_GET["q"];
if(strlen($q)>0){
  $def="";
  for($i=0; $i<count($a); $i++){
    if(strtolower($q)==strtolower(substr($a[$i][0],0,strlen($q))))$def .= "<br />" . $a[$i][1];
  }
}
echo $q;
echo($def == "") ? "<br />Definition not found" : $def;

Setting your words & definitions like this makes more sense. You can even have '-' (hyphens) in your words/definitions without it causing a problem.

0

well for example if i type in "po" it should give me all possible definitions such as police and pocket but instead it gives me definition for police and just word pocket where it shouldn't even return "word" it should only return definition.

The problem is that when you type "po", your $def ends up with the following:

$def="police-protects people from crimes and danger<br>pocket-a small holding space atached to the close"

so, when you split at the hyphens, $definition SHOULD get the string

"protects people from crimes and danger<br>pocket" because it is BETWEEN hyphens. What you needed to do was NOT to store those hyphens to begin with:

...
if ($def=="")
{//value of array is placed in the def variable
$def=preg_replace('#^([^-]+).#','',$a[$i]);
}
else
{//else it is placed below def variable
$def=$def."<br>" . preg_replace('#^([^-]+).#','',$a[$i]);
}
...
0

thanks for your replays it did solve my problem im not having any more problems really appreciate it.
just one more queastion to

Ardav

could you explain me this line

echo($def == "") ? "<br />Definition not found" : $def;

i am not very familiar with this and i am finding hard time figuring it out.
thanks again for your help

0

Sorry - I was trying to minify the code and used a 'ternary operator':

echo($def == "") ? "<br />Definition not found" : $def;

is a short way of saying this:

if($def == ""){
  echo "<br />Definition not found";
}else{
  echo $def;
}

I don't usually use this when answering posts, but thought it would be useful in order to see how little code is actually needed to run your routine. Hope that clears it up.

0

yeah this does help a lot cos i wasn't used to that standard of coding thanks again everything is working now
really appreciate your help
thank you

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.