Merging arrays to a single array to find minimum value - php

I have a csv file and I am trying to get the minimum value for a particular column. However I am not getting an exact value even after exploding the values. After I explode and I do a var_dump on it, it produces a different array for each different row; so each value in the column is the first element of a different array. How do I merge this different arrays into one array in order to find the minimum value in the column, or is there any other way to go about it? Below is my code. Any help will be appreciated.
merging
while (($data = fgetcsv($handle, 0, ",")) !== false) {
$table2Add =false;
if($row >=$x && $row <=$y) {
$table2Add=true; }
if($row == 1) {
$table2 .= '<thead><tr>';
$table2 .= '<th></th>';
$table2 .= '<th>Min</th>';
$table2 .= '<th>Max</th>';
$table2 .= '<th>Average</th>';
$table2 .= '</tr></thead><tbody>';
}
else {
if($table2Add) $table2 .= '<tr><th>Voltage</th>';
for($c = 0; $c <= 10; $c++) {
$value = empty($data[$c]) ? " " : $data[$c];
if($c==1) {
var_dump((explode('STRING',$data[$c])));
//$ar=explode('STRING', $data[$c]);
//var_dump(min($ar));
if ($table2Add) $table2 .='<td>'.round($value,2).'</td>';
}
if($c==2) {
if ($table2Add) $table2 .='<td>'.round($value,2).'</td>';
}
if($c==3) {
if ($table2Add) $table2 .='<td>'.round($value,2).'</td>';
}
}
if($table2Add) $table2 .= '</tr>';
if($table2Add) $table2 .= '<tr><th>Current</th>';
for($c = 0; $c <= 10; $c++) {
$value = empty($data[$c]) ? " " : $data[$c];
if($c==4) {
if ($table2Add) $table2 .='<td>'.round($value,2).'</td>';
}
if($c==5) {
if ($table2Add) $table2 .='<td>'.round($value,2).'</td>';
}
if($c==6) {
if ($table2Add) $table2 .='<td>'.round($value,2).'</td>';
}
}
if($table2Add) $table2 .= '</tr>';
if($table2Add) $table2 .= '<tr><th>Power Factor</th>';
for($c = 0; $c <= 10; $c++) {
$value = empty($data[$c]) ? " " : $data[$c];
if($c==7) {
if ($table2Add) $table2 .='<td>'.round($value,2).'</td>';
}
if($c==8) {
if ($table2Add) $table2 .='<td>'.round($value,2).'</td>';
}
if($c==9) {
if ($table2Add) $table2 .='<td>'.round($value,2).'</td>';
}
}
if($table2Add) $table2 .= '</tr>';
}
$row++;
}

Related

menu only showing the first child php

i have this code:
$Maxcount = 10;$count = 1;$query = "SELECT pages.paid,
pages.pag_desc,
pages.isActive,
pages.pag_pag_id,
pages.pag_type,
parent.pag_desc,";while ($count <= $Maxcount) {$query .= "parent".$count.".pag_desc AS parent".$count;if ($count != $Maxcount) {$query .= ", ";$count++;} $query .= " FROM pages ";$query .= "LEFT JOIN pages parent ON pages.pag_pag_id = parent.paid ";$count = 1;while ($count <= $Maxcount) { if ($count === 1) {
$query .= "LEFT JOIN pages parent".$count." ON parent.pag_pag_id = parent".$count.".paid "; }else {
$query .= "LEFT JOIN pages parent".$count." ON parent".($count-1).".pag_pag_id = parent".$count.".paid " }$count++;}$query .="LEFT JOIN tblgroups_rights_gri on pages.paid = tblgroups_rights_gri.gri_pag_id
INNER JOIN users_usergroups on tblgroups_rights_gri.gri_usrg_id = users_usergroups.gid
WHERE uid = '$uid' AND $where AND pages.isActive=1";
$result = $db->query($query);
if (mysqli_num_rows($result)) {
$menu = array();
while ($row = $result->fetch_assoc()) {
$menu[] = $row;
}
}
and for showing the menu i used this recursive function :
foreach($menu as $row){
$x[$row['parent3']][$row['parent2']][$row['parent1']][$row['pag_desc']]=$row['pag_desc'];
}function recursive($x, $out) { if (is_array($x)){
//$out .= "<ul>";
foreach($x as $i => $data) {
if ($i === '') {
$out = recursive($data, $out);
continue;
}
$out .= "<li>";
if (is_array($data)){
$out .= ' <a href="#" class="dropdown-toggle" data-toggle="dropdown">';
}
else
{
$out .= '<a href="#">';
}
$out .= $i;
if (is_array($data)){
$out .= '<b class="caret"></b></a>';
}
else
{
$out .= "</a>";
}
if (is_array($data)){
$out .= "<ul class='dropdown-menu'>";
$out = recursive($data, $out);
$out .= "</ul></li>";
}
else
{
$out .= "</li>";
}
}
}
return $out;
}
the result is that its showing only the parent and the first child but in database the child have another child etc...
for example:
parent user have a child admin and admin have another child called editor
but its only showing user as a parent and admin as a child
any help please? thank u in advance

