Curly brackets problem or a nested if problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bolson
Forum Newbie
Posts: 6
Joined: Wed Apr 29, 2009 2:10 pm

Curly brackets problem or a nested if problem

Post by bolson »

Curly brackets problem or a nested if problem i am not sure which it is or both.
this is generated code from a program. it works using xampp on windows but when i
run it on an linux server this section of code seems to be a problem that i have not been
able to figure out. any help would be great.

Code: Select all

 
<?php function showpagenav($page, $pagecount)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<?php if ($page > 1) { ?>
<td><a href="TEIHOST_MERCHANT.php?page=<? echo $page - 1 ?>"><<&nbsp;Prev</a>&nbsp;</td>
<?php } ?>
<?php
  global $pagerange;
 
  if ($pagecount > 1) {
 
  if ($pagecount % $pagerange != 0) {
    $rangecount = intval($pagecount / $pagerange) + 1;
  }
  else {
    $rangecount = intval($pagecount / $pagerange);
  }
  for ($i = 1; $i < $rangecount + 1; $i++) {
    $startpage = (($i - 1) * $pagerange) + 1;
    $count = min($i * $pagerange, $pagecount);
 
    if ((($page >= $startpage) && ($page <= ($i * $pagerange)))) {
      for ($j = $startpage; $j < $count + 1; $j++) {
        if ($j == $page) {
?>
<td><b><? echo $j ?></b></td>
<?php } else { ?>
<td><a href="TEIHOST_MERCHANT.php?page=<? echo $j ?>"><? echo $j ?></a></td>
<? php} } } else { ?>
<td><a href="TEIHOST_MERCHANT.php?page=<? echo $startpage ?>"><? echo $startpage ."..." .$count ?></a></td>
<?php } } } ?>
<?php if ($page < $pagecount) { ?>
<td>&nbsp;<a href="TEIHOST_MERCHANT.php?page=<? echo $page + 1 ?>">Next&nbsp;>></a>&nbsp;</td>
<?php } ?>
</tr>
</table>
<? } ?>
 
and this shows on the web page

