Table with random number value as first cell - php

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>

Related

How to use css to style table for php iteration values

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;
}

Change on every other row

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++;
}

How to create a function that takes the parameters from the code and returns a string?

So, I've been trying to learn PHP. I've got an assignment where I'm supposed to make a list of products with prices, and depending on what day it is, it's supposed to give you a discount. That works so far. However, one thing i'm trying to do is to not echo the result directly in the code; I want to create a function that takes the necessary parameters (Taste, Price, Amount) and returns a formatted string. I haven't been able to do this, despite trying a lot. Here's the code I have. This is the first time i'm using Stack Overflow, so forgive me if i didn't type it in correctly.
<?php
$donuts = array(
array("Strawberry", 10),
array("Chocolate", 20),
array("Vanilla", 30)
);
$weekday = date('N');
$week = date('W');
$hour = date('G');
if ($weekday == 4) {
echo "Hi";
}
?>
<?php
$today = date("l F j, Y");
if ($weekday % 2 == 1 && $week % 2 == 1 && $hour >= 14 && $hour < 17){
echo "Congratulations! You get a 5% discount, and your wares will be delivered tomorrow!";
}
PRINT "$today";
?>
<table border="1">
<tr>
<th>Taste</th>
<th>Price</th>
<th>Discount</th>
<th>Amount</th>
</tr>
<?php
for($i = 0; $i<count($donuts); $i++) {
echo "<tr>";
echo "<td>".$donuts[$i][0]."</td>";
if( $weekday == 1){
echo"<td>".getPricePercent($donuts[$i][1], .5)." kr</td>";
}
if( $weekday == 3){
echo"<td>".getPricePercent($donuts[$i][1], 1.1)." kr</td>";
}
$pris = $donuts[$i][1];
if ($weekday == 5 && $pris > 20){
echo"<td>".($pris-1)." kr</td>";
}
else { echo"<td>".getPrice($donuts[$i][1], .9)." kr</td>";}
?>
<td><input type="number" name="donuts-<?php echo $i; ?>" /></td>
<?php
echo "</tr>";
}
?>
</table>
<?php
function getPrice($price, $percent) {
return ($price);
}
function getPricePercent($price, $percent){
return ($price * $percent);
}
?>
I dont exactly understand what you are trying to print in price and discount section. You can add getDiscount function to make your code clean.
function getDiscount($weekday, $price){
if($weekday == 1){
return .5;
} else if($weekday == 3){
return 1.1;
} else if($weekday == 5 && $price > 20){
return $price-1;
}
}
Then you can do that,
<table border="1">
<tr>
<th>Taste</th>
<th>Price</th>
<th>Discount</th>
<th>Amount</th>
</tr>
<?php
for($i = 0; $i<count($donuts); $i++) {
echo "<tr>";
echo "<td>".$donuts[$i][0]."</td>";
echo"<td>".getPricePercent($donuts[$i][1], getDiscount($weekday,$donuts[$i][1]))." kr</td>";
echo"<td>".getPrice($donuts[$i][1], getDiscount($weekday,$donuts[$i][1]))." kr</td>";
?>
<td><input type="number" name="donuts-<?php echo $i; ?>" /></td>
<?php
echo "</tr>";
}
?>
</table>
Still, i dont understand, are you trying to dynamically update values of price depending on the amount ?
If you want to concat the strings, you can achieve it by using something like this:
echo "Price" . $price . "Amount" . $amount;

table printing one more cell instead of new line