Getting Mysql field from an array

I have the following script to display files inside a directory
<?PHP
# The current directory
$directory = dir("./");
# If you want to turn on Extension Filter, then uncomment this:
$allowed_ext = array(".deb", ".ext", ".ext", ".ext", ".ext", ".ext");
$do_link = TRUE;
$sort_what = 0; //0- by name; 1 - by size; 2 - by date
$sort_how = 0; //0 - ASCENDING; 1 - DESCENDING
# # #
function dir_list($dir){
$i=0;
$dl = array();
if ($hd = opendir($dir)) {
while ($sz = readdir($hd)) {
if (preg_match("/^\./",$sz)==0) $dl[] = $sz;$i.=1;
}
closedir($hd);
}
asort($dl);
return $dl;
}
if ($sort_how == 0) {
function compare0($x, $y) {
if ( $x[0] == $y[0] ) return 0;
else if ( $x[0] < $y[0] ) return -1;
else return 1;
}
function compare1($x, $y) {
if ( $x[1] == $y[1] ) return 0;
else if ( $x[1] < $y[1] ) return -1;
else return 1;
}
function compare2($x, $y) {
if ( $x[2] == $y[2] ) return 0;
else if ( $x[2] < $y[2] ) return -1;
else return 1;
}
}else{
function compare0($x, $y) {
if ( $x[0] == $y[0] ) return 0;
else if ( $x[0] < $y[0] ) return 1;
else return -1;
}
function compare1($x, $y) {
if ( $x[1] == $y[1] ) return 0;
else if ( $x[1] < $y[1] ) return 1;
else return -1;
}
function compare2($x, $y) {
if ( $x[2] == $y[2] ) return 0;
else if ( $x[2] < $y[2] ) return 1;
else return -1;
}
}
##################################################
# Getting The information
##################################################
$i = 0;
while($file=$directory->read()) {
$file = strtolower($file);
$ext = strrchr($file, '.');
if (isset($allowed_ext) && (!in_array($ext,$allowed_ext)))
{
// dump
}
else {
$temp_info = stat($file);
$new_array[$i][0] = $file;
$new_array[$i][1] = $temp_info[7];
$new_array[$i][2] = $temp_info[9];
$new_array[$i][3] = date("F d, Y", $new_array[$i][2]);
$i = $i + 1;
}
}
$directory->close();
##################################################
# Sorting the information
#################################################
switch ($sort_what) {
case 0:
usort($new_array, "compare0");
break;
case 1:
usort($new_array, "compare1");
break;
case 2:
usort($new_array, "compare2");
break;
}
###############################################################
# Displaying the information
###############################################################
$i2 = count($new_array);
$i = 0;
echo "<table class='CSSTableGenerator'>
<tr>
<td width=290>File name (Download)</td>
<td align=center width=70>Downloads</td>
<td align=center width=70>Depiction</td>
<td align=center width=50>Size</td>
<td align=center width=85>Modified</td>
</tr>";
for ($i=0;$i<$i2;$i++) {
if (!$do_link) {
$line = "<tr><td>" . $new_array[$i][0];
$line .= '</td><td>Depiction</td>';
$line .= "<td>" . number_format(($new_array[$i][1]/1024)) . " KB</td>";
$line .= "<td>" . $new_array[$i][3] . "</td></tr>";
}else{
$line = '<tr><td align=left ><A class="ex1" HREF="' .
$new_array[$i][0] . '">' .
$new_array[$i][0] .
"</A></td>";
$line .= '<td> </td>';
$line .= '<td>Depiction</td>';
$line .= "<td>" . number_format(($new_array[$i][1]/1024)) . " KB</td>";
$line .= "<td>" . $new_array[$i][3] . "</td></tr>";
}
echo $line;
}
echo "</table>";
?>
The output looks like that:
I am trying to fill the downloads column by getting the download counts of each file from mysql table
So i am facing 2 issues here:
How to get the stats using the array of the files $new_array[$i][0]
How can i then add this mysql query output inside $line .= '<td> </td>';
I have tried getting the stats from the table using this:
include("../config.php");
$query = "SELECT stats FROM download WHERE filename='$new_array[$i][0]'";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$line .= '<td>' . $row['stats'] . '</td>';
}
but it didn't work, i think the issue is with the array $new_array[$i][0] because i tried writing a filename filename='com.name.app3_2.2-1_iphoneos-arm.deb' and i got the stats of this file
In your "getting the information part", you must initialise your arrays thusly:
##################################################
# Getting The information
##################################################
$i = 0;
$new_array = array();
while($file=$directory->read()) {
$file = strtolower($file);
$ext = strrchr($file, '.');
if (isset($allowed_ext) && (!in_array($ext,$allowed_ext)))
{
// dump
}
else {
$new_array[$i] = array();
$temp_info = stat($file);
$new_array[$i][0] = $file;
$new_array[$i][1] = $temp_info[7];
$new_array[$i][2] = $temp_info[9];
$new_array[$i][3] = date("F d, Y", $new_array[$i][2]);
$i = $i + 1;
}
}
$directory->close();
##################################################
In addition, I have noted that your line:
$query = "SELECT stats FROM download WHERE filename='$new_array[$i][0]'";
Would cause $query to be equal to:
SELECT stats FROM download WHERE filename='Array[0]'
When $i is 0.
What you should do is use the mysqli library thusly:
$mysqli = new mysqli("example.com", "user", "password", "database");
$query = "SELECT stats FROM download WHERE filename=?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param('s', $new_array[$i][0]);
$stmt->execute();
$stmt->bind_result($stats);//stats turns into a reference by default.
while($stmt->fetch()) { //$stats now contains the stats
$line .= '<td>'. htmlentities($stats).'</td>';
}
To deal with the problem of missing cells you just described, you might try:
$mysqli = new mysqli("example.com", "user", "password", "database");
$query = "SELECT stats FROM download WHERE filename=?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param('s', $new_array[$i][0]);
$stmt->execute();
$stmt->bind_result($stats);//stats turns into a reference by default.
$fetched = false;
while($stmt->fetch()) { //$stats now contains the stats
$line .= '<td>'. htmlentities($stats).'</td>';
$fetched = true;
}
if(!$fetched) {
$line .= '<td></td>'
}

