Table of numbers in a pattern using PHP - php

A couple of days ago, I made a post similar to this one.
I continued my work, but went back to this exercise after.
I need 2 more patterns in a table. The last 2 tables, I did with nested For-loops, which worked excellent, but now I'm stuck again.
What I need:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
What I tried:
<table>
<b>Patroon III</b>
<?php
$rows = 6;
for($row = 1; $row <= $rows; $row++){
echo "<tr>";
for($col = 6; $col >= $row; $col--){
if($col <= $row){
echo "<td class='td2'>" . $col . "</td>";
}
else {
echo "<td class='td2'>" . " " . "</td>";
}
}
echo "</tr>";
}
?>
</table>
What I get:
1
2
3
4
5
6
And the second table I need:
1 2 3 4 5 6
2 3 4 5 6
3 4 5 6
4 5 6
5 6
6
What I tried:
<table>
<b>Patroon IV</b>
<?php
$rows = 6;
for($row = 1; $row <= $rows; $row++){
echo "<tr>";
for($col = 1; $col <= $row; $col++){
if($col >= $row){
echo "<td class='td2'>" . $col . "</td>";
}
else {
echo "<td class='td2'>" . " " . "</td>";
}
}
echo "</tr>";
}
?>
</table>
What I get:
1
2
3
4
5
6
Any suggestions? I'm thinking about another nested for-loop, but I don't know where to start. Not sure if the if-else statement is needed.

This is the very basic thing in php. You just need to think about the logic. That's it.
Here is your solution to your question
<b>Patroon III</b>
<table border=1>
<?php
$rows = 6;
for($row = 1; $row <= $rows; $row++){
echo "<tr>";
for($col = 6; $col >= 1; $col--){
if($col <= $row){
echo "<td class='td2'>" . $col . "</td>";
}
else {
echo "<td class='td2'>" . " " . "</td>";
}
}
echo "</tr>";
}
?>
</table>
<b>Patroon IV</b>
<table border=1>
<?php
$rows = 6;
for($row = 1; $row <= $rows; $row++){
echo "<tr>";
for($col = 1; $col <= $rows; $col++){
if($col >= $row){
echo "<td class='td2'>" . $col . "</td>";
} else {
echo "<td class='td2'>" . " " . "</td>";
}
}
echo "</tr>";
}
?>
</table>

Related

Fill a table vertically in php

I'd like to fill a table vertically. For example one name on one row, the age on the second row alternately, and limit the table to 4 columns:
Is this possible with this kind of code?
Here is my Code:
<?php
$age = array("26","16","17","19","24","30");
$name = array("adam","andrew","tim","mike","don","eddy");
echo "<table border=1>";
for($i=0;$i<count($age);$i++)
{
if ($i > 0 && $i % 4 == 0)
{
echo "</tr><tr>";
}
echo "<td>";
echo $name[$i];
echo "</td>";
echo "<td>";
echo $age[$i];
echo "</td>";
}
echo "</tr>";
?>
Please try this. It is not the most elegant way but will give you an idea and maybe improve your answer. I will suggest you maybe use multi-dimensional array to store age and name together.
$age = array("26","16","17","19","24","30");
$name = array("adam","andrew","tim","mike","don","eddy");
$ageChunks = array_chunk($age, 4); //divide age array into chunks of 4
$nameChunks = array_chunk($name, 4); //divide name array into chunks of 4
//print table data
function printData($td)
{
echo "<td> ".$td." <td>";
}
//print table row. Assumption here is $nameChunks and $ageChunks are same length.
function printRow($nameChunks, $ageChunks)
{
foreach ($nameChunks as $key=>$val) {
echo "<tr>";
array_map("printData", $nameChunks[$key]);
echo "</tr><tr>";
array_map("printData", $ageChunks[$key]);
echo "</tr>";
}
}
//print table
function printTable($nameChunks, $ageChunks)
{
echo "<table>";
printRow($nameChunks, $ageChunks);
echo "</table>";
}
printTable($nameChunks, $ageChunks);
you need to change condition
if ($i > 0 && $i % 4 == 0) to if ($i > 0 && $i % 2 == 0) because you print 2 columns per iteration

PHP array with for loop

