Add table row with jquery after two for loops - php

Normally I specify the wanted element by eq() function from jQuery.
Now I need to specify a table row within two for loops but I can't access to $i and $l.
This is the code.
$content_count = count($content);
$languages_count = count($languages);
for ($i = 0; $i < $content_count; $i++) {
for ($l = 0; $l < $languages_count; $l++) {
echo '<tr>';
echo ' <td class="dataTableConfig col-left" style="border-right:1px solid #a3a3a3;">' . $lang_img . TEXT_TITLE . '</td>';
echo ' <td class="dataTableConfig col-single-right">';
echo xtc_draw_input_field('content_title[' . $i . '][' . $languages[$l]['id'] . ']', ((isset($content_lang['content_title'])) ? $content_lang['content_title'] : ''), 'size="60"');
echo ' </td>';
echo '</tr>';
}
}
I tried it whith this code
echo '<tr>';
echo ' <td class="dataTableConfig col-left" style="border-right:1px solid #a3a3a3;">' . $lang_img . TEXT_MASK_ALIAS . '</td>';
echo ' <td class="dataTableConfig col-single-right">';
echo xtc_draw_input_field('mask_alias[' . $i . ']['. $languages[$l]['id'] . ']', ((isset($content_lang['mask_alias'])) ? $content_lang['mask_alias'] : ''), 'size="60"');
echo ' </td>';
echo '</tr>';
?>
<script>
$(document).ready(function() {
var content = <?php echo $content_count; ?>;
var languages = <?php echo count($languages); ?>;
for (var i = 0; i < content.length; i++) {
for (var l = 0; l < languages.length; l++) {
$("[name^='mask_alias']").closest("tr").detach().insertAfter($("[name=^'content_title']").closest("tr"));
}
}
});
</script>
But it's not at the right place and the counts of $i and $l are wrong.

I found a solution with this code
<script>
$(document).ready(function() {
var content = <?php echo $content_count; ?>;
var languages = <?php echo json_encode($languages); ?>;
var row = '';
for (var i = 0; i < content; i++) {
$.each(languages, function(key, value) {
row = '<tr>';
row += ' <td class="dataTableConfig col-left" style="border-right:1px solid #a3a3a3;">';
row += ' <div style="float:left;margin-right:5px;">';
row += ' <img src="/lang/' + value['directory'] + '/admin/images/' + value['image'] + '" alt="' + value['name'] + '" title="' + value['name'] + '" style="border:0;">';
row += ' </div>';
row += ' <?php echo TEXT_MASK_ALIAS; ?>';
row += ' </td>';
row += ' <td class="dataTableConfig col-single-right">';
row += ' <input type="text" name="mask_alias[' + i + '][' + value['id'] + ']" value="<?php echo (isset($content_lang['mask_alias']) ? $content_lang['mask_alias'] : ''); ?>" size="60">';
row += ' </td>';
row += '</tr>';
$('[name="content_title[' + i + '][' + value['id'] + ']').closest('tr').after(row);
});
}
});
</script>

Related

Table with dynamic looping

