使用php添加排序到Mysqli表

时间:2015-04-21 16:09:58

标签: php mysqli

我一直试图解决这个问题好几个小时。 我觉得它的内容就像if语句应该如何工作一样简单。 贝娄是我试图添加的代码:在代码下面我输入了我试图使用的php:但是我一直在收到有关if语句结构的错误吗?

此外,我相信我的代码本身有问题,因为只有当我插入代码时才会出现此错误。要确认这是伪的: 如果URL中存在sort参数,则在选择表时使用该值。否则使用非订单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;
				        }
&#13;
&#13;
&#13;

&#13;
&#13;
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");
}
&#13;
&#13;
&#13;

我查看了代码,但我无法看到声明中的问题。 PHP告诉我像意外的错误}当我删除它像意外的其他等。

如果您看到任何内容,请分享一些关于此的内容,以及任何一般的结构改进:) 错误代码:解析错误:语法错误,意外&#39;}&#39;

我还在学习我的批评。

提升

1 个答案:

答案 0 :(得分:0)

我认为你有一个没有开放if语句的其他人,检查一下(在脚本的最后几行),用你的编辑进行测试并得到相同的

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

**/
?>

希望这会有所帮助:)