...

View Full Version : For loop error inside Php



camaross
11-09-2011, 01:34 AM
This is part of a search engine code to render the result to a html page. It works flawlessly, until I add the most outside 'for' loop.

This is the error message
Parse error: syntax error, unexpected '}' in /home/search.php on line 100
Line 100 is the last line with "}"

What am I missing here? Thank you in advance.



for ($coun=1000; $coun<1103; $coun ++) {

$nu="A" . $coun;

/* Accessing the rows */
$extract=mysql_query("SELECT * FROM $table WHERE code='$nu' ORDER BY id ASC");
$number=mysql_numrows($extract);

if($number>0):

while($row = mysql_fetch_array($extract)){
$code[] =$row['code'];
$type[] =$row['type'];
$definition[] =$row['definition'];
$make[] =$row['make'];
$english[] =$row['english'];
$description[]=$row['description'];
}

for ($num=0; $num<$number; $num++) {
$theResults .= "
<table style=\"position:relative; width: 600px; left:-10px; border-collapse:collapse;\" >
<tr>
<th style=\"width:60px; padding-left:2px; text-align:center; background-color:#FF9; font-size:14px;font-weight:bold; \">$nu</th>
<td style=\"font-weight:bold; \">$definition[$num]</td>
</tr>
</table>";

}

}

Spookster
11-09-2011, 01:46 AM
It's your "if" statement throwing everything out of whack.



if($number>0):



What is it your are attempting to do there?

camaross
11-09-2011, 01:57 AM
Thanks for the reply.

Sometimes, the search returns no results. The purpose of the "if" statement is to rule out those occasions.


It's your "if" statement throwing everything out of whack.



if($number>0):



What is it your are attempting to do there?

imbiber
11-09-2011, 02:04 AM
But, you haven't told if what to do in that instance.

Ahh, don't listen to me. How do you delete a post?

Spookster
11-09-2011, 02:12 AM
Thanks for the reply.

Sometimes, the search returns no results. The purpose of the "if" statement is to rule out those occasions.

Then you need to put curly braces around all the code that falls under that condition.

camaross
11-09-2011, 02:17 AM
OMG, you found my problem. I just realize that my "endif" is outside the loop further down the code.

Now the code is working greatly.

Thank you so much!!!




It's your "if" statement throwing everything out of whack.



if($number>0):



What is it your are attempting to do there?

Fou-Lu
11-09-2011, 02:26 AM
OMG, you found my problem. I just realize that my "endif" is outside the loop further down the code.

Now the code is working greatly.

Thank you so much!!!

For this reason, you should never mix and match standard and alternate syntax. Choose one and always stick with it. I personally prefer the C style syntax, so I will always choose the braces (I find that with each brace on a new line, it makes identifying entire blocks easier). If you come from a basic background, the alternate syntax may be easier to follow.

camaross
11-09-2011, 02:32 AM
I agree. But sometimes, when you have too many curly brackets, they could be confusing.

I like Matlab, which has the capability of "smart indent".


For this reason, you should never mix and match standard and alternate syntax. Choose one and always stick with it. I personally prefer the C style syntax, so I will always choose the braces (I find that with each brace on a new line, it makes identifying entire blocks easier). If you come from a basic background, the alternate syntax may be easier to follow.

MattF
11-09-2011, 04:32 AM
I like Matlab, which has the capability of "smart indent".

Try using the Tab key. It works wonders for indentation.

Fou-Lu
11-09-2011, 02:53 PM
Lol
Most IDEs as well will carry a format feature. I use eclipse, and I have a couple of custom templates I wrote to do the formatting. I believe the default in eclipse is to keep the brace on the same line, but I don't like that. It will auto indent even non-indented blocks when you format it, but it tends to stick to a wrap point which can sometimes cause issues (only when during format, never when writing).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum