Dynamic Table with select onChange Event not working - php

I have a table, with 12 lines (rows) and 10 columns, for columns odd lines (rows), I have one div inside of cell, with index in Id NumColum_NumLine, and for pair lines (rows), i have one select element equal for all. like this enter image description here
What i need is when i select load image on div above.
<table border="solid">
<?php
for ($i = 1; $i <= 12; $i++) {
echo "<tr>";
for ($A = 1; $A <= 10; $A++)
{
if ($i % 2 == 0)
{
echo '<td id="data">';
$songs = file('pkmlist.txt');
$options = '';
$num = 1;
foreach ($songs as $song) {
$options .= '<option value="'.$num.'" onchange="load_img('.$A.'_'.($i-1).')\">'.$song.'</option>';
$num++;
}
$select = '<select name="songs">'.$options.'</select>';
echo $select;
//echo ''.$A.'_'.($i-1).'</td>';
}
else
{
echo '<td id="Pkm"> <div id="'.$A.'_'.$i.'"> '.$A.'_'.$i.' </div> </td>';
}
}
echo "</tr>";
}
?>
</table>
OnChange don't work.

There I found some syntax error. try this ...
<table border="solid">
<?php
for ($i = 1; $i <= 12; $i++) {
echo "<tr>";
for ($A = 1; $A <= 10; $A++)
{
if ($i % 2 == 0)
{
echo '<td id="data">';
$songs = file('pkmlist.txt');
$options = '';
$num = 1;
foreach ($songs as $song) {
$options .= '<option value="'.$num.'">'.$song.'</option>';
$num++;
}
$select = '<select name="songs" onchange="load_img('.$A.'_'.($i-1).');" >'.$options.'</select>';
echo $select;
//echo ''.$A.'_'.($i-1).'</td>';
}
else
{
echo '<td id="Pkm"> <div id="'.$A.'_'.$i.'"> '.$A.'_'.$i.' </div> </td>';
}
}
echo "</tr>";
}
?>
</table>

Related

Looping through an array with line breaks every 6th element as a table?

