Chezshire Posted August 6, 2008 Share Posted August 6, 2008 Hi, I'm new to PHP and don't know a lot, but I do know that when I hit something bigger then myself to come ask the experts for help and guidance. I've learned a lot from all of you and I'm hoping to get a little more learnin' tonight Problem: I've implemented a Switch Statement, the statement appears to prevent my PHP action form working. Here is an example of the code without the switch statement http://www.xpg.us/institute/institute1.2.php Here is what happens once the switch statement is added http://www.xpg.us/institute/institute1.4.php Here is the PHP code - any ideas of what I screwed up? <TD width='124' align='center'><p><a href='/images/XPGmanhattan.jpg' title="Download a map of Manhattan's amazing sites and sounds!"><img src="/images/XPGmanhattan_sm.jpg" alt="Brochure" width="124" height="250" border="0"><span class='fineprint'>See Manhattan</span></a> </TR> <TR> <TD COLSPAN="3" align="left"> <?php //title echo "<br>\n<h2><font color='#A5B9D5'>TRAINING SQUADRONS</font></h2>\n"; //get characters by squadron $sql = "SELECT id, squadronDB, codename FROM cerebra ORDER BY squadronDB"; $result = mysql_query($sql) or trigger_error(mysql_error,E_USER_ERROR); while($row = mysql_fetch_assoc($result)){ //here's the key. Use a while loop to repeat through the code $thisID = $codename["id"]; $squadronDB = getSquad($thisID); $squadronDB = getChar($char_ID); switch ($i) { case "1": echo "i is Alpha"; break; case "2": echo "i is Alpha"; break; case "3": echo "i is Alpha"; break; case "4": echo "i is Alpha"; break; // start character div echo '<div style="float: left; padding-top: 5; width: 220;">'; echo "<span class='fineprint'>"; // makes thin bar echo '<hr align="centered" color="#050F1D" size="2" width="66%">'; // image div echo '<div style="float: left; padding-left: 5;"><img src="/cerebra/images/'.$row['id'].'-.jpg" width="60" height="75" border="2px" bordercolor="#415582"></div>'; // name div // styles echo '<div style="float: left; vertical-align: top; padding-left: 5;"><p>'; // span and model name echo '<span class="fineprint"><i>'.$row['squadronDB']; // link and codename echo ' <br>as <a href="/cerebra/display.php?id='.$row['id'].'">'.$row['codename'].'</a></i></span></p></div>'; // end character div echo '</div>'; //these two lines (or any lines up to the closing brace) will be repeated for each row returned by the query } } // You forgot to close your while. ?> </TD> </TR> </TABLE> </DIV> Link to comment https://forums.phpfreaks.com/topic/118396-display-issue-due-to-code-conflict-with-switch-statement-help/ Share on other sites More sharing options...
Third_Degree Posted August 6, 2008 Share Posted August 6, 2008 $i is never defined... Link to comment https://forums.phpfreaks.com/topic/118396-display-issue-due-to-code-conflict-with-switch-statement-help/#findComment-609349 Share on other sites More sharing options...
Chezshire Posted August 6, 2008 Author Share Posted August 6, 2008 Oh, That sounds so simple when you say it Thanks for the pointer, if i can get a few more tips that would be appreciated. I tried the following, but it didn't work: switch ($i="squadronDB") { And switch ($i=="squadronDB") { and switch ($i == "squadronDB") { So i'm not sure how to define it, any suggestions? This is a variable i want to define correct? I'm currently googling away on defining 'variables' in search of the solution. Thanks! Link to comment https://forums.phpfreaks.com/topic/118396-display-issue-due-to-code-conflict-with-switch-statement-help/#findComment-609365 Share on other sites More sharing options...
Third_Degree Posted August 6, 2008 Share Posted August 6, 2008 I think you mean this? $i = $squadronDB; switch($i) { Link to comment https://forums.phpfreaks.com/topic/118396-display-issue-due-to-code-conflict-with-switch-statement-help/#findComment-609370 Share on other sites More sharing options...
Chezshire Posted August 6, 2008 Author Share Posted August 6, 2008 Thank you for the suggestion Third_Degree, I tried: $i = $squadronDB; switch($i) { I also tried: switch ("squadronDB"=="$i") { case "1": echo "i is Alpha"; Any other ideas? I'm all ears (eyes?) thanks! Link to comment https://forums.phpfreaks.com/topic/118396-display-issue-due-to-code-conflict-with-switch-statement-help/#findComment-609384 Share on other sites More sharing options...
MasterACE14 Posted August 6, 2008 Share Posted August 6, 2008 your not closing the switch? switch ($i) { case "1": echo "i is Alpha"; break; case "2": echo "i is Alpha"; break; case "3": echo "i is Alpha"; break; case "4": echo "i is Alpha"; break; } // missing this parenthesis <<<< Link to comment https://forums.phpfreaks.com/topic/118396-display-issue-due-to-code-conflict-with-switch-statement-help/#findComment-609388 Share on other sites More sharing options...
wildteen88 Posted August 6, 2008 Share Posted August 6, 2008 Thank you for the suggestion Third_Degree, I tried: $i = $squadronDB; switch($i) { I also tried: switch ("squadronDB"=="$i") { case "1": echo "i is Alpha"; Any other ideas? I'm all ears (eyes?) thanks! I suggest you read up on how to use swtich statements as your attempts will never work. Your switch statement is expecting a value between 1 and 4, depending on the value it'll display a message (which seems to be the same message no matter what the value is). The value can be any variable, you dont need to use $i. What does the $squadronDB variable contain? Link to comment https://forums.phpfreaks.com/topic/118396-display-issue-due-to-code-conflict-with-switch-statement-help/#findComment-609524 Share on other sites More sharing options...
Chezshire Posted August 7, 2008 Author Share Posted August 7, 2008 Thanks for the advice, I am reading up on switch statements and a lot of other things too. But there is a lot of stuff to take in still which is why i come here and ask questions. As I tried to explain above, the value comes from the squardonDB variable which delivers a value between 0 and 24 which determines what squad they are on, I'm simply trying to get the first to work (1 through 4 which equals Alpha, 5-8 equals Beta, ect). with the hope that if i can get that one to work i can finish the rest off. Now based on what I've read so far on switch statements, I do need the '$i', although it's not working. I'm looking to make the code work so if you can help it's appreciated. Link to comment https://forums.phpfreaks.com/topic/118396-display-issue-due-to-code-conflict-with-switch-statement-help/#findComment-610315 Share on other sites More sharing options...
Third_Degree Posted August 7, 2008 Share Posted August 7, 2008 try this (you don't need $i as previously mentioned) switch($squadronDB) { Link to comment https://forums.phpfreaks.com/topic/118396-display-issue-due-to-code-conflict-with-switch-statement-help/#findComment-610355 Share on other sites More sharing options...
wildteen88 Posted August 7, 2008 Share Posted August 7, 2008 As I tried to explain above, the value comes from the squardonDB variable which delivers a value between 0 and 24 which determines what squad they are on, I'm simply trying to get the first to work (1 through 4 which equals Alpha, 5-8 equals Beta, ect). with the hope that if i can get that one to work i can finish the rest off. For what you're trying to do you wouldn't need a switch statement. You'd so something like this if($squardonDB >= 1 && $squardonDB <= 4) { $squad = 'Alpha'; } elseif($squardonDB >= 5 && $squardonDB <= { $squad = 'Beta'; } elseif($squardonDB >= 9 && $squardonDB <= 12) { $squad = 'squad3'; } elseif($squardonDB >= 13 && $squardonDB <= 16) { $squad = 'squad4'; } elseif($squardonDB >= 16 && $squardonDB <= 19) { $squad = 'squad5'; } elseif($squardonDB >= 20 && $squardonDB <= 23) { $squad = 'squad6'; } However if you want to use a swtich it can be done like this too switch ($squardonDB) { case 1: case 2: case 3: case 4: echo "is Alpha"; break; case 5: case 6: case 7: case 8: echo "is Beta"; break; case 9: case 10: case 11: case 12: echo "squad3"; break; // etc } Now based on what I've read so far on switch statements, I do need the '$i', although it's not working. I'm looking to make the code work so if you can help it's appreciated. I'd like to know where you read that from. I have never done that. If that was the true I wouldn't of said the switch can take any variable. Link to comment https://forums.phpfreaks.com/topic/118396-display-issue-due-to-code-conflict-with-switch-statement-help/#findComment-610757 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.