MasterACE14 Posted September 16, 2007 Share Posted September 16, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/69554-solved-echoing-an-array-that-has-been-searched-for/ Share on other sites More sharing options...
Dragen Posted September 16, 2007 Share Posted September 16, 2007 try something like this: <?php $item_search = $_POST["item"]; if($key = array_search($item_search,$weapons)){ echo 'Item is ' . $weapons[$key]; }else{ echo 'not found'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/69554-solved-echoing-an-array-that-has-been-searched-for/#findComment-349496 Share on other sites More sharing options...
MasterACE14 Posted September 16, 2007 Author Share Posted September 16, 2007 I keep getting "not found" now, I've tried different values, both string and integer from my 2 dimensional array. Quote Link to comment https://forums.phpfreaks.com/topic/69554-solved-echoing-an-array-that-has-been-searched-for/#findComment-349498 Share on other sites More sharing options...
Dragen Posted September 16, 2007 Share Posted September 16, 2007 try echoing $item_search. It may be that you've got magic quotes or something and it's escaping characters. Also what does the $weapons array look like? Quote Link to comment https://forums.phpfreaks.com/topic/69554-solved-echoing-an-array-that-has-been-searched-for/#findComment-349499 Share on other sites More sharing options...
MasterACE14 Posted September 16, 2007 Author Share Posted September 16, 2007 $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." ), ); Quote Link to comment https://forums.phpfreaks.com/topic/69554-solved-echoing-an-array-that-has-been-searched-for/#findComment-349502 Share on other sites More sharing options...
MasterACE14 Posted September 16, 2007 Author Share Posted September 16, 2007 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'; Quote Link to comment https://forums.phpfreaks.com/topic/69554-solved-echoing-an-array-that-has-been-searched-for/#findComment-349504 Share on other sites More sharing options...
Dragen Posted September 16, 2007 Share Posted September 16, 2007 ah.. probably because it's a multi-array.. try doing something like this: <?php $item_search = $_POST["item"]; foreach($weapons as $key => $val){ if($key = array_search($item_search, $val)){ echo 'Item is ' . $val[$key]; }else{ echo 'not found'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/69554-solved-echoing-an-array-that-has-been-searched-for/#findComment-349506 Share on other sites More sharing options...
rarebit Posted September 16, 2007 Share Posted September 16, 2007 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>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/69554-solved-echoing-an-array-that-has-been-searched-for/#findComment-349507 Share on other sites More sharing options...
MasterACE14 Posted September 16, 2007 Author Share Posted September 16, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/69554-solved-echoing-an-array-that-has-been-searched-for/#findComment-349510 Share on other sites More sharing options...
rarebit Posted September 16, 2007 Share Posted September 16, 2007 Remove the else statement (as so not to display the not founds), and use your display table feature from earlier to display the full result... it'll all be in $w Quote Link to comment https://forums.phpfreaks.com/topic/69554-solved-echoing-an-array-that-has-been-searched-for/#findComment-349514 Share on other sites More sharing options...
Dragen Posted September 16, 2007 Share Posted September 16, 2007 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.. Quote Link to comment https://forums.phpfreaks.com/topic/69554-solved-echoing-an-array-that-has-been-searched-for/#findComment-349515 Share on other sites More sharing options...
MasterACE14 Posted September 16, 2007 Author Share Posted September 16, 2007 That displays everything but the actual values for the array. :-\ here's the link so you can see for yourself if you like: http://www.crikeygames.com.au/conflictingforces/index.php?page=encyclopedia Quote Link to comment https://forums.phpfreaks.com/topic/69554-solved-echoing-an-array-that-has-been-searched-for/#findComment-349522 Share on other sites More sharing options...
rarebit Posted September 16, 2007 Share Posted September 16, 2007 What language are you writing your games in? (sorry it's a bit off topic1) Quote Link to comment https://forums.phpfreaks.com/topic/69554-solved-echoing-an-array-that-has-been-searched-for/#findComment-349523 Share on other sites More sharing options...
MasterACE14 Posted September 16, 2007 Author Share Posted September 16, 2007 PHP, just text browser based MMORPG's. Quote Link to comment https://forums.phpfreaks.com/topic/69554-solved-echoing-an-array-that-has-been-searched-for/#findComment-349524 Share on other sites More sharing options...
MasterACE14 Posted September 16, 2007 Author Share Posted September 16, 2007 I got to get to bed guys, I really appreciate all the help you 2 have given me , if you figure out why its not displaying the values please post it in this thread, I'll check it tommorow afternoon. Thanks again guys, cya's later. Regards ACE Quote Link to comment https://forums.phpfreaks.com/topic/69554-solved-echoing-an-array-that-has-been-searched-for/#findComment-349529 Share on other sites More sharing options...
Dragen Posted September 16, 2007 Share Posted September 16, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/69554-solved-echoing-an-array-that-has-been-searched-for/#findComment-349537 Share on other sites More sharing options...
MasterACE14 Posted September 19, 2007 Author Share Posted September 19, 2007 was it just me, or was PHP Freaks down for a couple days? by the way, I still cant get the search page to display the found arrays. Quote Link to comment https://forums.phpfreaks.com/topic/69554-solved-echoing-an-array-that-has-been-searched-for/#findComment-351032 Share on other sites More sharing options...
MasterACE14 Posted September 19, 2007 Author Share Posted September 19, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/69554-solved-echoing-an-array-that-has-been-searched-for/#findComment-351037 Share on other sites More sharing options...
Dragen Posted September 19, 2007 Share Posted September 19, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/69554-solved-echoing-an-array-that-has-been-searched-for/#findComment-351176 Share on other sites More sharing options...
MasterACE14 Posted September 21, 2007 Author Share Posted September 21, 2007 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])){ Quote Link to comment https://forums.phpfreaks.com/topic/69554-solved-echoing-an-array-that-has-been-searched-for/#findComment-352169 Share on other sites More sharing options...
Dragen Posted September 21, 2007 Share Posted September 21, 2007 try changing this: if(in_array($item_search, $val[$itm_cr])){ to this: if($item_search == $val[$itm_cr]){ That simply checks that the text you've entered matches the variable for the checkbox you've selected. Quote Link to comment https://forums.phpfreaks.com/topic/69554-solved-echoing-an-array-that-has-been-searched-for/#findComment-352624 Share on other sites More sharing options...
MasterACE14 Posted September 22, 2007 Author Share Posted September 22, 2007 with that I keep getting "not found" no matter what I type in, and no matter what checkbox's I tick :-\ Quote Link to comment https://forums.phpfreaks.com/topic/69554-solved-echoing-an-array-that-has-been-searched-for/#findComment-352723 Share on other sites More sharing options...
Dragen Posted September 22, 2007 Share Posted September 22, 2007 The values of the checkboxes need to be the same as the array keys Quote Link to comment https://forums.phpfreaks.com/topic/69554-solved-echoing-an-array-that-has-been-searched-for/#findComment-352868 Share on other sites More sharing options...
MasterACE14 Posted September 23, 2007 Author Share Posted September 23, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/69554-solved-echoing-an-array-that-has-been-searched-for/#findComment-353236 Share on other sites More sharing options...
Dragen Posted September 24, 2007 Share Posted September 24, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/69554-solved-echoing-an-array-that-has-been-searched-for/#findComment-353936 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.