I have a MySQL database for a website am building, i have displayed contents of a table in the MySQL database on my page using the PHP code shown below. What i wanted to know was if there was a way to Merge rows of a column with the same value.
note: i don't want the changes to occur in the database but just on the html table displayed on the page
here is the code i used
$arr = array();
while($row = mysql_fetch_row($result)) {
if (empty($arr[$row[0]])) $arr[$row[0]] = array();
$arr[$row[0]] [$row[1]] [$row[2]] [$row[3]] [$row[4]] [$row[5]] [$row[6]][] = $row[7];
}
foreach ($arr as $key => $subordinatearr) {
echo '<tr>';
echo '<td rowspan="', count($subordinatearr), '">', $key, '</td>';
echo '<td>', $subordinatearr[0], '</td>';
echo '<td>', $subordinatearr[1], '</td>';
echo '<td>', $subordinatearr[2], '</td>';
echo '<td>', $subordinatearr[3], '</td>';
echo '<td>', $subordinatearr[4], '</td>';
echo '<td>', $subordinatearr[5], '</td>';
echo '<td>', $subordinatearr[6], '</td>';
if (count($subordinatearr) > 1) {
for ($i = 1; $i < count($subordinatearr); $i++) {
echo '</tr><tr><td>', $subordinatearr[$i], '</td>';
}
}
echo '</tr>';
}
Please change this script to
while($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td>".$row['Location']."</td>";
echo "<td>".$row['Vessel ']."</td>";
echo "</tr>";
}
Store the result in an array.
$arr = array();
while($row = mysql_fetch_row($result)) {
if (empty($arr[$row[0]]) $arr[$row[0]] = array();
$arr[$row[0]][] = $row[1];
}
Then put the array using rowspan like as follows:
foreach ($arr as $key => $subordinatearr) {
echo '<tr>';
echo '<td rowspan="', count($subordinatearr), '">', $key, '</td>';
echo '<td>', $subordinatearr[0], '</td>';
if (count($subordinatearr) > 1) {
for ($i = 1; $i < count($subordinatearr); $i++) {
echo '</tr><tr><td>', $subordinatearr[$i], '</td>';
}
}
echo '</tr>';
}
If data is too big, this may cause an memory error.
I did not understand this part clearly - "Merge rows of a column with the same value."
If you don't want to show duplicate records of a column, you can do it in two ways -
Option 1
Keep the value of specific column in an array. Before displaying that column check if the value exists in the array using is_array(). If exists, then skip, otherwise show and add into the array.
Option 2
While making the query, use DISTINCT(col_name) to get only the unique values.
Related
I have one array that I am showing like this:
echo '<table>';
foreach ($rowData as $row => $tr) {
echo '<tr>';
foreach ($tr as $td)
echo '<td>' . $td .'</td>';
echo '</tr>';
}
echo '</table>';
The second and fourth columns are always the name of the column. The result is something like:
Name: John Locke - NickName: Locke
Age: 20 - Adress: Ok
See the pattern?
How can I put these array in my database?
As my database table structure is:
ID - Name - NickName - Age - Adress
I don't have a clue how to do that with the array i'm using..
--UPDATE
$table = $html->find('table', 0);
$rowData = array();
foreach($table->find('tr') as $row) {
// initialize array to store the cell data from each row
$flight = array();
foreach($row->find('td') as $cell) {
foreach($cell->find('span') as $e){
$e->innertext = '';
}
// push the cell's text to the array
$flight[] = $cell->plaintext;
}
$rowData[] = $flight;
}
echo '<table>';
foreach ($rowData as $row => $tr) {
echo '<tr>';
echo '<td>' . $row . '</td>';
foreach ($tr as $td)
echo '<td>' . $td .'</td>';
echo '</tr>';
}
echo '</table>';
you can do that:
$insert = "";
foreach ($rowData as $row => $tr) {
$insert .= "('".$tr[0]."','".$tr[0]."','".$tr[0]."','".$tr[0]."'),";
}
$insert = substr($insert, 0, -1)
$sql = "Insert into YourTable values ".$insert;
As you already know what the array positions relate to, IE positions 0 through 3. You can simply iterate as you are doing now and compile a query instead.
Just drop this snippet into your code, ive knocked this up on the fly and its a bit hackish, probably a better way. Just look at what it does on your screen and see where if any it needs correcting.
$insert = '';
for ($i=0; $i<=3; $i++):
$insert .= "'" . $tr[$i] . "'";
if ($i !== 3):
$insert .= ',';
endif;
endfor;
echo $insert;
Once you are seeing what looks like a correct insert then you can (using safe methods) insert it into your database.
the roblem is where there is the punch commentator
i need when the column has INTER name or OUTER name to format the content of the $cell
with $cell=number_format($cell,2,',','.')
i'm just starting using php so don be too specific thanks
<?php
// printing table rows
$rigapadi = 1 ;
while($row = mysql_fetch_row($result))
{
echo "<tr>";
$rigapadi=$rigapadi+1;
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
if ($rigapadi % 2 == 0) {
# if column name = 'INTER' or 'OUTER' $cell = number_format($cell, 2, ',', '.');
echo "<td align=\"center\">$cell</td>";
} else {
echo "<td bgcolor=\"#E9EEF5\" align=\"center\">$cell</td>";
}
echo "</tr>\n";
}
mysql_free_result($result);
echo "</table>";
echo "<p/>";
?>
The mysql_ are obsoltes. You must not use them. And please indent your code properly when you ask for help.
<?php
$rigapadi = 1;
while($row = mysql_fetch_assoc($result)) {
echo '<tr>';
$rigapadi++;
foreach($row as $name => $cell) {
if($rigapadi % 2 === 0) {
if($name === 'INTER' || $name === 'OUTER') {
echo '<td align="center">' . number_format($cell, 2, ',', '.') . '</td>';
} else {
echo '<td align="center">' . $cell . '</td>';
}
} else {
echo '<td align="center" bgcolor="#E9EEF5">' . $cell . '</td>';
}
}
echo "</tr>\n";
}
mysql_free_result($result);
echo "</table>";
?>
Note that I use fetch_assoc to get the $name.
PS: <p/> does not exists and align and bgcolor and not valids but acceptable if this is a HTML code for an e-mail.
I have an Array of Arrays and Want to create a Tabular data layout. This is not the exact code, as how their generated is quite the cluster (Coming from COBOL interaction) but this should give me enough to make it work if someone can think of a clean way to code this.
array(
Array(847.0, 97010, 11)
Array(847.0, 97012, 10)
Array(847.1, 97010, 08)
Array(847.1, 97012, 14)
)
So I want to put these into a Table that looks something like
97010 97012
847.0 11 10
847.1 08 14
The first 2 elements of the arrays will always be the two axis and the 3rd the contents of the table.
Any Suggestions? thanks!
$table = array();
$columns = array();
// copy the array (x, y, value) into a table
// keeping track of the unique column names as we go
foreach ($dataSet as $point) {
// provided sample data used floats, ensure it is a string
$x = strval($point[0]);
$y = strval($point[1]);
$data = $point[2];
if (!isset($table[$x])) {
$table[$x] = array();
}
$table[$x][$y] = $data;
// quick and dirty style 'unique on insert'
$columns[$y] = true;
}
// switch the column names from title => true to just titles
$columns = array_flip($columns);
// Display the table
echo '<table>';
// Header row
echo '<tr>';
echo '<th> </th>';
foreach ($columns as $columnTitle) {
echo '<th>' . $columnTitle . '</th>';
}
echo '</tr>';
// Bulk of the table
foreach ($table as $rowTitle => $row) {
echo '<tr>';
echo '<th>' . $rowTitle . '</th>';
foreach ($columns as $columnTitle => $junk) {
if (isset($row[$columnTitle]) {
echo '<td>' . $row[$columnTitle] . '</td>';
} else {
// Handle sparse tables gracefully.
echo '<td> </td>';
}
}
echo '</tr>';
}
echo '</table>';
From what I understood:
$arr[847.0][97010] = '11';
$arr[847.0][97012] = '10';
$arr[847.1][97010] = '08';
$arr[847.1][97012] = '14';
And you may create a table:
$result = "<table>";
foreach($arr as $row_key => $row) {
$result .= "<tr>";
foreach($row as $column_key => $data) {
$result .= "<td>".$data."</td>";
}
$result .= "</tr>";
}
$result .= "</table>";
echo $result;
The table below prints of the results of a query called $sqlStr3. It works fine. The query is set to return the top 25 results.
I would like to apply a unique CSS class (called "class1" for purposes of this question) to the top ten results in the query. How can I do this?
Thanks in advance,
John
$result = mysql_query($sqlStr3);
$count = 1;
$arr = array();
echo "<table class=\"samplesrec1edit\">";
while ($row = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td class="sitename1edit2a">'.$count++.'.</td>';
echo '<td class="sitename1edit1">'.stripslashes($row["username"]).'</td>';
echo '<td class="sitename1edit2">'.number_format(($row["totalScore2"])).'</td>';
echo '</tr>';
}
echo "</table>";
use this:
$result = mysql_query($sqlStr3);
$count = 1;
$arr = array();
echo "<table class=\"samplesrec1edit\">";
while ($row = mysql_fetch_array($result)) {
if($count < 11){
echo '<tr class="top-10">';
}else{
echo '<tr class="non-top-10">';
}
echo '<td class="sitename1edit2a">'.$count++.'.</td>';
echo '<td class="sitename1edit1">'.stripslashes($row["username"]).'</td>';
echo '<td class="sitename1edit2">'.number_format(($row["totalScore2"])).'</td>';
echo '</tr>';
}
echo "</table>";
access the td of top10 using this css
.top-10 td{
//definitions
}
and access the td of non top10 lines with
.non-top-10 td{
//definitions
}
... class="...<?php if ($count <= 10) { ?> class1<?php } ?>" ...
And move the increment to the end of the loop.
you can do it in the server side via php like:
echo '<tr '.($count <=10 ? 'myclass' : '').' > ;
or in jquery in front side like:
$("someTableSelector").find("tr:lt(10)").addClass('myclass')
How about assigning each result from TOP10 a unique class called top_result_[n]?
while ($row = mysql_fetch_array($result)) {
echo '<tr'.($count++ <=10 ? ' class="top_result_'.$count.'"' : '').'>';
echo '<td class="sitename1edit2a">'.$count.'.</td>';
echo '<td class="sitename1edit1">'.stripslashes($row["username"]).'</td>';
echo '<td class="sitename1edit2">'.number_format(($row["totalScore2"])).'</td>';
echo '</tr>';
}
echo "</table>";
I'm not sure wether you want to apply ONE class for all 10 results or UNIQUE class for each from 10 results, as for the first case, the code would be:
while ($row = mysql_fetch_array($result)) {
echo '<tr'.($count++ <=10 ? ' class="top_result"' : '').'>';
echo '<td class="sitename1edit2a">'.$count.'.</td>';
echo '<td class="sitename1edit1">'.stripslashes($row["username"]).'</td>';
echo '<td class="sitename1edit2">'.number_format(($row["totalScore2"])).'</td>';
echo '</tr>';
}
echo "</table>";
Use CSS:
.samplesrec1edit tr:nth-child(-n+10) {
cssrule:value;
}
This technique only works in newer browsers however. The following link has compatibility charts.
http://reference.sitepoint.com/css/pseudoclass-nthchild
I need help figuring out these php print (echo) statements and where to place them. I have an embedded function 'strotime' that is transforming time (column 'StartTime') to a format, but I cannot get it to print out correctly. No errors, just no changes or use of the function.
Can someone help me figure out where to place this properly in this foreach loop?
(as you can see, i placed at beginning and tried an if statment too..but no luck). Thanks for
your help.
$keys = array('Server', 'Target','Logdate','Set','StartTime', 'Length','Size','Status');
echo '<table><tr>';
foreach ($keys as $column)
echo '<th>' . $column . '</th>';
echo '</tr>';
foreach ($data as $row){
echo '<tr>';
foreach ($keys as $column)
//if ($column == 'StartTime') {
// echo '<td>' . date("Y-m-d H:i:s",strtotime($row[$column])) . '</td>';
if (isset($row[$column])){
echo '<td>' . $row[$column] . '</td>';
} elseif ($column =='StartTime') {
echo '<td>' . date("Y-m-d H:i:s",strtotime($row[$column])) . '</td>';
} elseif ($column == 'Status') {
echo '<td> Check for Errors </td>';
} else {
echo '<td> </td>';
}
//}
}
echo '</table>';
In the beginning if foreach ($data as $row){ loop, do this:
$row['StartTime'] = date("Y-m-d H:i:s",strtotime($row['StartTime']));
And then display it like any other column.
Change
if (isset($row[$column])) {
to
if (isset($row[$column]) && $column != "StartTime") {
and by the way: you're missing the </tr> tags.