I am trying to print an array so every 6th element is on a new line. I'm trying to do this in a table. I would prefer if it could be answered in PHP, but Javascripts Ok.
$array_control = array(
"1","5","6","2","1",
"2","1","6","4","3",
"3","2","5","6","6",
"4","3","1","5","4",
"6","4","2","3","6"
);
$arrayLength = count($array_control);
$i = 0;
while ($i < $arrayLength)
{
if ($i==0) {
echo "<tr>";
}
if (is_int($i/5)) {
echo '<td style="width:16%;">'.$array_control[$i].'</td>';
echo "</tr>";
echo "<tr>";
}else{
echo '<td style="width:16%;">'.$array_control[$i-1].'</td>';
}
$i++;
}
Any help would be great!
The %-operator (modulus) solves this for you:
echo '<table><tr>';
for($i = 1; $i <= count($array_control); $i++ ){
echo '<td style="width:16%;">';
if( ($i % 6) == 0){
echo $array_control[$i-1] . '</td></tr><tr>';
} else{
echo $array_control[$i-1] . '</td>';
}
}
echo '</tr></table>';
or if you feel sassy:
echo '<table><tr>';
for($i = 1; $i <= count($array_control); $i++ ){
echo '<td style="width:16%;">' . $array_control[$i-1], ( ($i % 6) == 0 ) ? '</td></tr><tr>' : '</td>';
}
echo '</tr></table>';
A for loop seems better, and maybe the mod operator is more clear.
echo '<tr>';
for ($i < = 0; i < $arrayLength; ++ $i) {
echo '<td style="width:16%;">'.$array_control[$i].'</td>';
if ($i % 6 == 5) {
echo '</tr>';
echo '<tr>';
}
}
echo '</tr>';
This isn't quite ideal, because you'll get an empty '' at the end, but the browser should hide that. A little more logic can be added if you need to eliminate that too.
Further, with the array structure you're showing, you could eliminate the $arrayLength variable entirely, and instead use:
foreach ($array_control as $i => $val) {
echo '<td style="width:16%;">'.$val.'</td>';

How should I display the result using HTML <table> tag in columns of 50 records each(except the last column)?

I've written a program which currently generates 540 different numbers.
I want to display these 540 different numbers in a columns having 50 records each(except the last column where the number of records should be less than 50) using HTML <table> tag but I'm not able to do so as there are so many loops and conditions are getting applied.
Following is my program :
<?php
function findFourElements() {
$possible_numbers = '';
$sr_no = 0;
$row_count = 0;
for ($i = 0; $i <= 9; $i++) {
for ($j = 0; $j <= 9; $j++) {
for ($k = 0; $k <= 9; $k++) {
for ($l = 0; $l <= 9; $l++) {
$possible_numbers[0] = $i;
$possible_numbers[1] = $j;
$possible_numbers[2] = $k;
$possible_numbers[3] = $l;
if((int)$possible_numbers[0] + (int)$possible_numbers[1] + (int)$possible_numbers[2] + (int)$possible_numbers[3] === 14) {
$sr_no++;
echo $sr_no. ') ' . $possible_numbers[0].$possible_numbers[1].$possible_numbers[2].$possible_numbers[3]. '<br>';
}
}
}
}
}
}
findFourElements();
?>
Please somebody help me out with my logic.
You can use this code:
<?php
function findFourElements()
{
$response = [];
$possible_numbers = '';
$sr_no = 0;
$row_count = 0;
for ($i = 0; $i <= 9; $i++) {
for ($j = 0; $j <= 9; $j++) {
for ($k = 0; $k <= 9; $k++) {
for ($l = 0; $l <= 9; $l++) {
$possible_numbers[0] = $i;
$possible_numbers[1] = $j;
$possible_numbers[2] = $k;
$possible_numbers[3] = $l;
if ((int)$possible_numbers[0] + (int)$possible_numbers[1] + (int)$possible_numbers[2] + (int)$possible_numbers[3] === 14) {
$sr_no++;
$response[] = $possible_numbers[0] . $possible_numbers[1] . $possible_numbers[2] . $possible_numbers[3];
}
}
}
}
}
return $response;
}
$response = findFourElements();
$data = array_chunk($response, 50);
$srNo = 1;
?>
<table>
<tr>
<?php foreach ($data as $key => $value) { ?>
<td>
<table>
<tr>
<th>Sr. No.</th> <th>Vehicle No.</th>
</tr>
<?php foreach ($value as $val) { ?>
<tr>
<td>
<?php echo $srNo ?>
</td>
<td>
<?php echo $val ?>
</td>
</tr>
<?php $srNo++; } ?>
</table>
</td>
<?php } ?>
</tr>
</table>
if you don't want to work with return response you can add extra code inside the function.
You should use a while statement.
<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
<?php
/** Calculations
(# = amount)
540/50 = 10.8 (# of rows)
50*0.8 = 40 (# of records in last row)
*/
echo "<table>";
$x = 0;
$ranNum = 0;
echo "<tr>";
while ($x < 540) {
/* a round number (1,2,3,4,5,...) %1 = 0.
So (Number/50)%1 is only 0 if it's in the multiplication table of 50*/
if ((($x/50)%1) == 0) {
echo "</tr>";
echo "<tr>";
}
$ranNum = rand(1000,9999); // random number
echo "<td id='Track".$x."'>".$ranNum."</td>";
$x++;
}
echo "</tr>";
echo "</table>";
?>
</body>
</html>

Break PHP into 3 columns and display one by one

I'm trying to break a PHP array into 3 columns (has to be columns, not rows) so it would look something like this:
Item 1 Item 5 Item 9
Item 2 Item 6 Item 10
Item 3 Item 7 Item 11..............
Item 4 Item 8
Update to Meldin answer
$cols = 3; // Number of columns
$len = ceil(count($items) / $cols);
echo '<ul class="floatleft">';
for ($i = 0; $i < count($items); $i++) {
if ($i % $len == 0)
echo '</ul><ul class="floatleft">';
echo "<li>" . $items[$i] . "</li>";
}
echo '</ul>';
Also you can try this using two foreach() loops
$len = ceil(count($items)/3);
$itm = array_chunk($items,$len,true);
foreach($itm as $cols){
echo '<div class="floatleft">';
echo '<ul>';
foreach($cols as $col){
echo '<li>'.$col.'</li>';
}
echo '</ul>';
echo '</div>';
}
Use this
echo '<ul class="floatleft">';
for($i=0;$i<count($items);$i++){
if($i%4==0)
echo '</ul><ul class="floatleft">';
echo "<li>".$items[$i]."</li>";
}
echo '</ul>';
Try this:
$total = 10;
$cols = 3;
$rows = ceil($total/$cols);
echo "<table>";
for($i = 1; $i <= $rows; $i++) {
echo "<tr>";
for($j = 1; $j <= $cols; $j++) {
$value = $i + 4*($j-1);
if ($value <= $total) {
echo "<td>".$value."</td>";
}
}
echo "</tr>";
}
echo "</table>";

PHP Skip something a certain amout of times in loop

I have a ton of for loops for generating a table, with results from my MySQL DB. Some of the more important information are the start and end date. I calculate how many days this absence has besides the start date(For example 02.03.2015 - 04.03.2015, 2 days). I save this result in $difference, however I get this result during a for loop, so I cant simply go to the for loop and say $xyz - difference.
So I thought about skiping the <td>'s in the loop, as many times as the difference.
Here in the picture you see that the <td>'s are off the amount of days the absence lasts.
How can I skip them as many times as the difference, so the tables looks right again?
My code is still a mess so no comments about that:
echo '<table class="table table table-bordered table-striped table-hover">';
echo '<thead>';
for ($l = 0; $l < $days; $l++) {
if ($l == 0) {
echo '<th>', '<b>Day</b>', '</th>';
} else {
$date = "$current-$month-$l";
$date = date('D', strtotime($date));
$date = substr($date, 0, -1);
echo '<th class="center">', $date,'</th>';
}
echo "\n";
}
echo '</thead>';
echo '<tbody>';
for ($l = 0; $l < $days; $l++) {
if ($l == 0) {
echo '<td>', '<b>Onshore</b>', '</td>';
} else {
echo '<th class="center">', $l, '</th>';
}
echo "\n";
}
for ($i = 0; $i < $count_user; $i++) {
echo '<tr>';
$result = mysql_query("select start, end, type_FK, employee_FK FROM absences where employee_FK = {$array_user[$i]['employee_ID']} and MONTH(start) = $month and YEAR(start) = $current");
while ($row = mysql_fetch_assoc($result)) {
$array_absences[] = $row;
}
$count = 0;
if (!empty($array_absences)) {
$count = count($array_absences);
}
for ($j = 0; $j < $days; $j++) {
$true = 0;
if ($j == 0 && $i == $count_on) {
echo '<td>';
echo '<b>Offshore</b>';
echo '</td>';
for($k = 0; $k < $days -1; $k++){
echo '<td>';
echo '</td>';
}
echo '</tr>';
}
if ($j == 0) {
echo '<td>';
echo $array_user[$i]['name'], ' ', $array_user[$i]['surname'];
echo '</td>';
}
for ($k = 0; $k < $count; $k++) {
$array_absences[$k]['start'] = substr($array_absences[$k]['start'], -2);
$array_absences[$k]['end'] = substr($array_absences[$k]['end'], -2);
$array_absences[$k]['start'] = ereg_replace("^0", "", $array_absences[$k]['start']);
$array_absences[$k]['end'] = ereg_replace("^0", "", $array_absences[$k]['end']);
$difference = $array_absences[$k]['end'] - $array_absences[$k]['start'];
if ($j == $array_absences[$k]['start'] && $array_absences[$k]['employee_FK'] == $array_user[$i]['employee_ID']) {
$true = 1;
$result = mysql_query("select approved from absences where DAY(start) = $j and MONTH(start) = $month");
while ($row = mysql_fetch_assoc($result)) {
$approved[] = $row;
$n++;
}
$now = date('Y-m-d');
$absence = strtotime("$current/$month/$j");
$absence = date('Y-m-d',$absence);
for ($q = 0; $q < $difference+1; $q++) {
if ($approved[$n]['approved'] == 1) {
echo '<td class="center green">';
} elseif ($approved[$n]['approved'] == 0 && $now <= $absence) {
echo '<td class="center orange">';
} elseif ($approved[$n]['approved'] == 0 && $now > $absence) {
echo '<td class="center red">';
}
for ($l = 0; $l < $count_types; $l++) {
if ($array_absences[$k]['type_FK'] == $types[$l]['type_ID']) {
echo $types[$l]['short'];
}
}
echo '</td>';
}
}
}
//Days that are not absences
//Skip this the amounts of $difference
echo '<td ';
//If weekend special coloring
$date = "$current-$month-$j+1";
$date = new DateTime($date);
$day = $date->format("w");
if ($day == 6 || $day == 0) {
echo 'class = "weekend"';
}
echo '>';
echo '</td>';
echo "\n";
}
echo '</tr>';
}
I'm afraid I did not really get the sense of your explanations, however, to skip something in a loop, you can use the continue keyword for this.
For example :
for($i = 0; $i++; i<1000){
if($i < 200 && $i > 100)
continue;
// Computations are here....
}

Foreach multiple result set print in HTML table

I am trying to pull two results from 2 sql statements and print into HTML table.
but some how results are not coming correct.please hel me to resolve.
Below is code 2nd result is not printing correct results - duplicated results.
<?php
global $wpdb;
$result1=$wpdb->get_results("select post_name,id,cat from wp_posts where post_name like '%java%');
$result2=$wpdb->get_results("select post_name,id,cat from wp_posts where post_name like '%oracle%');
?>
<table id="table_id">
<thead>
<tr>
<th>JAVA</th>
<th>ORACLE</th>
</tr>
</thead>
<tbody>
<?php foreach($result1 as $rows1) { ?>
<?php foreach($result2 as $rows2) { ?>
<tr>
<td> <?php echo $rows1->post_name ; ?> </td>
<td> <?php echo $rows2->post_name ; ?> </td>
</tr>
<?php
}
?>
<?php
}
?>
global $wpdb;
$result1=$wpdb->get_results("SELECT post_name,id,cat FROM wp_posts WHERE post_name LIKE '%java%'");
$result2=$wpdb->get_results("SELECT post_name,id,cat FROM wp_posts WHERE post_name LIKE '%oracle%'");
After getting to objects , we put the required values in two arrays so that we can create a multi-dimensional array.
$result_one = array();
$result_two = array();
//putting values in array
foreach ($result1 as $result) {
$result_one[] = $result->post_name;
}
foreach ($result2 as $result) {
$result_two[] = $result->post_name;
}
//Creating a multi-dimensional array so that we can print two columns easily
$results = array(
$result_one,
$result_two,
);
//Initialising the variables to be used as counters and array indexes.
$i = 0; $j = 0; $k = 0; $p = 1;
//getting length of array
$array_length = count($result_one);
Now start two loops to fetch data from multi-dimensional array created.
In this case: To create the table properly we need values in this order:
$results[0][0], $results[1][0], $results[0][1], $results[1][1],
$results[0][2], $results[1][2] and so on.
So we have to create loops accordingly.
while ($i < 2) {
while ($j < $array_length) {
if (fmod($p,2)) echo '<tr>'; // So that <tr> will be printed only after two <td>
echo "<td>". $results[$i][$j]. "</td>";
if($i == 0) { $i = 1; } else { $i =0; } // Toggling values of $i from 0 to 1.
$k++;
if(fmod($k, 2) == 0){ $j ++; } // Increasing $j after 2 steps.
$p++;
if (fmod($p,2)) echo '</tr>';
}
$i ++;
}
If you need three columns:
Repeat steps one two and three so that you can create an array like this:
$results = array(
$result_one,
$result_two,
$result_three,
);
Change first while loop to
while ($i < 3)
And change the following statements as:
if (fmod($p,3)) echo '<tr>';
if($i == 0) { $i = 1; } elseif ($i == 1) {$i=2} else { $i = 0; }
if(fmod($k, 3) == 0){ $j ++; }
if (fmod($p,3)) echo '</tr>';
I hope you got better understanding now and can make the required changes yourself.
This seems complicated. But this should work. Please try.
Anyone please feel free to edit the while loop to make it simpler.
something i missed out for 3rd result array.please correct me.
$results = array(
$result_one,
$result_two,
$result_three,
);
$i = 0; $j = 0; $m = 0; $k = 0; $p = 1;
$array_length = count($result_one);
?>
<table id="table_id">
<thead>
<tr>
<th>ORACLE</th>
<th>JAVA</th>
<th>SAP</th>
</tr>
</thead>
<tbody>
<?php while ($i < 3) {
while ($j < $array_length) {
if (fmod($p,3)) echo '<tr>';
echo "<td>" . $results[$i][$j] . "</td>";
if($i == 0) { $i = 1; } elseif ($i == 1) {$i=2} else { $i = 0; }
$k++;
if(fmod($k, 3) == 0){ $j ++; }
$p++;
if (fmod($p,3)) echo '</tr>';
}
$i ++;
}
?>

Categories