Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts

    For loop error inside Php

    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
    Code:
    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.


    PHP Code:
    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>"
    ;
      
    }


    Last edited by camaross; 11-09-2011 at 01:29 AM.

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    It's your "if" statement throwing everything out of whack.

    PHP Code:
    if($number>0): 

    What is it your are attempting to do there?
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • Users who have thanked Spookster for this post:

    camaross (11-09-2011)

  • #3
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts
    Thanks for the reply.

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

    Quote Originally Posted by Spookster View Post
    It's your "if" statement throwing everything out of whack.

    PHP Code:
    if($number>0): 

    What is it your are attempting to do there?

  • #4
    New to the CF scene
    Join Date
    Nov 2011
    Location
    Melbourne, Australia
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post
    But, you haven't told if what to do in that instance.

    Ahh, don't listen to me. How do you delete a post?
    Last edited by imbiber; 11-09-2011 at 01:06 AM. Reason: I don't know what I'm talking about.

  • Users who have thanked imbiber for this post:

    camaross (11-09-2011)

  • #5
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by camaross View Post
    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.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • Users who have thanked Spookster for this post:

    camaross (11-09-2011)

  • #6
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts
    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!!!



    Quote Originally Posted by Spookster View Post
    It's your "if" statement throwing everything out of whack.

    PHP Code:
    if($number>0): 

    What is it your are attempting to do there?

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by camaross View Post
    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.

  • Users who have thanked Fou-Lu for this post:

    camaross (11-09-2011)

  • #8
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts
    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".

    Quote Originally Posted by Fou-Lu View Post
    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.

  • #9
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by camaross View Post
    I like Matlab, which has the capability of "smart indent".
    Try using the Tab key. It works wonders for indentation.

  • Users who have thanked MattF for this post:

    camaross (11-10-2011)

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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).

  • Users who have thanked Fou-Lu for this post:

    camaross (11-10-2011)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •