littlehelp Posted July 10, 2009 Share Posted July 10, 2009 ok what i have is a switch statement through ajax that i want to make dependent to the query from the database, so like this for the switch statement function doIt($it) { switch ($it) { while($row=mysql_fetch_array($res)){//i want to use a wile loop this does not work case '$count'://i want to use a counter and increment the counter each time through return "<div style=\"width: 100px; height: 84px; text-align: center; float: left;\"><img src=\"images/pokemon_sprites/001.png\" width=\"100\" height=\"100\" /></div><div style=\"overflow: hidden; width: 100px; float: left; padding-top: 32px; line-height: normal;\"><strong>Bulbasaur</strong><br><span class=\"smallVerdana\">Level 1<br /><div class=\"clear\"><img src=\"battle_files/spacer.gif\" alt=\"sp\"></div> <div style=\"width: 100px; text-align: left;\"> <div class=\"hpBar\" style=\"width: 100%;\"><img src=\"battle_files/spacer.gif\" alt=\"sp\" style=\"height: 12px;\"></div>45/45 Hit Points<br /><div class=\"clear\"><img src=\"battle_files/spacer.gif\" alt=\"sp\"></div></div>";//'.$i[iD].'. break; } } } and then output each switch depending on the number of results from the batabase and increment it, hopefully i am saying this correctly. Thank you in advance Quote Link to comment https://forums.phpfreaks.com/topic/165513-solved-php-switch-statement-with-a-while-loop/ Share on other sites More sharing options...
ignace Posted July 10, 2009 Share Posted July 10, 2009 hopefully i am saying this correctly Where does $res and $count come from? <?php function doIt($it) { switch ($it) { case '$count'://i want to use a counter and increment the counter each time through while($row=mysql_fetch_array($res)){//i want to use a wile loop this does not work return "<div style=\"width: 100px; height: 84px; text-align: center; float: left;\"><img src=\"images/pokemon_sprites/001.png\" width=\"100\" height=\"100\" /></div><div style=\"overflow: hidden; width: 100px; float: left; padding-top: 32px; line-height: normal;\"><strong>Bulbasaur</strong><br><span class=\"smallVerdana\">Level 1<br /><div class=\"clear\"><img src=\"battle_files/spacer.gif\" alt=\"sp\"></div> <div style=\"width: 100px; text-align: left;\"> <div class=\"hpBar\" style=\"width: 100%;\"><img src=\"battle_files/spacer.gif\" alt=\"sp\" style=\"height: 12px;\"></div>45/45 Hit Points<br /><div class=\"clear\"><img src=\"battle_files/spacer.gif\" alt=\"sp\"></div></div>";//'.$i[iD].'. } break; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/165513-solved-php-switch-statement-with-a-while-loop/#findComment-872974 Share on other sites More sharing options...
littlehelp Posted July 10, 2009 Author Share Posted July 10, 2009 Where does $res and $count come from? They are variables above the switch that are of no real consequence since i am asking about the switch, if you need to see them i can show them to you. Let me know thanks Quote Link to comment https://forums.phpfreaks.com/topic/165513-solved-php-switch-statement-with-a-while-loop/#findComment-872976 Share on other sites More sharing options...
Mchl Posted July 10, 2009 Share Posted July 10, 2009 I have no idea what you're trying to do. switch does not work like this. Quote Link to comment https://forums.phpfreaks.com/topic/165513-solved-php-switch-statement-with-a-while-loop/#findComment-872978 Share on other sites More sharing options...
littlehelp Posted July 10, 2009 Author Share Posted July 10, 2009 Ok let me try to be more concise, what i trying to do is have a switch on the bottom that has varying lengths, such as if the database only has 1 result there will only be one there, if it has 2 then there will be 2 switches ... and so on to 6. If you need more info i can pm you some information and you can look at it in action. Quote Link to comment https://forums.phpfreaks.com/topic/165513-solved-php-switch-statement-with-a-while-loop/#findComment-872984 Share on other sites More sharing options...
Mchl Posted July 10, 2009 Share Posted July 10, 2009 IMHO you don't need switch but something else. An array perhaps? Quote Link to comment https://forums.phpfreaks.com/topic/165513-solved-php-switch-statement-with-a-while-loop/#findComment-872990 Share on other sites More sharing options...
aggrav8d Posted July 10, 2009 Share Posted July 10, 2009 Technically you can put statements between the switch() and the first case:. Realistically, nobody does it. Quote Link to comment https://forums.phpfreaks.com/topic/165513-solved-php-switch-statement-with-a-while-loop/#findComment-873011 Share on other sites More sharing options...
littlehelp Posted July 10, 2009 Author Share Posted July 10, 2009 I know that does not work, i was showing what i was trying to do is all. Quote Link to comment https://forums.phpfreaks.com/topic/165513-solved-php-switch-statement-with-a-while-loop/#findComment-873016 Share on other sites More sharing options...
littlehelp Posted July 10, 2009 Author Share Posted July 10, 2009 I have never used a switch with an array can you show me an example or a tutorial? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/165513-solved-php-switch-statement-with-a-while-loop/#findComment-873076 Share on other sites More sharing options...
wildteen88 Posted July 10, 2009 Share Posted July 10, 2009 Ok let me try to be more concise, what i trying to do is have a switch on the bottom that has varying lengths, such as if the database only has 1 result there will only be one there, if it has 2 then there will be 2 switches ... and so on to 6. If you need more info i can pm you some information and you can look at it in action. From that you do not need to be using a switch at all. You need to provide a more thorough explaination of what you're. such as what is in your database? What are you wanting to do with the results etc? Quote Link to comment https://forums.phpfreaks.com/topic/165513-solved-php-switch-statement-with-a-while-loop/#findComment-873084 Share on other sites More sharing options...
littlehelp Posted July 10, 2009 Author Share Posted July 10, 2009 From that you do not need to be using a switch at all. You need to provide a more thorough explaination of what you're. such as what is in your database? What are you wanting to do with the results etc? I sent you a pm with a more helpful explanation. Quote Link to comment https://forums.phpfreaks.com/topic/165513-solved-php-switch-statement-with-a-while-loop/#findComment-873137 Share on other sites More sharing options...
littlehelp Posted July 11, 2009 Author Share Posted July 11, 2009 Let me try to explain in more detail what i am doing. I have a div at the top of my page that has a pokemon in it that was selected through the use of ajax. On the bottom i have a selection of pokemon(depending on how far you are in the game and how many you have caught) up to 6. So i am trying to get the information from the database on the pokemon that the user owns so they can be switched in and out of battle. The info in the db is... their name, hp, xp,level and stats. I am using the results in the switch to select which one is in battle. Hopefully this answers all of your questions wildteen. As you see the switch is working correctly(without the use of the db and statically assigning the information of the pokemon). Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/165513-solved-php-switch-statement-with-a-while-loop/#findComment-873255 Share on other sites More sharing options...
littlehelp Posted July 13, 2009 Author Share Posted July 13, 2009 Bump Quote Link to comment https://forums.phpfreaks.com/topic/165513-solved-php-switch-statement-with-a-while-loop/#findComment-874326 Share on other sites More sharing options...
Mchl Posted July 13, 2009 Share Posted July 13, 2009 No point in bumping if you don't post some more code of yours. Quote Link to comment https://forums.phpfreaks.com/topic/165513-solved-php-switch-statement-with-a-while-loop/#findComment-874408 Share on other sites More sharing options...
littlehelp Posted July 13, 2009 Author Share Posted July 13, 2009 And as i stated in the pm to you, tell me what it is exactly that you need to see so i don't post needless code and i would be happy to. Quote Link to comment https://forums.phpfreaks.com/topic/165513-solved-php-switch-statement-with-a-while-loop/#findComment-874729 Share on other sites More sharing options...
Mchl Posted July 13, 2009 Share Posted July 13, 2009 I guess the switch code as you have it now without database would be good for start. Quote Link to comment https://forums.phpfreaks.com/topic/165513-solved-php-switch-statement-with-a-while-loop/#findComment-874833 Share on other sites More sharing options...
littlehelp Posted July 13, 2009 Author Share Posted July 13, 2009 This is the switch.php <div id="pokemonwitch"> <div id="options"> <a href="ajaxswitch.php?content=1"><div class="pokemonSlot" title="header=[<font class='detailsBoxHeader'> <img src='images/pokemon_sprites_small/001.gif' alt='Bulbasaur' style='vertical-align:middle;' /> Bulbasaurs's Details...</font>] body=[<font class='detailsBoxContent'> Unique #: 1 <br /> Experience: 0<br /> Level: 1<br /> HP: 45/45<br /> Status: normal<br /> Att. 1: Horn Attack - PP Left: 25/25<br /> Att. 2: Tail Whip - PP Left: 30/30<br /> Att. 3: Stomp - PP Left: 20/20<br /> Att. 4: Fury Attack - PP Left: 20/20<br /> Attached Item: <i>none</i><br /> Available evolve: <i>none</i> </font>]"><div class="pokemonSlot"><img src="images/pokemon_sprites/001.png" alt="Bulbasaur" title="Bulbasaur"></div> <div class="clear"><img src="battle_files/spacer.gif" alt="sp"></div> <div class="pokemonSlot"><label for="battleTeam1"> <strong>Bulbasaur</strong><br><span class="smallVerdana">Level 1<br>45/45 HP</span></label></div></div></a> <a href="ajaxswitch.php?content=2"><div class="pokemonSlot" title="header=[<font class='detailsBoxHeader'> <img src='images/pokemon_sprites_small/002.gif' alt='Ivysaur' style='vertical-align:middle;' /> Venasaur's Details...</font>] body=[<font class='detailsBoxContent'> Unique #: 1 <br /> Experience: 0<br /> Level: 1<br /> HP: 60/60<br /> Status: normal<br /> Att. 1: Horn Attack - PP Left: 25/25<br /> Att. 2: Tail Whip - PP Left: 30/30<br /> Att. 3: Stomp - PP Left: 20/20<br /> Att. 4: Fury Attack - PP Left: 20/20<br /> Attached Item: <i>none</i><br /> Available evolve: <i>none</i> </font>]"><div class="pokemonSlot"><img src="images/pokemon_sprites/002.png" alt="Venasaur" title="Venasaur"></div> <div class="clear"><img src="battle_files/spacer.gif" alt="sp"></div> <div class="pokemonSlot"><label for="battleTeam1"> <strong>Venasaur</strong><br><span class="smallVerdana">Level 1<br>60/60 HP</span></label></div></div></a> <a href="ajaxswitch.php?content=3"><div class="pokemonSlot" title="header=[<font class='detailsBoxHeader'> <img src='images/pokemon_sprites_small/003.gif' alt='Ivysaur' style='vertical-align:middle;' /> Ivysaurs's Details...</font>] body=[<font class='detailsBoxContent'> Unique #: 1 <br /> Experience: 0<br /> Level: 1<br /> HP: 80/80<br /> Status: normal<br /> Att. 1: Horn Attack - PP Left: 25/25<br /> Att. 2: Tail Whip - PP Left: 30/30<br /> Att. 3: Stomp - PP Left: 20/20<br /> Att. 4: Fury Attack - PP Left: 20/20<br /> Attached Item: <i>none</i><br /> Available evolve: <i>none</i> </font>]"><div class="pokemonSlot"><img src="images/pokemon_sprites/003.png" alt="Ivysaur" title="Ivysaur"></div> <div class="clear"><img src="battle_files/spacer.gif" alt="sp"></div> <div class="pokemonSlot"><label for="battleTeam1"> <strong>Venasaur</strong><br><span class="smallVerdana">Level 1<br>80/80 HP</span></label></div></div></a> </div> </div> so this is as if they had only 3 pokemon. And this is the content.php. function doIt($it) { switch ($it) { case "1": return "<div style=\"width: 100px; height: 84px; text-align: center; float: left;\"><img src=\"images/pokemon_sprites/001.png\" width=\"100\" height=\"100\" /></div><div style=\"overflow: hidden; width: 100px; float: left; padding-top: 32px; line-height: normal;\"><strong>Bulbasaur</strong><br><span class=\"smallVerdana\">Level 1<br /><div class=\"clear\"><img src=\"battle_files/spacer.gif\" alt=\"sp\"></div> <div style=\"width: 100px; text-align: left;\"> <div class=\"hpBar\" style=\"width: 100%;\"><img src=\"battle_files/spacer.gif\" alt=\"sp\" style=\"height: 12px;\"></div>45/45 Hit Points<br /><div class=\"clear\"><img src=\"battle_files/spacer.gif\" alt=\"sp\"></div></div>"; break; case "2": return "<p><div style=\"width: 100px; height: 84px; text-align: center; float: left;\"><img src=\"images/pokemon_sprites/002.png\" width=\"100\" height=\"100\" /></div><div style=\"overflow: hidden; width: 100px; float: left; padding-top: 32px; line-height: normal;\"><strong>Ivysaur</strong><br><span class=\"smallVerdana\">Level 1<br /><div class=\"clear\"><img src=\"battle_files/spacer.gif\" alt=\"sp\"></div> <div style=\"width: 100px; text-align: left;\"> <div class=\"hpBar\" style=\"width: 100%;\"><img src=\"battle_files/spacer.gif\" alt=\"sp\" style=\"height: 12px;\"></div>60/60 Hit Points<br /><div class=\"clear\"><img src=\"battle_files/spacer.gif\" alt=\"sp\"></div></div>"; break; case "3": return "<p><div style=\"width: 100px; height: 84px; text-align: center; float: left;\"><img src=\"images/pokemon_sprites/003.png\" width=\"100\" height=\"100\" /></div><div style=\"overflow: hidden; width: 100px; float: left; padding-top: 32px; line-height: normal;\"><strong>Venasaur</strong><br><span class=\"smallVerdana\">Level 1<br /><div class=\"clear\"><img src=\"battle_files/spacer.gif\" alt=\"sp\"></div> <div style=\"width: 100px; text-align: left;\"> <div class=\"hpBar\" style=\"width: 100%;\"><img src=\"battle_files/spacer.gif\" alt=\"sp\" style=\"height: 12px;\"></div>80/80 Hit Points<br /><div class=\"clear\"><img src=\"battle_files/spacer.gif\" alt=\"sp\"></div></div>"; break; } } thanks alot for your help. Let me know if you need additional information. The hp xp level name and id# which is the 00X will be pulled from the db which is already setup. Quote Link to comment https://forums.phpfreaks.com/topic/165513-solved-php-switch-statement-with-a-while-loop/#findComment-874865 Share on other sites More sharing options...
littlehelp Posted July 17, 2009 Author Share Posted July 17, 2009 I want to try to give just a little more info so i am not posting for no reason. What i am doing is pulling UP TO 6 records from a mysql database then outputting the information so that when it is clicked it will be moved to the top without refreshing the whole page, i posted the switch i am using to do it with ajax. I hope it has been explained thuroughly enough if not tell me what else you need and i will be happy to respond. Thank you everyone. Quote Link to comment https://forums.phpfreaks.com/topic/165513-solved-php-switch-statement-with-a-while-loop/#findComment-876800 Share on other sites More sharing options...
littlehelp Posted July 17, 2009 Author Share Posted July 17, 2009 Nevermind i fixed it myself by using an onClick event, it seems it was a very easy fix after some trial and error. Quote Link to comment https://forums.phpfreaks.com/topic/165513-solved-php-switch-statement-with-a-while-loop/#findComment-876858 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.