Jump to content

[SOLVED] Echo'ing an Array that has been searched for...


MasterACE14

Recommended Posts

How do you echo an Array that you have a search form for?

 

Heres my search form:

<form name="search" method="post" action="index.php?page=encyclopedia_search">
item: <input type="text" size="15" maxlength="60" name="item">
  <input type="submit" value="Search">
</form>

 

and here's the result page:

<?php

$item_search = $_POST["item"];

echo 'Item is '.array_search($item_search,$weapons);


?>

 

I want to display the whole array in a table if it exists. And if it doesn't, just give an error.

 

Currently when I try a search for a value I know "Does Exist" it just comes up with "Item is " thats it.

 

How do I display the whole array if it finds it, and if theirs multiple arrays that have the same value that was searched for, how do I display them all in separate tables?

 

Regards ACE

Link to comment
Share on other sites

$weapons array:

$weapons = array( array( id => 0,
					 weapon => "L85A2", 
					 price => 250,
					 damage => 15,
					 type => "Rifle",
					 rarity => "Basic",
					 description => "The L85A2 is a bullpup assault rifle, it is very powerful and a 
           				                 iron-sight comes standard.",
					 options => "You can give it a Laser."
					),
		   array( id => 1,
					 weapon => "AK-47", 
					 price => 325,
					 damage => 25,
					 type => "Rifle",
					 rarity => "Good",
					 description => "The AK-47(Kalashnikov) is a Russian made assault rifle, it is one of the most
						                 popular weapons in the world, and is also one of the most reliable.",
					 options => "You can give it a iron-sight."
					),
               array( id => 2,
					 weapon => "L96", 
					 price => 400,
					 damage => 30,
					 type => "Sniper Rifle",
					 rarity => "Good",
					 description => "The L96(super magnum) is a sniper rifle with deadly accuracy.",
					 options => "You can give it a stand."
					),
             );

Link to comment
Share on other sites

I changed the results page to this:

<?php

$item_search = $_POST["item"];

if($key = array_search($item_search,$weapons)){
echo 'Item is ' . $weapons[$key];
echo '<br>';
echo $item_search;
}else{
echo 'not found';
echo '<br>';
echo $item_search;
}

?>

 

and it still says "not found" but it echo's whatever I typed in the search box fine.

 

by the way, I have the following include at the top of the results page:

/* includes */
include '/home/ace/public_html/conflictingforces/weapons.php';

Link to comment
Share on other sites

It's because $weapons is a multidimensional array, and array_search() works on a single array, therefore:

$weapons = array( array( id => 0,
					 weapon => "L85A2", 
					 price => 250,
					 damage => 15,
					 type => "Rifle",
					 rarity => "Basic",
					 description => "The L85A2 is a bullpup assault rifle, it is very powerful and a 
           				                 iron-sight comes standard.",
					 options => "You can give it a Laser."
					),
		   array( id => 1,
					 weapon => "AK-47", 
					 price => 325,
					 damage => 25,
					 type => "Rifle",
					 rarity => "Good",
					 description => "The AK-47(Kalashnikov) is a Russian made assault rifle, it is one of the most
						                 popular weapons in the world, and is also one of the most reliable.",
					 options => "You can give it a iron-sight."
					),
               array( id => 2,
					 weapon => "L96", 
					 price => 400,
					 damage => 30,
					 type => "Sniper Rifle",
					 rarity => "Good",
					 description => "The L96(super magnum) is a sniper rifle with deadly accuracy.",
					 options => "You can give it a stand."
					),
             );
             
$item_search = "Good";

foreach ($weapons as $w)
{
if($key = array_search($item_search,$w))
{
	echo 'Item is ' . $w[$key].'<br>';
}else{
	echo 'not found<br>';
}
}

Link to comment
Share on other sites

yep, I got that working now. for example I put "250" in, and it results in "Item is 250not foundnot found"

So how can I remove the 2 not founds that come up for the other 2 weapons, and how can I make it display all the properties of the weapon with the "250"(price) in it? and is it possible to display more then 1 weapon for example if I put in "Good"(from the rarity field) and make it create 2 tables, 1 for each weapon?

 

Regards ACE

Link to comment
Share on other sites

you could try this:

<?php
$item_search = $_POST["item"];

foreach($weapons as $key => $val){
if($key = array_search($item_search, $val)){
	$itm[] = $key;
}
}

