Jump to content

while loop inside a switch statement


illithid

Recommended Posts

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();
?>

Link to comment
Share on other sites

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;
}
?>

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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:

Link to comment
Share on other sites

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;
	}

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.