check column values in php not working

I want to take the print of students reports with following PHP code. (the 4th column is Marks). I am unable to modify the code. Can any body help me . And i need the reports where the marks is more than 75. in this regard?
<?php
$row = 1;
if(($handle = fopen("xyz.csv", "r")) !== false) {
$table1 = '<table div id="kk">';
while (($data = fgetcsv($handle, 1000, ",")) !== false) {
$table1Add = false;
if($row >=1 && $row <= 53)
$table1Add = true;
$num = count($data);
if($row == 1) {
$table1 .= '<thead><tr>';
for($c = 0; $c <= 8; $c++) {
$value = empty($data[$c]) ? " " : $data[$c];
$table1 .= '<th>'.$value.'</th>';
}
$table1 .= '</tr></thead><tbody>';
} else {
if($table1Add) $table1 .= '<tr>';
for($c = 0; $c <= 8; $c++) {
$value = empty($data[$c]) ? " " : $data[$c];
if($table1Add) $table1 .= '<td>'.$value.'</td>';
}
if($table1Add) $table1 .= '</tr>';
}
$row++;
}
$table1 .= '</tbody></table>';
fclose($handle);
echo $table1;
}
?>
Try:
<?php
$row = 1;
if(($handle = fopen("xyz.csv", "r")) !== false) {
$table1 = '<table div id="kk">';
while (($data = fgetcsv($handle, 1000, ",")) !== false) {
$table1Add = false;
if($row >=1 && $row <= 53)
$table1Add = true;
$num = count($data);
if($row == 1) {
$table1 .= '<thead><tr>';
for($c = 0; $c <= 8; $c++) {
$value = empty($data[$c]) ? " " : $data[$c];
$table1 .= '<th>'.$value.'</th>';
}
$table1 .= '</tr></thead><tbody>';
} else {
if($table1Add) $table1 .= '<tr>';
/* this bit edited */
$temp_holder = '';
$flag = false;
for($c = 0; $c <= 8; $c++) {
if($c==3 && $data[$c]>75) { $flag = true; }
if($table1Add){
$temp_holder .= '<td>'.(empty($data[$c]) ? " " : $data[$c]).'</td>';
}
if($c==8 && flag==true) { $table1 .= $temp_holder; }
/* till here */
}
if($table1Add) $table1 .= '</tr>';
}
$row++;
}
$table1 .= '</tbody></table>';
fclose($handle);
echo $table1;
}
?>
What's done above is, we will use a temp variable to keep all the <td> data, when you reach the 4th column (which is 3 starting from 0), check if the marks is greater than 75, if yes, set the flag to true. If the flag is true, that means that row has marks greater than 75, so append all the <td> values from the temp variable to the table1 variable.