I have created table with dynamic looping (if I have 100 of records, it should show only 15 record in one table)
With this I have 20 records creating two tables is done but the problem is in column dynamic is not getting and same records are displaying, for displaying in one table 15 records and 5 records in other table:
Here is my code:
<?php
$total = $totalQues / 15;
for ($row = 0; $row < $total; $row++)
{
echo '<table aria-describedby="example2_info" id="example2" class="table table-bordered table-hover dataTable custom-table pull-left col-xs-offset-1 col-lg-offset-2 col-sm-offset-2 col-md-offset-2">';
echo '<tbody aria-relevant="all" aria-live="polite" role="alert">';
echo '<tr class="odd black">';
echo '<td class=" text-center">Q</td>';
echo '<td class="text-center">C</td>';
echo '<td class="text-center">U</td>';
echo '</tr>';
$j = 0;
$k = 0;
$l = 0;
for ($i = 0; $i < 15; $i++)
{
$j = $i + $totalQues;
$k = $i + 30;
$l = $i + 45;
$m = $i + 1;
$n = $k + 1;
$o = $l + 1;
if (empty($r[1][$i]->corr_ans))
{
echo '<tr style = "color:green;">
<td>' . $m . '</td>
<td></td>
<td></td>
</tr>';
}
else
{
$uans = strtolower($r[1][$i]->corr_ans);
$cans = strtolower($r[1][$i]->user_ans);
$u = ord($uans);
$cr = ord($cans);
// echo "u=".$u."cr=".$cr;
if ($u == $cr):
echo '<tr style = "color:green;">
<td>' . $m . '</td>
<td>' . strtoupper($r[1][$i]->corr_ans) . '</td>
<td>' . strtoupper($r[1][$i]->user_ans) . '</td>
</tr>';
else:
echo '<tr style = "color:red;">
<td>' . $m . '</td>
<td>' . strtoupper($r[1][$i]->corr_ans) . '</td>
<td>' . strtoupper($r[1][$i]->user_ans) . '</td>
</tr>';
endif;
}
}
echo '</tbody>';
echo '</table>';
}
?>
I believe the following code should do the job for you:
$numberOfTables = $totalQues/15;
$maxRows = 15
for($table = 0; $table < $numberOfTables; $table ++){
echo '<table aria-describedby="example2_info" id="example2" class="table table-bordered table-hover dataTable custom-table pull-left col-xs-offset-1 col-lg-offset-2 col-sm-offset-2 col-md-offset-2">';
echo '<tbody aria-relevant="all" aria-live="polite" role="alert">';
echo '<tr class="odd black">';
echo '<td class=" text-center">Q</td>';
echo '<td class="text-center">C</td>';
echo '<td class="text-center">U</td>';
echo '</tr>';
$tableMax = ($table < $numberOfTables-1 ? $maxRows*($table+1) : $totalQues)
for($row = $table*$maxRows; $row < $tableMax; $row++){
if (!empty($r[1][$row]->corr_ans)) {
$uans = strtolower($r[1][$row]->corr_ans);
$cans = strtolower($r[1][$row]->user_ans);
$u = ord($uans);
$cr = ord($cans);
//echo "u=".$u."cr=".$cr;
$color = ($u == $cr ? 'green' : 'red');
echo '<tr style = "color:'.$color.'">
<td>' . $row . '</td>
<td>' . strtoupper($r[1][$row]->corr_ans) . '</td>
<td>' . strtoupper($r[1][$row]->user_ans) . '</td>
</tr>';
}
}
echo '</tbody>';
echo'</table>';
}
?>

How to display all record row from database table into a table