I am trying to limit the printing of table cells to 3 per row. This worked in one example, but clearly not working when I tried to use the same code somewhere else in the site. This is the code:
$n=3;
echo "<table cellpadding='10' cellspacing='10' style='margin-right:-70px;'><tr>";
$users_count = count($users);
for($i=0; $i<$users_count;$i++)
{
$temp = array();
$temp = $users[$i];
echo "<td>";
echo "<div id='kitchen_box'>";
echo "<div id='kitchen_box_details'>";
echo "<h4>".$temp->fullname . "</h4><br>";
if(strcmp($temp->address, '') == 0)
echo $temp->city;
else
echo $temp->address.", ".$temp->city;
echo "</div>";
echo "<div id='kitchen_box_pic'><img id='kitchen_image' src='".$temp->profilepic."' /></div>";
echo "</div>";
echo "</td>";
if($i != 0){
if($i % $n == 0 && $i != $users_count-1){
echo "</tr><tr>";
}
else{
echo ""; //if it is the last in the loop - do not echo
}
}
}
echo "</table>";
I can't see why this wouldn't work! I would really appreciate support on the matter :)
There are several issues with your code. But your main issue is that you're using a zero-based increment, but doing a 1-based check. So, a table of your the results of an $i!=0 && $i%$n==0 goes like this:
$i $result
0 false
1 false
2 false
3 true
So, you see, the result closes the row after the fourth, not the third cell. To fix this, change the line to:
if($i % $n == $n-1 && $i != $users_count-1){
You should also include a closing </tr> tag with your closing </table> tag.
Incidentally, you shouldn't give the same ID to multiple elements on a page. Each of your kitchen_box and kitchen_box_div DIV tags will have the same ID. If you want this for CSS, use classes. Otherwise, you might try adding the value of $i to each ID.
Nitpicking on request:
The line $temp = array(); seems a little pointless, especially since you don't want $temp to be an array, but an object.
The else{ echo ""; } lines are also redundant.
You don't need the if($i != 0) check now because that case will not pass the next test anymore.
Otherwise the code seems fine to me.
I think the problem is that your $i starts at zero.
Let's look at your conditions before creating a new row :
if($i != 0){
if($i % $n == 0 && $i != $users_count-1)
If $i = 0, the first condition isn't matched. Then the second isn't for $i = 1 and $i = 2. And then your script creates another ... in your first row before matching your if conditions for the first time.
I guess you could move this part of the code right after the beginning of the for instructions :
for($i=0; $i<$users_count;$i++)
{
if($i != 0)
{
if($i % $n == 0 && $i != $users_count-1){
echo "</tr><tr>";
}
else{
echo ""; //if it is the last in the loop - do not echo
}
}
// Echo your <td> ... </td>
}
echo "</tr></table>";

Table designing in while loop

I want to show 4 images from database in a table (2*2) I know how to do that. But I have also special condition (if there are less picture than 4, it will show default pictures to show 4 images totally)
I manage this in a while loop. If there are 4 images there is no problem, it works as I want but when there are fewer images (which means default images will be completed 4 images in total) it doesn't work. I can't figure out how to do that.
Any help?
<table>
<tr>
<?php
$todisplay = 4;
$query = mysql_query("select * from Images where Country_ID=$co_id LIMIT 0,4;");
while ($row = mysql_fetch_array($query)) {
$x++;
echo "<td><img src='".$row['I1_Th'] . "'/></td>";
$displayed_number++;
if ($x % 2==0) {
echo "</tr><tr>";}
}
echo str_repeat("<td>
<img src='images/png/defthumb.png'> </td>", $todisplay-$displayed_number);
?>
</tr></table>
You were not far from it - simply create another loop that does the same thing, but with the default image instead. Also, the $displayed_number seems to hold the same value as $x, so I deleted it.
<table>
<tr>
<?php
$todisplay = 4;
$query = mysql_query("select * from Images where Country_ID=$co_id LIMIT 0,4;");
$x = 0;
while ($row = mysql_fetch_array($query)) {
$x++;
echo "<td><img src='".$row['I1_Th'] . "'/></td>";
if ($x % 2==0) {
echo "</tr><tr>";}
}
while ( $x < $todisplay) {
$x++;
echo "<td><img src='images/png/defthumb.png'/></td>";
if ($x % 2==0) {
echo "</tr><tr>";}
}
?>
</tr></table>
Instead of looping over the rows returned by your query, why not just loop four times and attempt to fetch a row instead?
<table>
<tr>
<?php
$tablerows = 2;
$query = mysql_query("select * from Images where Country_ID=$co_id LIMIT " + ($tablerows * 2) + ";");
for ($x = 1; $x <= ($tablerows * 2); $x++) {
if ($row = mysql_fetch_array($query)) {
echo "<td><img src='".$row['I1_Th'] . "'/></td>";
} else {
echo "<td><img src='images/png/defthumb.png'></td>";
}
if ($x % 2==0 && $x != $tablerows * 2) {
echo "</tr><tr>";}
}
?>
</tr></table>

Categories