PHP pagination error with only 1 page - php

I have a problem with pagination, i see only first page, i try to undestand. I have a file HTML with a script and the script launch a file php. But the result is only a file
<?php
include 'database.php';
$output = '';
$start=0;
$limit=2;
if(isset($_GET['id'])){
$id=$_GET['id'];
$start=($id-1)*$limit;
}else{
$id=1;
}
$search = $_POST["search"];
if($search == ''){
$sql = "SELECT * FROM dati ORDER BY id DESC LIMIT $start, $limit";
}
else {
$sql = "SELECT * FROM dati WHERE nome LIKE '%".$_POST["search"]."%' ORDER BY id DESC LIMIT $start, $limit";
}
$retval = mysqli_query( $dbconfig, $sql);
$output .= '
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Nome</th>
<th>Categoria</th>
<th>Numero di Telefono</th>
</tr>
</thead>
<tbody>';
while($row=mysqli_fetch_array($retval)){
$output .= '
<tr>
<td>'. $row['nome'] . '</td>
<td>'. $row['categoria'] . '</td>
<td>'. $row['telefono'] . '</td>
</tr>';
}
$output .= '
</tbody>
</table>';
$ret = mysqli_num_rows(mysqli_query( $dbconfig, $sql));
$total=ceil($ret/$limit);
$output .= '
<div class="row">
<ul class="page">';
for($i=1;$i<=$total;$i++){
if($i==$id) {
$output .= '<li class="corrente">'.$i.'</li>';
}else {
$output .= '<li><a href=?id='.$i.'>'.$i.'</a></li>';
}
}
$output .= '
</ul>
</div>';
echo $output;
?>
This is file PHP with pagination but nothing
<input type="submit" name="test" id="test" value="Ricerca">

Related

MySql Table with different colors by value

I am using mysql queries to fetch data from db. All my data are showing fine in tables. Now I want to color the status by value less than 2 or more than 3.
The below code is not working.
Need help.
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
$output .= '<div class="table-responsive">
<table class="table table bordered">
<tr>
<th>name</th>
<th>genre</th>
<th>time</th>
<th>status</th>
<th>more...</th>
</tr>';
?>
<?php
function status_style($row) {
if ($row < 2) return 'background-color: #ff0000'; //red
if ($row > 3) return 'background-color: #33cc33'; //green
return '';
}
?>
<?php
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["name"].'</td>
<td>'.$row["genre"].'</td>
<td>'.$row["time"].'</td>
<td>'.$row["status"].'</td>
<td>'.$row["more"].'</td>
</tr>
';
}
echo $output;
{
echo 'Data Not Found';
}
?>
You have already created function to color the rows but haven't called the function during showing output. That's why it is not working.
Change your $output variable with:
$output .= '
<tr>
<td>'.$row["name"].'</td>
<td>'.$row["genre"].'</td>
<td>'.$row["time"].'</td>
<td style="'.status_style($row["status"]).'">'.$row["status"].'</td>
<td>'.$row["more"].'</td>
</tr>
';
You can try something like this:
<?php
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
$output .= '<div class="table-responsive">
<table class="table table bordered">
<tr>
<th>name</th>
<th>genre</th>
<th>time</th>
<th>status</th>
<th>more...</th>
</tr>';
function status_style($row) {
if ($row < 2) return $color = '#ff0000'; //red
if ($row > 3) return $color = '#33cc33'; //green
return $color = '';
}
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr style='"'background-color: '"' . $color . '"'>
<td>'.$row["name"].'</td>
<td>'.$row["genre"].'</td>
<td>'.$row["time"].'</td>
<td>'.$row["status"].'</td>
<td>'.$row["more"].'</td>
</tr>
';
}
echo $output;
{
echo 'Data Not Found';
}
You didn't do anything with the background color.

Mysql search query with php