I'm trying to made a table which can get database table value all row record. I've made it and I can do it but the data record just showing one data from the record database table, and it always looping the record value to the right while I clicked the button for showing the record into the table. Does anyone know what wrong with my code here?
This is the php, jquery and the html script, and the screenshot of the page:
$(document).ready(function() {
$("#ajaxButton").click(function() {
$.ajax({
type: "Post",
url: "employee.php",
success: function(data) {
var list = JSON.parse(data);
var tr = "";
$.each(list, function(i, v) {
tr = +"<tr>";
tr += "<td>" + v['no'] + "</td>";
tr += "<td>" + v['sensor1'] + "</td>";
tr += "<td>" + v['sensor2'] + "</td>";
tr += "<td>" + v['sensor3'] + "</td>";
tr += "<td>" + v['sensor4'] + "</td>";
tr += "<td>" + v['sensor5'] + "</td>";
tr += "<td>" + v['sensor6'] + "</td>";
tr += "<td>" + v['sensor7'] + "</td>";
tr += "<td>" + v['sensor8'] + "</td>";
tr += "<td>" + v['sensor9'] + "</td>";
tr += "<td>" + v['sensor10'] + "</td>";
tr += "<td>" + v['sensor11'] + "</td>";
tr += "<td>" + v['sensor12'] + "</td>";
tr += "<td>" + v['ambien'] + "</td>";
tr += "<td>" + v['average'] + "</td>";
tr += "<td>" + v['deffiasi'] + "</td>";
tr += "<td>" + v['status'] + "</td>";
tr += "</tr>";
});
$("#table_s tbody").append(tr);
}
});
});
});
row += '<td>' + data.rows[i].date + '</td>';
row += '<td>' + data.rows[i].company + '</td>';
row += '<td>' + data.rows[i].location + '</td>';
<html>
<head>
<script language="javascript" type="text/javascript" src="jquery-1.6.2.js"></script>
<script language="javascript" type="text/javascript" src="ajax.js"></script>
</head>
<body>
<form name="table_s" id="table_s" class="table_s">
<table id="table_s" class="table_s"cellspacing='0' class="js-serial" border="2">
<thead>
<tr>
<th><center>No.</center></th>
<th><center>S1</center></th>
<th><center>S2</center></th>
<th><center>S3</center></th>
<th><center>S4</center></th>
<th><center>S5</center></th>
<th><center>S6</center></th>
<th><center>S7</center></th>
<th><center>S8</center></th>
<th><center>S9</center></th>
<th><center>S10</center></th>
<th><center>S11</center></th>
<th><center>S12</center></th>
<th><center>Ambien</center></th>
<th><center>Average</center></th>
<th><center>Deff</center></th>
<th><center>Status</center></th>
</tr>
</thead>
<tbody>
<tr>
</tr>
</tbody>
</table>
<input type="button" value="Click Here" id="ajaxButton"/>
</body>
</html>
<?php
$con=mysqli_connect("localhost","root","","silo");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
// Data for Titik1
$query = mysqli_query($con,"SELECT * FROM termocouple");
$rows = array();
while($tmp= mysqli_fetch_array($query)) {
$rows[] = $tmp;
}
echo json_encode($rows);
mysqli_close($con);
?>
You should append tr within the loop.
success: function(data){
var list = JSON.parse(data);
for(var i = 0; i < list.length; i++){
var tr = "<tr>";
tr += "<td>"+list[i]['no']+"</td>";
tr += "<td>"+list[i]['sensor1']+"</td>";
tr += "<td>"+list[i]['sensor2']+"</td>";
tr += "<td>"+list[i]['sensor3']+"</td>";
tr += "<td>"+list[i]['sensor4']+"</td>";
tr += "<td>"+list[i]['sensor5']+"</td>";
tr += "<td>"+list[i]['sensor6']+"</td>";
tr += "</tr>";
$("#table_s tbody").append(tr);
}
}

How to add a pagination in a html table using PHP?

I have PHP snippet that displayed a result in the table row. I'm trying to a pagination at the end of the table with (1 2 3 4 ...10). The minimum row will be displayed every page is 15 only.
But my code is not working. Please help me
$results_per_page = 10;
$current = get_query_var('paged') ? get_query_var('paged') : 1;
$start = ($current - 1) * $rows_per_page;
$end = $start + $rows_per_page;
$end = (sizeof($rows) < $end) ? sizeof($rows) : $end;
if (isset($_POST['list_position']) && $_POST['list_position'] != 'Select by Position'){
$list_position= $_POST['list_position'];
$result_position= $wpdb->get_results($wpdb->prepare("SELECT DISTINCT id, submit_time, last_name, first_name, middle_name, mobile_number, email, location, position, message FROM tablename WHERE position= '" . $list_position . "' ORDER BY position ASC", OBJECT));
echo '<table>';
echo '<tr>';
$dir="";
$file="";
$optionId = 0;
echo '<th>Submit Time</th>';
echo '<th>Last Name</th>';
echo '<th>First Name</th>';
echo '<th>Middle Name</th>';
echo '<th>Mobile Number</th>';
echo '<th>Email</th>';
echo '<th>Position</th>';
echo '<th>Message</th>';
echo '<th>Resume</th>';
echo '<th>Processed?</th>';
foreach ($result_position as $record_s){
$optionId++;
//$optionId = $record_s['id'];
for ($i=$start;$i < $end ;++$i ){
echo '<tr>';
echo '<td id="submit_time">' . $record_s->submit_time . '</td>';
echo '<td id="last_name">' . $record_s->last_name . '</td>';
echo '<td id="first_name">' . $record_s->first_name . '</td>';
echo '<td id="middle_name">' . $record_s->middle_name . '</td>';
echo '<td id="mobile_number">' . $record_s->mobile_number . '</td>';
echo '<td id="email">' . $record_s->email . '</td>';
echo '<td id="position">' . $record_s->position . '</td>';
echo '<td id="message">' . $record_s->message . '</td>';
echo '<td id="resumeFile'.$id.'">Download Resume</td>';
echo '<td id="radioOption><label for="Yes">Yes</label>
<input type="radio" id="processedOptionYes'.$optionId.'" name="processedOption" value="Yes" onclick="proccessedCheck('.$optionId.',\'Yes\')"/>
<label for="No">No</label>
<input type="radio" id="processedOptionNo'.$optionId.'" name="processedOption" value="No" onclick="proccessedCheck('.$optionId.',\'No\')"/></td>';
echo '</tr>';
}
}
echo '</table>';
}
jQuery pagination one another option if you want to try? https://datatables.net/examples/basic_init/zero_configuration.html Here is datatables link you can easily implements in php.

