Could somebody help me to make pagination? [closed] - php

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
edited>>
Your posts don't help me at all
I made this
function lgsl_query_css_all($request)
{
global $lgsl_config;
lgsl_database();
$eiluciu_psl = 2;
$psl_num = 1;
if (isset($_GET['puslapis']))
{
$psl_num = $_GET['puslapis'];
}
$offset = ($psl_num-1)* $eiluciu_psl;
$mysql_query = "SELECT `type`,`ip`,`c_port`,`q_port`,`s_port` FROM `{$lgsl_config['db']['prefix']}{$lgsl_config['db']['table']}` WHERE `disabled`=0 ORDER BY `cache_time` ASC LIMIT $offset,$eiluciu_psl";
$mysql_result = mysql_query($mysql_query) or die(mysql_error());
$server_list = array();
while ($mysql_row = mysql_fetch_array($mysql_result, MYSQL_ASSOC))
{
if (strpos($request, "c") === FALSE && lgsl_timer("check")) { $request .= "c"; }
$server = lgsl_query_cached($mysql_row['type'], $mysql_row['ip'], $mysql_row['c_port'], $mysql_row['q_port'], $mysql_row['s_port'], $request);
if ($lgsl_config['hide_offline'][0] && !$server['b']['status']) { continue; }
$server_list[] = $server;
}
$query_count = "SELECT COUNT(id) AS numrows FROM lgsl";
$result_count = mysql_query($query_count) or die (mysql_error());
$row = mysql_fetch_array($result_count,MYSQL_ASSOC);
$numrows = $row['numrows'];
$max_psl = ceil($numrows/$eiluciu_psl);
$self = $_SERVER['PHP_SELF'];
$nav = '';
for($puslapis = 1; $puslapis <= $max_psl; $puslapis++)
{
if ($puslapis == $psl_num)
{
$nav .= " $puslapis ";
}
else
{
$nav .= " $puslapis ";
}
}
if ($psl_num > 1)
{
$puslapis = $psl_num - 1;
$prev = " « << ";
$first = " 1 ";
}
else
{
$prev = ' ';
$first = ' ';
}
if ($psl_num < $max_psl)
{
$puslapis = $psl_num + 1;
$next = " >> ";
$last = " $max_psl ";
}
else
{
$next = ' ';
$last = ' ';
}
// 1-4
$vienas = $psl_num + 1;
if($vienas < $max_psl)
{
$vienas_echo = "$vienas";
} else {
$vienas_echo = " ";
}
$du = $psl_num + 2;
if($du < $max_psl)
{
$du_echo = "$du";
} else {
$du_echo = " ";
}
$trys = $psl_num + 3;
if($trys < $max_psl)
{
$trys_echo = "$trys";
} else {
$trys_echo = " ";
}
$keturi = $psl_num + 4;
if($keturi < $max_psl)
{
$keturi_echo = "$keturi";
} else {
$keturi_echo = " ";
}
// 10-40
$desimt = $psl_num + 10;
if($desimt < $max_psl)
{
$desimt_echo = "$desimt";
} else {
$desimt_echo = " ";
}
$dvim = $psl_num + 20;
if($dvim < $max_psl)
{
$dvim_echo = "$dvim";
} else {
$dvim_echo = " ";
}
$trim = $psl_num + 30;
if($trim < $max_psl)
{
$trim_echo = "$trim";
} else {
$trim_echo = " ";
}
$kiam = $psl_num + 40;
if($kiam < $max_psl)
{
$kiam_echo = "$kiam";
} else {
$kiam_echo = " ";
}
$be = $puslapis -1;
$dabar = "" . $be . "";
echo "<div class=\"pagination\" align=\"center\"><ul><li>" . $prev .
"</li><li>" . $first . "</li><li>$dabar</li><li> </li><li>" . $vienas_echo . "</li><li>" . $du_echo .
"</li><li>" . $trys_echo . "</li><li>" . $keturi_echo . "</li><li> <strong>...</strong> </li><li>" . $desimt_echo .
"</li><li>" . $dvim_echo . "</li><li>" . $trim_echo .
"</li><li>" . $kiam_echo . "</li><li>" . $last . "</li><li>" . $next . "</li></ul></div>";
return $server_list;
}
$output .= "
<div style='text-align:center; font-size:10px; font-face:arial'>
<table id='myTable' cellpadding='3' cellspacing='1' style='margin:auto' class='boxes' >
<thead>
<tr class='lgsl'>
<th class='lgsl' style='width:20px;' title='Server Status'>On/Off</th>
<th class='lgsl' style='width:25px;' title='Game'>Gra</th>
<th class='lgsl' style='width:150px;' title='IP address and Port'>IP Serwera</th>
<th class='lgsl' style='width:200px;' title='Server Name'>Nazwa Serwera</th>
<th class='lgsl' style='width:85px;' title='Current Map'>Mapa</th>
<th class='lgsl' style='width:60px;' title='Players online'>Graczy</th>
<th class='lgsl' style='width:100px;' title='User Rating'>Szczegoly</th>
</tr>
</thead>";
foreach ($server_list as $server)
{
$misc = lgsl_server_misc($server);
$server = lgsl_server_html($server);
$output .= "
<tr style='".lgsl_bg().";table-layout:fixed'>
<td>
<img alt='' src='{$misc['icon_status']}' title='{$misc['text_status']}' />
</td>
<td>
<img alt='' src='{$misc['icon_game']}' title='{$misc['text_type_game']}' />
</td>
<td title='{$lgsl_config['text']['slk']}' style='text-align:center'>
<a href='steam://{$misc['software_link']}' class='Tips1' title=' Connect {$server['b']['ip']}:{$server['b']['c_port']}'>
{$server['b']['ip']}:{$server['b']['c_port']}
</a>
</td>
<td title='{$server['s']['name']}' style='text-align:center'>
<div style='width:100%; overflow:hidden; height:1.3em'>
{$misc['name_filtered']}
</div>
</td>
<td style='white-space:nowrap; text-align:center'>
{$server['s']['map']}
</td>
<td style='white-space:nowrap; text-align:center'>
{$server['s']['players']} / {$server['s']['playersmax']}
</td>
<td style='white-space:nowrap; text-align:center'>
<a href='".lgsl_link($server['o']['id'])."'>
<img alt='' src='{$misc['icon_details']}' style='border:none' title='{$lgsl_config['text']['vsd']}' />
</a>
</td>
</tr>";
}
$output .= "
</table>
</div>";
But it looks ugly so i thought someone has better solution
http://img851.imageshack.us/img851/820/unledoxy.png
and i need somehow to place it under the server list

