Adding Sorting to Mysqli table using php [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
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>' . $row[2] . '</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

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>' . $row[2] . '</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 :)

Related

How to increment a counter inside of a loop

I don't understand, I have a variable $i which is not incrementing in my loop.
while ($ligne = mysqli_fetch_array($resu2,MYSQL_ASSOC)) {
echo '<tr>';
for($i = 1; $nbjour >= $i; $i++)
{
while ($ligne2 = mysqli_fetch_array($resu3,MYSQL_ASSOC)) {
$element=$ligne2['jourIndisponibilite'];
echo "1er ".$element;
echo "2eme ".$ligne['idSPProfessionnel'];
echo ' ';
echo $i;
echo ' ';
if ($ligne2['jourIndisponibilite']==$i && $ligne['idSPPprofessionnel']==$ligne2['idSpProfessionnel']){
echo '<td height=25>';
echo 'X';
echo '</td>';
}
}
echo '<td height=20></td>';
}
echo '</tr>';
}
echo "</table>";
echo '</br>';
?>

Information not showing up my MySQL Database

I'm trying to open a new php page from the sNumber and display the data from the student table on student profile page from the sNumber. But I can't retrieve the data, it goes right to the error. Any help will be appreciated. Thanks
studentlist.php
<div class="memtable">
<?php
$reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;
echo '<div class="pagination"><ul>';
if ($total_pages > 1) {
echo paginate($reload, $show_page, $total_pages);
}
echo "</ul></div>";
// display data in table
echo "<table class='table table-bordered'>";
echo "<thead><tr><th>Last Name</th> <th>First Name</th> <th>School</th> <th>Snumber</th></tr></thead>";
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++) {
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) {
break;
}
// echo out the contents of each row into a table
$lastName = "<a href = 'studentprofile.php?id= " .mysql_result($result, $i, 'sNumber'). "'>" . mysql_result($result, $i, 'lastName') . "</a>";
echo "<tr " . $cls . ">";
echo '<td>' . $lastName . '</td>';
echo '<td>' . mysql_result($result, $i, 'firstName') . '</td>';
echo '<td>' . mysql_result($result, $i, 'school') . '</td>';
echo '<td>' . mysql_result($result, $i, 'sNumber') . '</td>';
echo "</tr>";
}
// close table>
echo "</table>";
// pagination
?>
</div>
studentprofile.php
<?php
include('phpdocs/connect.inc.php');
include('header.php');
if ( isset( $_GET[ "sNumber" ] ) )
$student_sNumber = $_GET['sNumber'];
$getStudentInfo = " SELECT sNumber FROM student WHERE student.sNumber = " . $student_sNumber;
?>
<!DOCTYPE html>
<html>
<head>
<title>Student</title>
<link href="css/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="transoverlay">
<?php
if ($result = mysql_query($getStudentInfo)) {
/* fetch associative array */
while ($row = mysql_fetch_assoc($result)) {
echo "<h1 class='tv'>" . $row["sNumber"]. ", ". $row['firstName']."</h1>";
}
mysql_free_result($result);
}else{
echo "<div class='tv'>Student Data could not be listed. </div>";
}
?>
<hr color="#1a1a1a">
</div>
</body>
<?php include('footer.php');?>
</html>
the url uses id but you checking for sNumber change one of those
you need to quote student number in the query as its a string
$getStudentInfo = "SELECT sNumber FROM student WHERE student.sNumber ='". $student_sNumber."'";

How to get the highest value from a database row?