grab a value from a database, and use a forloop to fill a drop down menu in php

I've created a rather large dynamic site as a project, and everything is working, except one function.
I'm using PHP to display an HTML table that populates from my database. I can grab the value of ip_stock, which is an int value, I want to take that value, and forloop through it, to populate a drop down menu for each item to be purchased in the table. Each item will have a different value in ['ip_stock']. Here is my code, minus some of the HTML at the top, which is not very relative, but the connection is made and successful, my forloop doesn't work, and doesn't work if I put it in the selection code itself.
<?php
$pagerows = 5;
if (isset($_GET['p']) && is_numeric($_GET['p'])) {
$pages=$_GET['p'];
} else {
$query = "SELECT COUNT(ip_id) FROM in_product";
$result = mysqli_query($dbhandle, $query);
$rows = #mysqli_fetch_array($result, MYSQLI_NUM);
$records = $rows[0];
if ($records > $pagerows) {
$pages = ceil($records/$pagerows);
}else{
$pages = 1;
}
}
if (isset($_GET['s']) && is_numeric ($_GET['s'])) {
$start = $_GET['s'];
}else{
$start = 0;
}
echo "<table class='tableadmin2'>
<tr>
<td class='td2'><b>Add to Cart: </b></td>
<td class='td2'><b>Product Information: </b></td>
<td class='td2'><b>Product Photo: </b></td>
<td class='td2'><b>Select Amount: </b></td>
</tr>";
$display6="SELECT *
FROM in_product LIMIT $start, $pagerows; " ;
$displayResult6 = #mysqli_query($dbhandle, $display6)
or die(mysqli_error($dbhandle));
while($row6 = mysqli_fetch_array($displayResult6, MYSQLI_ASSOC)) {
$i = 1;
$x = $row6['ip_stock'];
$y = " ";
for($i = 1; $i <= $x; $i++) {
$y = "<option value=' . $i . '> $i </option>";
}
echo "<tr>
<input type='hidden' id='prod' value='" . $row6['ip_id'] . "' /></td>
<td class='td2'><a href='addcartInstate.php?ip_id=" . $row6['ip_id'] . "'>Add To Cart&nbsp</a></td>
<td class='td2'><strong> " . $row6['ip_name'] . " </strong><br> " . $row6['ip_desc'] . " <br> $" . $row6['ip_price'] . " </td>
<td class='td2'><img alt='first' src=" . $row6['ip_image'] . " width='300' height='250'></td>
<td class='td2'><br>
<b>Quantity: </b><br>
<select id='orderIn_quantity' name='orderIn_quantity'>
<option value='Select'> Select </option>
$y
</select>
</td>
</tr>";
}
echo "</table>";
if(isset($_POST['orderIn_quantity'])) {
$orderIn_quantity = $_POST['orderIn_quantity'];
}
if($pages > 1) {
echo '<p class="table3">';
$current = ($start/$pagerows) + 1;
if ($current != 1) {
echo 'Previous Page ';
}
if ($current != $pages) {
echo 'Next Page ';
}
}
?>
As far I can see it will display only the last value, correct? Because you keep overwriting your previous option with the new one. You need to use string concatenation.
You need to change
$y = "<option value=' . $i . '> $i </option>";
to
// this will append new string to previous string
$y .= "<option value=' . $i . '> $i </option>";
You need to append to $y not overwrite it. Also you should probably stick to eiter concatenation or variable interpretation when echoing strings, so when you output $y do it with concatenation:
$y='';
for($i = 1; $i <= $x; $i++) {
$y =. "<option value='" . $i . "'>". $i ."</option>";
}
echo "<tr>
<input type='hidden' id='prod' value='" . $row6['ip_id'] . "' /></td>
<td class='td2'><a href='addcartInstate.php?ip_id=" . $row6['ip_id'] . "'>Add To Cart&nbsp</a></td>
<td class='td2'><strong> " . $row6['ip_name'] . " </strong><br> " . $row6['ip_desc'] . " <br> $" . $row6['ip_price'] . " </td>
<td class='td2'><img alt='first' src=" . $row6['ip_image'] . " width='300' height='250'></td>
<td class='td2'><br>
<b>Quantity: </b><br>
<select id='orderIn_quantity' name='orderIn_quantity'>
<option value='Select'> Select </option>
". $y ."
</select>
</td>
</tr>";

