djfox Posted April 9, 2009 Share Posted April 9, 2009 Let`s say I have: $res2 = mysql_query("SELECT id, evolve_next, evolve_level, name FROM pokedex WHERE id='$poke[1]'"); $pokemon = mysql_fetch_row($res2); mysql_free_result($res2); And I want PHP to check to see if , exists in $pokemon[1]. If it does, do one thing. If not, do something else. If it does exist, how it could separate the elements that are between the commas (there may just one or more than one comma) and have an option for each of those. Example: If $pokemon[1] has "Vaporeon,Jolteon,Flareon", then be like: Vaporeon: Do whatever Jolteon: Do whatever else Flareon: Do something different or if $pokemon[1] has "Pikachu", then do other stuff. How could this be accomplished? Quote Link to comment https://forums.phpfreaks.com/topic/153259-solved-make-a-if-like-statement/ Share on other sites More sharing options...
Maq Posted April 9, 2009 Share Posted April 9, 2009 To separate things you can use explode. Something like: $pokemon = "Vaporeon"; $pieces = expode(",", $pokemon[1]); if(in_array($pokemon, $pieces) { echo "This record has " . $pokemon . "!"; } Quote Link to comment https://forums.phpfreaks.com/topic/153259-solved-make-a-if-like-statement/#findComment-805115 Share on other sites More sharing options...
djfox Posted April 9, 2009 Author Share Posted April 9, 2009 Wouldn`t this mean I would have to make several $pokemon statements for it to check for each and every pokemon name that might be in the list? That would be extremely time consuming to setup, not to mention make the file huge since we`re looking at over 550 names to work with. isn`t there a tweaking to that code we could do to remove the massive workload? Quote Link to comment https://forums.phpfreaks.com/topic/153259-solved-make-a-if-like-statement/#findComment-805123 Share on other sites More sharing options...
.josh Posted April 9, 2009 Share Posted April 9, 2009 If you want to have your script do something different for each name, then you are going to have to specifically address each name. Quote Link to comment https://forums.phpfreaks.com/topic/153259-solved-make-a-if-like-statement/#findComment-805130 Share on other sites More sharing options...
djfox Posted April 9, 2009 Author Share Posted April 9, 2009 You sure? What this is addressing is the evolution stages that the pokemon would take place. Some pokemon have multiple others they can evolve into while others have just one. I can do one where it`s just the single choice, but the multiple choices I can`t figure out how to work it into a simple not-spend-a-year-coding-for-it code because there are so many of them. Quote Link to comment https://forums.phpfreaks.com/topic/153259-solved-make-a-if-like-statement/#findComment-805146 Share on other sites More sharing options...
Maq Posted April 9, 2009 Share Posted April 9, 2009 Can I see your form? Quote Link to comment https://forums.phpfreaks.com/topic/153259-solved-make-a-if-like-statement/#findComment-805148 Share on other sites More sharing options...
djfox Posted April 9, 2009 Author Share Posted April 9, 2009 <?php $res = mysql_query("SELECT id, species, level FROM pokemon_owned WHERE id='$p'"); $poke = mysql_fetch_row($res); mysql_free_result($res); $res2 = mysql_query("SELECT id, evolve_next, evolve_level, name FROM pokedex WHERE id='$poke[1]'"); $pokemon = mysql_fetch_row($res2); mysql_free_result($res2); $res3 = mysql_query("SELECT id, name FROM pokedex WHERE name='$pokemon[1]'"); $e = mysql_fetch_row($res3); mysql_free_result($res3); echo "You can evolve your pokemon into a different pokemon in which it could gain new strengths. However, this cannot be undone."; echo " Make certain that you are absolutely sure that you want to evolve this pokemon before clicking the button below. If you"; echo " would to know what kind of pokemon yours will change into from evolving, you can view the pokedex. If your"; echo " pokemon is not a high enough level to evolve, you will see what level your pokemon needs to be before it can evolve.<p>"; if ($pokemon[1]) { if ($poke[2] >= $pokemon[2]) { echo "You can evolve your $pokemon[3] into $e[1]. Click the button below to evolve it."; echo "<form action='evolve.php' method='post'>"; echo "<input type='hidden' name='pok' value='$p'><input type='hidden' name='ev' value='$e[0]'>"; echo "<p><input type='image' src='$butnext' name='submit'></form>"; } else { echo "Your pokemon is not a high enough level. Your pokemon must be at level $pokemon[2] before it can evolve."; } } elseif (!$pokemon[1]) { echo "This pokemon does not evolve."; } ?> This is how it is currently, where right now it only works if the pokemon has just one form to evolve into (it does not work for multiple forms). (Example - It will work for Pikachu because Pikachu`s only choice is Raichu. But it won`t work for Eevee because has 7 forms to choose from.) Quote Link to comment https://forums.phpfreaks.com/topic/153259-solved-make-a-if-like-statement/#findComment-805156 Share on other sites More sharing options...
djfox Posted April 9, 2009 Author Share Posted April 9, 2009 No one has any idea, huh? XD It is a bit of a stumper. Quote Link to comment https://forums.phpfreaks.com/topic/153259-solved-make-a-if-like-statement/#findComment-805448 Share on other sites More sharing options...
djfox Posted April 9, 2009 Author Share Posted April 9, 2009 Wait, wait, I got it. <?php $pieces = explode(",", $pokemon[1]); foreach ($pieces as $evo) { $res3b = mysql_query("SELECT id, name FROM pokedex WHERE name='$evo'"); $e2 = mysql_fetch_row($res3b); mysql_free_result($res3b); if ($e2[1]) { echo "<input type='radio' name='ev' value='$e2[0]'> $e2[1] <br>"; } ?> I should`ve seen this sooner. XD This works for both single choices and multiple choices. Quote Link to comment https://forums.phpfreaks.com/topic/153259-solved-make-a-if-like-statement/#findComment-805472 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.