Retrieving employee salaries with mysql and php using nymber_format - php

I am returning employee information from a musql db using php. I am getting the salary to returned with a formatted comma using the number_format() method but when the data is returned, all employees have the same salary. How do I get php to return individual salaries from the employees table?
PHP/MySQL
<?php
require_once("db.php");
$sql = "SELECT `*` FROM `employees`";
$results = mysqli_query($connect, $sql) or die(mysql_error());
$row = mysqli_fetch_array($results, MYSQL_BOTH) or die(mysql_error());
$salary = $row['salary'];
$rows_sal = number_format($salary);
echo("<table>");
while($row = mysqli_fetch_array($results, MYSQL_BOTH))
{
echo("<tr>");
echo "<td>" . $row['empid'] . '</td>' .
"<td>" . $row['lastname'] . '</td>' .
"<td>" . $row['firstname'] . '</td>' .
"<td>" . $row['department'] . '</td>' .
"<td>" . $row['position'] . '</td>' .
"<td>" . $rows_sal . '</td>';
echo '</br>';
echo('</tr>');
}
echo("</table>");
?>

number_format doesn't work on an array. Do it like this:
<?php
require_once("db.php");
$sql = "SELECT `*` FROM `employees`";
$results = mysqli_query($connect, $sql) or die(mysql_error());
$row = mysqli_fetch_array($results, MYSQL_BOTH) or die(mysql_error());
//$salary = $row['salary'];
//$rows_sal = number_format($salary);
echo("<table>");
while($row = mysqli_fetch_array($results, MYSQL_BOTH))
{
echo("<tr>");
echo "<td>" . $row['empid'] . '</td>' .
"<td>" . $row['lastname'] . '</td>' .
"<td>" . $row['firstname'] . '</td>' .
"<td>" . $row['department'] . '</td>' .
"<td>" . $row['position'] . '</td>' .
"<td>" . number_format($row['salary']) . '</td>';
echo '</br>';
echo('</tr>');
}
echo("</table>");
?>