Create rows and columns based on COUNT query

Simple question I believe for anyone with minimal php skills (which I don't have sufficient amounts of haha)
$numrows = $retour['nb'] / 4;
echo $numrows;
echo "<table><tr>";
while ($callback = mysql_fetch_assoc($queryLocations2))
{
echo utf8_encode('<td><img src="/flags/' . strtolower($callback['loc_code']) . '.png" id="' . $callback['loc_id'] . '"><input type="checkbox" value="' . $callback['loc_url'] . '" />' . $callback['loc_city'] . ', ' . utf8_encode($callback['loc_state']) . '</td>');
}
echo "</tr></table>";
}
How would I go about presenting a table that will hold 4 results(4 columns) per row, based on the value of $numrows?
Thank you!
Output tr tags inside while loop:
$count = 0;
echo "<table>";
while ($callback = mysql_fetch_assoc($queryLocations2))
{
if ($count % 4 == 0)
echo '<tr>';
$count++;
echo utf8_encode('<td><img src="/flags/' . strtolower($callback['loc_code']) . '.png" id="' . $callback['loc_id'] . '"><input type="checkbox" value="' . $callback['loc_url'] . '" />' . $callback['loc_city'] . ', ' . utf8_encode($callback['loc_state']) . '</td>');
if ($count % 4 == 0)
echo '</tr>';
}
if ($count % 4 != 0)
{
// need to add missing td-s here
echo '</tr>';
}
echo "</table>";
$numrows = floor($retour['nb'] / 4);
echo $numrows;
$i=0;
echo "<table>";
while ($callback = mysql_fetch_assoc($queryLocations2))
{
if($i==4)
{
echo "<tr>";
echo utf8_encode('<td><img src="/flags/' . strtolower($callback['loc_code']) . '.png" id="' . $callback['loc_id'] . '"><input type="checkbox" value="' . $callback['loc_url'] . '" />' . $callback['loc_city'] . ', ' . utf8_encode($callback['loc_state']) . '</td>');
echo "</tr>";
$i=0;
}
$i++;
}
while ($i<4)
{
echo '<td></td>';
$i++;
}
echo "</table>";
}
//if numrows used for # of rows then use following
$count=0;
while ($callback = mysql_fetch_assoc($queryLocations2) && $count<=$numrows)
{
if($i==4)
echo "<tr>";
echo utf8_encode('<td><img src="/flags/' . strtolower($callback['loc_code']) . '.png" id="' . $callback['loc_id'] . '"><input type="checkbox" value="' . $callback['loc_url'] . '" />' . $callback['loc_city'] . ', ' . utf8_encode($callback['loc_state']) . '</td>');
if($i==4)
{
echo "</tr>";
$i=0;
$count++;
}
$i++;
}
while ($i<4)
{
echo '<td></td>';
$i++;
}
echo "</table>";
}

Categories