I need to do a mysql query that will search (in columns) nome and cognome (in the Prenotazioni table).
The problem is: the query must search name and surname or surname and name (they can also be 3 or 4 values) and I have only 1 search form. If I have two john with different surnames (black and white), and i put in search form john black or white john, The query will give me both results
How i can fix it?
index.php
<table border="10">
<tr>
<td align="center"><b>CERCA</b></td>
</tr>
<tr>
<td>
<table>
<form method="post" action="./cerca.php">
<tr>
<td>Cerca:</td>
<td width="250" align="left"><input type="text" autocomplete="off" size="70" name="ricerca"/>
</td>
</tr>
<tr>
<td></td>
<td align="left"><input style="width:100px;" type="submit" value="Cerca"/>
</tr>
</form>
</table>
</td>
</tr>
</table>
cerca.php
<!doctype html><html>
<head>
<meta charset="utf-8">
<title>Ricerca</title>
</head>
<body>
<?php
include('./config.php');
$name = isset($_POST['ricerca']) ? preg_replace('/\s+/', ' ', trim($_POST['ricerca'])) : '';
if ($name != '') {
$q = 'SELECT * FROM Prenotazioni WHERE 1=2';
$parts = explode(' ', $name);
foreach ($parts as $part) {
$q .= ' OR nome LIKE \'%' . $part . '\' OR cognome LIKE \'%' . $part . '\'';
}
$exec = mysql_query($q);
if (mysql_num_rows($exec)) {
echo '<tr>
<td width="242">
<table border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr></tr>
<tr>
<td align="center" bgcolor="#FFFFFF"><strong>Nome</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Cognome</strong></td>
</tr>';
while($row = mysql_fetch_array($exec)) {
echo '<tr>
<td align="center" bgcolor="#FFFFFF">' . $row['nome'] . '</td>
<td align="center" bgcolor="#FFFFFF">' . $row['cognome'] . '</td> <p><br>
<br>';
}
}
else {
echo 'Errore: nessuna corrispondenza trovata.';
}
}
else {
echo 'Errore: Inserisci un nome.';
}
?>
I faced this same challenge 3days ago and this resolved it: modify line 6-11 of cerca.php to this
$parts = explode(" ",$name);
$count = count($parts);
$q = "SELECT * FROM Prenotazioni WHERE";
for($i = 0;$i < $count;$i++)
{
if($i != $count-1)
$q = $q.
" (nome LIKE '%".$parts[$i]."%' OR
cognome LIKE '%".$parts[$i]."%') AND ";
else
$q = $q.
" (nome LIKE '%".$parts[$i]."%' OR
cognome LIKE '%".$parts[$i]."%')";
}
$exec = mysql_query($q);
Given "john black","black john" and "bla joh" return the SAME RESULTS and "white john" isn't the same as "black john". I hope it helps you
make mysql query string using above mu code and run this query and make row according to you
$q = "SELECT * FROM Prenotazioni WHERE";
for($i = 0;$i < $count;$i++)
{
if($i) {
$q .= ' and ';
}
$q = $q . " (concat(nome, ' ', cognome) LIKE '%'.$parts[$i].'%')";
}
EDIT
GOOD CODE:
$name = isset($_POST['ricerca']) ? preg_replace('/\s+/', ' ', trim($_POST['ricerca'])) : '';
if ($name != '') {
$parts = explode(" ",$name);
$count = count($parts);
$q = "SELECT * FROM Prenotazioni WHERE";
for($i = 0;$i < $count;$i++)
{
if($i != $count-1)
$q = $q.
" (nome LIKE '%".$parts[$i]."%' OR
cognome LIKE '%".$parts[$i]."%') AND ";
else
$q = $q.
" (nome LIKE '%".$parts[$i]."%' OR
cognome LIKE '%".$parts[$i]."%')";
}
$exec = mysql_query($q);
if (mysql_num_rows($exec)) {
echo '<tr>
<table border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr></tr>
<tr>
<td align="center" bgcolor="#FFFFFF"><strong>Nome</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Cognome</strong></td>
</tr>';
while($row = mysql_fetch_array($exec)) {
echo '<tr>
<td align="center" bgcolor="#FFFFFF">' . $row['nome'] . '</td>
<td align="center" bgcolor="#FFFFFF">' . $row['cognome'] . '</td>';
}
}
else {
echo 'Errore: nessuna corrispondenza trovata.';
}
}
else {
echo 'Errore: Inserisci un nome.';
}