That's because you're working with the salary outside the foreach loop. (And, by the way, you're leaving the first row out of the table).
Before I suggest you how to do it, let me explain what it's happening with your actual code: (follow the comments)
<?php
require_once("db.php");
$sql = "SELECT `*` FROM `employees`";
$results = mysqli_query($connect, $sql) or die(mysql_error());
//here you're asking THE FIRST row:
$row = mysqli_fetch_array($results, MYSQL_BOTH) or die(mysql_error());
$salary = $row['salary'];
//so $rows_sal is the first salary. All this have nothing to do with the iteration below.
$rows_sal = number_format($salary);
echo("<table>");
//here you're asking for the next results (leaving 1st row outside the table!)
while($row = mysqli_fetch_array($results, MYSQL_BOTH))
{
echo("<tr>");
echo "<td>" . $row['empid'] . '</td>' .
"<td>" . $row['lastname'] . '</td>' .
"<td>" . $row['firstname'] . '</td>' .
"<td>" . $row['department'] . '</td>' .
"<td>" . $row['position'] . '</td>' .
//and here you're referencing the value set before the while.
"<td>" . $rows_sal . '</td>';
echo '</br>';
echo('</tr>');
}
echo("</table>");
?>
Now this is what you should do: (follow the comments)
<?php
require_once("db.php");
$sql = "SELECT `*` FROM `employees`";
$results = mysqli_query($connect, $sql) or die(mysql_error());
//don't ask for results yet!
echo("<table>");
while($row = mysqli_fetch_array($results, MYSQL_BOTH)) //including 1st row
{
//now $row is EACH row, NOW you can format your salary:
$salary = $row['salary'];
$rows_sal = number_format($salary);
echo("<tr>");
echo "<td>" . $row['empid'] . '</td>' .
"<td>" . $row['lastname'] . '</td>' .
"<td>" . $row['firstname'] . '</td>' .
"<td>" . $row['department'] . '</td>' .
"<td>" . $row['position'] . '</td>' .
"<td>" . $rows_sal . '</td>';
echo '</br>';
echo('</tr>');
}
echo("</table>");
?>
Hope it helps.

Related

Delete specific row in database table and generatet html table

This is my delete page :
<?php
require('includes/config.php');
$id = $_GET['ID'];
$pdoConnect = new PDO($db);
$query='DELETE * FROM studentraspored WHERE ID = "' . $id . '" ';
$pdoResult = $db->prepare($query);
$pdoExec = $pdoResult->execute($query);
header('location:index.php');
?>
This is generated table in my “memberpage.php”:
if (count($rows)){
foreach ($rows as $row) {
$_SESSION['row'] = $rows;
$id = floatval($row['ID']);
echo "<tr>" .
'<form action="delete_raspored.php" method="post">'.
"<td>" . $row["ID"] . "</td>" .
"<td>" . $row["den"] . "</td>" .
"<td>" . $row["chas"] . "</td>" .
"<td>" . $row["predmet"] . "</td>" .
"<td>" . $row["profesor"] . "</td>" .
"<td>" . $row["prostorija"] . "</td>" .
"<td>" . $row["tip"] . "</td>" .
'<td><input type="submit" id="' . $id . '" value="Delete" ></td>'.
"</form>".
"</tr>"
This not working properly. I don't understand why maybe something i missed with floatval
Start by trying this:
<?php
require('includes/config.php');
$id = $_GET['ID'];
$query='DELETE FROM studentraspored WHERE ID = ?';
$pdoResult = $db->prepare($query);
$pdoResult->execute(array($id));
header('location:index.php');
exit();
Note the placeholder in place of the actual value, this will prevent SQL injections. The value is passed in in the execute, or you could bind it (http://php.net/manual/en/pdostatement.bindparam.php). http://php.net/manual/en/pdo.prepared-statements.php
The delete syntax was also off, delete deletes a whole row not specific columns, http://dev.mysql.com/doc/refman/5.7/en/delete.html.
In your form I also don't see an element named ID so that could be another issue and your form is submitting via POST, not GET.

php foreach get all data from database table

I would like to get all the values of: echo "<td class=\"points\">" . $row2['PIY'] . "/" . $row2['PIK'] . "</td>"; Some how it only returns the first on. After that i would like to calculate the sum of them.
At the moment the code only gets the first.
echo "<table class=\"zebra\">";
$sum1=0;
$sum2=0;
$numbering =1;
$query2 = "SELECT pisteet_1 As PIY, pisteet_2 as PIK, nimi As NIM, opisto As OPI, pisteet.kaupunki_id As KA FROM
pisteet INNER JOIN joukkueet ON joukkueet.id = pisteet.team_id INNER JOIN oppilaitokset ON oppilaitokset.opisto_id = joukkueet.opisto_id GROUP BY nimi, team_id ORDER BY team_id ASC";
foreach ($db->query($query2) as $row2) {
echo "<tr class=\"all " . $row2['KA'] . "\">";
echo "<td>" . $numbering . "</td>";
echo "<td>" . $row2['NIM'] ."<span>" . $row2['OPI'] ."</span></td>";
//--------should get all the points----------
echo "<td class=\"points\">" . $row2['PIY'] . "/" . $row2['PIK'] . "</td>";
$sum1 +=$row2['PIY'];
$sum2 +=$row2['PIK'];
echo '<td class="Sum">'.$sum1.'/'.$sum2."</td>";
//-------------------------------------------
echo "</tr>";
$numbering ++;
}
echo '</table>';

Generated array values changing after While statements

This is a weird issue I have came across and was wondering if anyone my have insight. Not sure if the mktime does not function as I am trying to get it to, or what may be going on.
Last night, things were working fine - the months being displayed were correct. Today, though, for some reason the values of my $aGMonV are changing somewhere after the foreach and before the while(row_* = mysqli_fetch_array* statements.
While the var_dump returns %2014-03% as the first month (which is correct) - the table that is generated returns %2013-09% as the first month. All the queries being ran are being run with %2013-09% and NOT starting at current month.
My code is below:
$aGMon = array();
for ($i = 0; $i < 20; $i++)
{ $aGMon[] = date('Y-m', mktime(0,0,0,date('n')-$i,1)); }
foreach ($aGMon as $aGMonK => $aGMonV)
{
$aGMonO = $aGMonV;
$aGMonV = " '%" . $aGMonV . "%' ";
$result_E = mysqli_query($con,"select kWh_AVG from UseElecM where Month LIKE " . $aGMonV . ";");
$result_G = mysqli_query($con,"select TotalMCF from UseGas where Month LIKE " . $aGMonV . ";");
$result_P = mysqli_query($con,"select (A.Minutes+E.Minutes_L500+E.Minutes_Free) as Minutes, (A.Texts+E.Texts) as Texts, (A.MMS+E.MMS) as MMS, (A.MBData+E.MBData) as MBData from UseSprintA A left outer join UseSprintE E on A.Bill = E.Bill where A.Bill LIKE " . $aGMonV . ";");
$result_T = mysqli_query($con,"select cast((avg(Average)) as decimal (10,1)) as ATF from CF6MCI where Date LIKE " . $aGMonV . ";");
var_dump($aGMonV);
while($row_E = mysqli_fetch_array($result_E))
while($row_G = mysqli_fetch_array($result_G))
while($row_P = mysqli_fetch_array($result_P))
while($row_T = mysqli_fetch_array($result_T))
{
echo "<td class='UUMonth'>" . ($aGMonO) . "<div class='UUMonthO'>Average temperature: " . $row_T['ATF'] . " F</div></td>";
echo "<td>" . $row_E['kWh_AVG'] . "</td>";
echo "<td>" . $row_G['TotalMCF'] . "</td>";
echo "<td>" . $row_P['Minutes'] . "</td>";
echo "<td>" . $row_P['Texts'] . "</td>";
echo "<td>" . $row_P['MMS'] . "</td>";
echo "<td>" . $row_P['MBData'] . "</td>";
echo "</tr>";
}
}
Results of the code are as follows:
Result of code
user3260912 try removing the while, try like this:
$aGMon = array();
for ($i = 0; $i < 20; $i++)
{ $aGMon[] = date('Y-m', mktime(0,0,0,date('n')-$i,1)); }
foreach ($aGMon as $aGMonK => $aGMonV)
{
$aGMonO = $aGMonV;
$aGMonV = " '%" . $aGMonV . "%' ";
$result_E = mysqli_query($con,"select kWh_AVG from UseElecM where Month LIKE " . $aGMonV . ";");
$result_G = mysqli_query($con,"select TotalMCF from UseGas where Month LIKE " . $aGMonV . ";");
$result_P = mysqli_query($con,"select (A.Minutes+E.Minutes_L500+E.Minutes_Free) as Minutes, (A.Texts+E.Texts) as Texts, (A.MMS+E.MMS) as MMS, (A.MBData+E.MBData) as MBData from UseSprintA A left outer join UseSprintE E on A.Bill = E.Bill where A.Bill LIKE " . $aGMonV . ";");
$result_T = mysqli_query($con,"select cast((avg(Average)) as decimal (10,1)) as ATF from CF6MCI where Date LIKE " . $aGMonV . ";");
//var_dump($aGMonV);
$row_E = mysqli_fetch_array($result_E);
$row_G = mysqli_fetch_array($result_G);
$row_P = mysqli_fetch_array($result_P);
$row_T = mysqli_fetch_array($result_T);
echo "<td class='UUMonth'>" . ($aGMonO) . "<div class='UUMonthO'>Average temperature: " . $row_T['ATF'] . " F</div></td>";
echo "<td>" . $row_E['kWh_AVG'] . "</td>";
echo "<td>" . $row_G['TotalMCF'] . "</td>";
echo "<td>" . $row_P['Minutes'] . "</td>";
echo "<td>" . $row_P['Texts'] . "</td>";
echo "<td>" . $row_P['MMS'] . "</td>";
echo "<td>" . $row_P['MBData'] . "</td>";
echo "</tr>";
}

mysqli_fetch loop not working

I have a loop inside other loop which is not working, this is the code:
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['rowId'] . "</td>";
echo "<td>" . $row['startDate'] . "</td>";
echo "<td>" . $row['eventName'] . "</td>";
echo "<td>" . $row['betName'] . "</td>";
$string1 = "SELECT * FROM newCell WHERE rowId ='";
$string2 = $row['rowId']."'";
$result2 = $string1.$string2;
echo "<td>" . $result2 . "</td>";
while($row2 = mysqli_fetch_array($result2))
{
echo "<td>" . $row2['odds'] . "</td>";
echo "<td>" . $row2['outcomeName'] . "</td>";
}
echo "</tr>";
}
When I query $result2 directly into the BBDD for the first result it shows three results but the code doesn't go in the second LOOP. Why? Any error here?
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['rowId'] . "</td>";
echo "<td>" . $row['startDate'] . "</td>";
echo "<td>" . $row['eventName'] . "</td>";
echo "<td>" . $row['betName'] . "</td>";
$string1 = "SELECT * FROM newCell WHERE rowId ='";
$string2 = $row['rowId']."'";
$result2 = $string1.$string2;
echo "<td>" . $result2 . "</td>";
$result2 = mysqli_query($connection, $result2);
while($row2 = mysqli_fetch_array($result2))
{
echo "<td>" . $row2['odds'] . "</td>";
echo "<td>" . $row2['outcomeName'] . "</td>";
}
echo "</tr>";
}
Use:
$query = "SELECT ....";
$result2 = mysqli_query($db, $query);
while($row2 = mysqli_fetch_array($result2))
{
echo "<td>" . $row2['odds'] . "</td>";
echo "<td>" . $row2['outcomeName'] . "</td>";
}
Before read this How can I prevent SQL injection in PHP? topic. After try to use mysql_query()
Try This
<?php
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['rowId'] . "</td>";
echo "<td>" . $row['startDate'] . "</td>";
echo "<td>" . $row['eventName'] . "</td>";
echo "<td>" . $row['betName'] . "</td>";
$string1 = "SELECT * FROM newCell WHERE rowId ='";
$string2 = $row['rowId']."'";
$result2 = $string1.$string2;
echo "<td>" . $result2 . "</td>";
$results = mysqli_query($db,$result2);
while($row2 = mysqli_fetch_array($results))
{
echo "<td>" . $row2['odds'] . "</td>";
echo "<td>" . $row['outcomeName'] . "</td>";
}
echo "</tr>";
}
?>
Before fetching data execute mysql query using mysqli_query() function then run mysqli_fetch_array(). It would be good if you count result as $count = mysqli_num_rows($query) and manage your code with if... else .
I think $result2 should be output of mysqli_query not just merely query. Talking in analogous to MySQL.
Probably you should have something like this
$result2 = mysqli_query($result2);

printing [mysql_query] result in a table

I want to print mysql_query result in a table. I know how to do it but I am just confused. I tried this.
<?php
mysql_connect("localhost","root","") or die("Could not Connect.");
mysql_select_db("check") or die("Could not Select DB");
$table = "cc";
$i = 1;
$query = "select * from $table";
$sql = mysql_query($query);
if($sql){
echo "<table border='5'><tr>";
while($i<=2 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " . $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
while($i<=4 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " . $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
while($i<=6 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " . $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
while($i<=8 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " . $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
echo "</tr></table>";
}
?>
As you can see it is written again and again with a slight change of 2,4,6,8 in the while loop. It works but the problem is I cant rewrite it again and again as when the website will go live it will have more than 1000 entries. Could You guys help me out by suggesting another way to do this?
""** I need it to be like these dots (dots represent records in the database) **"""
. . . .
. . . .
. . . .
THANKS in Advance.
Ramzy
<?php
mysql_connect("localhost","root","") or die("Could not Connect.");
mysql_select_db("check") or die("Could not Select DB");
$table = "cc";
$query = "select * from $table";
$sql = mysql_query($query);
if($sql){
echo "<table border='5'><tr>";
while($row = mysql_fetch_array($sql)){
echo "<td>" . $row['id'] . " : " . $row['name'] . "</td>";
}
echo "</tr></table>";
}
?>
while($row = mysql_fetch_array($sql)) {
echo "<td>" . $row['id'] . " : " . $row['name'] . "</td>";
}
I don't really see what's the problem here.
By the way you should never call you're array like this $row[id] but you should quote the key instead $row['id']; Because if a constant id exists it will screw up your code and also for performance reason.
Just use
$limit = 1000;//place any value you need here to limit the number of rows displayed
while ($i<=$limit && $row = mysql_fetch_array($sql)){
echo "<td>" . $row['id'] . " : " . $row['name'] . "</td>";
++$i;
}
Also, that limit is unnecessary if all you want is to flush every record to the output. You could just do
while ($row = mysql_fetch_array($sql)){
echo "<td>" . $row['id'] . " : " . $row['name'] . "</td>";
}
And it will stop as soon as there are no more records.
To print all database rows into an HTML-table, use:
echo '<table>';
$i = 0; // $i is just for numbering the output, not really useful
while($row = mysql_fetch_array($sql))
{
echo '<tr><td>' . $i . ' - ' . $row['id'] . ' : ' . $row['name'] . '</td></tr>';
$i++;
}
echo '</tr></table>';
here is a general function I use:
function query_result_to_html_table($res, $table_id = NULL, $table_class = NULL, $display_null = true)
{
$table = array();
while ($tmp = mysql_fetch_assoc($res))
array_push($table, $tmp);
if (!count($table))
return false;
echo "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" "
. ($table_id ? "id=\"$table_id\" " : "")
. ($table_class ? "class=\"$table_class\" " : "") . ">";
echo "<tr>";
foreach (array_keys($table[0]) as $field_name) {
echo "<th>$field_name";
}
foreach ($table as $row) {
echo "<tr>";
foreach ($row as $col => $value) {
echo "<td>";
if ($value === NULL)
echo "NULL";
else
echo $value;
}
echo "\n";
}
echo "</table>\n";
return true;
}
I Got The Answer.. I wanted it to be like this. I made this and It Actually Works.
<?php
$i = 1;
mysql_connect("localhost" , "root" , "") or die('Could not Connect.');
mysql_select_db("db") or die('Could not select DB.');
$query = "select * from `check`";
$sql = mysql_query($query) or die(mysql_error());
echo "<table border='5' width='50%'><tr><th>Name</th><th>Gender</th></tr></table><table border='5' width='50%'><tr>";
if($i<3){
echo "<td align='center'>".$row['name']."</td>";
echo "<td align='center'>".$row['gender']."</td>";
++$i;
} else {
echo "<td align='center'>".$row['name']."</td><td align='center'>".$row['gender']."</td>";
echo "</tr>";
$i = 1;
echo "<tr>";
}
}
echo "</table>";
?>
</div>
Thank You Guys For Your Support

Categories