i have array of 10 item and each item has own price.
i have to select some item and display the selected item's average and if any of selected item's above the average i have to display it's pic in 2x2 table different pic in each cell.
i write this code but the problem is the pic repeat in the 4 cell's:
<?php
echo "<table border=2>";
$incVar = 0;
for ($x = 0; $x < 2; $x++) {
echo"<tr>";
for ($y = 0; $y < 2; $y++) {
echo"<td>";
while($incVar <=9){
if (isset($camera[$incVar]) && $camera[$incVar] >= $avreag){
echo '<img src="' . $pic[$incVar] . '" width="200" height="200">';
}
$incVar++;
}
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
first you have to create image array because if you will display in this manner and if some items does not match the condition then it will leave blank <td>
solution :
<?php
echo "<table border=2>";
$incVar = 0;
for ($x = 0; $x < 2; $x++) {
echo"<tr>";
for ($y = 0; $y < 2; $y++) {
echo"<td>";
if (isset($camera[$incVar]) && $camera[$incVar] >= $avreag) {
echo '<img src="' . $pic[$incVar] . '" width="200" height="200">';
}
$incVar++;
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
no you need to display $imagesArr in 4x4 for loop

Echo out SQL result to create a timetable

I am having problems printing out a timetable using the results from a SQL statement and some HTML layout with PHP.
I have the times of the classes along the top of the page.
I am trying to put the days of the week along the side of the page and then check if the result of the SQL statement (containing a module) should be printed in the specific day and time.
//Print out the top of the array to display the times
echo "<div><table class='table table-bordered'><thead><tr><th></th><th>9-10</th><th>10-11</th><th>11-12</th><th>12-13</th><th>13-14</th><th>14-15</th><th>15-16</th><th>16-17</th></tr></thead><tbody>'";
//Now loop through the result of the SQL statement that contains the modules associated with the selected course
while($row = mysqli_fetch_array($result1)) {
for($d = 1; $d < 6; $d++){
$printday = $days[$d];
echo "$printday";
for($t = 9; $t < 17; $t++) {
if($row['Day'] == $d && $row['Time'] == $t){ //fix this area so that it moves along
echo "<td>" . $row['ModuleName'] . "<br/>\n " .$row['Location'] . "</td>";
} //if
else {
echo "<td></td>";
} //else
} //for 2
echo "</tr>";
} //for 1
} //while
The problem is that I am printing out Monday-Friday 3 times as there are 3 $row results. Any idea how I could get this to work.
Your looping through your data in the wrong spot, you first need to put that in an array so you can loop through it for each day/time.
//Print out the top of the array to display the times
echo "<div><table class='table table-bordered'><thead><tr><th>Day</th><th>9-10</th><th>10-11</th><th>11-12</th><th>12-13</th><th>13-14</th><th>14-15</th><th>15-16</th><th>16-17</th></tr></thead><tbody>'";
//Now loop through the result of the SQL statement that contains the modules associated with the selected course
$courses = array();
while($row = mysqli_fetch_array($result1)) {
$courses[] = $row;
} //while
for($d = 1; $d < 6; $d++){
$printday = $days[$d];
echo "<tr><td>" . $printday . "</td>";
for($t = 9; $t < 17; $t++) {
unset($course_row);
foreach($courses as $course){
if($course['Day'] == $d && $course['Time'] == $t){ //fix this area so that it moves along
$course_row .= $course['ModuleName'] . "<br/>\n " .$course['Location'] . "<br/>\n<br/>\n";
} //if
}
if(isset($course_row)){
echo "<td>" . $course_row . "</td>";
} else {
echo "<td> </td>";
} //else
} //for 2
echo "</tr>";
} //for 1

Change <td> color on condition

Hi i have this PHP which take sql query divide it into two columns and print table. Now i need to change color of TD where value > 0. I've add color change by class. It's working but not correct. It change color of whole string but not TD cell.
$stmt=ociparse($olink, $sql);
if (!$stmt) {
$e = oci_error($olink);
trigger_error(htmlentities($e['message']), E_USER_ERROR);
}
$r =ociexecute($stmt,OCI_NO_AUTO_COMMIT);
if (!$r) {
$e = oci_error($stmt); // For oci_execute errors pass the statement handle
print htmlentities($e['message']);
print "\n<pre>\n";
print htmlentities($e['sqltext']);
printf("\n%".($e['offset']+1)."s", "^");
print "\n</pre>\n";
}
$ncols = oci_num_fields($stmt);
$cur_dt = 1;
echo "<TABLE border=\"1\" width=\"100%\" align='center' cellspacing='0' cellpadding='0' class=\"sortable\">";
/* echo "\n<tr>"; */
for ($i = 1; $i <= $ncols; $i++) {
echo "<th bgcolor=\"#2B75C1\" class=\"header\">".oci_field_name($stmt, $i)."</th>";
}
for ($i = 1; $i <= $ncols; $i++) {
echo "<th bgcolor=\"#2B75C1\" class=\"header\">".oci_field_name($stmt, $i)."</th>";
}
$str=1;
while (oci_fetch($stmt)) {
if ($str % 2 == 1)
{
echo "\n";
echo "<tr";
$hrr="";
echo ">";
}
for ($i = 1; $i <= $ncols; $i++) {
echo "<td";
echo $hrr;
if (oci_result($stmt, 2) >= $cur_dt) {$hrr= " class=\"hour\"";echo $hrr;}
echo ">";
echo oci_result($stmt, $i);
}
echo "</td>";
if ($str % 2 == 0) {echo "</tr> \n";}
$str++;
}
echo "</TABLE>";
oci_close($olink);
?>
</div>
</body>
</html>
0 in second column should be blue, but it ged "hour" class and change color
create a class in your css file like .change{background-color:green} in your
<td>
for ($i = 1; $i <= $ncols; $i++) {
$hrr ="";
if (oci_result($stmt, 2) >= $cur_dt) {$hrr = 'class="change"';}
echo "<td ".$hrr.">";
echo oci_result($stmt, $i);
}
echo "</td>";
if ($str % 2 == 0) {echo "</tr> \n";}
$str++;
}
Use css to style a single cell style="background-color: #2B75C1;"
If you want to change the color of the string style="color: #2B75C1;"
echo '<th style="background-color: #2B75C1;" class=\"header\">'.oci_field_name($stmt, $i).'</th>';
Replace
echo "<td";
echo $hrr;
if (oci_result($stmt, 2) >= $cur_dt) {$hrr= " class=\"hour\"";echo $hrr;}
echo ">";
echo oci_result($stmt, $i);
}
echo "</td>";
For
echo '<td' . (oci_result($stmt, 2) >= $cur_dt ? ' class="hour"' : '') . '>';
// ^ shouldn't be there '$i'?
echo oci_result($stmt, $i);
echo '</td>'; // closing tag for <td> in optional, you can remove that to make your HTML more readable.
// remove $hrr variable, you don't need it.
I think, you have to replace the "2" with $i . Replace the following code
if (oci_result($stmt, 2) >= $cur_dt)
With the solution.
Solution 1 :
if (oci_result($stmt, $i ) >= $cur_dt)
Solution 2 :
if ( (oci_result($stmt, $i ) >= $cur_dt) || oci_result($stmt, $i ) > 0)

want to display results in horizontal table using php mysql

I am using the following code to get results and want to have it displayed in two rows horizontially.
echo "<table border='0' width='700px' align='center'>";
echo "<tr>";
while($number = mysqli_fetch_array($result2))
{
echo "<td class='ball_p'>" . $number['number'] . "</td>";
**echo "</tr><tr>";**
echo "<td class='search'>" . $number['count'] . "</td>";
}
echo "<tr>";
echo "</table>";
Hence I need this part of the code not to be included in the loop echo "";
Please help, this must be really simple.
I want the results to be displayed 10 across as I limit my query to
the top 10.
So it would be:
Number Number Number etc. Count Count Count etc.
Because the tables need to be drawn one row at a time, you could store all of your number values in one array and all of your count values in a second array, then loop through each array as you're building your rows. So first get your values from the database:
// let's get a variable with the total number of records, i.e. horizontal cells:
$totalRecords = 0;
// get values from db:
while($number = mysqli_fetch_array($result2))
{
$numberArr[] = $number['number'];
$countArr[] = $number['count'];
$totalRecords++;
}
Then in another set of loops render the table (one loop for each row):
// first row
echo "<tr>";
for ($i = 0; $i < $totalRecords; ++$i) // you could also use count($numberArr) instead of $totalRecords
{
echo "<td class='ball_p'>" . $numberArr[$i] . "</td>";
}
echo "</tr>";
// second row
echo "<tr>";
for ($i = 0; $i < $totalRecords; ++$i)
{
echo "<td class='ball_p'>" . $countArr[$i] . "</td>";
}
echo "</tr>";
What about this:
$i = 0;
while($number = mysqli_fetch_array($result2))
{
echo "<td class='ball_p'>" . $number['number'] . "</td>";
while($i++ >= 1)
{
echo "</tr><tr>";
$i = 0;
}
echo "<td class='search'>" . $number['count'] . "</td>";
}
I haven't tested it but the ++ after the $i in the second while loop should increment it after the evaluation which should skip the </tr><tr> the first time and print it the second.

Categories