Code: Select all

 
1) { ?> 1) { if ($pagecount % $pagerange != 0) { $rangecount = intval($pagecount / $pagerange) + 1; } else { $rangecount = intval($pagecount / $pagerange); } for ($i = 1; $i < $rangecount + 1; $i++) { $startpage = (($i - 1) * $pagerange) + 1; $count = min($i * $pagerange, $pagecount); } if ((($page >= $startpage) && ($page <= ($i * $pagerange)))) { for ($j = $startpage; $j < $count + 1; $j++) { if ($j == $page) { ?>
<< Prev                  Next >> 
0); { ?>
Index Page  Prior Record    Next Record
 
Last edited by John Cartwright on Thu Oct 15, 2009 2:20 pm, edited 1 time in total.
Reason: Use proper [code=php] [/code] tags
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Curly brackets problem or a nested if problem

Post by Eric! »

Can you explain the problem you're having and edit your post to remove the spaces from your code tag brackets?

+1 for effort, but they should look like [syntax=php]CODE[/syntax] or [syntax=php]CODE[/syntax]
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Curly brackets problem or a nested if problem

Post by Eric! »

Thanks to John I can read it now. But dude, that is one peice of mixed up code. I think you should get a prize for that one. lol

You have a syntax problem on line 32 (there could be other problems too, I don't know)

change line 32 to

Code: Select all

<?php } } } else { ?>
You also switch between short tags <? ?> and long tags <?php ?>. Sometimes the short tags are not recognized by the parser because of how the server is setup.
bolson
Forum Newbie
Posts: 6
Joined: Wed Apr 29, 2009 2:10 pm

Re: Curly brackets problem or a nested if problem

Post by bolson »

thanks Eric! but it did not work i guess the problem is i am not sure where the else statement
on line 30. does it belong to the if statement on line 13 or line 25
i think that some how the Curly brackets are not matching up.
User avatar
desperado
Forum Commoner
Posts: 46
Joined: Wed Dec 10, 2008 8:49 am

Re: Curly brackets problem or a nested if problem

Post by desperado »

your quotes all add up.

line 30 is from }else{ on line 28, started on line 25.

It looks to me like you may have an invisible control charater where your code splits.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Curly brackets problem or a nested if problem

Post by Eric! »

Sorry I don't have more time to find your syntax problem, but I made it more readable. I didn't find a problem with the brackets.

Code: Select all

<?php 
function showpagenav($page, $pagecount)
{
  echo '<table class="bd" border="0" cellspacing="1" cellpadding="4"><tr>';
  if ($page > 1) 
  { 
    echo '<td><a href="TEIHOST_MERCHANT.php?page='.($page-1).'><<&nbsp;Prev </a>&nbsp;</td>';
  }
  global $pagerange;
 
  if ($pagecount > 1) 
  {
    if (($pagecount % $pagerange) != 0) 
    {
      $rangecount = intval($pagecount / $pagerange) + 1;
    }
    else 
    {
      $rangecount = intval($pagecount / $pagerange);
    }
    for ($i = 1; $i < $rangecount + 1; $i++) 
    {
      $startpage = (($i - 1) * $pagerange) + 1;
      $count = min($i * $pagerange, $pagecount);
 
      if ((($page >= $startpage) && ($page <= ($i * $pagerange)))) 
      {
        for ($j = $startpage; $j < $count + 1; $j++) 
        {
          if ($j == $page) 
          {
            echo '<td><b>'.$j.'</b></td>';
          } 
          else 
          { 
            echo '<td><a href="TEIHOST_MERCHANT.php?page='.$j.'">'.$j.'</a></td>';
          } 
       } 
     } 
     else 
     { 
       echo '<td><a href="TEIHOST_MERCHANT.php?page='.$startpage.'">'.$startpage.'...'.$count.'</a></td>';
     } 
   } 
  } 
  if ($page < $pagecount) 
  { 
    echo '<td>&nbsp;<a href="TEIHOST_MERCHANT.php?page='.($page + 1).'">Next&nbsp;>></a>&nbsp;</td>';
  } 
  echo '</tr></table>';
} 
?>
It's not parsing right because the output is the following when it should do nothing

Code: Select all

1) { echo ''.$j.''; } } } else { echo ''.$startpage.'...'.$count.''; } } } if ($page < $pagecount) { echo ' Next >> '; } echo ''; } ?>
And I gotta go.
bolson
Forum Newbie
Posts: 6
Joined: Wed Apr 29, 2009 2:10 pm

Re: Curly brackets problem or a nested if problem

Post by bolson »

thanks a lot that did fix most of the stuff that was displaying on the page
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Curly brackets problem or a nested if problem

Post by Eric! »

I broke it down to just this

Code: Select all

<?php 
function showpagenav($page,$pagecount)
{
  if($pagecount>1) 
  {
   echo 'Hello';
  }
} 
?>
and for some reason it only parses to the greater than sign and then treats the rest as text. Like this:

Code: Select all

1) { echo 'Hello'; } } ?>
This only happens if the expression is >, other expressions work fine.

For example the equivalent:

Code: Select all

<?php 
function showpagenav($page,$pagecount)
{
  if(!$pagecount<=0) 
  {
   echo 'Hello';
  }
} 
?>
works fine.

Have I lost it or does someone see something I'm doing wrong here? 8O

FYI--I'm typing these in fresh, no cutting and pasting. It is behaving like the > is really ?> ending the php script...
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Curly brackets problem or a nested if problem

Post by Eric! »

Bolson, are you running these on a live server or on a development tool?

I just found that if I run them live they work right, but on phpDesigner they generate the problems I mentioned above.

EDIT: After upgrading to phpdesigner7 the problem goes away again. Stupid IDE.
Post Reply