I have a problem. I have a one page website and if there is posted a message a posts shows up and below the post there needs to be an image, but on the last on the index page I don't want an image to show up. So it's like if the id from the database is the highest that post doesn't get a image below it.
Here is the code:
<?php
include 'functions/image/functions.php';
$countQuery = $db->prepare("SELECT paginaNummer AS max FROM pages ;");
$countRow = $countQuery->fetch();
$maxId = $countRow['max'];
$query = $db->prepare("SELECT * FROM pages");
$query->execute();
$num_cols = 1;
$i = 0;
$imgList = getImagesFromDir($root . $path);
$img = getRandomFromArray($imgList);
while($row = $query->fetch()) {
if(isset($row)) {
echo "<section data-stellar-background-ratio='0.5'>";
echo "<div class='panelContainer'>";
echo "<div class='panel2'>";
echo "<div class='symbol3'></div>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</section>";
echo "<section class='wrapper'>";
echo "<div class='box'>";
echo "<div class='content'>";
echo $i++ % $num_cols == 0 ? '' : '';
echo "<div id='message'><h2> ", $row['berichtNaam'], "</h2>";
echo $row['paginaContent'], "<br />";
if (isset($_SESSION['login']['5']) && $_SESSION['login']['5'] == 2) {
echo " <a class='edit' href='functions/admin/edit.php?id=" . $pageNumber . " '>Edit</a>";
echo " <a class='delete' href='functions/admin/deletePost.php?id=" . $pageNumber . " '>Delete</a>";
} elseif (isset($_SESSION['login']['5']) && $_SESSION['login']['5'] == 1) {
echo " <a class='edit' href='functions/admin/edit.php?id=" . $pageNumber . " '>Edit</a>";
} else {
}
echo "</div>";
echo "</div>";
echo "</section>";
echo max($row);
if (count($row['paginaNummer']) == max($row)){
} else {
echo "<a href='/'><img src='<?php echo $path . $img ?>'alt=''/></a>";
}
echo "</section>";
}
}
echo "</div></div>";
?>
I won't get any further with this part I hope you can help me with this problem
It's hard to understand what you need. But try this query:
$countQuery = $db->prepare("SELECT paginaNummer AS max
FROM pages ORDER BY paginaNummer DESC LIMIT 1");
Or may be this:
$countQuery = $db->prepare("SELECT paginaNummer AS max
FROM pages ORDER BY id DESC LIMIT 1");
Update: this code changes instead of top:
<?php
include 'functions/image/functions.php';
$query = $db->prepare("SELECT * FROM pages");
$query->execute();
$maxrow = $query->rowCount();
$num_cols = 1;
$i = 0;
$imgList = getImagesFromDir($root . $path);
$img = getRandomFromArray($imgList);
$n = 0;
while($row = $query->fetch()) {
if(isset($row)) {
$n++;
echo "<section data-stellar-background-ratio='0.5'>";
echo "<div class='panelContainer'>";
echo "<div class='panel2'>";
echo "<div class='symbol3'></div>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</section>";
echo "<section class='wrapper'>";
echo "<div class='box'>";
echo "<div class='content'>";
echo $i++ % $num_cols == 0 ? '' : '';
echo "<div id='message'><h2> ", $row['berichtNaam'], "</h2>";
echo $row['paginaContent'], "<br />";
if (isset($_SESSION['login']['5']) && $_SESSION['login']['5'] == 2) {
echo " <a class='edit' href='functions/admin/edit.php?id=" . $pageNumber . " '>Edit</a>";
echo " <a class='delete' href='functions/admin/deletePost.php?id=" . $pageNumber . " '>Delete</a>";
} elseif (isset($_SESSION['login']['5']) && $_SESSION['login']['5'] == 1) {
echo " <a class='edit' href='functions/admin/edit.php?id=" . $pageNumber . " '>Edit</a>";
} else {
}
echo "</div>";
echo "</div>";
echo "</section>";
echo max($row);
if ($n == $maxrow){
} else {
echo "<a href='/'><img src='<?php echo $path . $img ?>'alt=''/></a>";
}
echo "</section>";
}
}
echo "</div></div>";
?>

If statement not running block of code?

I am trying to query the table with the $_GET parameter, but when I add the parameter nothing is ordering by specified value. Is there something wrong with how I have written this if statement?
if (isset($_GET['sort'])) {
$sorting = $_GET['sort'];
$result = $mysqli->query("SELECT * FROM routes ORDER BY $sorting");
}
else if (!isset($_GET['sort'])) {
$result = $mysqli->query("SELECT * FROM routes _");
}
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_title'>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>";
// Check for Sorting Request
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>' . $row[2] . '</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 class="col-md-6" style="text-align:right;">';
echo '<ul class="paginated-btns">';
for ($i = 1; $i <= $total_pages; $i++)
{
if (isset($_GET['page']) && $_GET['page'] == $i)
{
echo "<li><a href='adminviewer.php?page=$i&page_sel=$per_page'>$i</a></li>";
}
else
{
echo "<li><a href='adminviewer.php?page=$i&page_sel=$per_page'>$i</a></li>";
}
}
echo '</ul>';
*/
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>';
I'm learning PHP so I can imagine something is wrong with the logic, but I can't seem to figure it out.
The values of the $_GET['sort'] are columns of the table; for example: route_id, route_name, route_price, ...

pagination in php and sql - appears me the same rows in every page

This is my code:
<?php
mysql_select_db("jvl_teste");
$per_page = 2;
$pages_query = mysql_query("SELECT COUNT('id') FROM utilizadores");
$pages = ceil(mysql_result ($pages_query, 0) / $per_page);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$results = mysql_query("SELECT * FROM utilizadores LIMIT $start, $per_page");
echo '<table border="1" align="center">
<tr class="d0">
<td align="center"><strong>Nome</strong></td>
<td align="center"><strong>Idade</strong></td>
</tr>';
$rowColors = Array('#ffffff','#ffffcc'); $i= 0;
while($row = mysql_fetch_array($results)) {
echo '<tr style="background-color:'.$rowColors[$i++ % count($rowColors)].';">';
echo '<td align="center">' . $row['nome'] . "</td>";
echo '<td align="center">' . $row['idade'] . "</td>";
$id = $row['id'];
echo "<td align='center'><a href='detalhes.php?id={$id}'><img src='css/imagens/detalhes.png']}'/></a></td>";
echo "</tr>";
}
echo "</table>";
if($pages >= 1){
for ($x=1;$x<=$pages;$x++){
echo ''.$x.' ';
}
}
?>
The problem is that in page 1, 2, 3.. etc appears me all the same first two results of the table rows in every page. What is the problem? Can you give me the solution please?
#danielpsc says you have different name for your parameter in the links and in your PHP code, but they should be the same.
You need to change :
echo ''.$x.' ';
To (remove the "s") :
echo ''.$x.' ';

Categories