I am trying to create hailstone sequence table done in php.
Here is my simple code to produce the sequence:
<?php
function HailstoneNumbers($x){
static $c;
echo $x." ";
if ($x == 1 && $c == 0){
// N is initially 1.
echo "got 1!";
return $c;
}
else if ($x == 1 && $c != 0){
// N is reduced to 1.
$c++;
echo "Done! number of steps = ",$c;
}
else if ($x % 2 == 0){
// If N is Even.
$c++;
HailstoneNumbers((int)($x / 2));
}
else if ($x % 2 != 0){
// N is Odd.
$c++;
HailstoneNumbers(3 * $x + 1);
}
return $c;
}
$x = $_GET['x'];
if(isset($_GET['x'])){
$N = HailstoneNumbers($x);
}
?>
It does the job of creating the values:
However, I want to create a table in this style:
it is 2 row and n columns where n is the total number of iteration.
The heading elements are in bold, and left entries and right entries are separated by a border.
Each entry has a border at its bottom except for the last entry.
I am quite new to php. I need to connect the number of steps to the number of columns and style it, but how do I create a css document for this?
Just add some html. you can add borders css for exact look.
echo '<table style="border: 1px solid">
<thead>
<tr>
<td>Iteration</td>
<td>X</td>
</tr>
</thead>
<tbody>';
function HailstoneNumbers($x){
static $c = 0;
// echo $x." ";
echo '<tr>
<td>'.$c.'</td>
<td>'.$x.'</td>
</tr>';
if ($x == 1 && $c == 0){
// N is initially 1.
echo "got 1!";
return $c;
}
else if ($x == 1 && $c != 0){
// N is reduced to 1.
$c++;
// echo "Done! number of steps = ",$c;
}
else if ($x % 2 == 0){
// If N is Even.
$c++;
HailstoneNumbers((int)($x / 2));
}
else if ($x % 2 != 0){
// N is Odd.
$c++;
HailstoneNumbers(3 * $x + 1);
}
return $c;
}
Related
I have problem with that the first row misses a <tr> and then there is one line and then there is a </tr> and then it works perfect with one <tr> and two lines and </tr> and so on. How to change the code to make that work?
Any ideas to use "div" instead of "table"?
<table width="300" border="0" cellspacing="2">
<tbody>
<?
$query = mysql_query("SELECT * FROM pages ORDER BY id ASC");
while($r = mysql_fetch_array($query))
{
if(++$s % 2 == 0) { echo '<tr>'."\r\n"; }
echo '<td> · '.$r['domain'].' </td>'."\r\n";
if(++$i % 2 != 0) { echo '</tr>'."\r\n"; }
}
?>
</tbody>
</table>
Use $s++ instead of ++$s, so you test the value before you increment it. And there's no need to use a separate $i variable, you can just use the same $s variable without incrementing it.
$s = 0;
while ($r = mysql_fetch_array($query)) {
if ($s++ % 2 == 0) {
echo "<tr>\r\n";
}
echo '<td> · '.$r['domain'].' </td>'."\r\n";
if ($s % 2 == 0) {
echo "</tr>\r\n";
}
}
// If we ended loop without closing the row, do it now
if ($s % 2 == 0) {
echo "</tr>\r\n";
}
Sounds like you're trying to build a 2-column table? You don't need two counters for that, one will do:
$cells = 0;
while(...) {
if (($cells % 2) == 0) {
echo '<tr>'; // start a new row for cells 0, 2, 4, etc...
}
echo '<td> ....';
if (($cells % 2) == 1) {
echo '</tr>'; // close the row after outputting cells 1,3,5,etc...
}
$cells++;
}
I'm working through a snippet of code. The goal is to create a program where a user can enter three values (row, col, and highlight). The user then would then click a button and it would generate a table of 100 values. The first cell must be a random number, the second would be random number + 1. I'm just not sure how to incorporate that into my code. I must use $randnumber = rand(0,100).
Here is what I have so far...
<table border="1">
<?
i = 0;
while($i < 100) {
if($i % 10 == 0) {
echo "<tr>";
}
echo ("<td>".$i."</td>");
$i++;
}
?>
this should do:
<?php $randnumber = rand(0,100); ?>
<table border="1">
<tr>
<?php
$i = 0;
while($i < 100) {
if($i % 10 == 0) {
echo "</tr><tr>";
}
echo ("<td>".($randnumber+$i)."</td>");
$i++;
}
?>
</tr>
</table>
I'm a beginner learning PHP. I have tried to make a loop that has a different behaviour for both even and odd numbers. I've been playing around with it for a while, yet I still can't get it to work. Has anyone got a solution?
$count = 0;
$mod = $count % 2;
while ($count < 10)
{
if ($mod == 0) {
echo "even, ";
} else {
echo "odd, ";
}
$count++;
}
A silly mistake, mod inside while() loop.
$count = 0;
while ($count < 10) {
$mod = $count % 2; //Here
if ($mod == 0) {
echo "even, ";
} else {
echo "odd, ";
}
$count++;
}
$count = 0;
$mod = $count %2;
Is were your problem is.
You have to use the modulus (%) operator inside the for loop. Also, there is no need to store the value from the use of the modulus operator at all, it can be compared directly inside the for-loop.
for ($count = 0; $count < 10, $count++) {
if ($count % 2 == 0) {
echo "even, ";
} else {
echo "odd, ";
}
}
You can also switch the while to a for like this.
Welcome to PHP.
Edit #1:
As you are getting a new value of $count every execution of the for-loop the old value if $count % 2 will be incorrect. It has to recalculate for every $count. First it checks if 0 is divisible by 2, then onto 1 and so forth. For every value of $count you have to check the divisibility.
In most programming languages you aren't computing a variable onto another, instead you are taking the value of the variable. Like $a = $b + $c; in that case, if you change the value of $b or $c it does not automatically update $a. Instead you have to call $a = $b + $c again. It is the same with % operator.
$count = 0;
while ($count < 10) {
$mod = $count % 2;
if ($mod == 0) {
echo "even, ";
} else {
echo "odd, ";
}
$count++;
}
use for loop instead of while loop
for($count=0;$count<10;$count++)
{
if(($count % 2) == 0)
echo "even,";
else
echo "odd,";
}
I'm trying to count elements in a loop to break each number of elements and show in groups
My little script
$data="house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*";
$exp_filter = explode("*", trim($data));
for ($x = 0; $x <= count($exp_filter); $x++)
{
print "".$exp_filter[$x]."";
if ($x%5 == 0)
{
print "<br>";
}
}
As you can see in the little script each 5 rounds I want show the tag for break and show as in groups of elements.
The problem it´s always show in the first line one element and after this the rest, and no works fine.
The index of $exp_filter starts at 0, so this block of code
if ($x % 5 == 0)
{
print "<br>";
}
should be
if (($x+1) % 5 == 0)
{
print "<br>";
}
Here's the complete modified code
$data = "house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*";
$exp_filter = explode("*", trim($data));
for ($x = 0; $x <= count($exp_filter); $x++)
{
print "".$exp_filter[$x]."";
if (($x + 1) % 5 == 0)
{
print "<br>";
}
}
Working example: http://codepad.org/iEsKK98M
$data="house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*";
$exp_filter=explode("*",trim($data));
for($x=1;$x<=count($exp_filter);$x++)
{
print "".$exp_filter[$x]."";
if($x%5==0)
{
print "<br>";
}
}
Try this. The problem is you started at 0 in for, you should start from 1 ;)
Quickfix:
Demo
<?php
$data="house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*";
$exp_filter=explode("*",trim($data));
for($x=1;$x<=count($exp_filter);$x++)
{
print "".$exp_filter[$x]."";
if($x > 0 && $x%5==0)
{
echo "<br />";
}
}
?>
I would use array chunk and implode instead:
$data="house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*";
foreach(array_chunk(explode('*', $data), 5) as $chunk){
echo implode(' ', $chunk) . '<br>';
}
Live example: http://codepad.viper-7.com/ED2wHR
Short
Generating table with barcodes of items. Each item has exact quantity in database table. Fields in tables are limited: 65, if more then 65 then build a second table, then a third one...How to generate tables with this conditions?
Detailed
Let's say we want to generate a table with 65 available fields (5x13).
My plan is the following
User selects items' checkboxes
When user submits form, PHP gets values of checked checkboxes
PHP gets quantities of each item from database
Generating table
For ex. the quantity for item id 55 is 2 and for 56 is 4 then the table must look like that
My code looks like that (I know that it's wrong, but I can't figure out how it must be. There must be more than 5 counters: rows counter, columns counter, $_POST['id'] counter, items' quantity counter, table counter (if total sum is more than 65))
UPDATE
<?php
$items = array();
foreach ($_POST['checkbox'] as $id) {
$stmt = $db->prepare("SELECT `qt` FROM `items` WHERE `id`=?");
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->bind_result($qt);
$stmt->fetch();
$stmt->close();
for ($cnt = 1; $cnt <= $qt; $cnt++)
$items[] = $id;
}
$i = 0;
foreach ($items as $item) {
for ($j = 0; $j < $item['quantity']; $j++) {
// check if it's the beginning of a new table
if ($i % 65 == 0)
echo '<table>';
// check if it's the beginning of a new row
if ($i % 5 == 0)
echo '<tr>';
echo '<td><img src="bc.php?id=' . $item['id'] . '" alt="' . $item['name'] . '" /></td>';
// check if it's the end of a row
if (($i - 1) % 5 == 0)
echo '</tr>';
// check if it's the end of a table
if (($i - 1) % 65 == 0)
echo '</tr></table>';
$i++;
}
}
// if the last row wasn't closed, close it
if ($i % 5 != 0)
echo '</tr>';
// if the last table wasn't closed, close it
if ($i % 65 != 0)
echo '</table>';
?>
Any suggestion?
EDIT 4
<?php
$i = 0;
foreach ($_POST['checkbox'] as $id) {
$stmt = $db->prepare("SELECT `qt` FROM `items` WHERE `id`=?");
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->bind_result($qt);
$stmt->fetch();
$stmt->close();
for ($cnt = 1; $cnt <= $qt; $cnt++) {
// check if it's the beginning of a new table
if ($i % 65 == 0)
echo '<table>';
// check if it's the beginning of a new row
if ($i % 5 == 0)
echo '<tr>';
echo sprintf('<td><img src="bc.php?id=%1$d" alt="%1$d" /></td>', $id);
// check if it's the end of a row
if (($i + 1) % 5 == 0)
echo '</tr>';
// check if it's the end of a table
if (($i + 1) % 65 == 0)
echo '</table>';
$i++;
}
}
// if the last table isn't full, print the remaining cells
if ($i % 65 != 0) {
for ($j = $i%65; $j < 65; $j++) {
if ($j % 65 == 0) echo '<table>';
if ($j % 5 == 0) echo '<tr>';
echo '<td></td>';
if (($j + 1) % 5 == 0) echo '</tr>';
if (($j + 1) % 65 == 0) echo '</table>';
}
}
You need to just repeatedly make sheets of 65 cell tables until you're done.
I'm going to pseudo code:
data rows = resultOfMyQuery();
int index = 0;
while(index < rows.count()) {
index = createASheet(rows, index);
}
END;
int createASheet(data rows, int index) {
int availableCells = 65;
int column = 0;
print("<table>");
while (availableCells > 0) {
if (index < rows.count()) {
data row = rows.get(index);
int quantity = row.getQuantity();
if (quantity > availableCells) {
// Stay on this item with reduced quantity for next sheet.
row.setQuantity(quantity - availableCells);
rows.set(index, row);
} else {
// Move on to next item on this (or next) sheet.
index++;
}
for (i=0; i<quantity && availableCells>0; quantity--) {
column = makeACell(column, StringFormat("<TAG ATTRIB='%d'></TAG>",row.getId()));
availableCells--;
}
} else {
// fill in empty cells
column = makeACell(column, "");
availableCells--;
}
}
print("</table>");
return index;
}
int makeACell(int column, String filling) {
String cell = "";
if (column == 0) {
cell.append("<tr>");
}
cell.append("<td>").append(filling).append("</td>");
if (column == 4) {
cell.append("</tr>");
}
print cell;
return (column+1) % 5;
}
Why don't you use an existing barcode system like QR-codes? These standards offer libraries to build the codes and are readable by various devices.
If you really want to built your own barcode I strongly suggest to use the binary system to store your values. Think about check sums too.