The simplest way to make pagination is to COUNT the total number of rows, and then add a LIMIT to it.
Count the rows:
SELECT COUNT(*) WHERE `disabled`=0 ORDER BY `cache_time` ASC;
Then add your limit:
SELECT `type`,`ip`,`c_port`,`q_port`,`s_port` FROM `{$lgsl_config['db']['prefix']}{$lgsl_config['db']['table']}` WHERE `disabled`=0 ORDER BY `cache_time` ASC LIMIT $start, $perpage;
Where $perpage=20; or any number you would like and $start=(int)$_GET['page']*$perpage;
Simply add links at the bottom for each of the pages you would like to display:
for($a=0;$a<$rows;$a++){
echo"<a href='table.php?page=$a'>$a</a>";
}
That is the gist of it anyways to get you started.

To paginate the mysql query, add a limit clause
LIMIT $begin, $end
Where:
$begin = ($page_num-1)*$NUM_PER_PAGE;
$end = $begin + $NUM_PER_PAGE;

Related

Load more mysql results without updating whole page

I am loading a query where I maximum want to show 3 result in a div.
Below code is working fine for just that.
It is likewise working fine when I want to go to next page and load 3 more.
Only issues is that whole page are updating everytime I go back and forward.
Can someone help me with this solutions, so when I go back a forward only the div is updated with the new results, and the whole page is not updating, but just staying like it is. Thank you
<div class="row">
<div class="col-md-6">
<div class="infobox-large">
<?
$con = connectDB();
$query = "SELECT `id` FROM `users` WHERE `email`='$email'";
$result = mysql_query($query, $con) or die($query."<br/>
<br/>".mysql_error());
$user_id = mysql_result($result, 0);
// Sætter maks antal pr side
$offset = (isset($_GET['offset'])) ? $_GET['offset'] : 0; // Hvis ikke
$offset er sat, sættes den til 0
$pr_side = 3; // Antal resultater pr. side
// Tæller antal poster i databasen
$tael = mysql_query("SELECT COUNT(*) AS antal from tb_user_tip where
user_id = '".$user_id."' and point=''") or die (mysql_error());
$antal = mysql_result($tael,0);
$limit = 3;
$pages = ceil($antal/$limit);
if(isset($_GET['offset']) && is_numeric($_GET['offset'])){
if($_GET['offset'] < 1){
$offset = 1;
}elseif($_GET['offset'] > $pages){
$offset = $pages;
}else{
$offset = intval($_GET['offset']);
}
}else{
$offset = 1;
}
$end = $offset * $limit;
$start = $end - $limit;
?>
<span class="headline-box grey">Nuværende tips <?echo "(".$antal.")";?>
</span><br /><br />
<?
$sql_current = "select * from tb_user_tip where user_id = '".$user_id."' and
point='' order by start_date_time ASC LIMIT $start,$pr_side";
$result = mysql_query($sql_current, $con);
$current_tip_array = array();
while($row_current = mysql_fetch_array($result)){
$home = $row_current['home_name'];
$away = $row_current['away_name'];
$home_image_name = "";
if (strpos(strtolower($home), " ") != false) {
$arrName = split(" ", strtolower($home));
for ($i = 0; $i < sizeof($arrName) - 1; $i++) {
$home_image_name = $home_image_name . $arrName[$i] . "-";
}
$home_image_name = $home_image_name . $arrName[sizeof($arrName) - 1]
. ".png";
//$home_image_name = $arrName[0]."-".$arrName[1].".png";
} else {
$home_image_name = strtolower($home) . ".png";
}
$away_image_name = "";
if (strpos(strtolower($away), " ") != false) {
$arrName = split(" ", strtolower($away));
for ($i = 0; $i < sizeof($arrName) - 1; $i++) {
$away_image_name = $away_image_name . $arrName[$i] . "-";
}
$away_image_name = $away_image_name . $arrName[sizeof($arrName) - 1]
. ".png";
//$home_image_name = $arrName[0]."-".$arrName[1].".png";
} else {
$away_image_name = strtolower($away) . ".png";
}
$home_image = "/images/soccer/" . $home_image_name;
$away_image = "/images/soccer/" . $away_image_name;
$item_current = array(
"event_id" => $row_current['event_id'],
"home_name" => $row_current['home_name'],
"away_name" => $row_current['away_name'],
"start_date_time" => $row_current['start_date_time'],
"league_name" => $row_current['league_name'],
"market_style" => $row_current['market_style'],
"odd_style" => $row_current['odd_style'],
"odd" => $row_current['odd'],
"tip_amount" => $row_current['tip_amount'],
"home_image" => $home_image,
"away_image" => $away_image
);
array_push($current_tip_array, $item_current);
echo "<div class='tip-box neautral bg-white grey heading-in-box'>";
//Tip left - home name, league
echo "<div class='home text-left'><span class='large green
bold'>".$row_current['home_name']."</span><br /><br />Liga<br /><span
class='darkgreen tiny bold'>".$row_current['league_name']."</span></div>";
//Tip center - date & time, logo home & away, market style, odds style
echo "<div class='logoodds'><span class='darkgreen tiny bold'>".$row_current['start_date_time']."</span><br /><br /><img src='../".$home_image."' class='HOME team_img'> - <img src='../".$away_image."' class='AWAY team_img'><br /><br /><span class='darkgreen small bold'>".$row_current['market_style']."<br />".$row_current['odd_style']." ".$row_current['odd']."</span></div>";
//Tip right - away name, tip amount
echo "<div class='away text-right'><span class='large green bold'>".$row_current['away_name']."</span><br /><br />Indsats<br /><span class='darkgreen tiny bold'>".$row_current['tip_amount']."</span></div>";
echo "</div>";
}
if($offset > 1)
echo '<img src="img/arrowbackgreen50.png" class="arrow_small">';
echo '' . $offset . ' af ' . $pages . ' ';
if($offset < $pages)
echo '<img src="img/arrowgreen50.png" class="arrow_small">';
?>
</div>
</div>

Having some diffculty parsing tree data in ul li format in php

i'm try to create trial balance i somehow achieve tree view in my array and now i want to print my code but don't know how to make tree because i don't know how many children i can get in future and i'm not very good at recursion below is my tree prepared array and i also attach my result that i can make via foreach loop but stuck in children node i can make children nodes via foreach but the problem is it's not a best approach to follow so please help me out to print my tree structure data as per my array. Any help would be appreciated.
Here my tree array look like : Tree Array
My result look like this this is only root nodes of an array and i using jstree to show hierarchy.
<?
$total_opening_debit = 0;
$total_opening_credit = 0;
$total_debit = 0;
$total_credit = 0;
foreach($this->trees as $key => $value)
{
foreach($value as $tree)
{
if($tree["id"] != "")
{
?>
<li>
<span class="first"><? echo $tree["name"]; ?></span>
<span class="other">
<?
if ($tree["opening_debit"] > $tree["opening_credit"])
{
echo format_currency($tree["opening_debit"] - $tree["opening_credit"], 2) . " Dr.";
}
elseif ($tree["opening_credit"] > $tree["opening_debit"])
{
echo format_currency($tree["opening_credit"] - $tree["opening_debit"], 2) . " Cr.";
}
else
{
echo "0";
}
$total_opening_debit += $tree["opening_debit"];
$total_opening_credit += $tree["opening_credit"];
?>
</span>
<span class="other">
<?
if($tree["period_debit"]<>"" && $tree["period_debit"]<>"0")
{
echo format_currency($tree["period_debit"], 2);
$total_debit += $tree["period_debit"];
}
else
{
echo 0;
}
?>
</span>
<span class="other">
<?
if($tree["period_credit"]<>"" && $tree["period_credit"]<>"0")
{
echo format_currency($tree["period_credit"], 2);
$total_credit += $tree["period_credit"];
}
else
{
echo 0;
}
?>
</span>
<span class="other">
<?
if ($tree["opening_debit"] + $tree["period_debit"] > $tree["opening_credit"] + $tree["period_credit"])
{
$closing = format_currency($tree["opening_debit"] + $tree["period_debit"] - $tree["opening_credit"] - $tree["period_credit"], 2) . " Dr.";
}
elseif ($tree["opening_credit"] + $tree["period_credit"] > $tree["opening_debit"] + $tree["period_debit"])
{
$closing = format_currency($tree["opening_credit"] + $tree["period_credit"] - $tree["opening_debit"] - $tree["period_debit"], 2) . " Cr.";
}
else
{
$closing = "0";
}
echo $closing;
//calculate all opeing and closing debit & credit.
if ($tree["opening_debit"] + $tree["period_debit"] > $tree["opening_credit"] + $tree["period_credit"])
{
$total_closing_debit += ($tree["opening_debit"] + $tree["period_debit"] - $tree["opening_credit"] - $tree["period_credit"]);
}
if ($tree["opening_credit"] + $tree["period_credit"] > $tree["opening_debit"] + $tree["period_debit"])
{
$total_closing_credit += ($tree["opening_credit"] + $tree["period_credit"] - $tree["opening_debit"] - $tree["period_debit"]);
}
?>
</span>
<?
if(count($tree["children"]) > 0)
{
//make_children_tree();
}
?>
</li>
<?
}
}
}
?>
i solved myself here what approach i choose i extract children array into other array and on that array i use my recursion to do magic.
<? $array = $trial_tree["children"]; ?>
$trial_balance_tree = $this->trees;
$total_opening_debit = 0;
$total_opening_credit = 0;
$total_debit = 0;
$total_credit = 0;
$total_closing_debit = 0;
$total_closing_credit = 0;
$total_opening = 0;
$total_closing = 0;
function makeListItems($a) {
global $total_opening_debit,$total_opening_credit,$total_credit,$total_debit,$total_closing_debit,$total_closing_credit,$total_opening,$total_closing;
$out = '';
foreach($a as $val) {
//print_r($key); echo is_array($val);exit;
if($val["id"] > 0)
{
$out .= '<li>';
$out .= "<span class='first'>" . $val["name"] . "</span>";
$out .= "<span class='other'>";
if ($val["opening_debit"] > $val["opening_credit"])
{
$out .= format_currency($val["opening_debit"] - $val["opening_credit"], 2) . " Dr.";
}
elseif ($val["opening_credit"] > $val["opening_debit"])
{
$out .= format_currency($val["opening_credit"] - $val["opening_debit"], 2) . " Cr.";
}
else
{
$out .= "0";
}
$total_opening_debit += $val["opening_debit"];
$total_opening_credit += $val["opening_credit"];
$out .= "</span>";
$out .= "<span class='other'>";
if($val["period_debit"]<>"" && $val["period_debit"]<>"0")
{
$out .= format_currency($val["period_debit"], 2);
$total_debit += $val["period_debit"];
}
else
{
$out .= 0;
}
$out .= "</span>";
$out .= "<span class='other'>";
if($val["period_credit"]<>"" && $val["period_credit"]<>"0")
{
$out .= format_currency($val["period_credit"], 2);
$total_credit += $val["period_credit"];
}
else
{
$out .= 0;
}
$out .= "</span>";
$out .= "<span class='other'>";
if ($val["opening_debit"] + $val["period_debit"] > $val["opening_credit"] + $val["period_credit"])
{
$closing = format_currency($val["opening_debit"] + $val["period_debit"] - $val["opening_credit"] - $val["period_credit"], 2) . " Dr.";
}
elseif ($val["opening_credit"] + $val["period_credit"] > $val["opening_debit"] + $val["period_debit"])
{
$closing = format_currency($val["opening_credit"] + $val["period_credit"] - $val["opening_debit"] - $val["period_debit"], 2) . " Cr.";
}
else
{
$closing = "0";
}
$out .= $closing;
//calculate all opeing and closing debit & credit.
if ($val["opening_debit"] + $val["period_debit"] > $val["opening_credit"] + $val["period_credit"])
{
$total_closing_debit += ($val["opening_debit"] + $val["period_debit"] - $val["opening_credit"] - $val["period_credit"]);
}
if ($val["opening_credit"] + $val["period_credit"] > $val["opening_debit"] + $val["period_debit"])
{
$total_closing_credit += ($val["opening_credit"] + $val["period_credit"] - $val["opening_debit"] - $val["period_debit"]);
}
$out .= "</span>";
if(array_key_exists('children', $val)) {
$out .= makeList($val['children']);
}
$out .= '</li>';
}
}
return $out;
}
function makeList($a) {
$out = '<ul>';
$out .= makeListItems($a);
$out .= '</ul>';
return $out;
}
?>
<table style="width:100%;background-color: #A5C6DB;">
<tr>
<th class="table_first" style="width:42.5%;">Ledger Name</th>
<th class="table_other" style="width:14.7%;">Opening</th>
<th class="table_other" style="width:14.7%;">Debit</th>
<th class="table_other" style="width:14.7%;">Credit</th>
<th class="table_other" style="width:15%;">Closing</th>
</tr>
</table>
<?
echo "<div id='trial_balance_tree'>";
echo makeList($trial_balance_tree);
echo "</div>";
Now my view look like this and thats exactly i want.

PHP - Search results not paginating

I'm trying to get search results to paginate if there are greater than 10 items found in the database. For some reason, even though the code recognises there are more than 10 items and creates links for subsequent pages, all search results are listed on the first page only. Anyone able to help please? Code is below:
for($i = 0; $i < $terms_count; $i++)
{
$search_terms_array[$i] = trim($search_terms_array[$i]);
${"query".$i} = $this->mysqli_link->query("SELECT prod_id, prod_tags FROM table WHERE prod_tags LIKE '%" . $search_terms_array[$i] . "%'");
if(${"query".$i}->num_rows < 1)
{
$zerocount++;
}
else
{
$rows = array();
while($row = ${"query".$i}->fetch_array())
{
$rows[] = $row;
}
foreach($rows as $row)
{
$search_id_results[] = $row['prod_id'];
}
}
}
if($zerocount == $terms_count)
{
echo $this->err_handle->fetch_error_text("search_terms_0_results");
return;
}
else
{
$search_results = array_values(array_unique($search_id_results));
$search_results_count = count($search_results);
$search_page_count = ceil($search_results_count / 10);
$search_page_first_result = ($search_page - 1) * 10;
echo '<p>Go to page: ';
for($i = 1; $i <= $search_page_count; $i++)
{
if($i == $search_page)
{
echo " <strong>" . $i . "</strong>";
}
else
{
echo ' ' . $i . '';
}
}
echo '</p><p> </p><p> </p>';
for($i = 0; $i < $search_results_count; $i++)
{
$query = $this->mysqli_link->query("SELECT * FROM table WHERE prod_id='" . $search_results[$i] . "' LIMIT " . $search_page_first_result . ", 10");
while($row = $query->fetch_array())
{
echo "<h4>" . $row['prod_name'] . "</h4><p><img src=\"includes/images/product_images/" . $row['prod_category'] . "/" . $row['prod_pic_filename'] . "\" alt=\"\" width=\"150\" height=\"200\" /></p><p>Price: £" . $row['prod_price'] . "</p><p>" . $row['prod_code'] . "</p><input type=\"number\" name=\"prod_qty\" maxlength=\"2\" /><input type=\"submit\" name=\"add_to_basket\" value=\"Add To Basket\" /></form></p><p> </p><p> </p>";
}
}
echo '<p>Go to page: ';
for($i = 1; $i <= $search_page_count; $i++)
{
if($i == $search_page)
{
echo " <strong>" . $i . "</strong>";
}
else
{
echo ' ' . $i . '';
}
}
echo '</p><p> </p><p> </p>';
}
Ok so I found a solution to this problem and the full function is now as follows:
public function product_search($search_terms, $search_page, $search_flag_check)
{
if($search_flag_check == "invalid")
{
echo $this->err_handle->fetch_error_text("invalid_ns_term");
return;
}
if($search_terms == "")
{
echo $this->err_handle->fetch_error_text("no_search_string");
return;
}
$search_terms = htmlspecialchars($search_terms);
$search_terms = $this->mysqli_link->real_escape_string(filter_var($search_terms, FILTER_SANITIZE_FULL_SPECIAL_CHARS, FILTER_FLAG_NO_ENCODE_QUOTES));
$search_terms_array = explode(" ", $search_terms);
$terms_count = count($search_terms_array);
$zerocount = 0;
$search_id_results = array();
for($i = 0; $i < $terms_count; $i++)
{
$search_terms_array[$i] = trim($search_terms_array[$i]);
${"query".$i} = $this->mysqli_link->query("SELECT prod_id, prod_tags FROM table WHERE prod_tags LIKE '%" . $search_terms_array[$i] . "%'");
if(${"query".$i}->num_rows < 1)
{
$zerocount++;
}
else
{
$rows = array();
while($row = ${"query".$i}->fetch_array())
{
$rows[] = $row;
}
foreach($rows as $row)
{
$search_id_results[] = $row['prod_id'];
}
}
}
if($zerocount == $terms_count)
{
echo $this->err_handle->fetch_error_text("search_terms_0_results");
return;
}
else
{
$search_results = array_values(array_unique($search_id_results));
$search_results_count = count($search_results);
$search_page_count = ceil($search_results_count / 10);
$search_page_first_result = ($search_page - 1) * 10;
$search_page_results_limit = 10;
if($search_page_first_result < 1)
{
$search_page_first_result = 1;
}
echo '<p>Go to page: ';
for($i = 1; $i <= $search_page_count; $i++)
{
if($i == $search_page)
{
echo " <strong>" . $i . "</strong>";
}
else
{
echo ' ' . $i . '';
}
}
echo '</p><p> </p><p> </p>';
$search_page_upper_limit = $search_page_first_result + 9;
if(array_key_exists($search_page_upper_limit, $search_results))
{
$search_results_limit = $search_page_first_result + $search_page_results_limit;
}
else
{
end($search_results);
$search_results_limit = key($search_results);
reset($search_results);
}
for($i = $search_page_first_result; $i <= $search_results_limit; $i++)
{
$query2 = $this->mysqli_link->query("SELECT * FROM table WHERE prod_id='" . $search_results[$i] . "'");
$row = $query2->fetch_array();
echo "<h4>" . $row['prod_name'] . "</h4><p><img src=\"includes/images/product_images/" . $row['prod_category'] . "/" . $row['prod_pic_filename'] . "\" alt=\"\" width=\"150\" height=\"200\" /></p><p>Price: £" . $row['prod_price'] . "</p><p>" . $row['prod_code'] . "</p><input type=\"number\" name=\"prod_qty\" maxlength=\"2\" /><input type=\"submit\" name=\"add_to_basket\" value=\"Add To Basket\" /></form></p><p> </p><p> </p>";
}
echo '<p>Go to page: ';
for($i = 1; $i <= $search_page_count; $i++)
{
if($i == $search_page)
{
echo " <strong>" . $i . "</strong>";
}
else
{
echo ' ' . $i . '';
}
}
echo '</p><p> </p><p> </p>';
}
}
It took a bit of thinking and I was about to give up, but then I thought about using the array keys to calculate the limits for each search page and after a bit of googling on relevant PHP array functions it all fell into place quite well.
I understand the code may not be very tidy right now and there may be ways to optimize/improve it, however for the time being it does the job.

Colspan breaks table

ORIGINAL
I have a page that displays all the company boardrooms and bookings for a certain day. It looks like this:
As you can see, some of the rows create an extra <td>
Here is the code that generates the table:
<table celladding="0" cellspacing="0" class="table table-bordered col-xs-12">
<thead>
<tr style="background-color:#FFF">
<?php
$width = (100 / (count($boardrooms->toArray()) + 1));
$width = 100 / 7;
?>
<th style="border-right:none; width:<?= $width; ?>%; border-bottom: none;">Boardroom</th>
<?php
foreach($boardrooms as $boardroom) { ?>
<th rowspan="2" style="width:<?= $width; ?>%"><?php echo $boardroom ?></th>
<?php } ?>
</tr>
<tr>
<th>Time</th>
</tr>
</thead>
<?php
$writerow = false;
for($hour = 8; $hour < 17; $hour++) {
for($minute = 0; $minute < 4; $minute++) {
echo '<tr><td>' . $hour . ':' . str_pad(($minute*15),2,"0") . '</td>';
foreach($boardrooms as $boardroom) {
foreach($boardroomBookings as $booking) {
if(date('H:i',strtotime($hour . ':' . str_pad(($minute*15),2,"0"))) >= date('H:i',strtotime($booking->start_time)) &&
date('H:i',strtotime($hour . ':' . str_pad(($minute*15),2,"0"))) < date('H:i',strtotime($booking->end_time)) &&
$boardroom == $booking->boardroom->name && (empty($boardroomWritten[$booking->id]) || !$boardroomWritten[$booking->id])) {
$boardroomWritten[$booking->id] = true;
$writerow = true;
$rowspan = (strtotime($booking->end_time) - strtotime($booking->start_time))/900;
echo '<td style="background-color:#8cc63f; border-bottom:none" rowspan="' . $rowspan . '">' . $this->Html->link($booking->name, array('action' => 'view', $booking->id)) . '</td>';
break;
} else {
$writerow = false;
}
}
if(!$writerow) {
echo '<td></td>';
}
}
echo '</tr>';
}
}
?>
</table>
How can I prevent the rows from displaying the extra empty cell on rows that have a booking?
EDIT
Here's the same table with black borders:
And what it looks like without a booking:
And what it should look like with a booking:
If you don't have a booking you need to insert a td pair unless you are inside a colspan:
<?php
for($hour = 8; $hour < 17; $hour++) {
for($minute = 0; $minute < 4; $minute++) {
$bookTime = date('H:i ',strtotime($hour . ':' . str_pad(($minute*15),2,"0"))) ;
echo '<tr><td>' . $bookTime . '</td>';
foreach($boardrooms as $boardroom) {
$hasBooking = false;
foreach($boardroomBookings as $booking) {
$sTime = strtotime($booking->start_time) ;
$eTime = strtotime($booking->end_time) ;
$startTime = date('H:i',$sTime);
$endTime = date('H:i', $eTime) ;
if($bookTime >= $startTime && $bookTime < $endTime && $boardroom == $booking->boardroom->name) {
$hasBooking = true;
if(empty($boardroomWritten[$booking->id]) || !$boardroomWritten[$booking->id]) {
$boardroomWritten[$booking->id] = true;
$rowspan = ($eTime - $sTime)/900 ;
echo '<td style="background-color:#8cc63f; border:1px black solid" rowspan="' . $rowspan . '">' . "somelink" . '</td>';
break;
}
}
}
if (!$hasBooking) {
echo "<td></td>" ;
}
}
echo '</tr>';
}
}

Drop down menu filter for SELECT COUNT results php

I have a script which retrieves all rows of data within a sales list (npc_sales_list) but what I am trying to do is set up a drop down menu which filters the results oand I don't know where to start as I haven't done any php in while.
The main filters I am trying to do are to see the previous month's sales only (npc_entry_date) and the sales type (npc_usertype). If someone could point me in the right direction or show me some resource links that would be great.
Im not fussed whether the filter is automatic or if you need to hit a submit button. Any suggestions welcome...
<?php
require_once ('./includes/config.inc.php');
$page_title = 'Page title';
include ('./includes/header.html');
if (!isset($_SESSION['admin_sp_id'])) {
$url = 'http://' . $_SERVER['HTTP_HOST']
. dirname($_SERVER['PHP_SELF']);
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
$url = substr ($url, 0, -1);
}
$url .= '/login.php';
ob_end_clean();
header("Location: $url");
exit();
}
require_once ('database.php');
$display = 500;
if (isset($_GET['np'])) {
$num_pages = $_GET['np'];
} else {
$query = "SELECT COUNT(*) FROM npc_sales_list ORDER BY npc_entry_date DESC";
$result = #mysql_query ($query);
$row = mysql_fetch_array ($result, MYSQL_NUM);
$num_records = $row[0];
if ($num_records > $display) {
$num_pages = ceil ($num_records/$display);
} else {
$num_pages = 1;
}
}
if (isset($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}
$link1 = "{$_SERVER['PHP_SELF']}?sort=fna";
$link2 = "{$_SERVER['PHP_SELF']}?sort=lna";
if (isset($_GET['sort'])) {
switch ($_GET['sort']) {
case 'fna':
$order_by = 'sp_firstname ASC';
$link1 = "{$_SERVER['PHP_SELF']}?sort=fnd";
break;
case 'fnd':
$order_by = 'sp_firstname DESC';
$link1 = "{$_SERVER['PHP_SELF']}?sort=fna";
break;
case 'lna':
$order_by = 'sp_surname ASC';
$link2 = "{$_SERVER['PHP_SELF']}?sort=lnd";
break;
case 'lnd':
$order_by = 'sp_surname DESC';
$link2 = "{$_SERVER['PHP_SELF']}?sort=lna";
break;
default:
$order_by = 'npc_entry_date DESC';
break;
}
// $sort will be appended to the pagination links.
$sort = $_GET['sort'];
} else { // Use the default sorting order.
$order_by = 'npc_entry_date DESC';
$sort = 'drd';
}
$query = "SELECT us.sp_firstname , us.sp_surname , us.sp_amb_club_no , us.sp_position_title , us.sp_dealer_name ,
us.sp_dealer_code , us.sp_region , us.sp_department, sp_business_phone, us.sp_mobile_phone , us.sp_dealer_category, us.sp_email,
sa.npc_item_id , sa.npc_part_no, sa.npc_quantity, sa.npc_customer_name, sa.npc_registration_no , sa.npc_suo_no , sa.npc_amb_club_no , sa.npc_repair_order , sa.npc_invoice_no ,
sa.npc_entry_userdate, sa.npc_image_upload, sa.npc_usertype, sa.npc_points, sa.npc_bonus_points, sa.npc_active, sa.npc_entry_date,
DATE_FORMAT(sa.npc_entry_date, '%d-%m-%Y') AS dr , sa.sp_user_id
FROM sp_user AS us, npc_sales_list AS sa
WHERE us.sp_user_id = sa.sp_user_id
ORDER BY
$order_by LIMIT $start, $display";
$result = #mysql_query ($query);
//
// Table header.
echo '<table width="100%" cellspacing="1" cellpadding="4" style="font-size:10px;">
<tr>
<td align="center"><b>Invoice No.</b></td>
<td align="center"><b>Member ID</b></td>
<td align="center"><b>Surname</b></td>
<td align="center"><b>Part #</b></td>
<td align="center"><b>Sale Type</b></td>
<td align="center"><b>Qty</b></td>
<td align="center"><b>Invoice Date</b></td>
<td align="center"><b>Customer Rego</b></td>
<td align="center"><b>Submission Date</b></td>
<td align="center"><b>Status</b></td>
</tr>';
// Fetch and print all the records. echo '<td align="left"><strong>' . $row['sp_invoice_no'] . '</strong></td> ';
$bg = '#ffffff'; // Set the background color.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$bg = ($bg=='#eaeced' ? '#ffffff' : '#eaeced');
echo '<tr bgcolor="' . $bg . '">';
echo '<td align="center"><strong>Details</strong></td> ';
echo '<td align="center"><strong>' . $row['sp_amb_club_no'] . '</strong></td> ';
echo '<td align="center"><strong>' . $row['sp_surname'] . '</strong></td> ';
echo '<td align="center"> <strong>' . $row['npc_part_no'] . '<strong></td>';
echo '<td align="center"> ' . $row['npc_usertype'] . ' </td>';
echo '<td align="center"> ' . $row['npc_quantity'] . ' </td>';
echo '<td align="center"> ' . $row['npc_entry_userdate'] . ' </td>';
echo '<td align="center"> ' . $row['npc_registration_no'] . ' </td>';
echo '<td align="center"> ' . $row['dr'] . ' </td>';
echo '<td align="center" style="color:#5a8e22; font-weight:bold;"> ' . $row['npc_active'] . ' </td>';
echo '</tr>
';
}
echo '</table>';
mysql_free_result ($result);
mysql_close();
if ($num_pages > 1) {
echo '<br /><p>';
$current_page = ($start/$display) + 1;
if ($current_page != 1) {
echo '<a href="view-all-sales.php?s=' . ($start - $display) . '&np=' .
$num_pages . '&sort=' . $sort .'">Previous</a> ';
}
for ($i = 1; $i <= $num_pages; $i++) {
if ($i != $current_page) {
echo '<a href="view-all-sales.php?s=' . (($display * ($i - 1))) .
'&np=' . $num_pages . '&sort=' . $sort .'">' . $i . '</a> ';
} else {
echo $i . ' ';
}
}
if ($current_page != $num_pages) {
echo '<a href="view-all-sales.php?s=' . ($start + $display) . '&np=' .
$num_pages . '&sort=' . $sort .'">Next</a> ';
}
echo '</p>';
}
include ('./includes/footer_admin_user.html');
?>

Categories