I can't seem to add the title Bonus % to the 5th column, what am I doing wrong?
Here is my code, I have tried to add the title the way I did for the other columns but it won't work.
<?php
for($i=0; $i<33; $i++) {
$staff[$i][0] = $i + 1;
$staff[$i][1] = rand(100000, 600000);
$staff[$i][2] = rand(14000, 40000);
$staff[$i][4] = rand(1,7);
if (rand(14000, 40000) >= 20000)
$staff[$i][3] = "yes";
else
$staff[$i][3] = "no";
}
print "<table border=1><tr><th>" . "Number of Staff" . "</th><th>" . "Staff Number" . "</th><th>" . "Salary" . "</th><th>" . "Bonus?" . "</th></tr>". "Bonus %" . "</th></tr>";
for($j=0; $j<33; $j++) {
print "<tr>";
for ($k=0; $k<5; $k++) {
print "<td>" . $staff[$j][$k] . "</td>";
}
print "</tr>";
}
print "</table>"
?>
You table header th formatting is not correct. Can try like below. Also missing a terminating semi clone ; at last line print "</table>".
print "<table border='1'>
<tr>
<th>" . "Number of Staff" . "</th>
<th>" . "Staff Number" . "</th>
<th>" . "Salary" . "</th>
<th>" . "Bonus?" . "</th>
<th>". "Bonus %" . "</th>
</tr>
";
for($j=0; $j<33; $j++) {
print "<tr>";
for ($k=0; $k<5; $k++) {
print "<td>" . $staff[$j][$k] . "</td>";
}
print "</tr>";
}
print "</table>";
Replace
"</th></tr>". "Bonus %" . "</th></tr>"
By
"</th><th>". "Bonus %" . "</th></tr>"
You close tr before end of line
Related
I'm trying to write a simple if statement that should be returning true but is instead returning false. The issue I'm having is that for several items on my list $dataPeriod should be returning true matching '202265' I've tried converting it to a string thinking it didn't like that I was comparing a string to an int and that didn't seem to help.
I'm importing the data from this CSV file - https://www.harpercollege.edu/_resources/php/openclasslistweb.csv
<?php
$next = 0;
$file_to_read = fopen('openclasslistweb.csv', 'r');
if($file_to_read !== FALSE){
echo "<table class = 'open-list-table'>\n";
while(($data = fgetcsv($file_to_read, ',')) !== FALSE){
for($i = 0; $i < count($data); $i++) {
if ($i == 0){
$dataCourse = $data[$i];
}
if ($i == 1){
$dataCRN = $data[$i];
}
if ($i == 2){
$dataTitle = $data[$i];
}
if ($i == 3){
$dataMethod = $data[$i];
}
if ($i == 4){
$dataInstructor = $data[$i];
}
if ($i == 5){
$dataStartDate = $data[$i];
}
if ($i == 6){
$dataEndDate = $data[$i];
}
if ($i == 7){
$dataMonday = $data[$i];
}
if ($i == 8){
$dataTuesday = $data[$i];
}
if ($i == 9){
$dataWednesday = $data[$i];
}
if ($i == 10){
$dataThursday = $data[$i];
}
if ($i == 11){
$dataFriday = $data[$i];
}
if ($i == 12){
$dataSaturday = $data[$i];
}
if ($i == 13){
$dataSunday = $data[$i];
}
if ($i == 14){
$dataStartTime = $data[$i];
}
if ($i == 15){
$dataEndTime = $data[$i];
}
if ($i == 16){
$dataRoom = $data[$i];
}
if ($i == 17){
$dataBuilding = $data[$i];
}
if ($i == 18){
$dataSchedule = $data[$i];
}
if ($i == 19){
$dataDesc = $data[$i];
}
if ($i == 20){
$dataPeriod = $data[$i];
}
}
if (strpos($dataPeriod, "202265") !== false) {
if ($next == 0){
echo "<tr class='open-list-header'>";
}
else {
echo "<tr>";
}
echo "<td>" . $dataCourse . "</td>" . "<td>" . $dataCRN . "</td>" . "<td>" . $dataTitle . "</td>" . "<td>" . $dataMethod . "</td>" . "<td>" . $dataInstructor . "</td>" . "<td>" . $dataStartDate . "</td>" . "<td>" . $dataEndDate . "</td>" . "<td>" . $dataMonday . "</td>" . "<td>" . $dataTuesday . "</td>" . "<td>" . $dataWednesday . "</td>" . "<td>" . $dataThursday . "</td>" . "<td>" . $dataFriday . "</td>" . "<td>" . $dataSaturday . "</td>" . "<td>" . $dataSunday . "</td>" . "<td>" . $dataStartTime . "</td>" . "<td>" . $dataEndTime . "</td>" . "<td>" . $dataRoom . "</td>" . "<td>" . $dataBuilding . "</td>" . "<td>" . $dataSchedule . "</td>" . "<td>" . $dataDesc . "</td>" . "<td>" . $dataPeriod . "<td>";
echo "</tr>\n";
$next = $next + 1;
}
}
echo "</table>\n";
fclose($file_to_read);
}
?>
Your CSV has a lot of invisible characters or encoded characters, so some of your values aren't what you expect. I was able to get all of the unique values for $data[20], and with json_encode it shows this:
{
"0":"\u0000A\u0000c\u0000a\u0000d\u0000e\u0000m\u0000i\u0000c\u0000 \u0000P\u0000e\u0000r\u0000i\u0000o\u0000d\u0000",
"1":"\u00002\u00000\u00002\u00002\u00006\u00005\u0000",
"16":"\u0000O\u0000n\u0000l\u0000i\u0000n\u0000e\u0000 \u0000A\u0000N\u0000Y\u0000T\u0000I\u0000M\u0000E\u0000 \u0000(\u0000A\u0000s\u0000y\u0000n\u0000c\u0000h\u0000r\u0000o\u0000n\u0000o\u0000u\u0000s\u0000)\u0000",
"180":"\u00002\u00000\u00002\u00002\u00009\u00005\u0000",
"982":"\u00002\u00000\u00002\u00003\u00003\u00005\u0000",
"2104":"\u0000L\u0000L\u00001\u0000"
}
I want to use the function onclick to increment a variable [increment the variable paid]
paid is by default at 0 i want to increment at 1.
I try this code : onclick='paid++' but is not working...
My Code
foreach($query as $index=>$row) {
echo ($colorCount++%2) ? "<tr class='alt'>" : "<tr>";
echo "<td class='" . getPaidColor($row['paid']) . "' onclick='paid++ '></td>";
echo "<td class='" . getDelayColor($row['hour']) . "' onclick='postDeleteForm(" . $index . ")'></td>";
foreach($columns as $column) {
echo "<td>" . $row[$column] . "</td>";
}
}
I'm trying to re-write this code so that it goes with the columns that are going to be user-defined. With this, my challenge consists of
a) Needs to select random starting item from array
b) Select the next random color from the original array that is not equivalent to the most recent items selected based the number: $intNotesColumn + 1
I was thinking a do-while statement nested inside another was appropriate for this but am unsure how to go about this. Here is my code so far:
$metroUIcolors = array( "#A30061", "#8200CC", "008987", "#A05000", "#B85A93", "#C07807", "#E51400", "#297A29" );
$metroUIcolorsLength = count($metroUIcolors);
$intNotesColumn = 3; // This will be user-defined later
// Now I query the SQL database to get my base-code
if ($result->num_rows > 0) {
// output data of each row
echo '<table border=0 valign=top>'
. '<tr>'
. '<td colspan=' . $intNotesColumn . '>' . '<h1>header</h1>' . '</td>'
. '</tr>'
. '<tr>';
$counterRank = 1;
while($row = $result->fetch_assoc()) {
echo "<td bgcolor=" . $metroUIcolors[rand(0, $metroUIcolorsLength - 1)]. ">"
. "<h2>" . $row["username"] . '</h2><br />'
. "<p class='notes'>" . $row["notes"] . "</p>"
. "<p class='footnotes'>"
. "<br />Last Reset: " . $row["lastReset"]
. '<br />Last Update: ' . $row['lastUpdate']
. '<br />SessionID: ' . $row["sessionID"]
. "<br />Counter = " . $counterRank . "</td>". '</p>';
if ($counterRank % $intNotesColumn == 0)
{
echo '</tr><tr>';
}
$counterRank++;
}
echo '</tr></table>';
} else{
echo "No Notes Found in databases";
}
Then, why don't you do it order picking one color at a time. You could use shuffle() so that there will be a different starting color everytime.
<?php
$counterRank = 1;
// shuffle the array
shuffle($metroUIcolors);
while($row = $result->fetch_assoc()) {
$rand_color = $counterRank % $metroUIcolorsLength;
echo "<td bgcolor=" . $metroUIcolors[$rand_color]. ">";
// everything else
$counterRank++;
}
?>
If you insist on doing the way you said, you may create an array $colorCount which has color codes as keys and count as values.
<?php
$counterRank = 1;
$colorCount = array_fill_keys($metroUIcolors, 0);
while($row = $result->fetch_assoc()) {
do {
$rand_color = $metroUIcolors[rand(0, $metroUIcolorsLength - 1)];
} while ($colorCount[$rand_color] > 5);
echo "<td bgcolor=" . $rand_color. ">";
// everything else
$counterRank++;
$colorCount[$rand_color] += 1;
}
?>
I've made a foreach loop that print some variables found in an xml file:
foreach ($xml->result->rowset->row as $row)
{
echo $row["price"] . " " . $row["product"] . "</br>";
}
I would like to get the sum of all the "prices" shown. How can i do?
Just have a variable add up those values as you iterate:
$total = 0; // start with zero
foreach ($xml->result->rowset->row as $row)
{
$total += $row["price"]; // add price to total
echo $row["price"] . " " . $row["product"] . "</br>";
}
echo $total; // echo out total amount
Store the count in a variable:
$total = 0;
foreach ($xml->result->rowset->row as $row) {
echo $row["price"] . " " . $row["product"] . "</br>";
$total += $row['price']; // assumed row_price is some integer
}
echo $total;
Simply add it to a new variable:
$sum = 0;
foreach ($xml->result->rowset->row as $row) {
echo $row["price"] . " " . $row["product"] . "<br />";
$sum += $row["price"];
}
echo $sum . "<br />";
$sum = 0;
foreach ($xml->result->rowset->row as $row)
{
echo $row["price"] . " " . $row["product"] . "</br>";
$sum += $row["price"] ;
}
echo $sum ;
isnt that simple ?
$sum=0;
foreach ($xml->result->rowset->row as $row)
{
echo $row["price"] . " " . $row["product"] . "</br>";
$sum +=$row["price"];
}
echo $sum;
just try this code!!
I have this block of text in an array:
"Stefan Olsson"
"Kungsvägen"
"Skolgatan"
xxxx-xx-xx
0735xxxxxx,
"Pär Davidsson"
"Skolgatan"
"Myntvägen"
xxxx-xx-xx
0709xxxxxx,
I parse this type of content to an CSV-file, for later usage in Excel. However, I want to fromat this text to fit in different columns in excell. So, when I open the CSV-file in Execel, I want the name to be in one column, the address in the column besides etcetc. How can I accomplish this? Should I use PHPExcel? Or could it be done with plain old PHP?
Here is my PHP-code
$gatunamn = $_POST['gata'];
$ort = $_POST['omrade'];
$csv_data = array();
$newSpider->fetchPage($gatunamn, $ort, $offset=0);
$obj = json_decode($newSpider->html);
echo "<div id='rightcontent'><table id='one-column-emphasis'>";
echo "<th><input type='checkbox' name='csv_all' id='csv_all'></th><th>Namn</th><th>Adress</th><th>Adress2</th><th>Adress3</th><th>Personnummer</th><th>Telefonnummer</th><th>Telefonnummer2</th>";
$antal_sidor = round($obj->search->wp->totalHits / $obj->search->wp->pageSize);
echo "<td></td>";
foreach($obj->search->wp->features as $fish) //Loopar ut 50st (pageSize)
{
echo "<tr>";
echo "<td><input type='checkbox' value='csv' class='csv'></td>";
echo "<td>" . $fish->name . "</td>";
$csv_data[] .= utf8_decode($fish->name);
foreach($fish->addresses as $ad)
{
echo "<td>" . $ad->label . " " . $ad->postcode . " " . $ad->area . "</td>";
$csv_data[] .= utf8_decode($ad->label . " " . $ad->postcode . " " . $ad->area);
}
if(!empty($fish->dateOfBirth))
{
$convert_date = substr($fish->dateOfBirth, 0, -3); //Gör om datum från timestamp
echo "<td>" . date("Y-m-d", $convert_date) . "</td>";
$convert_datee = date("Y-m-d", $convert_date);
$csv_data[] .= $convert_datee;
}
if(!empty($fish->phoneNumbers))
{
foreach($fish->phoneNumbers as $ph)
{
echo "<td>" . $ph . "</td>";
$csv_data[] .= $ph . ",";
}
}
echo "</tr>";
}
echo "</table>";
$j = 0;
for($i = 1; $i <= $antal_sidor; $i++)
{
echo "<a href='curl2.php?gatunamn=$gatunamn&ort=$ort&offset=$j'>" . $i . "</a> ";
$j += 100;
}
echo "</div>";
echo "<div id='debug'><pre>";
var_dump($csv_data);
echo "</pre></div>";
}
if(isset($_POST['export']))
{
$fp = fopen("eniroo.csv","w");
foreach(explode(",", implode("\n",$csv_data)) as $rad) {
fputcsv($fp, array(implode(',', str_getcsv($rad, "\n"))));
}
echo "<div id='csv_info'>";
echo "<a href='eniro.csv'>Hämta CSV-fil</a>";
echo "</div>";
}
// Restructure the original array into rows
$myDataArray = array_chunk($myDataArray, 5);
// Then write to CSV
$fp = fopen('file.csv', 'w');
foreach($myDataArray as $dataRow) {
fputcsv($fh, $dataRow);
}
fclose($fh)