PHP array display does not display all columns - php

I managed to get the checkboxes to POST the data and displays each on the checkout page but the problem I have is when it gets to 4th column it stops when it hits a certain char limit also how would I turn this into a table on the checkout page.
Database snippet code:
print '<td><input type="checkbox" name="check_list[]"value='. $getColumn[0]. $getColumn[1]. $getColumn[2]. $getColumn[3]. $getColumn[4]. $getColumn[5].$getColumn[6].$getColumn[7].$getColumn[8].$getColumn[9].'</td>';
for ($column = 1; $column < pg_num_fields($res); $column++)
{
print "<td>" . $getColumn[$column] . "</td>";
}
}
print '</table>'
Checkout page
<?php
echo "<hr />\n";
$res = pg_query ($con, "select count(ref) from music");
$a = pg_fetch_row($res);
echo "<p>Total " . $a[0] . " music in database.</p>";
echo "<table border='1'>\n<thead>\n<tr>\n";
echo "<th>Artist</th><th>Composer</th><th>Genre</th><th>Title</th><th>Album</th><th>Label</th> <th>Price</th><th>Description</th>\n";
echo "</tr>\n</thead>\n<tbody>\n";
$res=pg_query($con, "SELECT * from music ORDER BY ref");
while ($a = pg_fetch_array ($res))
{
echo "<tr>";
for ($j = 0; $j < pg_num_fields($res); $j++) {
// htmlspecialchars converts things like & to HTML entity codes
echo "<td>" . htmlspecialchars($a[$j], ENT_QUOTES) . "</td>";
}
echo "</tr>\n";
}
echo "</tbody>\n</table>";
?>

I am sure, you are tried to do the following:
print '<table>';
for ($column = 1; $column < pg_num_fields($res); $column++) {
echo '<tr>';
print '<td><input type="checkbox" name="check_list[]" value="'.$getColumn[$column] .'" /></td>';
print "<td>" . $getColumn[$column] . "</td>";
echo '</tr>';
}
print '</table>';

Related

PHP For Loop jQuery UI Tabs

I am trying to display a list of Links in a table according to Distance and Category. I would like each distance to be a Tab and have the appropriate Links in each Tab. I am trying to accomplish this with A PHP Foreach loop and jQuery-UI Tabs.
Here is the code which gets the data and displays it in the table in each Tab.
The index function in the controller for the View and the function that gets the table data:
public function index() {
$data = array();
$db_name = $this->uri->segment(2);
$this->db->db_select($db_name);
$tables = $this->db->get('tableinfo');
$data['distances'] = array();
$data['tables'] = array(
'men' => array(),
'women' => array()
);
foreach($tables->result() as $row) {
if(!in_array($row->distance, $data['distances'])) {
array_push($data['distances'], $row->distance);
}
if(substr($row->displayname, 0, 4) == "Male") {
array_push($data["tables"]['men'], $row->displayname);
} else {
array_push($data["tables"]['women'], $row->displayname);
}
}
$data['dbname'] = $db_name;
$this->parser->parse('templates/header', $data);
$this->parser->parse('select/index', $data);
$this->parser->parse('templates/footer', $data);
}
public function gettable($table) {
$db_name = "championchipco_sowetomarathon2019";
$this->db->db_select($db_name);
redirect("results/index/" . $db_name . "/" . $table);
}
And the View:
<?php
$i = 1;
echo "<div class='row'>";
echo "<div class='col' id='tabs'>";
echo "<ul>";
foreach($distances as $distance) {
echo "<li><a href='#tabs-" . $i . "'>" . $distance . "</a></li>";
}
echo "</ul>";
foreach($distances as $distance) {
echo "<div id='tabs-" . $i . "'>";
echo "<table class='table' cellspacing='10' cellpadding='10'>";
echo "<tr><th style='font-size: 20px;'>Men</th><th style='font-size: 20px;'>Women</th><th style='font-size: 20px;'></tr>";
foreach($tables['men'] as $table) {
if(substr($table, -4) == $distance) {
echo "<tr>";
echo '<td>' . $tables['men'][$i] . '</td>';
echo '<td>' . $tables['women'][$i] . '</td>';
echo "</tr>";
}
}
echo "</table>";
echo "</div>";
$i++;
}
echo "</div>";
echo "</div>";
?>
At the moment, all of the data is being displayed in every Tab, instead of only display the Links for the particular category in a different tab. I can see that the 2nd table of Men and Women is slightly to the left of the top one so I think the loop is causing something to go wrong.
I have tried re-arranging the way the loops display the data in the View but cannot seem to get only the 10KM in the 10KM Tab, 21KM in 21KM Tab, etc.
Get the data of your second foreach by Ajax it will made your need simple
I mentioned to remove below foreach
foreach($distances as $distance) {
echo "<div id='tabs-" . $i . "'>";
echo "<table class='table' cellspacing='10' cellpadding='10'>";
echo "<tr><th style='font-size: 20px;'>Men</th><th style='font-size: 20px;'>Women</th><th style='font-size: 20px;'></tr>";
foreach($tables['men'] as $table) {
if(substr($table, -4) == $distance) {
echo "<tr>";
echo '<td>' . $tables['men'][$i] . '</td>';
echo '<td>' . $tables['women'][$i] . '</td>';
echo "</tr>";
}
}
echo "</table>";
echo "</div>";
$i++;
}

