How can I add LIMIT query in it? I have adding it in
$SQL = "SELECT * FROM eins_policy LIMIT 500 ";
but it did not display the list.
For example:
If I choose 500 records per page;
Total records is 3560 records;
If now I on page 1, meaning (Page number – 1) * 500 = 0, and my Offset is 0;
If now I on page 2, meaning (Page number – 1) * 500 = (2 – 1) * 500 = 500, and Offset is 500, meaning 501 to 1000 is the records fetched;
If now I on page 3, meaning (Page number – 1) * 500 = (3 – 1) * 500 = 1000, and Offset is 1000, meaning 1001 to 1500 is the records fetched;
policylist.php
function eins_listPolicy($page, $record ,$search)
{
//connect to database
base_connectDatabase();
$searchs = explode(" ", $search);
$SQL = "SELECT * FROM eins_policy ";
$countSearch = 0;
foreach ($searchs as &$value) {
if ($countSearch == 0) {
$SQL .= "WHERE ";
} else {
$SQL .= "AND ";
}
$SQL .= "(eins_plcy_code LIKE '%".$value."%' OR
eins_plcy_client IN (SELECT eins_clnt_id FROM eins_client WHERE eins_clnt_full_name LIKE '%".$value."%') OR
eins_plcy_insurance_company IN (SELECT eins_icomp_id FROM eins_insurance_company WHERE eins_icomp_name LIKE '%".$value."%') OR
eins_plcy_agent IN (SELECT eins_agent_id FROM eins_agent WHERE eins_agent_user_id IN (SELECT base_u_id FROM base_users WHERE base_u_name LIKE '%".$value."%')) OR
eins_plcy_policy_type IN (SELECT base_u_id FROM base_users WHERE base_u_name LIKE '%".$value."%') OR
eins_plcy_start_date LIKE '%".$value."%' OR
eins_plcy_end_date LIKE '%".$value."%' OR
eins_plcy_payment_status = '".$value."' OR
eins_plcy_status LIKE '%".$value."%')
";
$countSearch ++;
}
$result = base_executeSQL($SQL);
$total = base_num_rows($result);
if(is_int($total/$record)) {
$tpages = $total/$record;
} else {
$tpages = floor($total/$record)+1;
}
$start = $record * ($page-1);
$adjacents = 3;
$reload = 'policy_list.php';
$count = 0;
$zeroRecord = false;
$RSQL = "LIMIT ".$start.", ".$record."";
$getPolicySQL = base_executeSQL($SQL.$RSQL);
while($Policy_row = base_fetch_array($getPolicySQL))
if (base_num_rows($getPolicySQL)!= 0) {
$inscompName = eins_getInscompDetail($Policy_row["eins_plcy_insurance_company"]);
$agentName = eins_getAgentDetail($Policy_row["eins_plcy_agent"]);
$clientName = eins_getClientDetail($Policy_row["eins_plcy_client"]);
$polocytypeName = eins_getPolicytypeDetail($Policy_row["eins_plcy_policy_type"]);
$count = $count + 1;
echo "<tr>";
echo "<td>". $count ."</td>";
echo "<td>". $Policy_row["eins_plcy_code"] ."</td>";
echo "<td>". $polocytypeName['eins_ptype_name'] ."</td>";
echo "<td>". $inscompName['eins_icomp_name'] ."</td>";
echo "<td>". $agentName['eins_agent_full_name'] ."</td>";
echo "<td>". $clientName['eins_clnt_full_name'] ."</td>";
echo "<td>". $Policy_row["eins_plcy_payment_status"] ."</td>";
echo "<td>". $Policy_row["eins_plcy_status"] ."</td>";
echo "<td><img src=\"". BASE_IMG_ICON_EDIT ."\" /></td>";
echo "<td><img src=\"". BASE_IMG_ICON_DELETE ."\" /></td>";
echo "</tr>";
}
if (base_num_rows($getPolicySQL)== 0) {
echo "<tr><td colspan = \"9\" align = \"center\">There is no record!</td></tr>";
$zeroRecord = true;
}
if($zeroRecord == false) {
echo "<tfoot bgcolor=\"white\">";
echo "<tr>";
echo "<td colspan=\"10\">";
$record .= "&search=".$search;
eins_paginate($reload, $page, $tpages, $adjacents, $record, $total) ;
echo "</td>";
echo "</tr>";
}
//close the database
base_closeDatabase();
}
The correct usage of LIMIT is LIMIT offset, max rows
First 500 rows : ... LIMIT 0,500
The next 500: ... LIMIT 500,500 and so on.
Reference: http://dev.mysql.com/doc/refman/5.7/en/select.html
Related
I want to try multiple while in one while with multiple tables but it's doesn't work.
I have three table and I want a different-different value from table.
Here is my code.
$table1 = GROUPMASTER;
$table2 = LEDGERMASTER;
$table3 = TRANSECTIONMASTER;
$groupmasterdata = mysql_query("select * from $table1 WHERE groupname = 'Capital' OR groupname = 'Fund and Reserves' OR groupname = 'Long Term liabilities' OR groupname = 'Current Liabilities' ");
echo "<table align='center' border=1>";
echo "<tr>";
echo "<td colspan=2 style='text-align:center'><b> CAPITAL and LIABILITIES</b></td>";
echo "<td style='text-align:center'><b> Amount </b></td>";
echo "</tr>";
while($recored = mysql_fetch_array($groupmasterdata))
{
$onegroupname = $recored ['groupname'];
echo "<tr>";
echo "<td colspan=3><b>" . strtoupper($onegroupname) . "</b></td></tr>";
$groupnamelist = mysql_query("select groupname from $table1 WHERE under = '". $onegroupname ."'");
while($data = mysql_fetch_array($groupnamelist))
{
$gname = $data ['groupname'];
$getledgername = mysql_query("select ledgername from WHERE groupname = '". $gname ."'");
while($lname = mysql_fetch_array($getledgername))
{
$gettocashbal = mysql_query("select *, SUM(amount) AS totalto from $table3 WHERE voucherto = '". $ledgername ."'");
while($datato = mysql_fetch_array($gettocashbal))
{
$tototal = $datato['totalto'];
}
$getbycashbal = mysql_query("select *, SUM(amount) AS totalby from $table3 WHERE voucherby = '". $ledgername ."' ");
while($databy = mysql_fetch_array($getbycashbal))
{
$bytotal = $databy['totalby'];
}
$ledgertotal = $opbal - $tototal + $bytotal;
echo "<tr>";
echo "<td class='test'>" . $gname . "</td>";
echo "<td>Rs. $ledgertotal</td>";
echo "<td></td>";
echo "</tr>";
}
}
echo "<tr>";
echo "<td style='text-align:right'>Total</td>";
echo "<td></td>";
echo "<td>Rs.</td>";
echo "</tr>";
}
echo "</table>";
I want to try multiple while but doesn't work.
How can I solve these issues?
I tried different way to solve this...
//TABLE 1 = GROUPMASTER
$table1 = $_SESSION['table1'];
//TABLE 2 = LEDGERMASTER
$table2 = $_SESSION['table2'];
//TABLE 3 = TRANSECTIONMASTER
$table3 = $_SESSION['table3'];
echo "<table align='center' border=1>";
echo "<tr>";
echo "<td colspan=2 style='text-align:center'><b> CAPITAL and LIABILITIES</b></td>";
echo "<td style='text-align:center'><b> Amount </b></td>";
echo "</tr>";
echo "<tr>";
$liabilitytotal = 0;
echo "<td colspan=3><b>CAPITAL</b></td></tr>";
$categorytotal = 0;
$groupnamelist = mysql_query("select groupname from $table1 WHERE under = 'Capital'");
while($data = mysql_fetch_array($groupnamelist))
{
$grouptotal = 0;
$gname = $data ['groupname'];
echo "<tr>";
echo "<td class='test'>" . $gname . "</td>";
$ledgername = mysql_query("SELECT ledgername from $table2 WHERE groupname = '". $gname ."' ");
while($datato = mysql_fetch_array($ledgername))
{
$lname = $datato['ledgername'];
$getbytotalquery = mysql_query("SELECT SUM(amount) FROM $table3 WHERE voucherby = '". $lname ."' ");
$gettototalquery = mysql_query("SELECT SUM(amount) FROM $table3 WHERE voucherto = '". $lname ."' ");
$getopbal = mysql_result($getopbalquery,0,0);
$getbytotal = mysql_result($getbytotalquery,0,0);
$gettototal = mysql_result($gettototalquery,0,0);
$result = $getopbal - $gettototal + $getbytotal;
if ($result == null)
{
$result = 0;
}
$grouptotal = $grouptotal + $result;
}
$categorytotal = $categorytotal + $grouptotal;
echo "<td style='text-align:right'>$grouptotal</td><td></td></tr>";
}
$liabilitytotal = $liabilitytotal + $categorytotal;
echo "<tr>";
echo "<td></td>";
echo "<td></td>";
echo "<td style='text-align:right'>$categorytotal</td>";
echo "</tr>";
echo "</table>";```
I tried my best..
And I will try to also solve this query to another way...
Thank you.. I think and I hope It's helpful..
I want to read out data from an sql-database an show them in a table. This works well. Now, I would like to show only those columns with at least one value in it and not the empty ones (containing NULL, 0, empty string). This works with the following example:
enter code here
<TABLE width="500" border="1" cellpadding="1" cellspacing="1">
<?php
$query = mysql_query("SELECT * FROM guestbook", $db);
$results = array();
while($line = mysql_fetch_assoc($query)){
$results[] = $line;
$Name = array_column($results, 'Name');
$Home = array_column($results, 'Home');
$Date = array_column($results, 'Date');
$Emptycolumn = array_column($results, 'Emptycolumn');
$Comment = array_column($results, 'Comment');
$City = array_column($results, 'City');
}
echo "<TR>";
if(array_filter($Name)) {echo "<TH>Name</TH>";}
if(array_filter($Home)){echo "<TH>Home</TH>";}
if(array_filter($Date)){echo "<TH>Date</TH>";}
if(array_filter($Emptycolumn)){echo "<TH>Emptycolumn</TH>";}
if(array_filter($Comment)){echo "<TH>Comment</TH>";}
if(array_filter($City)){echo "<TH>City</TH>";}
echo "</TR>";
$query = mysql_query("SELECT * FROM guestbook", $db);
while($line = mysql_fetch_assoc($query)){
echo "<TR>";
if(array_filter($Name)) {echo "<TD>".$line['Name']."</TD>";}
if(array_filter($Home)) {echo "<TD>".$line['Home']."</TD>";}
if(array_filter($Date)) {echo "<TD>".$line['Date']."</TD>";}
if(array_filter($Emptycolumn)) {echo "<TD>".$line['Emptycolumn']."</TD>";}
if(array_filter($Comment)) {echo "<TD>".$line['Comment']."</TD>";}
if(array_filter($City)) {echo "<TD>".$line['City']."</TD>";}
echo "</TR>";
}
?>
</TABLE>
Since the column-names of my table are highly variable (depending on the query), the table is generated by looping through the result-array, first the column-names, then the values in the rows:
enter code here
$sql = "SELECT DISTINCT $selection FROM $tabelle WHERE
$whereclause"; //will be changed to PDO
$result = mysqli_query($db, $sql) or die("<b>No result</b>"); //Running
the query and storing it in result
$numrows = mysqli_num_rows($result); // gets number of rows in result
table
$numcols = mysqli_num_fields($result); // gets number of columns in
result table
$field = mysqli_fetch_fields($result); // gets the column names from the
result table
if ($numrows > 0) {
echo "<table id='myTable' >";
echo "<thead>";
echo "<tr>";
echo "<th>" . 'Nr' . "</th>";
for($x=0;$x<$numcols;$x++){
$key = array_search($field[$x]->name, $custom_column_arr);
if($key !== false){
echo "<th>" . $key . "</th>";
}else{
echo "<th>" . $field[$x]->name . "</th>";
}
}
echo "</tr></thead>";
echo "<tbody>";
$nr = 1;
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $nr . "</td>";
for ($k=0; $k<$numcols; $k++) { // goes around until there are no
columns left
echo "<td>" . $row[$field[$k]->name] . "</td>"; //Prints the data
}
echo "</tr>";
$nr = $nr + 1;
} // End of while-loop
echo "</tbody></table>";
}
}
mysqli_close($db);
Now, I tried to integrate the array_column() and array_filter()-blocks of the example above into the loops, but unfortunately, it didn´t work. I´m sure, this is easy for a professional and I would be very grateful, if someone could help me with this problem!
Thank you very much in advance!!
I need to output a table based on two other tables as shown below:
case: there are two table "tbl_schedule" and "tbl_report"
this is my script:
$sql = mysql_query("SELECT*, count(*) as schedule_date FROM mst_schedule WHERE schedule_date LIKE '%$date' GROUP BY schedule_account") or die (mysql_error());
while ($data = mysql_fetch_array($sql)) {
$account = schAccount($data['schedule_account']);
$sql2 = mysql_query("SELECT * FROM trn_reportsch WHERE schedule_id='$data[schedule_id]' GROUP BY schedule_id");
echo "<tr>";
echo "<td>".ucfirst($account['admin_fullname'])."</td>";
while ($data2 = mysql_fetch_array($sql2)) {
echo "<td>".$data2['rating']."</td>";
}
echo "<td>".$data['schedule_date']."</td>";
echo "</tr>";
}
So far I don't get the desired output. How should I change the script?
Your code is almost correct.
Add following lines:
$sql = mysql_query("SELECT*, count(*) as schedule_date FROM mst_schedule WHERE schedule_date LIKE '%$date' GROUP BY schedule_account") or die(mysql_error());
while ($data = mysql_fetch_array($sql)) {
$account = schAccount($data['schedule_account']);
$sql2 = mysql_query("SELECT * FROM trn_reportsch WHERE schedule_id='$data[schedule_id]' GROUP BY schedule_id");
echo "<tr>";
echo "<td>" . ucfirst($account['admin_fullname']) . "</td>";
$bad = $good = $vGood = 0; // <-- ADD THIS LINE
while ($data2 = mysql_fetch_array($sql2)) {
if($data2['rating'] <=2){ // BAD
$bad++;
} else if($data2['rating'] <= 3){ // GOOD
$good++;
} else if($data2['rating'] > 3){ // VERY GOOD
$vGood++;
}
}
echo "<td>" . $bad . "</td>"; // Display the final value for bad
echo "<td>" . $good . "</td>"; // Display the final value for good
echo "<td>" . $vGood . "</td>"; // Display the final value for very good
echo "<td>" . $data['schedule_date'] . "</td>";
echo "</tr>";
}
I created a web site and i added search option which show search results in table, i want to limit rows number to 5 by page please help me to do that:
i would like also to make search results in random sort for each search operation, not the same results are shown on the first page each search :
this is my search code :
$query = mysql_query("SELECT * FROM table WHERE company LIKE '$company' and activity LIKE '$activity'");
echo "<h3>search results</h3><p>";
echo "<table border='1' align='center' >
<tr>
<th>phone</th>
<th>city</th>
<th>activity</th>
<th>company</th>
</tr>";
while($row = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>" . $row['phone']. "</td>";
echo "<td>" . $row['city']. "</td>";
echo "<td>" . $row['activity']. "</td>";
echo "<td>" . $row['company']. "</td>";
echo "</tr>";
}
echo "</table>";
$anymatches=mysql_num_rows($result );
if ($anymatches == 0)
{
echo "<h3>sorry no results</h3>";
}
?>
thank you.
Change your query first into
$query = mysqli_query($con,"SELECT * FROM table WHERE company LIKE '%$company%' and activity LIKE '%$activity%' ORDER BY RAND()");
We can use a counter to limit the fetching to 5.
$counter=1;
while($row = mysqli_fetch_array($query)){
if($counter<6){
echo "<td>" . $row['phone']. "</td>";
echo "<td>" . $row['city']. "</td>";
echo "<td>" . $row['activity']. "</td>";
echo "<td>" . $row['company']. "</td>";
}
else {
/* NOTHING TO DO */
}
$counter=$counter+1;
} /* END OF WHILE LOOP */
$anymatches=mysqli_num_rows($result);
if ($anymatches == 0)
{
echo "<h3>sorry no results</h3>";
}
OR you can try this, just changing your query into:
$query = mysqli_query("SELECT * FROM table WHERE company LIKE '%$company%' and activity LIKE '%$activity%' ORDER BY RAND() LIMIT 5");
Please make it clear, if you want to show only 5 results or a pagination with 5 rows per page.
If pagination is what you're looking for:
We should start by storing the data first into a table storage. For example we have a table named search with even just one field, searchfield.
$searchword=mysqli_real_escape_string($con,$_POST['searchword']);
mysqli_query($con,"UPDATE search SET searchfield='$searchword'");
/* this is where you store your search text for later purposes */
And we'll fetch it right away (Don't get me wrong, this is for the pagination purposes)
$selectsearch = "SELECT * FROM search";
$querysearch = mysqli_query($con, $selectsearch) or die(mysqli_error($selectsearch));
while($rows = mysqli_fetch_array($querysearch)){
$search = $rows['searchfield'];
}
$query = "SELECT * FROM table WHERE company LIKE '%$search%' OR activity LIKE '%$search%'"; /* do your query search */
$result = mysqli_query($con, $query);
$count = mysqli_num_rows($result);
$r = mysqli_fetch_row($result);
$numrows = $r[0];
echo '<b> '.$count.' result/s found for "'.$search.'"</b>';
$rowsperpage = 5;
$totalpages = ceil($count / $rowsperpage);
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
$currentpage = (int) $_GET['currentpage'];
} else {
$currentpage = 1;
}
if ($currentpage > $totalpages) {
$currentpage = $totalpages;
}
if ($currentpage < 1) {
$currentpage = 1;
}
$offset = ($currentpage - 1) * $rowsperpage;
$select="SELECT * FROM table WHERE company LIKE '%$search%' OR activity LIKE '%$search%' LIMIT $offset, $rowsperpage"; /* do the query again but with limit */
$result=mysqli_query($con, $select);
/*start of the table*/
{
echo "<table border='1'>
<tr>
<th>phone</th>
<th>city</th>
<th>activity</th>
<th>company</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['city'] . "</td>";
echo "<td>" . $row['activity']. "</td>";
echo "<td>" . $row['company']. "</td>";
echo "</tr>";
}
echo "</table>";
}
echo '<table border="0"><tr><td>';
/* ***** build the pagination links ***** */
$range = 2;
if ($currentpage > 1) {
$prevpage = $currentpage - 1;
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'>Previous</a> ";
}
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
if (($x > 0) && ($x <= $totalpages)) {
if ($x == $currentpage) {
echo " <font color='#546f3e'><b>$x</b></font> ";
} else {
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
}
}
}
if ($currentpage != $totalpages) {
$nextpage = $currentpage + 1;
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>Next</a> ";
} // end if
/* ***** end build pagination links ***** */
echo '</td></tr></table>';
You haven't written any pagination code, use MySQL LIMIT , clause.
For ordering randomly, try this:
SELECT * FROM table WHERE company LIKE '$company' and activity LIKE '$activity' ORDER BY RAND()
Start with this. In this you need to pass the page number in URL (Eg yourwebsite/your-php.php?page=0)
$page = $_GET['page'];
$perPage = 5;
$start = $page * $perPage;
$query = mysql_query("SELECT * FROM table WHERE company LIKE '$company' and activity LIKE '$activity' limit " . $start . ',' . $perPage);
echo "<h3>search results</h3><p>";
echo "<table border='1' align='center' >
<tr>
<th>phone</th>
<th>city</th>
<th>activity</th>
<th>company</th>
</tr>";
while($row = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>" . $row['phone']. "</td>";
echo "<td>" . $row['city']. "</td>";
echo "<td>" . $row['activity']. "</td>";
echo "<td>" . $row['company']. "</td>";
echo "< /tr>";
}
echo "</table>";
$anymatches=mysql_num_rows($result );
if ($anymatches == 0)
{
echo "<h3>sorry no results</h3>";
}
?>
You need to implement pagination in to this by giving link to different page numbers.
I suggest to use some MVC framework rather than having all data retrieval and view in one file.
I am trying to display data in table form with 3 columns. Each should have a main category with some drop down lists. I get all the information to display but all is in one column and the drop down information does not display with the correct heading.
echo "<table>";
while ($row = mysql_fetch_array($result)) {
$count = 1;
if ($count = 1) {
$sCatID = ($row['CatID']);
echo "<tr valign='top'><td><b><a href='#" . $sCatID . "'>" . $sCatID . "</a></b><br>";
// column 1 categories
$result2 = mysql_query("SELECT * FROM test_prefixSubCat WHERE CatID=$sCatID");
// sub-cats
while ($row2 = mysql_fetch_array($result2)) {
$sSub = ($row2['CatID']);
$sSubID = ($row2['SubID']);
echo "<dd><a href='#'>" . $sSub . "</a><br>";
echo "</td>";
}
$count = 2;
} elseif ($count = 2) {
$sCatID = ($row['CatID']);
echo "<td><b><a href='.$sCatID.'>" . $sCatID . "</a></b><br>";
// column 1 categories
$result2 = mysql_query("SELECT * FROM test_prefixSubCat WHERE CatID=$sCatID");
// sub-cats
while ($row2 = mysql_fetch_array($result2)) {
$sSub = ($row2['CatID']);
$sSubID = ($row2['SubID']);
echo "<dd><a href='#'>" . $row2['Sub'] . "</a><br>";
echo "</td>";
}
$count = 3;
} elseif ($count = 3) {
$sCatID = ($row['CatID']);
echo "<td><b><a href='.$sCatID.'>" . $sCatID . "</a></b><br>";
// column 1 categories
$result2 = mysql_query("SELECT * FROM test_prefixSubCat WHERE CatID=$sCatID");
// sub-cats
while ($row2 = mysql_fetch_array($result2)) {
$sSub = ($row2['CatID']);
$sSubID = ($row2['SubID']);
echo "<dd><a href='.$sSub.'>" . $sSub . "</a><br>";
echo "</td></tr>";
}
$count = 1;
}
}
if ($count = 2) {
echo "<td> </td><td> </td></tr>";
} elseif ($count = 3) {
echo "<td> </td></tr>";
}
echo "</table>";
It doesn't seem to close the rows and table correctly... And it is also putting some of the drop down items before it displays the first heading.
If i display it in only one column it is working fine.
You should use == instead of single = in your if statements. Else it would execute everytime as that condition is always true.