if(isset($itm) && is_array($itm)){
foreach($itm as $k => $v){
	echo '<table border="0"><tr>';
	echo '<th>weapon</th><th>price</th><th>damage</th><th>type</th><th>rarity</th><th>description</th><th>options</th>';
	echo '</tr><tr>';
	echo '<td>' . $weapons[$v]['weapon'] . '</td>';
	echo '<td>' . $weapons[$v]['price'] . '</td>';
	echo '<td>' . $weapons[$v]['damage'] . '</td>';
	echo '<td>' . $weapons[$v]['type'] . '</td>';
	echo '<td>' . $weapons[$v]['rarity'] . '</td>';
	echo '<td>' . $weapons[$v]['description'] . '</td>';
	echo '<td>' . $weapons[$v]['options'] . '</td>';
	echo '</tr></table>';
}
}else{
echo 'not found';
}
?>

 

I haven't tested it but should work..

Link to comment
Share on other sites

okay, I've figured it out.. it's because I was treating array_search the same as in_array..

here you go (I've used in_array instead):

<?php
foreach($weapons as $key => $val){
if(in_array($item_search, $val)){
		echo '<table class="fix"><tr>';
		echo '<td colspan="2">WEAPON</td></tr>';
		echo '<tr><td>id</td><td>' . $weapons[$key]['id'] . '</td></tr>';
		echo '<tr><td>weapon</td><td>' . $weapons[$key]['weapon'] . '</td></tr>';
		echo '<tr><td>price</td><td>' . $weapons[$key]['price'] . '</td></tr>';
		echo '<tr><td>damage</td><td>' . $weapons[$key]['damage'] . '</td></tr>';
		echo '<tr><td>type</td><td>' . $weapons[$key]['type'] . '</td></tr>';
		echo '<tr><td>rarity</td><td>' . $weapons[$key]['rarity'] . '</td></tr>';
		echo '<tr><td>description</td><td>' . $weapons[$key]['description'] . '</td></tr>';
		echo '<tr><td>options</td><td>' . $weapons[$key]['options'] . '</td></tr>';
		echo '</table><br />';
}
}
?>

Although I must say I think your search function needs to be more accurate.. I hope you're not simply dragging the $_POST["item"] variable into use, without error checking it first.

Also an idea might be to allow the user to search for different aspects, such as a price search, or name search, instead of searching everything.

Have several input boxes for each searchable item, then check the search box name when finding the data.

Link to comment
Share on other sites

ok, I got that working nicely now, Thanks man, I was wondering though, how can I display "Not Found" if no array is found, and can I tune the search. for example, can I put radio buttons in, so the search can only look for items by their id, price, whatever, how would I implement that?(as you've mentioned above)

 

Regards ACE

Link to comment
Share on other sites

hi,

glad it's working!

to have a 'not found' message or whatever you want if nothings found just us an if else statement:

<?php
foreach($weapons as $key => $val){
if(in_array($item_search, $val)){
	$search = 1;
		echo '<table class="fix"><tr>';
		echo '<td colspan="2">WEAPON</td></tr>';
		echo '<tr><td>id</td><td>' . $weapons[$key]['id'] . '</td></tr>';
		echo '<tr><td>weapon</td><td>' . $weapons[$key]['weapon'] . '</td></tr>';
		echo '<tr><td>price</td><td>' . $weapons[$key]['price'] . '</td></tr>';
		echo '<tr><td>damage</td><td>' . $weapons[$key]['damage'] . '</td></tr>';
		echo '<tr><td>type</td><td>' . $weapons[$key]['type'] . '</td></tr>';
		echo '<tr><td>rarity</td><td>' . $weapons[$key]['rarity'] . '</td></tr>';
		echo '<tr><td>description</td><td>' . $weapons[$key]['description'] . '</td></tr>';
		echo '<tr><td>options</td><td>' . $weapons[$key]['options'] . '</td></tr>';
		echo '</table><br />';
}
}
if(!isset($search) || $search != 1){
echo 'not found'
}
?>

 

To add more options to your search criteria should be pretty simple.

If you have a series of checkboxes, one for each search criteria (price, name, type etc).

Then check which one is selected and search the array only in those sections.

Something like this may work:

<?php
$itm_cr = $_POST['checkbox']; // this gets the checkbox value
$item_search = $_POST["item"];

foreach($weapons as $key => $val){
if(in_array($item_search, $val[$itm_cr])){
	$search = 1;
		echo '<table class="fix"><tr>';
		echo '<td colspan="2">WEAPON</td></tr>';
		echo '<tr><td>id</td><td>' . $weapons[$key]['id'] . '</td></tr>';
		echo '<tr><td>weapon</td><td>' . $weapons[$key]['weapon'] . '</td></tr>';
		echo '<tr><td>price</td><td>' . $weapons[$key]['price'] . '</td></tr>';
		echo '<tr><td>damage</td><td>' . $weapons[$key]['damage'] . '</td></tr>';
		echo '<tr><td>type</td><td>' . $weapons[$key]['type'] . '</td></tr>';
		echo '<tr><td>rarity</td><td>' . $weapons[$key]['rarity'] . '</td></tr>';
		echo '<tr><td>description</td><td>' . $weapons[$key]['description'] . '</td></tr>';
		echo '<tr><td>options</td><td>' . $weapons[$key]['options'] . '</td></tr>';
		echo '</table><br />';
}
}
if(!isset($search) || $search != 1){
echo 'not found'
}
?>

 

 

EDIT: just changed the 'not found' code, because otherwise you'd have recieved a 'not found' message for every array that doesn't contain the search criteria..

instead of an else statement I've used an if statement to check if a variable is set. If not it outouts 'not found'. The variable $search is only set if a weapon is found in the search

Link to comment
Share on other sites

I've set up the check box's, and I ticked the "id" check box, and put "1" into the text box and pressed the "search button" and I received the following error:

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/ace/public_html/conflictingforces/lib/encyclopedia_search.php on line 29

 

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/ace/public_html/conflictingforces/lib/encyclopedia_search.php on line 29

 

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/ace/public_html/conflictingforces/lib/encyclopedia_search.php on line 29

 

Line 29:

	if(in_array($item_search, $val[$item_cr])){

Link to comment
Share on other sites

yeah, im alittle lost in the moment, heres what I got:

<?php
$item_cr = $_POST['checkbox']; // this gets the checkbox value
$item_search = $_POST["item"];

foreach($weapons as $key => $val){
if($item_search == $val[$item_cr]){
	$search = 1;
		echo '<table class="fix"><tr>';
		echo '<td colspan="2">WEAPON</td></tr>';
		echo '<tr><td>id</td><td>' . $weapons[$key]['id'] . '</td></tr>';
		echo '<tr><td>weapon</td><td>' . $weapons[$key]['weapon'] . '</td></tr>';
		echo '<tr><td>price</td><td>' . $weapons[$key]['price'] . '</td></tr>';
		echo '<tr><td>damage</td><td>' . $weapons[$key]['damage'] . '</td></tr>';
		echo '<tr><td>type</td><td>' . $weapons[$key]['type'] . '</td></tr>';
		echo '<tr><td>rarity</td><td>' . $weapons[$key]['rarity'] . '</td></tr>';
		echo '<tr><td>description</td><td>' . $weapons[$key]['description'] . '</td></tr>';
		echo '<tr><td>options</td><td>' . $weapons[$key]['options'] . '</td></tr>';
		echo '</table><br />';
}

}
if(!isset($search) || $search != 1){
echo "not found<br><br>";
}

?>

 

and the previous page with the form is this:

<form name="search" method="post" action="index.php?page=encyclopedia_search">
item: 	<input type="text" size="15" maxlength="80" name="item">
	<input type="submit" value="Search">

id:	  	<input type="checkbox" name="id">
weapon: <input type="checkbox" name="weapon">
price:	<input type="checkbox" name="price">
damage:	<input type="checkbox" name="damage">
type:	<input type="checkbox" name="type">
rarity:	<input type="checkbox" name="rarity">

</form>

Link to comment
Share on other sites

The problem is with your checkboxes.

I've just realised you should use radio instead ::)

They should all have the same name (which also means that only one radio can be selected at a time), but set the values to what you currently have as the names:

id:	<input type="radio" name="cat" value="id">
weapon:	<input type="radio" name="cat" value="weapon">
price:	<input type="radio" name="cat" value="price">
damage:	<input type="radio" name="cat" value="damage">
type:	<input type="radio" name="cat" value="type">
rarity:	<input type="radio" name="cat" value="rarity">

 

then on the other page, change this:

$item_cr = $_POST['checkbox'];

to this:

$item_cr = $_POST['cat'];

 

give that a try :)

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.