I am using the ProgressBar class from the following link for displaying a progress bar for image downloads..
http://pastebin.com/KSxjC01r
I am using the following code..
echo 'Starting Image Download...<br />';
$p = new ProgressBar();
echo '<div style="width: 300px;">';
$p->render();
echo '</div>';
//progress bar
for ($i = 0; $i < ($size = 100); $i++) {
$p->setProgressBarProgress($i*100/$size);
usleep(1000000*0.01);
}
fetch_image("$item", "../cbimages/$img ");
echo "Downloaded $img <br />";
}
echo "Finished downloading images....";
Everything is working fine..But for each download it is displaying only 99.0% in progress bar. After completion too it displays as 99.0% . What is wrong the code above. Where am i going wrong. Help requested..
Update:
Resolved the issue by changing the following line in the class file:
From:
if ($percentDone == 100) {
print('document.getElementById("'.$this->pbid.'").style.display = "none";');
}
To:
if ($percentDone == 100) {
print('document.getElementById("'.$this->pbid.'").style.width = "'.$percentDone.'%";');
Your for loop currently only counts to 99 because you used < instead of <=.
Changing it to this should work:
<?php
for ($i = 0; $i <= ($size = 100); $i++) {
$p->setProgressBarProgress($i * 100 / $size);
usleep(1000000 * 0.01);
}
Your loop isn't quite right
you have this:
for ($i = 0; $i < ($size = 100); $i++) {
The rule is that i < 100 so, when i == 100 the loop is not run. Meaning it is never passed to setProgressBarProgress()
This will work:
for ($i = 0; $i <= ($size = 100); $i++) {
The loop will still run when i == 100
Related
I am working on a random RPG Map generator that get's inserted into the database for later retrieval. Basically the user will enter some minor details on the form and specify how many tiles of each terrain they need to have on the map. Then the tiles are randomly inserted into the map and database. I insert the tiles into an Array and then shuffle the results. I do a loop through the array and loop through and X & Y grid to insert the terrain data. But everytime I do that, I get a memory error: Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 32 bytes) This is my code:
$gridarray = array();
for ($jungle1 = 0; $jungle1 <= $jungle -1; $jungle1++) {
array_push($gridarray,"jungle");
}
for ($swamp1 = 0; $swamp1 <= $swamp -1; $swamp1++) {
array_push($gridarray,"swamp");
}
for ($cave1 = 0; $cave1 <= $cave -1; $cave1++) {
array_push($gridarray,"cave");
}
for ($mountain1 = 0; $mountain1 <= $mountain -1; $mountain1++) {
array_push($gridarray,"mountain");
}
for ($ocean1 = 0; $ocean1 <= $ocean -1; $ocean1++) {
array_push($gridarray,"ocean");
}
for ($volcanic1 = 0; $volcanic1 <= $volcanic -1; $volcanic1++) {
array_push($gridarray,"volcanic");
}
for ($desert1 = 0; $desert1 <= $desert -1; $desert1++) {
array_push($gridarray,"desert");
}
for ($dirt1 = 0; $dirt1 <= $dirt -1; $dirt1++) {
array_push($gridarray,"dirt");
}
for ($forest1 = 0; $forest1 <= $forest -1; $forest++) {
array_push($gridarray,"dirt");
}
for ($arctic1 = 0; $arctic1 <= $arctic -1; $arctic++) {
array_push($gridarray,"arctic");
}
for ($grass1 = 0; $grass1 <= $grass -1; $grass++) {
array_push($gridarray,"grass");
}
echo '<table cellspacing="0" cellpadding="0" border="0">';
$gridsquare = $xsize * $ysize;
$grid = 0;
shuffle($gridarray);
for ($x = 1; $x <= $xsize; $x++) {
echo '<tr>';
for ($y = 1; $y <= $ysize; $y++) {
$terrain = $gridarray[$grid];
$terrain_img = 'http://www.sw-bfs.com/images/grids/' . $terrain . '.png';
$query2 = " INSERT INTO terrain ( ent_id, ent_type, grid_img, grid_type, grid_exit_e, grid_exit_w, grid_exit_s, grid_exit_n, grid_exit_u, grid_exit_d, x, y) VALUES ( '$loc_id', 'terrain', '$terrain_img', '$terrain','1', '1', '1', '1', '0', '0', '$x', '$y') ";
$result2 = mysql_query($query2) or die(mysql_error());
echo '<td><img src="' . $terrain_img . '" title="' . $terrain . '"/></td>';
$grid++;
}
echo '</tr>';
}
echo "</table>";
It is telling me that the error is occurring on the line that reads like this:
array_push($gridarray,"arctic");
}
Obviously I am doing something wrong or at least I am not writing the code efficiently enough. Forgive me, I am still learning. Can someone provide some better assistance? Thank you in advance.
Never mind. This:
for ($arctic1 = 0; $arctic1 <= $arctic -1; $arctic++) {
array_push($gridarray,"arctic");
Is missing a 1 in $arctic++. Should be like this:
for ($arctic1 = 0; $arctic1 <= $arctic -1; $arctic1++) {
array_push($gridarray,"arctic");
Now works perfectly. Feel free to use my snippet above to create random maps for the database.
is that possible to sum variable static values in the while or for loop? i have code and working on it but it sum variables only one time?
Here My Code
session_start();
$length=count($_SESSION['product1']);
$shipping2='280';
$shipping3='680';
$newshipping='0';
$newshipping1='0';
$i='0';
while($i= <$length)
{
$newshipping=$shipping2+$shipping3;
$newshipping1=$newshipping+$shipping2;
}
For Example I want like this
$shipping2='280'; should be sum with every result of $newshipping1
`$newshipping1`= $shipping2='280' + $shipping3='680' = `$newshipping1`=960
+ $shipping2='280'??
'$newshipping1`=960+ $shipping2=280+ $shipping2=280+ $shipping2=280 .....
when ever new product1 enter it should be add $shipping2=280
in the result of `$newshipping1`
I have completed my code here my final code
$length=count($_SESSION['product1']);
$shipping2=280;
$shipping3=680;
$newshipping=0;
for($i=0; $i <$length; $i++) {
if($i == 1) {
$newshipping = $shipping2+$shipping3;
} else if($i <= 100) {
$newshipping = $newshipping+$shipping2;
}
}
Your logic seems a bit confusing, however you can sum several integers. If you are trying to figure out the final amount of several iterations you should:
$shipping_one = 680;
$shipping_two = 260;
$shipping_three = 0;
$finalShipping = array();
while($i= <$length)
{
$finalShipping[] = $shipping_one + $shipping_two + $shipping_three;
}
$finalTotal = array_sum($finalShipping);
If you can clarify your question, I can clarify my answer.
this will be ((680) + (4 * 280)) like you explained in your last comment.
$length = 1;
echo "Test with $length : ".getShippingTotal($length)." <br />";
$length = 2;
echo "Test with $length : ".getShippingTotal($length)." <br />";
$length = 3;
echo "Test with $length : ".getShippingTotal($length)." <br />";
$length = 4;
echo "Test with $length : ".getShippingTotal($length)." <br />";
$length = 5;
echo "Test with $length : ".getShippingTotal($length)." <br />";
function getShippingTotal($length) {
$shipping2=280;
$shipping3=680;
$total = 0;
for($i=0; $i < $length; $i++) {
if($i == 0) {
$total += $shipping2+$shipping3;
} else {
$total += $shipping2;
}
}
return $total;
}
I have tested it and it gave me:
Test with 1 : 960
Test with 2 : 1240
Test with 3 : 1520
Test with 4 : 1800
Test with 5 : 2080
I want to limit the number of pages displayed on my pagination php script below. This was a script made a few years back for me, and although I have read through similar problems on here, their coding is very different.
Any help with this would be really appreciated.
Here is my current script:
<?php
if ($max_pages>1) {
echo "<br>";
if ($page>0) {
echo 'Previous';
}
for ($x=0;$x<$max_pages;$x++) {
if ($page<>$x) {
echo ''.($x+1).'';
}
else {
echo '<span class="pagination">'.($x+1).'</span>';
}
}
if (($page+1<>$max_pages)) {
echo 'Next';
}
}?>
Your current script cycles $x between 0 and $max_pages.
What you can do is first replace them with $from_page and $to_page:
$from_page = 0;
$to_page = $max_pages;
...
for ($x=$from_page; $x < $to_page; $x++)
at which point the script will work just as before.
Now if you want to only display from $N pages before to $N pages after $page,
$N = 5; // display 5+5+1 = 11 pages
$from_page = $page - $N; if ($from_page < 0) $from_page = 0;
$to_page = $from_page + 2*$N+1; if ($to_page > $max_pages) $to_page = $max_pages;
$from_page = $to_page - 2*$N-1; if ($from_page < 0) $from_page = 0;
Not the most elegant way perhaps, but it will try to fit an 11-page area centered on the current page. You may want to also display a link to pages 0 and $max_pages-1.
MRE version
<?php
if ($max_pages>1)
{
$N = 5; // display 5+5+1 = 11 pages
$to_page = min($max_pages, max(0, $page - $N) + 2*$N+1);
$from_page = max($to_page - 2*$N-1, 0);
echo "<br>";
if ($page > 0)
{
echo 'Previous';
}
for ($x=$from_page; $x < $to_page; $x++) {
if ($page != $x) {
echo ''.($x+1).'';
}
else {
echo '<span class="pagination">'.($x+1).'</span>';
}
}
if (($page+1<>$max_pages)) {
echo 'Next';
}
}?>
When I launch my web page, increment doesn't work correctly!
It should go like this: $i = from 1 to x (0,1,2,3,4,5,6 etc..).
But instead it jumps over every step giving result of (1,3,5,7 etc..).
Why is this code doing this?
<ul class="about">
<?php
$result = mysql_query("SELECT * FROM info WHERE id = 1");
while ($row = mysql_fetch_assoc($result))
{
$bioText = $row['bio'];
}
$endBioTxt = explode("\n", $bioText);
for ($i=0; $i < count($endBioTxt);)
{
if (checkNum($i) == true)
{
echo "<li class='left'><div>".$endBioTxt[$i]."</div></li>";
echo $i;
}
else
{
echo "<li class='right'><div>".$endBioTxt[$i]."</div></li>";
echo $i;
}
$i++;
}
// Function to check if number is prime
function checkNum($num){
return ($num % 2) ? TRUE : FALSE;
}
?>
</ul>
Output:
Sometext!(right side)
0
1
Sometext2!(right side)
2
3
...
Please DONT do this as other suggested:
for ($i=0; $i < count($endBioTxt); $i++)
do this:
$count = count($endBioTxt);
for ($i=0; $i < $count; $i++) {
}
No need to calculate the count every iteration.
Nacereddine was correct though about the fact that you don't need to do:
$i++;
inside your loop since the preferred (correct?) syntax is doing it in your loop call.
EDIT
You code just looks 'strange' to me.
Why are you doing:
while ($row = mysql_fetch_assoc($result))
{
$bioText = $row['bio'];
}
???
That would just set $bioText with the last record (bio value) in the recordset.
EDIT 2
Also I don't think you really need a function to calculate the modulo of a number.
EDIT 3
If I understand your answer correctly you want 0 to be in the left li and 1 in the right li 2 in the left again and so on.
This should do it:
$endBioTxt = explode("\n", $bioText);
$i = 0;
foreach ($endBioTxt as $txt)
{
$class = 'left';
if ($i%2 == 1) {
$class = 'right';
}
echo '<li class="'.$class.'"><div>'.$txt.'</div></li>';
echo $i; // no idea why you want to do this since it would be invalid html
$i++;
}
Your for statement should be:
for ($i=0; $i < count($endBioTxt); $i++)
see http://us.php.net/manual/en/control-structures.for.php
$i++; You don't need this line inside a for loop, it's withing the for loop declaration that you should put it.
for ($i=0; $i < count($endBioTxt);$i++)
{
if (checkNum($i) == true)
{
echo "<li class='left'><div>".$endBioTxt[$i]."</div></li>";
echo $i;
}
else
{
echo "<li class='right'><div>".$endBioTxt[$i]."</div></li>";
echo $i;
}
//$i++; You don't need this line inside a for loop otherwise $i will be incremented twice
}
Edit: Unrelated but this isn't how you check whether a number is prime or not
// Function to check if number is prime
function checkNum($num){
return ($num % 2) ? TRUE : FALSE;
}
This code works, please test it in your environment and then uncomment/comment what you need.
<?php
// This is how query should look like, not big fan of PHP but as far as I remember...
/*
$result = mysql_query("SELECT * FROM info WHERE id = 1");
$row = mysql_fetch_assoc($result);
$bioText = $row['bio'];
$endBioTxt = explode("\n", $bioText);
*/
$endBioTxt[0] = "one";
$endBioTxt[1] = "two";
$endBioTxt[2] = "three";
$endBioTxt[3] = "four";
$endBioTxt[4] = "five";
$totalElements = count($endBioTxt);
for ($i = 0; $i < $totalElements; $i++)
{
if ($i % 2)
{
echo "<li class='left'><div>".$endBioTxt[$i]."</div></li>";
}
else
{
echo "<li class='right'><div>".$endBioTxt[$i]."</div></li>";
}
/*
// This is how you should test if all your array elements are set
if (isset($endBioTxt[$i]) == false)
{
echo "Array has some values that are not set...";
}
*/
}
Edit 1
Try using $endBioTxt = preg_split('/$\R?^/m', $bioTxt); instead of explode.
I need to generate random number of divs with five items per div (and remaining items in the last div) from random number of $totalItems and also not all the items satisfy $OKItems... Hopefully the code explains better than me.
My problem is that this script generates empty divs with no content in them.
<?php
$OKItems = 0;
$totalItems = rand(2,30);
for ($i = 0; $i < $totalItems; $i++) {
echo ($OKItems == 0 || $OKItems % 5 == 0) ? 'div open<br />' : '';
$testValue = rand(0, 1);
if ($testValue != 0) {
echo '1';
$OKItems++;
}
echo ($OKItems % 5 == 0 || $i+1 == $totalItems) ? '<br />div close<br />' : '';
}
?>
This is what I might get:
div open
div close
div open
11111
div close
div open
div close
div open
div close
div open
11
div close
And this is what I would have wanted in this case:
div open
11111
div close
div open
11
div close
<?php
const N = 5;
$totalItems = rand(2,30);
$items = array() ;
for ($i = 0; $i < $totalItems; $i++) {
$testValue = rand(0, 1);
if ($testValue != 0) {
$items[] = 1 ;
}
if( N == sizeof($items) || (($i == $totalItems - 1) && 0 < sizeof($items)) ) {
echo "<div>" . join(",", $items) . "</div>";
$items = array() ;
}
}
I think you need a bit more structure to your code.
My approach would be to break it up into several stages, as opposed to trying to do all the logic in the loop that outputs data.
What I'd suggest:
Decide how many items to be tested
Test each item and only copy the ones that pass into a new array
Partition this new array into sets of 5
Output each partition as a div
Code (untested):
// Decide how many items to test
$totalItems = rand(2,30);
// Test these items and add them to an accepted array
$items = Array();
for ($i = 0; $i < $totalItems; $i++) {
$testValue = rand(0, 1);
if ($testValue != 0) { $items[] = "1" }
}
//Partition them into sections
$partitions = array_chunk($items,5);
//Output as divs
foreach($partitions as $partition):
echo 'div open <br />';
foreach($partition as $item):
echo $item . "<br />";
endforeach;
echo 'div close <br />';
endforeach;
When you split up the code into logical steps, it becomes much easier to maintain and debug.
<?php
$OKItems = 0;
$totalItems = rand(2,30);
for ($i = 0; $i < $totalItems; $i++) {
echo ($OKItems == 0 || $OKItems % 5 == 0) ? 'div open<br>' : '';
$testValue = rand(0, 1);
if ($testValue != 0) {
echo '1';
$OKItems++;
}
if($OKItems % 5 == 0 || $i+1 == $totalItems) {
echo '<br>div close<br>';
$OKItems = 0;
}
}
?>
That should be working ;)
I changed your check line for an if function that also resets your $OKItems. The problem you had (i think) was that you got a 0 as the random value and that would keep $OKitems on 5.