illithid Posted January 30, 2008 Share Posted January 30, 2008 Greetings all. I am having a problem running a while loop inside a switch statement. On an index.php I am creating for a website, I have the navigation on the left and the content on the right. The links for the navigation menu are gathered from the database via a while loop. On the content side I am using a switch statement to include the appropriate page, based on the user link selection. Inside the switch statement, I am trying to use, essentially, the same while loop that I am using for the navigation menu. I keep getting the following error : Parse error: syntax error, unexpected T_VARIABLE, expecting T_CASE or T_DEFAULT or '}' If I can get some additional eyes on the code to help me figure out the problem, that would be great. Thanks in advance. <?php /***********************************/ /** START PAGE LOAD TIMER. **/ /***********************************/ $time = time()+microtime(); $start = $time; /***********************************/ /** GATHER REQUIRED FILES **/ /***********************************/ require('./function.php'); /***********************************/ /** CONNECT TO THE SERVER **/ /***********************************/ connect(); /***********************************/ /** BEGIN PAGE CODE **/ /***********************************/ buildTop(); echo ' <table> <tr> <td> <ul>\n'; /***********************************/ /** GET ALL EDUCATION LINKS. **/ /***********************************/ $query = mysql_query("SELECT * FROM `links`"); /***********************************/ /** DISPLAY THE LINKS. **/ /***********************************/ while ($sql = mysql_fetch_array($query)) { $title = $sql['title']; echo '<li><a href="./index.php?page=' .$title. '">' .$title. '</a></li>'; } echo ' </ul> </td> <td>\n'; /***********************************/ /** START CODE LOGIC FOR LINKS **/ /***********************************/ switch($page) { while ($link = mysql_fetch_array($query) { $title = $link['title']; $address = $link['address']; case $link: include($address); break; } default: echo 'Welcome to the Education Trainer.'; break; } echo ' </td> </tr> </table>\n'; $time = time()+microtime(); $finish = $time; $totaltime = round(($finish - $start),6); echo ' <div style="text-align:center;font-size:12px;padding:5px;color:grey;"> Page loaded in : <span style="font-style:italic;">' .$totaltime. '</span> seconds.<br /> </div>'; /***********************************/ /** END PAGE CODE **/ /***********************************/ buildBottom(); ?> Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted January 30, 2008 Share Posted January 30, 2008 On this line while ($link = mysql_fetch_array($query) Why do you have the semi-colon in there? Try changing it to while ($link = mysql_fetch_array($query)) Not sure if thats causing the error, but it could be. Quote Link to comment Share on other sites More sharing options...
illithid Posted January 30, 2008 Author Share Posted January 30, 2008 Overlooked that....thanks for pointing that out, however I am getting a new error. Parse error: syntax error, unexpected T_WHILE, expecting T_CASE or T_DEFAULT or '}' Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted January 30, 2008 Share Posted January 30, 2008 In a switch statement, you need at least one 'case' tag and an optional 'default' tag. I don't see a 'case' tag in your code. Ken Quote Link to comment Share on other sites More sharing options...
illithid Posted January 30, 2008 Author Share Posted January 30, 2008 If I am understanding you, I need to put a 'case' tag in the switch statement before my while loop(which is creating my 'case' tags). Correct? Here is the code in question. <?php switch($page) { while ($link = mysql_fetch_array($query) /* while $link has data, create each 'case' tag */ { $title = $link['title']; $address = $link['address']; case $link: /* NOTE : here is my case */ include($address); break; } default: /* NOTE : here is my default */ echo 'Welcome to the Education Trainer.'; break; } ?> Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted January 30, 2008 Share Posted January 30, 2008 <?php switch($page) { ############ NEED CASE HERE ##################################### while ($link = mysql_fetch_array($query) /* while $link has data, create each 'case' tag */ { $title = $link['title']; $address = $link['address']; case $link: /* NOTE : here is my case */ include($address); break; } default: /* NOTE : here is my default */ echo 'Welcome to the Education Trainer.'; break; } ?> I put a comment in the code where you need a case (it's hard to miss ;P). If you want that part of the code to show all the time, you need to bring it outside of the switch statement. Quote Link to comment Share on other sites More sharing options...
illithid Posted January 30, 2008 Author Share Posted January 30, 2008 Ok, I did understand it correctly then. Just had to be sure. So, I changed the code around a bit and tested it..... Result : Parse error: syntax error, unexpected T_CASE edited code : <?php switch($page) { default : echo 'Welcome to the Education Trainer.'; break; case 'Home': echo 'Welcome home.'; break; while ($link = mysql_fetch_array($query)) { $title = $link['title']; $address = $link['address']; case $title: include($address); break; } } ?> The rest of the error message I am getting is pointing to line 68. I checked the reference to line. The reference : case $title: Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted January 30, 2008 Share Posted January 30, 2008 What are you trying to do, exactly? Write it in English, not PHP. From your explanation, we might be able to show you how to do it. Ken Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted January 30, 2008 Share Posted January 30, 2008 Everything in your switch statement has to be between a case or default. These lines inside your switch statement are not between a case <?php while ($link = mysql_fetch_array($query)) { $title = $link['title']; $address = $link['address']; ?> So you need to figure out a way to do this block of code differently <?php while ($link = mysql_fetch_array($query)) { $title = $link['title']; $address = $link['address']; case $title: include($address); break; } Quote Link to comment Share on other sites More sharing options...
tapos Posted January 30, 2008 Share Posted January 30, 2008 Please clear what you want to do. Also visit http://www.php.net/manual/en/control-structures.switch.php to have a clear idea about switch statement. Quote Link to comment Share on other sites More sharing options...
illithid Posted January 30, 2008 Author Share Posted January 30, 2008 I need my switch statement to update according to the number of links is in the database. I have a mysql_query to get link data from database and a while loop to loop through it, then I display it. I do this because, I have no clue how many links the website will have...also, they may change from time to time. links will look like this --> <a href="index.php?page=something">link</a> the content will be displayed on the other side of this page in the content area, dependant upon the link selected by the user. I will include content here, dependant upon the link the user selected in the Nav column. I do this by using a switch() statement. Unfortunately, I don't know how many links are in the database. This means I need to, again, use a while loop in conjunction with my mysql_query. If there are 2 links in the db, the query runs, gets data for the links. The loop runs through and puts data in the switch statement. ie. id | title | address ____________________ 1 | Home | index.php ____________________ 2 | News | news.php The above data would need to be added into the switch statement to reflect the following. switch($id) { case 'Home' : include('index.php'); break; case 'News' : include('news.php'); break; } But, if I have 5 links, the switch statement would change to reflect that data as well. 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.