While Loop For Table Rows From MySQL With Colour Coded Cells Based On Score

I have a MySQL table of results of collected responses on a form that I want to output in a table.
The user rates certain variables such as Sleep from good (1) to bad (7) and each users results are on each row.
I did that successfully, but to make it more readable I want to colour code the scores based on score.
E.g., if you score 2 or below the table cell should be green, and if you score 6 or above it is coloured red.
There are 5 different variables being rated so not sure if the method I am using would work or if something more suitable.
$sql = "SELECT * FROM Responses";
$result = mysqli_query($link, $sql);
while ($row = mysqli_fetch_array($result))
{
echo '<tr>';
if($row['Sleep'] <= 2)
{
echo "<td style='background-color: green;'>" . $row['Sleep'] . "</td>";
}
elseif ($row['Sleep'] >= 6)
{
echo "<td style='background-color: red;'>" . $row['Sleep'] . "</td>";
}
else
{
echo "<td>" . $row['Sleep'] . "</td>";
}
echo '</tr>';
}
No output
Maybe so
function getColor($number)
{
if ($numner <= 2)
return 'green';
else if ($numner > 2 && $var < 6)
return 'black';
else if ($number >= 6)
return 'red';
}
$sql = "SELECT * FROM Responses";
$result = mysqli_query($link, $sql);
while ($row = mysqli_fetch_array($result))
{
echo '<tr>';
echo '<td>' . $row["Name"] . '</td>';
echo '<td style="background-color: "' . $getColor($row["Sleep"]) . '">' . $row["Sleep"] . '</td>';
}
You just put your different variables in an array
function getColor($number)
{
if ($number <= 2)
return 'green';
else if ($number > 2 && $number < 6)
return 'none';
else if ($number >= 6)
return 'red';
}
echo "<table>";
while ($row = mysqli_fetch_array($result))
{
//replace var* with your variable
$variables = array("Sleep", "var2", "var3", "var4", "var5");
echo '<tr>';
echo '<td>' . $row["Name"] . '</td>';
$i=0;
while($i<5)
{
$score=$row[$variables[$i]];
echo '<td style="background-color: ' . getColor($score). ';">' . $score . '</td>';
$i++;
}
echo '</tr>';
}
echo "</table>";

Creating a html Table based on transposed Joomla data