Import specific rows and columns form csv file into table using php

I'm very new to php but have been experimenting fairly successfully with using 'fgetcsv' to bring in a CSV file and convert it into an html table.
However I also have a large CSV file with 70 columns and 700 rows, but I only want to display columns 1 to 47 and rows 3 to 21 in one table, and then same columns but rows 22 to 44 in another table.
I'd appreciate some help with this, below is the code I am currently using:
<?php
$row = 1;
if (($handle = fopen("CSV/test.csv", "r")) !== FALSE) {
echo '<table border="1">';
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
if ($row == 1) {
echo '<thead><tr>';
}else{
echo '<tr>';
}
for ($c=0; $c < $num; $c++) {
//echo $data[$c] . "<br />\n";
if(empty($data[$c])) {
$value = " ";
}else{
$value = $data[$c];
}
if ($row == 1) {
echo '<th>'.$value.'</th>';
}else{
echo '<td>'.$value.'</td>';
}
}
if ($row == 1) {
echo '</tr></thead><tbody>';
}else{
echo '</tr>';
}
$row++;
}
echo '</tbody></table>';
fclose($handle);
}
?>
<?php
$row = 1;
if(($handle = fopen("CSV/test.csv", "r")) !== false) {
$table1 = $table2 = '<table border="1">';
while (($data = fgetcsv($handle, 1000, ",")) !== false) {
$table1Add = $table2Add = false;
if($row >=3 && $row <= 21)
$table1Add = true;
if($row >=22 && $row <= 44)
$table2Add = true;
$num = count($data);
if($row == 1) {
$table1 .= '<thead><tr>';
$table2 .= '<thead><tr>';
for($c = 1; $c <= 47; $c++) {
$value = empty($data[$c]) ? " " : $data[$c];
$table1 .= '<th>'.$value.'</th>';
$table2 .= '<th>'.$value.'</th>';
}
$table1 .= '</tr></thead><tbody>';
$table2 .= '</tr></thead><tbody>';
} else {
if($table1Add) $table1 .= '<tr>';
if($table2Add) $table2 .= '<tr>';
for($c = 1; $c <= 47; $c++) {
$value = empty($data[$c]) ? " " : $data[$c];
if($table1Add) $table1 .= '<td>'.$value.'</td>';
if($table2Add) $table2 .= '<td>'.$value.'</td>';
}
if($table1Add) $table1 .= '</tr>';
if($table2Add) $table2 .= '</tr>';
}
$row++;
}
$table1 .= '</tbody></table>';
$table2 .= '</tbody></table>';
fclose($handle);
echo $table1;
echo $table2;
}
?>
for the columns try this
for ($c=0; $c < 47; $c++) {
//echo $data[$c] . "<br />\n";
if(empty($data[$c])) {
$value = " ";
}else{
$value = $data[$c];
}
if ($row == 1) {
echo '<th>'.$value.'</th>';
}else{
echo '<td>'.$value.'</td>';
}
}
So you print cells between column 0 and column 47.
For the row you have to set a a counter, and, inside the while statement, use an if to print the rows in the range that you need. It's better that you set an exit condition in while to exit after row 44 if you dont need the others.

Convert 3D array into a HTML table

