i have this code .
echo "<br>";
$start = 1;
$angka = $_POST[angka];
$a = $angka;
for($i=$start; $i<=$angka; $i++) {
for($j=$start;$j<=$angka;$j=$j+2){
echo $i;
if($j < $angka) echo $a;
}
$a--;
echo '<br>';
}
Reference: Print Looping for dynamic row php
This is not my expecting result .
First i want the result like this .
-2-4-
1-3-5
-2-4-
1-3-5
-2-4-
The rule is Number of rows and columns follow the number of numbers declared.If the declaration of the number 5, then the results will be displayed are 5 rows and 5 columns like the example above.
i think this code is working
<?php
$_POST['angka'] = 5;
$angka = $_POST['angka'];
for($i=1; $i<=$angka; $i++) {
for($j=1;$j<=$angka;$j++){
if($i%2 == 1) {
if($j%2 == 0) {
echo $j;
} else {
echo '-';
}
} else {
if($j%2 == 0) {
echo '-';
} else {
echo $j;
}
}
}
echo '<br>';
}
Result:
-2-4-
1-3-5
-2-4-
1-3-5
-2-4-
I have the following code:
$max = 4;
$page = isset($_GET['page']) ? ($_GET['page']) : '1';
$init = $page - 1;
$init= $max * $init;
$strCount = "SELECT COUNT(*) AS 'total_mytable' FROM mytable";
$varstrCount = $crud->viewdatas($strCount);
$total = 0;
if(count($varstrCount)){
foreach ($varstrCount as $row) {
$total = $row["total_mytable"];
}
}
$result = "SELECT * FROM mytable ORDER BY id_mytable LIMIT $init,$max";
$varresult = $crud->viewdatas($result);
content of page:
<?php
if(count($varresult)){
foreach ($varresult as $res) {
?>
<h5><?php echo $res['title'] ?></h5>
<?php
}
}
?>
<?php
$max_links = 10;
$previous = $page - 1;
$next = $page + 1;
$pgs = ceil($total / $max);
if($pgs > 1 ){
if($previous > 0){
echo "<li><a href='".BASE_URL."/category/$previous' aria-label='Previous'><span aria-hidden='true'>«</span></a></li>";
} else{
}
for($i=$page-$max_links; $i <= $pgs-1; $i++) {
if ($i <= 0){
}else{
if($i != $page{
if($i == $pgs){ //if end insert 3 dots
echo "<li><a href='".BASE_URL."/category/".($i)."'>".$i."</a></li> ...";
}else{
echo "<li><a href='".BASE_URL."/category/".($i)."'>".$i."</a></li>";
}
} else{
if($i == $pgs){ //if end insert 3 dots
echo "<li>".$i."</li> ...";
}else{
echo "<li>".$i."</li>";
}
}
}
}
if($next <= $pgs){
echo "<li><a href='".BASE_URL."/category/$next' aria-label='Next'><span aria-hidden='true'>»</span></a></li>";
}else{
}
}
?>
The result:
And I not understand the reason for the active number stay right off the paging menu
In the code I defined max-links for 10, but no show number 5 and if I define max links for 3 changes nothing , displays the same result as the image.
Thanks for any help
echo "<li>".$i."</li>";
on the above line add up a href, seems like you have css formatting for li>a because of which you are not getting the required formatting on only li. so for getting better formatting for all paging links current, prev, next. you need to add up a tags.
echo "<li><a>".$i."</a></li>"; //in your else part
I have the following bit of code and for some reason in the first WHILE loop the first value of $actor_list (when $i = 0) does not display. If I simply echo $actor_list[0] then it displays fine, but in the loop it will not display. I merely get [td][/td] as the output. The remaining values of the array display fine.
Also the line
echo '</tr><tr> </br>';
is not displaying.
What am I missing here? The value of $num_actors is an even number in my test scenario so there doesn't seem to be a reason for the above echo line to be skipped.
$actor_list = explode(" ", $actors);
$num_actors = count($actor_list);
if ($num_actors <= 6) {
foreach ($actor_list as $actor) {
echo '[td]'.$actor.'[/td] </br>';
}
} elseif ($num_actors <= 12) {
if ($num_actors % 2 == 0) {
$half_actors = $num_actors / 2;
while ($i <= ($half_actors - 1)) {
echo '[td]'.$actor_list[$i].'[/td] </br>';
$i++;
}
echo '</tr><tr> </br>';
while ($i <= $num_actors) {
echo '[td]'.$actor_list[$i].'[/td] </br>';
$i++;
}
}
}
You're not initialising the variable $i to 0, which means it will be set to 'null' and thus not reference the 0th index of the array; but when it's then incremented its value will become 1.
Note: [..] Decrementing NULL values has no effect too, but incrementing them results in 1.
See: http://php.net/manual/en/language.operators.increment.php
Try adding:
$i = 0;
before the if statement.
Made a few changes.
This is working for me.
Try it out.
<?php
$actor_list = 'act1 act2 act3 act4 act5 act6';
$actors = explode(" ", $actor_list);
$num_actors = count($actor_list);
//debug
//echo "<pre>";
//print_r($actor_list);
//echo "</pre>";
echo "<table>";
if ($num_actors <= 6) {
foreach ($actors as $actor) {
echo '<tr><td>'.$actor.'</td></tr>';
}
} elseif ($num_actors <= 12) {
if ($num_actors % 2 == 0) {
$half_actors = $num_actors / 2;
while ($i <= ($half_actors - 1)) {
echo '<tr><td>'.$actor_list[$i].'</td></tr>';
$i++;
}
while ($i <= $num_actors) {
echo '<tr><td>'.$actor_list[$i].'</td></tr>';
$i++;
}
}
}
echo "</table>";
?>
This is the problem I'm having with code:
I have a function that creates an updating bar;
I have a function that creates a loop, each number of loop makes some other stuffs and the same number represent the progress of the bar (from 1 to 155);
this loop works when user make a "search" on the site for "all" countries;
BUT user can also make a "search" only choosing 1-5 different countries;
this way we have a function that makes the PROGRESS BAR, another one that makes the PROGRESS PART of the BAR that should understand how to progress.
So the point is that I don't know how make the function for progress bar to interpret the situation in order to know that user choosed only some countries and not "all" countries (the option will post to the function some "numbers of the loop" like "3", "4", "56"... corresponding to choosed country) - and this is the easy part (with maybe isset($var)).
The next hard part is that after that it will be called the PROGRESS BAR function with those "country-numbers" so that if you choose "Canada-USA-France" the numbers will be "34-12-45" and the progress bar will write "34%-12%-45%".
But it's incorrect because if you have 3 options choosed, it should be 33%-66%-99% (and not 34%-12%-45%)...
What I don't know is how to make that function understand that everytime the function receive the "country-number", and it comes from user choice different from "all", the function should get that number and modify it (and this can be made with an array) in order to adapt it to the correct bar progression so that if you have: 34, than 12 than 45, the function should understand that the 1st time 34 should become 33, the 2nd time 12 should become 66 and the 3rd time 45 should become 99 (or 100)...
Here is the code part of interest:
1. receiving _POST and verifying it:
function getData() {
$a = strtolower($_POST["CountryOne"]);
$b = strtolower($_POST["CountryTwo"]);
$c = strtolower($_POST["CountryThree"]);
$d = strtolower($_POST["CountryFour"]);
$d = strtolower($_POST["CountryFive"]);
$numPost = count($_POST);
if ($numPost == 0) {
echo("<p class='Verify'>Select an option to start search...<br></p>");
} else {
echo "<div class='Verify' id='progressbar' style='width:620px;height:16px'></div>
<div class='VerifyBar' id='information' style='width'></div>";
};
2. Jump to next part after checking it user choosed "all" or "some countries" - here is the loop:
function loopNum($x) {
$i = 0+$x;
$y = $x+3;
for ($x = $i; $x <= $y; $x++) {
$iscountryID = ("$country".$x."");
createUrl($iscountryID);
} if($y != 155) {
return loopNum($x);
} else if($y < 155) {
echo("<font size='2' face='Tahoma, Geneva, sans-serif' style='font-variant: small-caps' color='#FF0000'><i>ERROR: unxepected data extraction interruption</font></i><br>");
} else {
echo("<font size='2' face='Tahoma, Geneva, sans-serif' style='font-variant: small-caps' color='#00CC00'><i>FINISHED: data extraction ended</font></i><br>");
}};
3. than it makes a lot of other stuff and call progress bar function
function completeBar($iscountryID) {
//***** PART UNDER CONSTRUCTION WHERE I NEED HELP *****
$countryOne = strtolower($_POST["CountryOne"]);
$countryTwo = strtolower($_POST["CountryTwo"]);
$countryThree = strtolower($_POST["CountryThree"]);
$countryFour = strtolower($_POST["CountryFour"]);
$countryFive = strtolower($_POST["CountryFive"]);
if (($countryOne !== 'all') and ($countryTwo !== 'all') and ($countryThree !== 'all') and ($countryFour !== 'all') and ($countryFive !== 'all')) {
$numQuery = array("$countryOne","$countryTwo","$countryThree","$countryFour","$countryFive");
foreach ($numQuery as $value) {
if(is_numeric($value)) {
$j++;
}}; // AT THIS POINT I WOULD KNOW HOW MANY VALUES IN THE ARREY ARE DIFFERENT FROM "ALL" AND "NO_COUNTRY" OPTIONS AND SO ARE NUMERIC VALUES
//***** FROM HERE I DUNNO HOW TO PRECEED *****
} else { //the next part works with "all" option choosed
if ($iscountryID < 155) {
$i = $iscountryID;
$percent = $i."%";
$pxbar = 4*$i."px";
$percentage = round((($pxbar*100)/624),0); //bar is 624px long
// Javascript for updating the progress bar and information
echo '<script language="javascript">
document.getElementById("progressbar").innerHTML="<div style=\"width:'.$pxbar.';background-color:#ddd;\">'.$percentage.'% </div>";
document.getElementById("information").innerHTML="'.$i.'/155 country(s) processed... loading your data, hold on..."</script>';
// This is for the buffer achieve the minimum size in order to flush data
echo str_repeat(' ',1024*64);
// Send output to browser immediately
flush();
sleep(1);
} else if ($iscountryID == 155) { //so on loop complete
$i = $iscountryID;
$percent = $i."%";
$pxbar = 4*$i."px";
$percentage = round((($pxbar*100)/624),0);
echo '<script language="javascript">
document.getElementById("progressbar").innerHTML="<div style=\"width:'.$pxbar.';background-color:#ddd;\">'.$percentage.'% </div>";
document.getElementById("information").innerHTML="'.$i.'/155 country(s) processed... loading your data, hold on..."</script>';
echo str_repeat(' ',1024*64);
flush();
sleep(1);
// Tell user that the process is completed
echo '<script language="javascript">
document.getElementById("progressbar").innerHTML="<div style=\"width:'.$pxbar.';background-color:#ddd;\">100% </div>";
document.getElementById("information").innerHTML="<div>Process completed, all countries verified...</div>";
document.getElementById("information").style.color="green";
</script>';
echo str_repeat(' ',1024*64);
flush();
sleep(1);
}
}};
Ah... I've tried to make the following working but the problem is that with the following, I'll have the function updating the bar getting values from the array too fast. Using the VAR sent from the rest of the file, I'll have an updating bar that follows the updating output of the server.
function partialBar() {
$countryOne = strtolower($_POST["CountryOne"]);
$countryTwo = strtolower($_POST["CountryTwo"]);
$countryThree = strtolower($_POST["CountryThree"]);
$countryFour = strtolower($_POST["CountryFour"]);
$countryFive = strtolower($_POST["CountryFive"]);
$numQuery = array("$countryOne","$countryTwo","$countryThree","$countryFour","$countryFive");
foreach ($numQuery as $value) {
if(is_numeric($value)) {
$j++;
};
};
if ($j == 1) {
$iscountryID = 155;
completeBar($iscountryID);
} else if ($j == 2) {
$iscountryID = array("78","155");
completeBar($iscountryID[0]);
completeBar($iscountryID[1]);
} else if ($j == 3) {
$iscountryID = array("52","104","155");
completeBar($iscountryID[0]);
completeBar($iscountryID[1]);
completeBar($iscountryID[2]);
} else if ($j == 4) {
$iscountryID = array("39","78","117","155");
completeBar($iscountryID[0]);
completeBar($iscountryID[1]);
completeBar($iscountryID[2]);
completeBar($iscountryID[3]);
} else if ($j == 5) {
$iscountryID = array("31","62","93","124","155"); //these numbers are the corresponding loop number in other to get 20%-40%-60%-80%-100% from the completeBar() function
completeBar($iscountryID[0]);
completeBar($iscountryID[1]);
completeBar($iscountryID[2]);
completeBar($iscountryID[3]);
completeBar($iscountryID[4]);
} else echo ("<div class='Verify'>Something went wrong!</div>");
};
Here is solution:
make a counter in the central function in order to know how many times have been called, so you know which number of the array you have to call for the correct calculation of the % of progress bar.
Here is where the following functions are called:
if (($countryNameTitOne == 'all') or ($countryNameTitTwo == 'all')
or ($countryNameTitThree == 'all') or ($countryNameTitFour == 'all')
or ($countryNameTitFive == 'all')) {
completeBar($iscountryID);
} else {
$callCounter = callCounterFunc();
partialbar($iscountryID,$callCounter);
}};
Than here are the functions called:
function completeBar($iscountryID) {
if ($iscountryID < 155) {
$i = $iscountryID;
$percent = $i."%";
$pxbar = 4*$i."px";
$percentage = round((($pxbar*100)/624),0);
echo '<script language="javascript">
document.getElementById("progressbar").innerHTML="<div style=\"width:'.$pxbar.';background-color:#ddd;\">'.$percentage.'% </div>";
document.getElementById("information").innerHTML="'.$i.'/155 country(s) processed... loading your data, hold on..."</script>';
echo str_repeat(' ',1024*64);
flush();
sleep(1);
} else if ($iscountryID == 155) {
$i = $iscountryID;
$percent = $i."%";
$pxbar = 4*$i."px";
$percentage = round((($pxbar*100)/624),0);
echo '<script language="javascript">
document.getElementById("progressbar").innerHTML="<div style=\"width:'.$pxbar.';background-color:#ddd;\">'.$percentage.'% </div>";
document.getElementById("information").innerHTML="'.$i.'/155 country(s) processed... loading your data, hold on..."</script>';
echo str_repeat(' ',1024*64);
flush();
sleep(1);
echo '<script language="javascript">
document.getElementById("progressbar").innerHTML="<div style=\"width:'.$pxbar.';background-color:#ddd;\">100% </div>";
document.getElementById("information").innerHTML="<div>Process completed, all countries verified...</div>";
document.getElementById("information").style.color="green";
</script>';
echo str_repeat(' ',1024*64);
flush();
sleep(1);
}
};
function partialBar($iscountryID,$callCounter) {
$countryOne = strtolower($_POST["CountryOne"]);
$countryTwo = strtolower($_POST["CountryTwo"]);
$countryThree = strtolower($_POST["CountryThree"]);
$countryFour = strtolower($_POST["CountryFour"]);
$countryFive = strtolower($_POST["CountryFive"]);
$numQuery = array("$countryOne","$countryTwo","$countryThree","$countryFour","$countryFive");
foreach ($numQuery as $value) {
if(is_numeric($value)) {
$j++;
}};
if ($j == 1) {
if ($callCounter == 1) {
$iscountryID = 155;
completeBar($iscountryID);
};
} else if ($j == 2) {
$iscountryID = array("78","155");
if ($callCounter == 1) {
completeBar($iscountryID[0]);
} else if ($callCounter == 2) {
completeBar($iscountryID[1]);
};
} else if ($j == 3) {
$iscountryID = array("52","104","155");
if ($callCounter == 1) {
completeBar($iscountryID[0]);
} else if ($callCounter == 2) {
completeBar($iscountryID[1]);
} else if ($callCounter == 3) {
completeBar($iscountryID[2]);
};
} else if ($j == 4) {
$iscountryID = array("39","78","117","155");
if ($callCounter == 1) {
completeBar($iscountryID[0]);
} else if ($callCounter == 2) {
completeBar($iscountryID[1]);
} else if ($callCounter == 3) {
completeBar($iscountryID[2]);
} else if ($callCounter == 4) {
completeBar($iscountryID[3]);
};
} else if ($j == 5) {
$iscountryID = array("31","62","93","124","155");
if ($callCounter == 1) {
completeBar($iscountryID[0]);
} else if ($callCounter == 2) {
completeBar($iscountryID[1]);
} else if ($callCounter == 3) {
completeBar($iscountryID[2]);
} else if ($callCounter == 4) {
completeBar($iscountryID[3]);
} else if ($callCounter == 5) {
completeBar($iscountryID[4]);
};
} else echo ("<div class='Verify'>Something went wrong!</div>");
};
function callCounterFunc() {
static $calls = 0;
++$calls;
return $calls;
};
the problem with the class is that if i want to display 30 records at a time and i have 60 total records the pagination divs that shows the page numbers only shows page #1 and not page #2. i have tried to figure out how to fix it but i have given up. any help would greatly be apreciated.
this is how i call attributes to the class.
$paginate = new Pagination;
$paginate->pageName = "index.php"; //sets the page to use
$paginate->perPage = 10; //show num of records per page
$paginate->adjacents = 3; //current page adjacent to
$paginate->sql = "select * from tbl_products"; //the main query
$query = $db->query($paginate->getData());
while($row = mysql_fetch_object($query)) {
print $row->pName."<br/>";
}
$paginate->showPagination(); //shows the pagination div
here is the class.
<?php
include_once("class.db.php");
class Pagination
{
var $param;
var $perPage;
var $adjacents;
var $start;
var $sql;
var $pageName;
function __construct()
{
$this->db = new MysqlDB;
$this->db->connect();
}
function setParam()
{
if(isset($_GET['page']) && is_numeric($_GET['page']) && ($_GET['page'] > 0))
{
$this->param = $_GET['page'];
}
else
{
$this->param = 1;
}
}
function setIndex()
{
$this->setParam();
return $this->start = ($this->param * $this->perPage) - $this->perPage;
}
function showPagination($param1=null,$param2=null,$param3=null)
{
$qRows = $this->db->query($this->sql);
$numRows = $this->db->num_rows($qRows);
$numOfPages = ceil($numRows / $this->perPage);
$param = $this->param;
$pageName = $this->pageName;
$string = "";
//set pagination parameters.
if($param1 != null)
{
if(isset($_GET[$param1]))
{
$param1Value = $_GET[$param1];
$string .= "&".$param1."=".$param1Value;
}
}
if($param2 != null)
{
if(isset($_GET[$param2]))
{
$param2Value = $_GET[$param2];
$string .= "&".$param2."=".$param2Value;
}
}
if($param3 != null)
{
if(isset($_GET[$param3]))
{
$param3Value = $_GET[$param3];
$string .= "&".$param3."=".$param3Value;
}
}
print "<div class='pagination'>";
print "<a href='$this->pageName?page=1$string' class='previous-off'> First </a>";
// ----------------------------------------------------------------------------
// PRINT ALL PAGES TO THE LEFT //
if(($param - $this->adjacents) > 1)
{
print "<span>...</span>";
$lowerLimit = $param - $this->adjacents;
//print all on left side.
for($i = $lowerLimit; $i< $param; $i++)
{
print "<a href='$pageName?page=$param = $i$string'> $i </a>";
}
}
else
{
//print all numbers between current page and first page.
for($i = 1; $i < $param; $i++)
{
print "<a href='$pageName?page=$i$string'> $i </a>";
}
}
// ----------------------------------------------------------------------------
//print current page
if(($param != 0) && ($param != $numOfPages))
{
print "<span class='current'>$param</span>";
}
// ----------------------------------------------------------------------------
//PRINT ALL PAGES TO THE RIGHT
if(($param + $this->adjacents) < $numOfPages)
{
$upperLimit = $param + $this->adjacents;
for($i=($param + 1); $i<=$upperLimit; $i++)
{
print "<a href='$pageName?page=$i$string'> $i </a>";
}
print "<span>...</span>";
}
else
{
//print all page numbers if out of padded range
for($i = ($param + 1); $i<$numOfPages; $i++ )
{
print "<a href='$pageName?page=$i$string'> $i </a>";
}
}
// ----------------------------------------------------------------------------
$lastPage = $numOfPages - 1;
print "<a class='next' href='$pageName?page=$lastPage$string'> Last </li>";
print "</div>";
}
function getData()
{
$query = $this->sql;
$this->start = $this->setIndex();
return "$query LIMIT $this->start, $this->perPage";
}
function errors()
{
$query = $this->sql;
print "$query LIMIT $this->start, $this->perPage"."<br/>";
print "this->start ".$this->start."<br/>";
print "this->param ".$this->param."<br/>";
print "this->perPage ".$this->perPage."<br/>";
print "this->setindex() ".$this->setIndex()."<br/>";
}
}
?>
Personally I tend to stray from SELECT * FROM tbl type queries as if you have a large table, it can be slow. Therefore, I run two queries: one to get the total number of records, and one to get the data set I need based on a paging parameter in the URL, e.g. example.com/news.php?page=2.
From there, I can instantiate my own paging class, which is pretty simple:
<?php
class Paging {
private $total;
private $batch;
private $page;
private $prefix;
public function __construct($total=0, $batch=10, $page=1, $url="") {
$this->total = intval($total);
$this->batch = intval($batch);
$this->page = intval($page);
$this->url = $url;
}
public function total_pages() {
return ceil($this->total/$this->batch);
}
public function render() {
$html = "";
// only display paging if we have more than the batch value
if ($this->total > $this->batch) {
$html.= '<p>Go to page:';
for ($i=1; $i <= $this->total_pages()) {
if ($i==$this->page) {
$html.= ' <span class="current">'.$i.'</span>';
}
else {
$html.= ' '.$i.'';
}
}
$html.= '</p>';
}
return $html;
}
}
Then to use:
<?php
// get total number of records
$sql = "SELECT COUNT(*) AS total FROM tbl";
$res = $db->query($sql);
$row = $res->fetch();
$total = $row['total'];
$batch = 20;
$page = intval($_GET['page']);
// instantiate paging class
require_once('paging.class.php');
$paging = new Paging($total, $batch, $page, "mypage.php?page=%s");
// do normal page stuff here...
// render the pagination links
echo $paging->render();
Feel free to use the above code and modify to your needs