SelfObscurity Posted October 17, 2010 Share Posted October 17, 2010 I'm using a switch with multiple nested switches. I tested the switch and nested switches by setting up the structure, and using basic text for the cases. For example, instead of one of the nested switch cases needing to = $_GET['id'], I just entered two cases, each equaling 1 and 2, then the default case. Once I begin to enter my SQL coding to connect to the databases, only certain information prints. For example; on my switch that is $_GET['page'], I have a case for 'news'. Inside this case, I have a nested switch called $_GET['id'], which will display information if the page=nes&id=whatever. That works, but when I go do the page=news, nothing displays. My coding is omitting displaying the default information if there is no $_GET['id']. Attached, I am going to have my .php file. The reason for not posting it here is because my coding is rather messy. Please help! [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted October 17, 2010 Share Posted October 17, 2010 If I am not mistaken it is because you in the case that you are checking for an id (under news) your query is still adding the id=$id...you need to remove that so that it can get the results. Also just a note...why dont you just use an if stament inside of the switch since there is only one case. Quote Link to comment Share on other sites More sharing options...
SelfObscurity Posted October 18, 2010 Author Share Posted October 18, 2010 Well, the case that calls for the ID in the news section, is the part that actually works. It's the default case that isn't showing any information. The reason I'm using switches instead of an if statement is because I was told it was easier. I'm doubting this. Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted October 18, 2010 Share Posted October 18, 2010 switches are not "easier" lol. they are used when you have several things that something can match. Any statement that uses a switch statement can easily use an if statement and look even cleaner. However, my wording on the last one got screwed up I think. this is the part of code I am talking about: switch ($_GET['page']) { case 'news': $id = $_GET['id']; switch ($id) { case $id: mysql_select_db ($db, $link); $query = "SELECT * from news WHERE id = '$id'"; $result = mysql_db_query ($db, $query, $link); while ($row = mysql_fetch_array ($result)) { echo ("ID $row[id] title is $row[title]<br />"); } break; default: mysql_select_db ($db, $link); $query = "SELECT * from news WHERE id = '$id'"; $result = mysql_db_query ($db, $query, $link); while ($row = mysql_fetch_array ($result)) { echo ("ID $row[id] title is $row[title]<br />"); } break; } break; If you notice in your default cause you still have the WHERE id='$id'. That needs to be removed and only needs to be in the case where you are checking for an id. However I would rewrite this section to look like this: switch ($_GET['page']) { case 'news': if(is_numeric($id)){ mysql_select_db ($db, $link); $query = "SELECT * from news WHERE id = '$id'"; $result = mysql_db_query ($db, $query, $link); while ($row = mysql_fetch_array ($result)) { echo ("ID $row[id] title is $row[title]<br />"); } } else { mysql_select_db ($db, $link); $query = "SELECT * from news"; $result = mysql_db_query ($db, $query, $link); while ($row = mysql_fetch_array ($result)) { echo ("ID $row[id] title is $row[title]<br />"); } } } break; Also I noticed in your code that you are doing mysql_select_db a bunch of times and it seems as if you are using the same database. You only need to call this function once to connect to a database and it will keep that connection. Which is the same for mysql_connect (unless you close the connection). Whenever you need to change databases you can then call it again but it is unnecessary to keep calling this. Quote Link to comment Share on other sites More sharing options...
SelfObscurity Posted October 18, 2010 Author Share Posted October 18, 2010 ngreenwood6, Thank you for your response. I've re-written the code you posted, and still, it will not show the default news information, but if I put &id=1 after the URL, it shows up. This isn't making sense to me! Quote Link to comment Share on other sites More sharing options...
Colton.Wagner Posted October 18, 2010 Share Posted October 18, 2010 This should automatically pull up the id equivalent to 1. switch ($_GET['page']) { case 'news': if(is_numeric($id)){ mysql_select_db ($db, $link); $query = "SELECT * from news WHERE id = '$id'"; $result = mysql_db_query ($db, $query, $link); while ($row = mysql_fetch_array ($result)) { echo ("ID $row[id] title is $row[title]<br />"); } } else { $id = "1"; mysql_select_db ($db, $link); $query = "SELECT * from news WHERE id='$id'"; $result = mysql_db_query ($db, $query, $link); while ($row = mysql_fetch_array ($result)) { echo ("ID $row[id] title is $row[title]<br />"); } } } break; Hope I could be of some help. ngreenwood6 made it so that it would display all the data in the database. I hope that could be some help. Thanks, Colton Wagner Quote Link to comment Share on other sites More sharing options...
SelfObscurity Posted October 18, 2010 Author Share Posted October 18, 2010 Colton, That's what I'm going for. The first half of the if statement, is to call information based on if id equals whatever the id field is. The second half of the if statement, calls everything. It's the second half that won't display anything, not even my table, just blank space. I've been literally working at it for 3 days! Quote Link to comment Share on other sites More sharing options...
Colton.Wagner Posted October 18, 2010 Share Posted October 18, 2010 Try making your id something not numeric it should display everything if it is a word or something. Let me know what happens. Thanks, Colton Wagner Quote Link to comment Share on other sites More sharing options...
SelfObscurity Posted October 18, 2010 Author Share Posted October 18, 2010 I'm unsure what changing what I use as a value for ID will do. When I go to my index.php, I'm displaying all news posts with no problem. I copied the exact PHP/SQL coding for the default news case, so it should be working. I believe I am going to change to if statements, see what I can accomplish. Quote Link to comment 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.