It only shows 'No record found' but I'm trying to show the records between the dates using search
<?php
$query = "SELECT title, count(title) as totalnotary , notary_date, book_no
FROM tbl_notary ";
// Date filter
if (isset($_POST['search'])) {
$fromDate = $_POST['fromDate'];
$endDate = $_POST['endDate'];
if (!empty($fromDate) && !empty($endDate)) {
$query = "SELECT title, count(title) as totalnotary ,
notary_date, book_no
FROM tbl_notary
Where 1
and notary_date between '" . $fromDate . "' and '" . $endDate . "'";
}
}
// Sort
$query .= " group by book_no,title,Year(notary_date),
month(notary_date),day(notary_date)
ORDER BY notary_date DESC";
$Records = mysqli_query($conn, $query);
// Check records found or not
if (mysqli_num_rows($Records) > 0) {
while ($Record = mysqli_fetch_assoc($Records)) {
$book_no = $Record['book_no'];
$title = $Record['title'];
$totalnotary = $Record['totalnotary'];
$notary_date = date("F j,Y", strtotime($Record['notary_date']));
echo "<tr>";
echo "<td>" . $book_no . "</td>";
echo "<td>" . $title . "</td>";
echo "<td>" . $totalnotary . "</td>";
echo "<td>" . $notary_date . "</td>";
echo "</tr>";
}
} else {
echo "<tr>";
echo "<td colspan='4'>No record found.</td>";
echo "</tr>";
}
?>
OK, so you are saying that here is your problem.
$query = "SELECT title, count(title) as totalnotary , notary_date, book_no
FROM tbl_notary
Where 1 //!!!!!What is this 1???? You are missing a column name probably
and notary_date between '" . $fromDate . "' and '" . $endDate . "'";
Try this query. You have WHERE 1 which can't do anything and probably is failing your query. Try with some error reporting and you will probably get an error there.
$query = "SELECT title, count(title) as totalnotary , notary_date, book_no
FROM tbl_notary
WHERE notary_date BETWEEN '" . $fromDate . "' AND '" . $endDate . "'";
Then put this below to see if query is failing or not.
$Records = mysqli_query($conn, $query) or die (mysqli_error($conn));
Related
i cant seem to make each movie appear under their respective date and have a correct time dropdown menu for the movie time selection
$sql_date = "SELECT DISTINCT sDate, sTitle FROM movieScreenings ";
$result_date = mysqli_query($db, $sql_date);
while($row = mysqli_fetch_array($result_date)) {
echo "<h2>" . $row['sDate'] . "</h2>";
$sql_movie = "SELECT * FROM movieList, movieScreenings WHERE title = '" . $row['sTitle'] . "'";
$result_movie = mysqli_query($db, $sql_movie);
while($row2 = mysqli_fetch_array($result_movie)) {
echo "<div class='box'>
<img class='poster' src='posters/" . $row2['poster'] . "'/>
<h2>" . $row2['title'] . "</h2>
<p>" . $row2['description'] . "</p>";
$sql_time = "SELECT DISTINCT sTime FROM movieScreenings WHERE sTitle = '" . $row2['title'] . "' AND sDate = '" . $row['sDate'] . "'";
$result_time = mysqli_query($db, $sql_time);
while($row3 = mysqli_fetch_array($result_time)) {
echo "<select name='sTime'>
<option value='" . $row3['sTime'] . "'>" . $row3['sTime'] . "</option>
</select>";
echo "</div>";
}
}
}
mysqli_free_result($result_date);
mysqli_free_result($result_movie);
mysqli_free_result($result_time);
// Close connection
mysqli_close($db);
You probably want to sort your first query by sDate with ORDER BY sDate.
In your second query you are again selecting from movieScreenings which is not necessary because I imagine all the data you are accessing (poster, title, description) is stored in movieList.
Thirdly, in your last while loop you are repeating the <select> tag for every screening time. This will result in multiple dropdown inputs. Also, you are including the closing </div> tag in this loop. So only loop the <option> tag.
With these changes your code would look like this:
$sql_date = "SELECT DISTINCT sDate, sTitle FROM movieScreenings ORDER BY sDate ASC";
$result_date = mysqli_query($db, $sql_date);
while($row = mysqli_fetch_array($result_date)) {
echo "<h2>" . $row['sDate'] . "</h2>";
$sql_movie = "SELECT * FROM movieList WHERE title = '" . $row['sTitle'] . "'";
$result_movie = mysqli_query($db, $sql_movie);
while($row2 = mysqli_fetch_array($result_movie)) {
echo "<div class='box'>
<img class='poster' src='posters/" . $row2['poster'] . "'/>
<h2>" . $row2['title'] . "</h2>
<p>" . $row2['description'] . "</p>";
$sql_time = "SELECT DISTINCT sTime FROM movieScreenings WHERE sTitle = '" . $row2['title'] . "' AND sDate = '" . $row['sDate'] . "'";
$result_time = mysqli_query($db, $sql_time);
echo "<select name='sTime'>";
while($row3 = mysqli_fetch_array($result_time)) {
echo "<option value='" . $row3['sTime'] . "'>" . $row3['sTime'] . "</option>";
}
echo "</select>";
echo "</div>";
}
}
...
And lastly, I would suggest using ID's as your foreign keys to prevent duplicates in the future. There might be multiple movies with the same title.
I'm learning PHP, I create a page to show my data table in MySQL. I want to paging, searching, sorting with that data.
I write some conditions to search and sort. But it's not working. My Paging seem work fine.
<?php
echo "<form action = 'Activiti_Data_Table_Detail.php' method = 'post'>";
echo "<input type = 'text' name = 'valueToSearch' placeholder = 'Value to search'><br><br>";
echo "<input type = 'submit' name = 'search' value = 'Filter'><br><br>";
echo "<input type = 'submit' name = 'ASC' value = 'Ascending'><br><br>";
echo "<input type = 'submit' name = 'DESC' value = 'Descending'><br><br>";
echo "<table id = 'datatable' border='1'>";
echo "<thead>";
echo "<tr>";
echo "<th>ID_</th>";
echo "<th>PROC_INST_ID_</th>";
echo "<th>BUSINESS_KEY_</th>";
echo "<th>PROC_DEF_ID_</th>";
echo "<th>START_TIME_</th>";
echo "<th>END_TIME_</th>";
echo "<th>DURATION_</th>";
echo "<th>START_USER_ID_</th>";
echo "<th>START_ACT_ID_</th>";
echo "<th>END_ACT_ID_</th>";
echo "<th>SUPER_PROCESS_INSTANCE_ID_</th>";
echo "<th>DELETE_REASON_</th>";
echo "<th>TENANT_ID_</th>";
echo "<th>NAME_</th>";
echo "</tr>";
echo "</thead>";
//connect to database
$con=mysqli_connect("localhost","root","123456","activiti") or die(mysqli_connect_errno());
//define how many results per page
$result_per_page = 10;
//number of results stored in database
$sql = "SELECT * FROM act_hi_procinst";
$result = mysqli_query($con, $sql);
$num_of_results = mysqli_num_rows($result); //225
//determine number of total pages available
$number_of_pages = ceil($num_of_results/$result_per_page);
//determine current page is on
if (!isset($_GET['page'])) {
$page = 1;
} else {
$page = $_GET['page'];
}
//determine the SQL LIMIT starting number for the result on the displaying page
$this_page_first_result = ($page - 1) * $result_per_page;
if(isset($_POST['search']) & isset($_POST['ASC'])){
$valueToSearch = $_POST['valueToSearch'];
$sql = "SELECT * FROM act_hi_procinst WHERE concat(ID_, PROC_INST_ID_, PROC_DEF_ID_, START_TIME_, END_TIME_, DURATION_) LIKE '%".$valueToSearch."%'
ORDER BY END_TIME_ ASC LIMIT" .$this_page_first_result . ',' . $result_per_page;
} elseif (isset($_POST['search']) & isset($_POST['DESC'])) {
$valueToSearch = $_POST['valueToSearch'];
$sql = "SELECT * FROM act_hi_procinst WHERE concat(ID_, PROC_INST_ID_, PROC_DEF_ID_, START_TIME_, END_TIME_, DURATION_) LIKE '%".$valueToSearch."%'
ORDER BY END_TIME_ DESC LIMIT" .$this_page_first_result . ',' . $result_per_page;
} elseif (isset($_POST['search'])) {
$valueToSearch = $_POST['valueToSearch'];
$sql = "SELECT * FROM act_hi_procinst WHERE concat(ID_, PROC_INST_ID_, PROC_DEF_ID_, START_TIME_, END_TIME_, DURATION_) LIKE '%".$valueToSearch."%'
LIMIT" .$this_page_first_result . ',' . $result_per_page;
} elseif (isset($_POST['ASC'])) {
$sql = "SELECT * FROM act_hi_procinst ORDER BY END_TIME_ ASC LIMIT" .$this_page_first_result . ',' . $result_per_page;
} elseif (isset($_POST['DESC'])) {
$sql = "SELECT * FROM act_hi_procinst ORDER BY END_TIME_ DESC LIMIT" .$this_page_first_result . ',' . $result_per_page;
} else {
$sql = "SELECT * FROM act_hi_procinst LIMIT" .$this_page_first_result . ',' . $result_per_page;
}
$result = mysqli_query($con, $sql);
while ($row = mysqli_fetch_array($result)) {
echo "<tbody>";
echo "<tr>";
echo "<td><a href='Data_Table_Detail.php' target='_blank'>" . $row['ID_'] . "</a></td>";
echo "<td>" . $row['PROC_INST_ID_'] . "</td>";
echo "<td>" . $row['BUSINESS_KEY_'] . "</td>";
echo "<td>" . $row['PROC_DEF_ID_'] . "</td>";
echo "<td>" . $row['START_TIME_'] . "</td>";
echo "<td>" . $row['END_TIME_'] . "</td>";
echo "<td>" . $row['DURATION_'] . "</td>";
echo "<td>" . $row['START_USER_ID_'] . "</td>";
echo "<td>" . $row['START_ACT_ID_'] . "</td>";
echo "<td>" . $row['END_ACT_ID_'] . "</td>";
echo "<td>" . $row['SUPER_PROCESS_INSTANCE_ID_'] . "</td>";
echo "<td>" . $row['DELETE_REASON_'] . "</td>";
echo "<td>" . $row['TENANT_ID_'] . "</td>";
echo "<td>" . $row['NAME_'] . "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "</form>";
//display the links to the page
for($page = 1; $page <= $number_of_pages; $page++) {
echo '' . $page . ' ';
}
mysqli_close($con);
?>
When I run this, it say: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result.
Can you help me?
Thank you!
try change all your ifs for this:
$valueToSearch = isset($_POST['search']) ? $_POST['valueToSearch'] : '';
$orderBy = isset($_POST['ASC']) ? 'ORDER BY END_TIME_ ASC' : (isset($_POST['DESC']) ? 'ORDER BY END_TIME_ DESC' : '');
$sql = "SELECT * FROM act_hi_procinst WHERE concat(ID_, PROC_INST_ID_, PROC_DEF_ID_, START_TIME_, END_TIME_, DURATION_) LIKE '%".$valueToSearch."%' " . $orderBy . " LIMIT " . $this_page_first_result . ',' . $result_per_page;
the below code is getting some values from DB by "select option form" , i recently added Pagination snip to limit the results, when i run the code it fetch 5 recorders as defined,but didn't show the remaining number of pages.
what im doing wrong here ?
<?php
$per_page = 5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page = 1;
}
$start_from = ($page - 1) * $per_page;
if (!empty($_POST['form_val']) && isset($_POST['form_val'])) {
$_POST['form_val'] = 0;
$sql = "SELECT u.log_id , u.user_name, s.site, u.date ,u.comment , l.location, e.picture FROM `pool` u, `location_all` l , `site_all` s JOIN db2.user e
where l.location_id = u.location and s.site_id = u.site and e.user_id = u.user_id";
if (!empty($_POST['Location']) && isset($_POST['Location'])) {
$sql = $sql . " AND location =" . $_POST['Location'];
}
$strtdate = $_POST['Sday'];
$enddate = $_POST['Eday'];
if (!empty($_POST['Sday']) && isset($_POST['Sday']) && !empty($_POST['Eday']) && isset($_POST['Eday'])) {
$sql = $sql . " AND date between '" . $strtdate . "' and '" . $enddate . "'";
} elseif (!empty($_POST['Sday']) && isset($_POST['Sday'])) {
$sql = $sql . " AND date>='" . $strtdate . "'";
} elseif (!empty($_POST['Eday']) && isset($_POST['Eday']))
$sql = $sql . " AND date<='" . $enddate . "'";
}
if (!empty($_POST['Site']) && isset($_POST['Site'])) {
$sql = $sql . " AND u.site=" . $_POST['Site'];
}
$sql = $sql . " LIMIT $start_from, $per_page";
if (mysqli_query($conn, $sql)) {
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) >= 1) {
$rowcount = mysqli_num_rows($result);
echo '<legend> ' . $rowcount . ' Records Found !!!</legend>';
echo '<br><br>';
echo "<table class='srchtable'>
<tr>
<th>Picture</th>
<th>Date</th>
<th>User Name</th>
<th>country</th>
<th>Location</th>
<th>Site</th>
<th>Comment</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td> <img src='" . $row['picture'] . "' alt='' style='width:70%; height:auto; border-radius: 50%;'> </td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['user_name'] . "</td>";
echo "<td>" . $row['country'] . "</td>";
echo "<td>" . $row['location'] . "</td>";
echo "<td>" . $row['site'] . "</td>";
echo "<td>" . $row['comment'] . "</td>";
echo "</tr>";
}
echo "</table>";
$total_pages = ceil($rowcount / $per_page);
echo "<center><a href='?page=1'>" . 'First Page' . "</a> ";
for ($i = 1; $i <= $total_pages; $i++) {
echo "<a href='?page=" . $i . "'>" . $i . "</a> ";
}
echo "<a href='?page=$total_pages'>" . 'Last Page' . "</a></center> ";
} else {
echo '<p>No Results Found !!!</p>';
}
}
}
?>
As I said in my comments, for displaying pagination links:
You're counting total number of rows but incorporating LIMIT and OFFSET clauses in your SELECT query, this won't give the correct number of row count. Your SELECT query should not contain this part, ... LIMIT $start_from, $per_page.
Since you're filtering the results based on several $_POST data, you should incorporate those conditions in your pagination links as well, otherwise when you visit a different page(through pagination link), you won't get the desired result, and that's because $_POST data will not be retained when you hop from page to page. Better that you change the method of your <form> from POST to GET, because in this way it'd be easier for you to catch and manipulate things when you hop from one page to another using pagination links.
So based on the above points, your code should be like this:
$per_page = 5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page = 1;
}
$start_from = ($page - 1) * $per_page;
if (!empty($_GET['form_val']) && isset($_GET['form_val'])) {
$_GET['form_val'] = 0;
$sql = "SELECT u.log_id , u.user_name, s.site, u.date ,u.comment , l.location, e.picture FROM `pool` u, `location_all` l , `site_all` s JOIN db2.user e
where l.location_id = u.location and s.site_id = u.site and e.user_id = u.user_id";
if (!empty($_GET['Location']) && isset($_GET['Location'])) {
$sql = $sql . " AND location =" . $_GET['Location'];
}
$strtdate = $_GET['Sday'];
$enddate = $_GET['Eday'];
if (!empty($_GET['Sday']) && isset($_GET['Sday']) && !empty($_GET['Eday']) && isset($_GET['Eday'])) {
$sql = $sql . " AND date between '" . $strtdate . "' and '" . $enddate . "'";
} elseif (!empty($_GET['Sday']) && isset($_GET['Sday'])) {
$sql = $sql . " AND date>='" . $strtdate . "'";
} elseif (!empty($_GET['Eday']) && isset($_GET['Eday'])) {
$sql = $sql . " AND date<='" . $enddate . "'";
}
if (!empty($_GET['Site']) && isset($_GET['Site'])) {
$sql = $sql . " AND u.site=" . $_GET['Site'];
}
$data_query = $sql . " LIMIT $start_from, $per_page";
$result = mysqli_query($conn, $data_query);
if (mysqli_num_rows($result) >= 1) {
$rowcount = mysqli_num_rows($result);
echo '<legend> ' . $rowcount . ' Records Found !!!</legend>';
echo '<br><br>';
echo "<table class='srchtable'>
<tr>
<th>Picture</th>
<th>Date</th>
<th>User Name</th>
<th>country</th>
<th>Location</th>
<th>Site</th>
<th>Comment</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td> <img src='" . $row['picture'] . "' alt='' style='width:70%; height:auto; border-radius: 50%;'> </td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['user_name'] . "</td>";
echo "<td>" . $row['country'] . "</td>";
echo "<td>" . $row['location'] . "</td>";
echo "<td>" . $row['site'] . "</td>";
echo "<td>" . $row['comment'] . "</td>";
echo "</tr>";
}
echo "</table>";
$query_result = mysqli_query($conn, $sql);
$total_rows = mysqli_num_rows($query_result);
$total_pages = ceil($total_rows / $per_page);
parse_str($_SERVER["QUERY_STRING"], $url_array);
unset($url_array['page']);
$url = http_build_query($url_array);
?>
<center>First Page
<?php
for ($i = 1; $i <= $total_pages; $i++) {
?>
<?php echo $i; ?>
<?php
}
?>
Last Page</center>
<?php
} else {
echo '<p>No Results Found !!!</p>';
}
}
I'm trying to design a webpage where it simply shows info from a database using PHP.
I almost solve all of my problem but it's the first time I'm using HTML and I am having trouble finding a solution for my problem.
The code I have so far is:
<?php
$connection = mysql_connect('localhost','XXXX','XXXX');
mysql_select_db('cardata_laptimer');
echo "<table>";
echo "<table border = 1>";
$query = "SELECT * from tempos GROUP BY piloto";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
$query = "SELECT * from tempos WHERE piloto = '" . $row['piloto'] . "' ORDER BY tempo";
$resultado = mysql_query($query);
echo "<td>" . $row['piloto'] . "</td></tr>";
while($rows = mysql_fetch_array($resultado))
{
echo "<td>" . $rows['tempo'] . "</td><td>" . $rows['data'] . "</td></tr>";
}
}
The output I'm getting can be seen in www.cardata.pt
1st problem: How to I make the "piloto" (for example AA) occupy the space of 2 cells?
2nd problem: I want the table to show "pilotos" side by side and the info for each one (tempo and data) down the piloto name.
Thanks in advance
To occupy the space of 2 cells add : colspan="2"
here is my edit of your code :
<?php
$connection = mysql_connect('localhost','XXXX','XXXX');
mysql_select_db('cardata_laptimer');
echo '<table border="0"><tr>';
$query = "SELECT * from tempos GROUP BY piloto";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
echo '<td><table border="1"><tr>';
$query = "SELECT * from tempos WHERE piloto = '" . $row['piloto'] . "' ORDER BY tempo";
$resultado = mysql_query($query);
echo '<td colspan="2">' . $row['piloto'] . "</td></tr><tr>";
while($rows = mysql_fetch_array($resultado))
{
echo "<td>" . $rows['tempo'] . "</td><td>" . $rows['data'] . "</td>";
}
echo '</tr></table></td>';
}
echo '</tr></table>';
?>
I'm new to PHP & MySQL and was trying to use php to create query for MySQL below is the current state of the code and it's still WIP but the if/elseif structure already gives the desired output which varies according to data input, which for example could be as follows
"SELECT * FROM tuntikortti WHERE PVM BETWEEN '2014-09-11' AND '2014-09-27' AND Henkilo =
'Van' AND Tyonumero = '123456' AND Toiminto = '123';"
I've confirmed that the query line itself works in MySQL but I would need to somehow get it inside the MySQL_query($con, The query here).
So how do I get the php output of whole if/elseif query line build in variable or similar to be used?
<?php
include("db_connect.php");
$pvm1 = $_POST['pvm1'];
$pvm2 = $_POST['pvm2'];
$henkilo = $_POST['henkilo'];
$tyonumero = $_POST['tyonumero'];
$toiminto = $_POST['toiminto'];
/* MySQL query line build, beginning */
ECHO "SELECT * FROM tuntikortti WHERE ";
/* PVM */
if(!empty($pvm1) && !empty($pvm2)){
echo "PVM BETWEEN '". $pvm1 ."' AND '". $pvm2 ."'";
} elseif(!empty($pvm1)) {
echo "PVM = '". $pvm1 ."'";
};
/* Henkilo */
if(!empty($henkilo) && !empty($pvm1)){
echo " AND Henkilo = '". $henkilo ."'";
} elseif(!empty($henkilo)) {
echo " Henkilo = '". $henkilo ."'";
};
/* Tyonumero */
if(!empty($tyonumero) && (!empty($pvm1) || !empty($henkilo))){
echo " AND Tyonumero = '". $tyonumero ."'";
} elseif(!empty($tyonumero)) {
echo " Tyonumero = '". $tyonumero ."'";
};
/* Toiminto */
if(!empty($toiminto) && (!empty($tyonumero) || !empty($pvm1) || !empty($henkilo))){
echo " AND Toiminto = '". $toiminto ."'";
} elseif(!empty($toiminto)) {
echo " Toiminto = '". $toiminto ."'";
};
echo ";";
$query = mysqli_query($sql,$con);
echo "<table border='1'>
<tr>
<th>TID</th>
<th>PVM</th>
<th>Henkilo</th>
<th>Tyƶnumero</th>
<th>Toiminto</th>
<th>Tunnit</th>
<th>Selite</th>
</tr>";
while($row = mysqli_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $row['TID'] . "</td>";
echo "<td>" . $row['PVM'] . "</td>";
echo "<td>" . $row['Henkilo'] . "</td>";
echo "<td>" . $row['Tyonumero'] . "</td>";
echo "<td>" . $row['Toiminto'] . "</td>";
echo "<td>" . $row['Tunnit'] . "</td>";
echo "<td class='selitetyyli'>" . $row['Selite'] . "</td>";
echo "</tr>";
}
echo "</table>";
include("db_close_connection.php");
?>
Check this example and try to modify your code,
$sql = '';
$sql = "SELECT * FROM tablename WHERE ";
if(condition){
$sql.= "fieldname = 'value'";
} elseif(condition) {
$sql.= "fieldname = 'value'";
};
$sql.= ";";
$query = mysqli_query($con,$sql);