PHP - Inputting data into MySQL cell

I am trying put row['user'] into cell but it doesn't work.
When I uncomment "echo" it works fine.
PHP:
function mod_Something($database)
{
$sql = "SELECT user FROM table_name";
if ($result = mysqli_query($database, $sql)) {
while ($row = mysqli_fetch_assoc($result)) {
$html = $html . '<tr><td>' . $row['user'] . '</td></tr>';
// echo $row['user'];
}
return $html;
}
}
I also have a HTML view file where I have:
<table id="data-table-basic" class="table table-striped">
<thead>
<tr>
<th>user</th>
</tr>
</thead>
<tbody>
%mod_Something%
</tbody>
</table>
I know that HTML isn't a function but I must return it because there is a script which allows to return "view".
try this:
function mod_Something($database)
{
$sql = "SELECT user FROM table_name";
if ($result = mysqli_query($database, $sql)) {
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr><td>' . $row['user']; . '</td></tr>';
}
return $html;
}
}
<table id="data-table-basic" class="table table-striped">
<thead>
<tr>
<th>user</th>
</tr>
</thead>
<tbody>
<?php mod_Something(); ?>
</tbody>
</table>
You are not getting anything from the function mod_Something because you are not building & returning HTML properly.
See the below code.
function mod_Something($database)
{
$html = "";
$sql = "SELECT user FROM table_name";
if ($result = mysqli_query($database, $sql)) {
while ($row = mysqli_fetch_assoc($result)) {
$html .= '<tr><td>' . $row['user'] . '</td></tr>';
}
}
return $html;
}
Hope this works for you!

php / MySQL - How to join 'cart' table together

