I am learning php and in doing it I had a problem.
I have to insert in a table of the payment data taken from a second table but only after checking that the id of the person corresponds to the id of the person who made the payment.
<?php
3 Query ...
while($rowSocio = mysqli_fetch_array($mostraSocio)) {
$idSocio = $rowSocio['socio_id'];
$nomeSocio = $rowSocio['socio_nome'];
$cognomeSocio = $rowSocio['socio_cognome'];
echo "<tr>";
echo "<td>{$cognomeSocio}</td>";
echo "<td>{$nomeSocio}</td>";
$i = 0;
$print = "0";
while($row = mysqli_fetch_array($mostraOperazione)) {
$id = $row['socio_id'];
$nome = $row['socio_nome'];
$cognome = $row['socio_cognome'];
$importo = $row['opfin_importo'];
$quota = $row['quota_nome'];
if($idSocio === $id) {
if($quota === "Prima Rata" && $print !== "1") {
echo "<td class='align-rx'>€ {$importo}</td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
$i++;
$print = "1";
} elseif($quota === "Seconda Rata" && $print !== "2") {
echo "<td></td>";
echo "<td class='align-rx'>€ {$importo}</td>";
echo "<td></td>";
echo "<td></td>";
$i++;
$print = "2";
} elseif($quota === "Terza Rata" && $print !== "3") {
echo "<td></td>";
echo "<td></td>";
echo "<td class='align-rx'>€ {$importo}</td>";
echo "<td></td>";
$i++;
$print = "3";
} elseif($quota === "Quota Stagionale" && $print !== "4") {
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
$i++;
$print = "4";
echo "<td class='align-rx'>€ {$importo}</td>";
} else {
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "</tr>";
$i++;
}
} else {
echo "</tr>";
$i++;
}
}
}
?>
so I only print the names of the people and not the payments they make
output:
enter image description here
Related
I am new to PHP, iam trying to color the cells of column4,column7,column9 based on condition which you can see in the IF block,below is my code what i tried,kindly help me to understand how to achieve this. i am using array because i have got more than 80 columns,in below example iam showing only 10 for explaining.
<?php
$con=mysqli_connect("server","user","password","db");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM table1");
echo "<table id='table_id' class='mytable'>
<thead>
<tr>
<th class='heading'>Column1</th>
<th class='heading'>Column2</th>
<th class='heading'>Column3</th>
<th class='heading'>Column4</th>
<th class='heading'>Column5</th>
<th class='heading'>Column6</th>
<th class='heading'>Column7</th>
<th class='heading'>Column8</th>
<th class='heading'>Column9</th>
<th class='heading'>Column10</th>
</tr>
</thead>";
echo "<tbody>";
while ($row = mysqli_fetch_array($result))
{
$colorfields = array($row['Column4'],$row['Column7'],$row['Column9']);
if ($colorfields < 195 && $colorfields !='')
$classname = "red";
else if($colorfields >= 195 && $colorfields < 199.99)
$classname = "yellow";
else if($colorfields >= 199.99)
$classname = "green";
echo "<tr>";
echo "<td class='normal_cell'>" . $row['Column1']."</td>";
echo "<td class='normal_cell'>" . $row['Column2']."</td>";
echo "<td class='normal_cell'>" . $row['Column3']."</td>";
echo "<td class=".$classname.">". $row['Column4']."</td>";
echo "<td class='normal_cell'>" . $row['Column5']."</td>";
echo "<td class='normal_cell'>" . $row['Column6']."</td>";
echo "<td class=".$classname.">". $row['Column7']."</td>";
echo "<td class='normal_cell'>" . $row['Column8']."</td>";
echo "<td class=".$classname.">". $row['Column9']."</td>";
echo "<td class='normal_cell'>" . $row['Column10']."</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
mysqli_close($con);
?>
I'd write a function that returns the 'red', 'yellow' or 'green', depending the value that is passed in as an argument.
function class_from_val($val) {
$classname = '';
if( $val < 195 && $val !='' ) {
$classname = 'red';
} else if( $val >= 195 && $val < 199.99 ) {
$classname = 'yellow';
} else if( $val >= 199.99 ) {
$classname = 'green';
} else {
// ? val = ""
}
return $classname;
}
Then I'd call the function where I need the classname returned
echo "<td class='normal_cell'>" .$row['Column3']."</td>";
echo "<td class='".class_from_val($row['Column4'])."'>".$row['Column4']."</td>";
echo "<td class='normal_cell'>" .$row['Column5']."</td>";
echo "<td class='normal_cell'>" .$row['Column6']."</td>";
echo "<td class='".class_from_val($row['Column7'])."'>".$row['Column7']."</td>";
Follow these general principles:
indent the code and space it
choose carefully your variable names
divide and rule (use functions)
example:
function getClassName($col, $field) {
if ( !in_array($col, [ 'Column4', 'Column7', 'Column9' ]) || empty($field) )
return 'normal_cell';
if ( $field >= 199.99 )
return 'green';
if ( $field >= 195 )
return 'yellow';
return 'red';
}
$cellFormat = '<td class="%s">%s</td>';
while ($row = mysqli_fetch_array($result)) {
echo '<tr>';
foreach ($row as $col => $field) {
printf($cellFormat, getClassName($col, $field), $field);
}
echo '</tr>';
}
echo "<table>";
for ($x=0; $x<=count
($arr['chart_data']); $x++) {
echo "<tr>\n";
foreach($arr['chart_data'][$x] as $key=>$val)
{
echo "<td align='center;' style='color:white;'>". $val . "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
I want to show only the first 4 fields in the table. I am decoding a multidimensional array in php. I
Try like
$cnt = 0;
foreach($arr['chart_data'][$x] as $key=>$val) {
if($cnt++ < 4) {
echo "<td align='center;' style='color:white;'>". $val . "</td>\n";
} else {
break;
}
}
echo "<table title='mxit:table:full' style='width: 100%' width='100%'><colgroup span='2' width='50%'></colgroup>";
foreach($arr['chart_data'] as $key => $element){
echo "<tr>";
foreach($element as $subkey => $subelement){
// $subelement =chop($subelement,'DIRECTSegment');
if($subkey++ < 2) {
if($key == 0)
{
echo "<td align='center;' style='color:white;'>$subelement</td>";
}
else if($subkey == 1)
{
echo "<td align='center;' style='color:white;'>$subelement</td>";
}
else
{
echo "<td align='center;' style='color:white;'><a href='getdata.php?key=$key'>".$subelement."</a></td>";
}
}
}
}
echo "</tr>";
echo "</table>";
How do i take only 10 elements of my array $arr['chart_data'] at a time ?
Set a counter, and then break the loop when $count hits 10.
$count = 0;
/* loop here */
if ($count == 10) break;
Anyway for all those who gave me negative votes and for my own betterment i solved my issue:
Here is what i did:
$totalfiles = count($arr['chart_data']);
$limit = 10;
$pages = ceil($totalfiles / $limit);
if (!isset($_GET['startrow']) or!is_numeric($_GET['startrow'])) {
//we give the value of the starting row to 0 because nothing was found in URL
$startrow = 0;
//otherwise we take the value from the URL
} else {
$startrow = (int) $_GET['startrow'];
}
echo "<table title='mxit:table:full' style='width: 100%' width='100%'><colgroup span='2' width='50%'></colgroup>";
$data = array_slice($arr['chart_data'], $startrow, 10); // same as offset 0 limit 50 in sql
foreach($data as $key => $element) {
echo "<tr>";
foreach($element as $subkey => $subelement) {
$subelement = chop($subelement, 'DIRECTSegment');
if ($subkey++ < 2) {
if ($key == 0 && $startrow == 0) {
echo "<td align='center;' style='color:white;'>$subelement</td>";
} else if ($subkey == 1) {
echo "<td align='center;' style='color:white;'>$subelement</td>";
} else {
echo "<td align='center;' style='color:white;'><a href='getdata.php?key=$key'>".$subelement.
"</a></td>";
}
}
}
echo "</tr>";
}
echo "</table>";
/* free result set */
$result - > close();
}
/* close connection */
$mysqli - > close();
//now this is the link..
echo '<a href="'.$_SERVER['PHP_SELF'].
'?startrow='.($startrow + 10).
'">Next</a>';
$prev = $startrow - 10;
//only print a "Previous" link if a "Next" was clicked
if ($prev >= 0)
echo ' <a href="'.$_SERVER['PHP_SELF'].
'?startrow='.$prev.
'">Previous</a>';
Implemented paging and using array slice to display 10 elements at a time.
Thanks for all the help
i got this script. it looks into a file and read all the lines and put it nicely in a table.. what i need to do now is to take all the table data and split it into two tables ..
eg. if their is 100 rows.. then instead of one long list, i will get 50 data in one table and the other 50 in the other table...
enter code here<?php
if(isset ($_GET['type']))
{
$otype = $_GET['type'];
}
else
{
$otype = 'm';
}
$statusFile = "d:\\CLIENTS\\status.txt";
$file_handler = fopen($statusFile, "r");
// read the contents
$contents = fread($file_handler, filesize($statusFile));
// close the file
fclose($file_handler);
if (strcasecmp($otype, "m") == 0)
{
echo $contents;
}
else
{
$lines = explode("\n",$contents);
$frow = explode(",", $lines[0]);
if (strcmp($frow[1],"1") == 0)
{
echo "Update Status: <b>Complete</b>";
//to count total lines in txt file
$statusFile = "d:\\CLIENTS\\status.txt";
$line = count(file($statusFile));
echo "There are".$line."lines in";
}
else
{
echo "Update Status: <b>Incomplete</b>";
}
echo "<table border=\"1\">";
for ($count = 1; $count < sizeof($lines); $count++)
{
$fields = explode(",",$lines[$count]);
$sz = sizeof($fields);
if ($sz > 1)
{
$str = "OK";
echo "<tr>";
echo "<td>" . $fields[0] . "</td>";
echo "<td>" . $fields[1] . "</td>";
//echo "<td>" . $fields[2] . "</td>";
if (strpos($fields[2],'OK') !== false)
{
echo "<td><font color='green'>". $fields[2] ."</font></td>";
//echo "<td style='background-color: #00FF00;'>". $fields[2] ."</td>";
}
else
{
//echo "<td><font color='red'>". $fields[2] ."</font></td>";
echo "<td style='background-color: #FF0000;'>". $fields[2] ."</td>";
}
echo "</tr>";
}
}
echo "</table>";
}
?>
Use a foreach instead of a for and $count to calculate if it is needed to echo a table tag. $count is only incremented when a row was inserted.
<style>
div.container {
width: 100%; /* Change the width to the desired width */
padding: 0;
}
div.container table {
width: 50%; /* Change the width to half of the width of the div */
margin: 0;
float: left;
}
</style>
<?php
if(isset ($_GET['type']))
{
$otype = $_GET['type'];
}
else
{
$otype = 'm';
}
$statusFile = "d:\\CLIENTS\\status.txt";
$file_handler = fopen($statusFile, "r");
// read the contents
$contents = fread($file_handler, filesize($statusFile));
// close the file
fclose($file_handler);
if (strcasecmp($otype, "m") == 0)
{
echo $contents;
}
else
{
$lines = explode("\n",$contents);
$frow = explode(",", $lines[0]);
if (strcmp($frow[1],"1") == 0)
{
echo "Update Status: <b>Complete</b>";
//to count total lines in txt file
$statusFile = "d:\\CLIENTS\\status.txt";
$line = count(file($statusFile));
echo "There are".$line."lines in";
}
else
{
echo "Update Status: <b>Incomplete</b>";
}
echo '<div class="conatiner">';
$count = 0;
foreach ($lines as $currLine)
{
$fields = explode(",",$currLine);
$sz = sizeof($fields);
if ($sz > 1)
{
if ($count == 0) {
echo "<table border=\"1\">";
}
elseif ($count % 50 == 0) {
echo "</table>";
echo "<table border=\"1\">";
}
$str = "OK";
echo "<tr>";
echo "<td>" . $fields[0] . "</td>";
echo "<td>" . $fields[1] . "</td>";
//echo "<td>" . $fields[2] . "</td>";
if (strpos($fields[2],'OK') !== false)
{
echo "<td><font color='green'>". $fields[2] ."</font></td>";
//echo "<td style='background-color: #00FF00;'>". $fields[2] ."</td>";
}
else
{
//echo "<td><font color='red'>". $fields[2] ."</font></td>";
echo "<td style='background-color: #FF0000;'>". $fields[2] ."</td>";
}
echo "</tr>";
$count++;
}
}
echo "</table>";
echo '</div>';
}
Hello i'm having problems adding numbered rows/serial numbers to my database query result. I've used $number to collect the actual number of rows.
Then another problem i'm trying to avoid is: Numbering of Column Headers.
Thanks for the help.
<?php
$number = mysql_num_rows($query);
for ($serial = 0; $serial < $number; $serial++)
{
echo "<tr>". $serial ."</tr>";
}
for ($i = 0; $i < $number_cols; $i++)
{
echo "<th>" . mysql_field_name($query, $i) . "</th>\n";
}
while ($row = mysql_fetch_row($query))
{
echo "<tr align=center>\n";
for ($i = 0; $i < $number_cols; $i++)
{
echo "<td>";
if (!isset($row[$i]))
{
echo "NULL";
}
else
{
echo $row[$i];
}
echo "</td>\n";
}
echo "</tr>\n";
}
echo "</table>";
echo "</span>";
echo "</div>";
?>
trying my best to get something sane out of your terrific code.
<?php
$serial = 1;
while ($row = mysql_fetch_row($query))
{
echo "<tr align=center>\n";
echo "<td>";
echo $serial++;
echo "</td>\n";
foreach ($row as $value)
{
echo "<td>$value</td>\n";
}
echo "</tr>\n";
}