ARRAY
$array['1'] = null;
$array['2']['21'] = null;
$array['3']['31'] = null;
$array['3']['32'] = null;
$array['4']['41'] = null;
$array['4']['42']['421'] = null;
$array['4']['42']['422'] = null;
$array['5']['51'] = null;
$array['5']['52'] = null;
$array['5']['53']['531'] = null;
$array['5']['53']['532'] = null;
$array['5']['53']['533'] = null;
$array['6']['61']['611'] = null;
$array['6']['62'] = null;
$array['6']['63']['631'] = null;
$array['6']['63']['632'] = null;
$array['6']['63']['633'] = null;
$array['6']['63']['634'] = null;
$array['7']['71']['711'] = null;
$array['8']['81']['811'] = null;
$array['8']['81']['812'] = null;
$array['9']['91']['911'] = null;
$array['9']['91']['912'] = null;
$array['9']['92']['921'] = null;
$array['9']['92']['922'] = null;
I'm trying to convert array above into a HTML table. I need to add rowspan to get output like shown as image below but run out of juice.
PHP
//If condition is satistied
if (isset($array) && count($array) > 0)
{
//echo '<pre>'; print_r($array); exit;
//Initiate variables
$row = null;
$level_one_array = $array;
//Iterate through menu level one
foreach ($level_one_array as $level_one_name => $level_two_array)
{
//If level two is an array
if (is_array($level_two_array))
{
//Iterate through menu level two
foreach ($level_two_array as $level_two_name => $level_three_array)
{
//If level two is an array
if (is_array($level_three_array))
{
//Iterate through menu level three
foreach ($level_three_array as $level_three_name => $null)
{
$row .= '<tr>';
$row .= '<td class="td_data">' . $level_one_name . '</td>';
$row .= '<td class="td_data">' . $level_two_name . '</td>';
$row .= '<td class="td_data">' . $level_three_name . '</td>';
$row .= '</tr>';
}
}
//If level two is not an array
else
{
$row .= '<tr>';
$row .= '<td class="td_data">' . $level_one_name . '</td>';
$row .= '<td class="td_data">' . $level_two_name . '</td>';
$row .= '<td class="td_data"> </td>';
$row .= '</tr>';
}
}
}
//If level two is not an array
else
{
$row .= '<tr>';
$row .= '<td class="td_data">' . $level_one_name . '</td>';
$row .= '<td class="td_data"> </td>';
$row .= '<td class="td_data"> </td>';
$row .= '</tr>';
}
}
$table = '<table>';
$table .= '<tr><td class="td_title">Menus</td><td class="td_title">Submenus/Items</td><td class="td_title">Items</td></tr>';
$table .= $row;
$table .= '</table>';
echo $table;
}
else
{
echo '<center>No record found</center>';
}
WHAT I NEED IS THIS:
I wrote new code that will do the trick. Working with rowspan is very tricky. The key elements are calculate_rowspan() and the flag $on_level_X_row to determine whether or not it is necessary to add <tr>.
It was necessary to detach the output from the most inner foreach because you have to handle multiple rows with it. Also I added "\n" after each </tr> to make the output a little more human readable.
function calculate_rowspan($array)
{
if (!is_array($array) || count($array) == 0)
return 1;
$rowspan = 0;
foreach ($array as $key=>$value)
{
$rowspan++;
if (is_array($value) && count($value) > 0)
{
$rowspan += count($value);
// -1 because first element is one same row
$rowspan--;
}
}
return $rowspan;
}
if (is_array($array) && count($array) > 0)
{
$output = '';
foreach ($array as $level_one_name => $level_two_array)
{
if (!is_array($level_two_array) || count($level_two_array) == 0)
{
$output .= '<tr><td>';
$output .= $level_one_name;
$output .= '</td><td></td><td></td></tr>'."\n";
}
else
{
$output .= '<tr>';
$output .= '<td rowspan="'.calculate_rowspan($level_two_array).'">';
$output .= $level_one_name;
$output .= '</td>';
$on_level_one_row = TRUE;
foreach ($level_two_array as $level_two_name => $level_three_array)
{
if ( ! $on_level_one_row)
$output .= '<tr>';
else
$on_level_one_row = FALSE;
if (!is_array($level_three_array) || count($level_three_array) == 0)
{
$output .= '<td>';
$output .= $level_two_name;
$output .= '</td><td></td></tr>'."\n";
}
else
{
$output .= '<td rowspan="'.calculate_rowspan($level_three_array).'">';
$output .= $level_two_name;
$output .= '</td>';
$on_level_two_row = TRUE;
foreach ($level_three_array as $level_three_name => $null)
{
if ( ! $on_level_two_row)
$output .= '<tr>';
else
$on_level_two_row = FALSE;
$output .= '<td>';
$output .= $level_three_name;
$output .= '</td></tr>'."\n";
}
}
}
}
}
print '<table>'."\n";
print $output;
print '</table>';
}

Categories