0

I have been trying to tackle this problem for many hours. i feel its something as simple as how the if statement should work. Bellow is my code that i am trying to add to: And underneath that code i have typed the php that i tried to use: But i keep getting errors regarding to the structure of the if statement?

Additionally I believe my code itself is at fault, as this error only happens when I insert my code. To confirm this is the pseudo: If sort parameter exists in URL then use the value when selecting the table. Otherwise use the non order sql.

-----------Trying to edit----------
if (isset($_GET[sort])) {
 $sorting = $_GET['sort'];
 $result = $mysqli->query("SELECT * FROM routes ORDER BY $sorting");
}

else {
 $result = $mysqli->query("SELECT * FROM routes");
}
-----------Trying to edit----------
            
 if ($result->num_rows != 0)
 {
  $total_results = $result->num_rows;
  $total_pages = ceil($total_results / $per_page);


  if (isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] <= $total_pages )
   {
   $show_page = $_GET['page'];
                        
   if ($show_page > 0 && $show_page <= $total_pages)
   {
    $start = ($show_page -1) * $per_page;
    $end = $start + $per_page;
   }
   else
   {
     $start = 0;
     $end = $per_page; 
   }               
   }
    else
   {

    $start = 0;
    $end = $per_page;
   }
                
                
               
   echo "<form action='../processors/delete.php' method='post'";
   echo "<br/><table class='table table-striped table-bordered' id='basic-datatable' border='0' cellpadding='10'>";
   echo "<thead><tr> <th>#</th>"; 
   echo "<th><a href='adminviewer.php?sort=route_id'>Route ID</a></th>"; 
   echo "<th><a href='adminviewer.php?sort=route_name'>Route</a></th>"; 
   echo "<th><a href='adminviewer.php?sort=route_price'>Price</a></th>";
   echo "<th><a href='adminviewer.php?sort=route_payment'>Payment</a></th>";
   echo "<th><a href='adminviewer.php?sort=route_net'>Weekly Net</a></th>";
   echo "<th><a href='adminviewer.php?sort=route_city'>City</a></th>";
   echo "<th><a href='adminviewer.php?sort=route_state'>State</a></th>";
      echo "<th><a href='adminviewer.php?sort=route_remarks'>Remarks</a></th>"; 
   echo "</tr></thead>";
   echo "<tbody>";
               
            for ($i = $start; $i < $end; $i++)
   {
    if ($i == $total_results) { break; }
                       
     $result->data_seek($i);
         $row = $result->fetch_row();
               
               
          echo "<tr>";
      echo '<td><input class="checked-box" type="checkbox" name="selec-row[]" value="' . $row[0] . '"></td>';
      echo '<td>' . $row[1] . '</td>';
      echo '<td><a href="detail.php?id=' . $row[0] . '">' . $row[2] . '</a></td>';
      echo '<td>' . $row[3] . '</td>';
      echo '<td>' . $row[4] . '</td>';
      echo '<td>' . $row[5] . '</td>';
      echo '<td>' . $row[6] . '</td>';
      echo '<td>' . $row[7] . '</td>';
      echo '<td>' . $row[8] . '</td>';
      echo "</tr>";
      }

      echo "</tbody>";
      echo "</table>";
      echo "<input class='del-btn' type='submit' value='Delete'>";
      echo "</form>";
            }
            else
            {
             echo "No results to display!";
            }
        
        
      echo '<div class="col-md-12" style="text-align: center;position:relative;top:-25px;">';
      echo "Showing 1 to " . $per_page . " of " . $total_results . " routes";
      echo '</div>';
         
      echo "</div>";
      echo '</div>';
      echo '</div>';
      echo '</div>';

      echo '<div class="pagination">';

      if (!isset($_GET['page'])) {
       $_GET['page'] = 1;
      }

        
      if ($_GET['page'] > $per_page) {
        $next_page = $i - 1;
        echo "<div class='pag-btn'><a href='adminviewer.php?page=$next_page&page_sel=$per_page'><- Previous</a></div>";
      }
      else {
       echo "<div class='pag-btn'>Previous</div>";
      }
      if ($_GET['page'] < $total_pages) {
         $next_page = $_GET['page'] + 1;
         echo "<div class='pag-btn'><a href='adminviewer.php?page=$next_page&page_sel=$per_page'>Next -></a></div>";
      }
      else {
        echo "<div class='pag-btn'>Next</div>";
      }             

      echo '</div>';

            }
           
            else  -----syntax error, unexpected '}'----
            {
             echo "Error: " . $mysqli->error;
            }

if(isset($_GET['sort'])) {
  $sorting = sanitize_input($_GET['sort']);
  $result = mysqli->query("SELECT * FROM table ORDER BY $sorting");
} else {
  $result = mysqli->query("SELECT * FROM table ORDER BY id");
}

I have looked over the code and i cant see issues with the statement. PHP is telling me errors like unexpected } and when i remove that its like unexpected else etc.

Please share some light on this that would be great, as well as any general structural improvements if you see any :) Error Code: Parse error: syntax error, unexpected '}'

I'm still learning so my critiquing.