How can I join this table together, showing the header only once? I have tried to take th out of the while loop but I had no luck, maybe I made a mistake?
Here is my code so far:
<?php
function cart() {
foreach($_SESSION as $name => $value) {
if ($value>0){
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id));?>
<center>
<table class='menufinal' border=0 width=75%>
<th>Remove Item</th>
<th>Item Name</th>
<th>Item Price</th>
<th>Quantity</th>
<th>Line Total</th>
<?php while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;?>
<tr>
<td><?echo '<img src="x.png"><br>'?></td>
<td><?echo $get_row['name']?></td>
<td><?echo '&pound' . number_format($get_row['price'], 2);?></td>
<td><?echo '- ' .$value. ' +' ?> </td>
<td> <?echo '&pound ' . number_format($sub, 2);?> </td>
</tr>
<?
}
}
if (empty($total)) {
if (empty($sub)) {
//do nothing
} else {
$total = $sub;
}
} else {
$total += $sub;
}
}
}
if (!empty($total)){
echo '<br>Total: &pound' . number_format($total, 2) . '<br>';
echo '<div id="dorc"><p><img src="dishes.png" width="240" height="152"> <img src="spacer.png" width="200"> <img src="checkout.png" width="240" height="152">';
} else {
header ('Location: index.php');
}
}
?>
Currently this code displays:
Change the script as follow. Hope it will work
$value) {
if ($value>0){
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id));?>
if($i==0)
{
print("
<center>
<table class='menufinal' border=0 width=75%>
<th>Remove Item</th>
<th>Item Name</th>
<th>Item Price</th>
<th>Quantity</th>
<th>Line Total</th>
</tr>
");
}
$i++;
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;?>
<tr>
<td><?echo '<img src="x.png"><br>'?></td>
<td><?echo $get_row['name']?></td>
<td><?echo '&pound' . number_format($get_row['price'], 2);?></td>
<td><?echo '- ' .$value. ' +' ?> </td>
<td> <?echo '&pound ' . number_format($sub, 2);?> </td>
</tr>
<?
}
}
if (empty($total)) {
if (empty($sub)) {
//do nothing
} else {
$total = $sub;
}
} else {
$total += $sub;
}
}
}
if (!empty($total)){
echo '<br>Total: &pound' . number_format($total, 2) . '<br>';
echo '<div id="dorc"><p><img src="dishes.png" width="240" height="152"> <img src="spacer.png" width="200"> <img src="checkout.png" width="240" height="152">';
} else {
header ('Location: index.php');
}
}
?>
Below is the corrected code, the creation of the table is moved outside of the loop.
Also the table header was added into a tr to create valid html, and also collected data into an $output variable to prevent a bug with your location redirect since data would already be written to the browser (you may vary well still have a problem with this since there is likely other output prior to this cart() function being called.
<?php
function cart() {
$output = '';
$output .= '<center>';
$output .= '<table class='menufinal' border=0 width=75%>';
$output .= '<tr>';
$output .= '<th>Remove Item</th>';
$output .= '<th>Item Name</th>';
$output .= '<th>Item Price</th>';
$output .= '<th>Quantity</th>';
$output .= '<th>Line Total</th>';
$output .= '</tr>';
foreach($_SESSION as $name => $value) {
if ($value>0){
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id));
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;
$output .= '<tr>';
$output .= '<td><img src="x.png"><br></td>';
$output .= '<td>' . $get_row['name'] . '</td>';
$output .= '<td>&pound ' . number_format($get_row['price'], 2) . '</td>';
$output .= '<td>- ' .$value. ' +</td>';
$output .= '<td>&pound ' . number_format($sub, 2) . '</td>';
$output .= '</tr>';
}
}
if (empty($total)) {
if (empty($sub)) {
//do nothing
} else {
$total = $sub;
}
} else {
$total += $sub;
}
}
}
$output .= '</table>';
if (empty($total)){
header ('Location: index.php');
exit;
}
$output .= '<br>Total: &pound' . number_format($total, 2) . '<br>';
$output .= '<div id="dorc"><p><img src="dishes.png" width="240" height="152"> <img src="spacer.png" width="200"> <img src="checkout.png" width="240" height="152">';
echo $output;
}
?>

php Multiple looping problem

Im writing a function to get the parent and child ids but for the third loop there is a
problem the loop gets even the previous loops id also .
How can i avoid it?
<?
$results = '
<table>
<thead>
<tr >
<td id="ticket" align="center" ><b>Task<br />ID</b></td>
<td id="ticket" align="center" ><b>col1</td>
<td id="ticket" align="center" ><b>col2</td>
</tr>
</thead>
<tbody>';
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
{
$results .='
<tr >
<td align="center">
'.$row['Task_id'].'
</td>';
$results .= '<td align="center">';
$gg = mysqli_query($dbc,"select * from Tasks where ParentTask_Id='".$row['Task_id']."'");
echo "<br>";
while ($rowdd = mysqli_fetch_assoc($gg))
{
$results .= $rowdd['Task_id']."<br><br>";
$gg2 = mysqli_query($dbc,"select * from Tasks where ParentTask_Id='".$rowdd['Task_id']."'");
while ($rowdd2 = mysqli_fetch_assoc($gg2))
{
$results2 = $rowdd2['Task_id']."<br><br>";
}
echo "<br>";
}
// $results .= $car ;
// $results .= $t;
$results .='</td>';
$results .=' <td align="left" >'?>
<?
$results .= $results2;
$results .='</td>';
$results .='
</tr>';
}
?>
Is the $results variable empty? I only see it being concatenated.
Also, on your table you have multiple ids that are the same. You either need to change that to a class or have a unique value for each id.

Categories