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
https://forums.phpfreaks.com/topic/88582-while-loop-inside-a-switch-statement/
Share on other sites

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.

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

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

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:

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

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.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.