Elevant

  • Please include the errors you are receiving, and indicate where, in the larger code block, you are attempting to add the smaller block. – Politank-Z Apr 21 '15 at 16:16
  • @BeingSunny - , I was curious to know how Code Review would be able to help, since Code Review is for working code, and this question is about adding new functionality that's not working... have you read the Code Review help center? – rolfl Apr 21 '15 at 16:16
  • I'll add some more detail right away –  Apr 21 '15 at 16:32
  • Would it be a better idea to add the code directly in the question? –  Apr 21 '15 at 16:34
  • You need to indent your code. Most likely you'll find the issue right away. – Luís Cruz Apr 21 '15 at 19:11

1 Answers1

0

I think you have an else without an opening if statement, check this out (in last lines of the script), made the test with your edit and get the same

<?php
// -----------Trying to edit----------
if (isset( $_GET[sort]) ) 
{
    $sorting = $_GET['sort'];
    $result = $mysqli->query("SELECT * FROM routes ORDER BY $sorting");
} else {
    $result = $mysqli->query("SELECT * FROM routes");
}
// -----------Trying to edit----------

if ($result->num_rows != 0)
{
        $total_results = $result->num_rows;
        $total_pages = ceil($total_results / $per_page);


        if (isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] <= $total_pages )
        {
            $show_page = $_GET['page'];

            if ($show_page > 0 && $show_page <= $total_pages)
            {
                $start = ($show_page -1) * $per_page;
                $end = $start + $per_page;
            } else {
                $start = 0;
                $end = $per_page; 
            }              
        } else {
            $start = 0;
            $end = $per_page;
        }

        echo "<form action='../processors/delete.php' method='post'";
        echo "<br/><table class='table table-striped table-bordered' id='basic-datatable' border='0' cellpadding='10'>";
        echo "<thead><tr> <th>#</th>"; 
        echo "<th><a href='adminviewer.php?sort=route_id'>Route ID</a></th>"; 
        echo "<th><a href='adminviewer.php?sort=route_name'>Route</a></th>"; 
        echo "<th><a href='adminviewer.php?sort=route_price'>Price</a></th>";
        echo "<th><a href='adminviewer.php?sort=route_payment'>Payment</a></th>";
        echo "<th><a href='adminviewer.php?sort=route_net'>Weekly Net</a></th>";
        echo "<th><a href='adminviewer.php?sort=route_city'>City</a></th>";
        echo "<th><a href='adminviewer.php?sort=route_state'>State</a></th>";
        echo "<th><a href='adminviewer.php?sort=route_remarks'>Remarks</a></th>"; 
        echo "</tr></thead>";
        echo "<tbody>";

        for ($i = $start; $i < $end; $i++)
        {
            if ($i == $total_results) 
            { 
                break; 
            }

            $result->data_seek($i);
            $row = $result->fetch_row();

                echo "<tr>";
            echo '<td><input class="checked-box" type="checkbox" name="selec-row[]" value="' . $row[0] . '"></td>';
            echo '<td>' . $row[1] . '</td>';
            echo '<td><a href="detail.php?id=' . $row[0] . '">' . $row[2] . '</a></td>';
            echo '<td>' . $row[3] . '</td>';
            echo '<td>' . $row[4] . '</td>';
            echo '<td>' . $row[5] . '</td>';
            echo '<td>' . $row[6] . '</td>';
            echo '<td>' . $row[7] . '</td>';
            echo '<td>' . $row[8] . '</td>';
            echo "</tr>";
        }

        echo "</tbody>";
        echo "</table>";
        echo "<input class='del-btn' type='submit' value='Delete'>";
        echo "</form>";

} else {
    echo "No results to display!";
}

echo '<div class="col-md-12" style="text-align: center;position:relative;top:-25px;">';
echo "Showing 1 to " . $per_page . " of " . $total_results . " routes";
echo '</div>';

echo "</div>";
echo '</div>';
echo '</div>';
echo '</div>';

echo '<div class="pagination">';

if ( !isset($_GET['page']) ) 
{
    $_GET['page'] = 1;
}

if ($_GET['page'] > $per_page) 
{
    $next_page = $i - 1;
    echo "<div class='pag-btn'><a href='adminviewer.php?page=$next_page&page_sel=$per_page'><- Previous</a></div>";
} else {
    echo "<div class='pag-btn'>Previous</div>";
}

if ($_GET['page'] < $total_pages) 
{
    $next_page = $_GET['page'] + 1;
    echo "<div class='pag-btn'><a href='adminviewer.php?page=$next_page&page_sel=$per_page'>Next -></a></div>";
} else {
    echo "<div class='pag-btn'>Next</div>";
}                           

echo '</div>';

/* The same thing, no opening if statement. Sure this is not the error?

}

else  -----syntax error, unexpected '}'----
{
    echo "Error: " . $mysqli->error;
}

**/
?>

Hope this helps :)

martinezjc
  • 3,415
  • 3
  • 21
  • 29
  • I'll have a check tomorrow morning. That ending part though should be response to the first if statement , so I perhaps needs an alternative way of implementing my if statement there –  Apr 21 '15 at 17:30
  • Be sure to include it in the question body to be sure if this is the issue, i made an edit and match every bracket and this was the result – martinezjc Apr 21 '15 at 17:31
  • Okay I'll add to the question –  Apr 21 '15 at 17:31