I'm trying to build a little program to automatize a little task; in it, one can write two values (as inputs in a form) and then, in another page, numbers will be written in rows and columns with all the numbers within the range given.
The thing is... There will (almost always) be 500 numbers in the range, so I want the result to be displayed in 10 columns of 50 rows each, something like this image:
I wanted the result page (the one with all the numbers) to THEN give the possibility to export this as xml or pdf, but that's another story..
By now, I just would like to get the display right.
I'm using HTML and PHP on XAMPP. I've been able to get the first column... but can't get to the generations of the others, like this (and so on until 50):
So far, this is what I have, I'll put here my PHP code, because I don't think the HTML is relevant (but will post it too if it's needed) - (The values are passed by POST method)
<?php
$cont = 0;
$i = 0;
$arrayValues = range($valueFrom, $valueTo);
if($cont <= 50){
echo "<tr>";
for($i = 0; $i < 50; $i++){
echo "<p>$arrayValues[$i]</p>";
$cont++;
}
echo "</tr>";
}
?>
Also, using something like this --> Use PHP to Generate HTML table with static Cell Count of MySQL Data and worked ok, but I would the numbers displayed in columns... so not in rows (horizontally), so..
YES | NO
1 4 7 | 1 2 3
2 5 8 | 4 5 6
3 6 9 | 7 8 9
That's why I stopped trying with tables and just used a tag for the result.
EDIT:
So, given the "start" value and "end" value for a range, I would like to be able to print all those values in 10 columns of 50 rows each (so when it reaches 50 rows in the first column, it automatically moves to a new column and so on), and vertically (like the little diagram I tried to put above this, in the "YES" side)
Any ideas or guidance will be appreciated! :)
Have a nice rest of the week!
You can do this by splitting your range up into chunks and indexing into the resulting array of columns in a nested loop.
<table>
<?php
$valueFrom = 1;
$valueTo = 500;
$rowCount = 50;
$columns = array_chunk(range($valueFrom, $valueTo), $rowCount);
$columnCount = count($columns);
for ($row = 0; $row < $rowCount; $row++) {
echo '<tr>';
for ($column = 0; $column < $columnCount; $column++) {
$number = $columns[$column][$row] ?? '';
echo "<td>$number</td>";
}
echo '</tr>';
}
?>
</table>
Related
i have a homework like this
Imaginative Tree
An imaginative tree grows in a garden. The tree only grows 1 m long in autumn and 2 times the height of the tree in spring. Make a program that determines the highest tree after Y years with the initial height X m.
in 1 year spring come first
i've tried to put multiple symbol inside the loop,
<?php
$holddata =0;
$length =0
function randomtree($year,$firstlength){
for ($i=0; $i < $year ; $i++) {
$holddata= $firstlength* 2;
$length= $holddata+1;
}
echo $length;
}
echo randomtree(2,3);
but the value not increasing ,i expected the $firstlength*2 and add 1 on it then loop them 2 times so the result will be 15 ,( 3*2+1=7 ,7*2+1=15)
You need to use the value of $firstlength as the basis of the start of each calculation and assign the result to it for the next iteration. This code also returns the result rather than echoing out out.
So to simplify your code you can use...
function randomtree($year,$firstlength){
for ($i=0; $i < $year ; $i++) {
$firstlength = ($firstlength* 2)+1;
}
return $firstlength;
}
echo randomtree(2,3);
I am looking way to sort the serial number of the table in descending order. I am using a here simple while loop, with a counter variable inside it.
Code sample:
$i= 0;
while(condition)
{
$i++;
echo "<td>".$i."</td>";
}
Output:
I am not using an autoincrement column here. I just want it with a simple counter, but in descending order.
example:
#
10
9
8
7
6
5
4
3
2
1
Any help is highly appreciated.
If you already have loop outputting the 1-10 version, you could simple change the output to show 11 minus the current count...
echo "<td>".(11-$i)."</td>";
Or to change the whole code, you could start at 11 and decrement the counter each time and output it that way
$i= 11;
while($i>0)
{
$i--;
echo "<td>".$i."</td>";
}
count first and after do a loop in reverse order
$i= 0;
while(condition)
{
$i++;
}
for ( cnt= $i, $i>= 0, $i--){
echo "<td>".$cnt."</td>";
}
If you are fetching from MYSQL Database and you're using PDO to connect to Database
//COUNT THE TOTAL NUMBER OF ROWS RETURNED
$sql = "SELECT COUNT(*) FROM all_tnxs ORDER BY id DESC";
$stmt = $con->query($sql);
$stmt_num_rows = $stmt->fetch();
$tot_rows = array_shift($stmt_num_rows);
$sn = $tot_rows
while(){
$sn--;
echo '<td>' . $sn . '</td>';
}
So whatever the total number of rows you have - fetching from the database it'll countdown to '0'using the while loop.
I have been looking for this for long but later figured it out myself so I decided to drop it here for anyone it might be helpful to...
Newbie here, working on their second ever function. I've googles a bit on my problem, looked at PHP.net and at some articles on stack overflow, WC3schools, CoralCode, etc., but i haven't found anything that I can understand as of yet (i've done my diligence before come begging I promise).
The concept of my function is that it will take an array of whatever size, and print it out in rows of four. Function currently returns repeats itself overall equal to the size of the array. So if the array has 8 items, it returns nine groups (groupings defined visually by having the white line between them) of nine items. The first and the last have 16 boxes in them, 2-8 have 32 boxes in them where i was hoping to get back two rows of four with a while line between the two rows
see here for visual error
see here for how it's meant to look
So to sum up, my loop is over iterating/returning too much/not returning the data in the way i hoped. I'm not sure how to phrase the question, but i've done my best to ask it as best I can.
function PrintFolio($aaPlaceholder)
{
//print out two rows of four from array with 8 items
//if array had 7 items, then print row of 4 next row 3
//if array had 16 items, print 4 rows of 4, etc.
$height = sizeof($aaPlaceholder);//get size of array
//loop through array of X items, for each group of 4 print as a row
for($row = 0; $row < $height; $row++) //looping through the rows
{
echo '<div class="row flush">'; //open div
for($image = 0; $image < 4; $image++) //each row is composed of 4 images
{
foreach($aaPlaceholder as $key => $value) {
printf(TEMPLATE_PORTFOLIO, $key, $value);
//replaced $template with constant TEMPLATE_PORTFOLIO!
}
}
echo '</div>'; //end div group of 4
}//end loop
}//end function
If the answer is obvious, please remember i'm very new and it's not obvious to me or honestly I would not be asking.
Thank you for any help or guidance
cheers
First, the height is not the size of the array but it is a fourth of it.
Second, the inner foreach, as already stated in the comments, should not be changed: it should be deleted. You are already looping through your elements, no need to do it once more.
function PrintFolio($aaPlaceholder)
{
//print out two rows of four from array with 8 items
//if array had 7 items, then print row of 4 next row 3
//if array had 16 items, print 4 rows of 4, etc.
$itemsCount = sizeof($aaPlaceholder);//get size of array
$height = (int)ceil(sizeof($aaPlaceholder)/4);//get size of array divided by 4
//loop through array of X items, for each group of 4 print as a row
for($row = 0; $row < $height; $row++) //looping through the rows
{
echo '<div class="row flush">'; //open div
for($image = 0; $image < 4; $image++) //each row is composed of 4 images
{
$aaPlaceholderIndex = $row*4+$image; //the index in the original array
if( $aaPlaceholderIndex < $itemsCount ) {
printf(TEMPLATE_PORTFOLIO, $aaPlaceholderIndex, $aaPlaceholder[$aaPlaceholderIndex]);
}
}
echo '</div>'; //end div group of 4
}//end loop
}//end function
EDIT: this works if your array has plain numeric keys from 0 to n. Otherwise (e.g. associative array) it should read $aaPlaceholderIndex = array_keys($aaPlaceholder)[$row*4+$image];
EDIT AGAIN: so you use associative arrays (key are strings and not integers 0..n). I rushed a bit my edit about this. You'd better:
define $keys = array_keys($aaPlaceholder); outside the loops, where you define $height
define $aaPlaceholderIndex = $keys[$row*4+$image];
move this definition inside the "if" statement, which becomes
if( $row*4+$image < $itemsCount ) {
$aaPlaceholderIndex = $keys[$row*4+$image];
printf(TEMPLATE_PORTFOLIO, $aaPlaceholderIndex, $aaPlaceholder[$aaPlaceholderIndex]);
}
This way you run array_keys just once (good for performance) and you avoid a Notice "Undefined offset" when retrieving $keys[$row*4+$image] when $row*4+$image is too big.
This is probably not the best answer, but this is what I figured out to do.
I created a new variable called 'limit' which is equal to the height var divided by four. I then commented out the second 'for loop' as it was causing problems, leaving only the first for loop and the foreach loop. This gave me back my array as hoped.
Revised code below:
function PrintFolio($aaPlaceholder)
{
//print out two rows of four from array with 8 items
//if array had 7 items, then print row of 4 next row 3
//if array had 16 items, print 4 rows of 4, etc.
$height = sizeof($aaPlaceholder);//get size of array
$limit = $height % 4;
//loop through array of X items, for each group of 4 print as a row
for($row = 0; $row <= $limit; $row++) //looping through the rows
{
echo '<div class="row flush">'; //open div
//for($image = 0; $image < 4; $image++) //each row is composed of 4 images
//{
foreach($aaPlaceholder as $key => $value) {
printf(TEMPLATE_PORTFOLIO, $key, $value);
//replaced $template with constant TEMPLATE_PORTFOLIO!
}
//}
echo '</div>'; //end div group of 4
}//end loop
}//end function
I need help. I need a loop that can execute some codes every 10 rows.
Suppose this is the scenario:
$rows = 15; // The row is for a generated report. I need to put a Thick Border, Horizontally every 10 rows.
This is the loop, and inside it I want another loop or any idea how can I execute some command every 10 rows assuming $row = 15, obviously the command should be executed once since the rows is only 15 and command will execute every 10 rows. Thanks everyone :)
$rows = 15;
for($c=0;$c<$size3;$c++)
{
//Location I want to execute a command every 10 rows.
}
Try for loop for this
for($i = 1;$i <= 40;$i++) {
if($i % 10 == 0) {
// your horizontal code here
} else {
// non horizontal code here
}
}
Edit Remember start the loop from 1 not from 0. See codepad
With 0
With 1
seems there is problem in $rows value, insted of adding condition on $rows %10 ==0 you may try setting one counter $i in loop which will get increment for each row and then you can try adding condition on $i %10 ==0
$startrow = 10 //10 if base 1 or 9 if base 0
$endrow = 15 //you said 15 so lets go with this
for($row = $startrow; $row < $endrow; $row+=10)
{
//do your thing here
}
Since you only want to perform the action once every 10 rows, why not start at row 10, then go by increments of 10 instead of incrementing the row 1 by 1.
This should be more efficient since there will be no wasted loops given your circumstance, it also eliminates the need for checking if the row is divisible by 10.
I have simple table that has about 80 rows, which I populate dynamically using PHP. What I am trying to do is to layout those rows in chunks for each column. So if I have 80 rows, I would like 4 columns of 20 rows or so, maybe the last column has less or more depending on the total number of rows. The total number of rows can change!
I am having trouble coming up with an implementation method that will not get messy! Anyone know of a simple way that I can implement this.
I have tried using a counter as I loop the data to populate the table and when a multiple of of 20 is reached move to the next block but that didn't work for me as I had extra rows left over.
foreach($indexes as $index){
$counter++;
echo '<tr>';
if($counter > 20){
$multiplier = $counter / 20;
$head = '<td></td>';
for($i=1; $i<$multiplier; $i++){
$head .= '<td></td>';
}
}
if($counter < 20){
$head = '';
}
echo "$head<td>$index</td><td><input id='$index' name='$index' type='checkbox' /></td>";
echo '</tr>';
}
Thanks all for any help
I would do :
$nbCols = 4;
$nbRows = count($indexes)/$nbCols;
for($row=0; $row<$nbRows; $row++) {
echo "<tr>";
for($i=0; $i<$nbCols; $i++) {
$index = $indexes[$row + ($i*$nbRows)];
echo "<td>$index</td><td><input id='$index' name='$index' type='checkbox' /></td>";
}
echo "</tr>";
}
Wouldn't you want to see the remainder of your division and deal with that also?
if($counter % 20 == 0){
// You've no remainder
}else{
// Do another loop to output the odd rows
}
Or you could % 2 == 0 to see if it's even, and then just multiply the whole result by 10.
Be sure to look at ceil() and floor() also for ensuring your number of rows is a round number.
if you dont mind to have this kind of cell order:
1 2 3 4
5 6 7 8
you can use <div style='float:left'>$cellValue</div> in the loop without use of table.