I'm struggling to create a table made of the joomla database. Currently I'm using the following code:
$query= "SELECT DATUM, A , B ,C, D, E, F, G FROM ".$db->quoteName('#__XYZTABLE'). " WHERE DATUM_ENG>= CURDATE()";
$db->setQuery($query);
$results = $db -> loadObjectList();
echo "<table><tr><th>Name</th><th>Department</th></tr>";
foreach($results as $row){
echo "<tr>"; echo "<td>".$row->last_name."</td>";
echo "<td>".$row->dept."</td>";
echo "</tr>"; }
echo "</table>";
Basically, this code works fine. The result is:
DATUM , A , B ,C, D, E, F, G
xxxx1 ,aa1,bb1,cc1,dd1,ee1,ff1,gg1
xxxx2 ,aa2,bb2,cc2,dd2,ee2,ff2,gg2
....
However, I want to create an html table which transposes the data and shows it like the following form:
xxxx1 , xxxx2, xxxx3 ....
A aa1 , aa2 , aa3
B bb1 , bb2 ,bb3
C cc1 , cc2 ,cc3
D .....
After searching in this forum and Google, I wasn't able to find anything similar. Can someone help me with a specific code?
Using some php array functions, you may take this:
$object = new stdClass();
// create some test values
$object->DATUM = '2016120';
$object->A = 'aa';
$object->B = 'bb';
$object->C = 'cc';
$object->D = 'dd';
$object->E = 'ee';
$object->F = 'ff';
$object->G = 'gg';
for ($i = 1; $i < 10; $i++){
$vars = get_object_vars($object);
$obj = new stdClass;
foreach ($vars as $key=>$var){
$obj->$key = $var.$i;
}
$results[] = $obj;
}
// end of creating test values
echo "<table><tr><th>DATUM</th><th>A</th><th>B</th><th>C</th><th>D</th></tr>";
foreach ( $results as $row ) {
echo "<tr>";
echo "<td>" . $row->DATUM . "</td>";
echo "<td>" . $row->A . "</td>";
echo "<td>" . $row->B . "</td>";
echo "<td>" . $row->C . "</td>";
echo "<td>" . $row->D . "</td>";
echo "<td>" . $row->E . "</td>";
echo "<td>" . $row->F . "</td>";
echo "<td>" . $row->G . "</td>";
echo "</tr>";
}
echo "</table>";
echo '<h3>New Variant</h3>';
echo '<table>';
$elements = count($results);
foreach (get_object_vars($results[0]) as $key=>$var){
echo '<tr>';
$tag = ($key === 'DATUM') ? 'th>' : 'td>';
echo '<'.$tag . $key .'</'.$tag;
for ($i = 0; $i < $elements; $i++){
echo '<'.$tag . $results[$i]->$key.'</'.$tag;
}
echo '</tr>';
}
echo '</table>';`

PHP: Show data from mysql database in table

I am new to php and trying to fetch data from database and trying to show it in html table. My issue is total number of returned record is 13 but in table it is just showing 12 record( it is skipping first record in html table) my code snippt is as below
$num_rows = mysql_num_rows($result);
echo $num_rows;
if ($num_rows > 0) {
$i = 0;
$dyn_table = '<table class="gridData">';
$dyn_table.= '<th>Mil Purchy</th><th>Next Mil Purchy</th><th>total Missing</th><th>Missing From-To</th>';
for ($x = 0; $x <= $num_rows; $x++) {
while ($row = mysql_fetch_array($result)) {
if ($i % 1 == 0) {
// if $i is divisible by our target number (in this case "3")
echo "<script> alert('$i') </script>";
$dyn_table.= '<tr><td>' . $row[0] . '</td>';
$dyn_table.= '<td>' . $row[1] . '</td>';
$dyn_table.= '<td>' . $row[2] . '</td>';
$dyn_table.= '<td>' . $row[3] . '</td>';
} else {
echo "<script> alert ('in else statement') </script>";
}
$i++;
}
$dyn_table.= '</tr></table>';
}
echo $dyn_table;
}
and on submit button I have written following code
<fieldset>
<?php
if ($_GET) {
if (ISSET($_GET['submit'])) {
// echo "<script> alert('waiting for function') </script>";
$From_Date = $_GET['DateFrom'];
$To_Date = $_GET['DateTo'];
Sequece($From_Date, $To_Date);
}
}
?>
</fieldset>
before anything it is important that you use mysqli() functions instead of mysql() because mysql() function series are being departed from php. Also you missed some <tr> and </tr>s. so here is your fixed code:
<?php $num_rows = mysqli_num_rows($result);
echo $num_rows;
if($num_rows > 0){
$i = 0;
$dyn_table = '<table class="gridData">';
$dyn_table .= '<tr><th>Mil Purchy</th><th>Next Mil Purchy</th><th>total Missing</th><th>Missing From-To</th>';
while($row = mysqli_fetch_array($result))
{
if ($i % 1 == 0) { // if $i is divisible by our target number (in this case "3")
echo "<script> alert('$i') </script>";
$dyn_table .= '</tr><tr><td>' .$row[0] . '</td>';
$dyn_table .= '<td>' .$row[1]. '</td>';
$dyn_table .= '<td>' .$row[2]. '</td>';
$dyn_table .= '<td>' .$row[3]. '</td>';
} else {
echo "<script> alert ('in else statement') </script>";
}
$i++;
}
$dyn_table .= '</tr></table>';
echo $dyn_table;
}?>
Also you are creating many <script> inside the <table> structure code, it is not wrong and it works but it is bad HTML so I recommend that you store all <script>s in an array and after creation of table, print out the array. any by the way, why sending many alerts? why not only print the alerts only?
Database is returning correct records that is 12.
You made a mistake in the following line of code:-
for ( $x = 0; $x<= $num_rows; $x++){
Simply replace above code block with the following one:-
for ( $x = 0; $x < $num_rows; $x++){
Here you are trying to get ($num_rows + 1) = 13 records from the database that's why you are not getting data for last record that is 13.
You do not need a for loop. Your while loop will stop when there are no more records. That's why it's there.
Remove for loop and check it may be work.
$num_rows = mysql_num_rows($result);
echo $num_rows;
if ($num_rows > 0)
{
$i = 0;
$dyn_table = '<table class="gridData">';
$dyn_table.= '<th>Mil Purchy</th><th>Next Mil Purchy</th><th>total Missing</th><th>Missing From-To</th>';
while ($row = mysql_fetch_array($result)) {
if ($i % 1 == 0)
{
// if $i is divisible by our target number (in this case "3")
echo "<script> alert('$i') </script>";
$dyn_table.= '<tr><td>' . $row[0] . '</td>';
$dyn_table.= '<td>' . $row[1] . '</td>';
$dyn_table.= '<td>' . $row[2] . '</td>';
$dyn_table.= '<td>' . $row[3] . '</td>';
}
else
{
echo "<script> alert ('in else statement') </script>";
}
$i++;
}
$dyn_table.= '</tr></table>';
echo $dyn_table;
}
13 will not divisible by 3 so it only shows 12 results
<?php
$num_rows = mysql_num_rows($result);
echo $num_rows;
if ($num_rows > 0) {
$i = 0;
$dyn_table = '<table class="gridData">';
$dyn_table .= '<th>Mil Purchy</th>
<th>Next Mil Purchy</th>
<th>total Missing</th>
<th>Missing From-To</th>';
while ($row = mysql_fetch_array($result)) {
if ($i % 1 == 0) { // if $i is divisible by our target number (in this case "3")
echo "<script> alert('$i') </script>";
$dyn_table .= '<tr><td>' . $row[0] . '</td>';
$dyn_table .= '<td>' . $row[1] . '</td>';
$dyn_table .= '<td>' . $row[2] . '</td>';
$dyn_table .= '<td>' . $row[3] . '</td>';
} else {
echo "<script> alert ('in else statement') </script>";
}
$i++;
}
$dyn_table .= '</tr></table>';
echo $dyn_table;
}?>

PHP Splitting data into 2 columns into an html table not working

Hey guys I'm back and I feel like I'm making a really stupid error in my code but I can't seem to find it. What I am trying to do is split data retrieved from a mysql table into two columns inside an html table. So far I got it working...sort of. The problem I am having is the data keeps replicating to both rows in the table versus being an individual entry in each cell. I have used googled and searched these forms and can't seem to see where I am making the error. Below is the code
// Build the table
echo '
<table>
<tr>
';
// Build the headers
for ($x = '1'; $x <= '2'; $x++)
echo '
<td>Truck</td>
<td>Home Base</td>
<td>Client</td>
<td>Job Details</td>
<td>Crew</td>
';
// Close off headers
echo '</tr>';
// Fetch the table contents if the rigs are active
while($row = mysqli_fetch_array($truck_full_query)) {
for ($y = '1'; $y <= '1'; $y++):
echo '<tr>';
for ($z = '1'; $z <= '2'; $z++):
echo '<td>' . $row['model'] . ' #' . $row['position'] . '<br>' . $row['unit_number'] . '</td>';
echo '<td>' . $row['dispatch_location'] . '</td>';
// Get client information
echo '<td> </td>';
// Get Job Details
echo '<td> </td>';
// Get Crew
echo '<td> </td>';
endfor;
echo '</tr>';
endfor;
}
// Close table
echo '</table>';
// Build the table
echo '<table><tr>';
// Build the headers
echo '<td>Truck</td><td>Home Base</td><td>Client</td><td>Job Details</td><td>Crew</td>';
echo '<td>Truck</td><td>Home Base</td><td>Client</td><td>Job Details</td><td>Crew</td>';
echo '</tr>';
$data = mysqli_fetch_all($truck_full_query);
$i = 0;
$t = sizeof($data);
for (; $i < $t; $i += 2) {
echo '<tr>';
$left_row = $data[$i];
echo '<td>' . $left_row['model'] .'</td><td>...</td><td>...</td><td>...</td><td>...</td>';
$right_row = $data[$i + 1];
echo '<td>' . $right_row['model'] .'</td><td>...</td><td>...</td><td>...</td><td>...</td>';
echo '</tr>';
}
echo '</table>
And don't forget to check if the last $right_row exists.

Categories