Click here to Skip to main content
11,642,855 members (76,469 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: PHP
create table test
(
	name varchar(20),
	gender varchar(20),
	salary varchar(20)
);
 
insert into test values ('nm1','male','20000');
insert into test values ('nm2','female','85000');
insert into test values ('nm3','male','1000');
insert into test values ('nm4','unknown',NULL);

I am new to PHP, just learning.

I've a sample data of my table. While showing data in front, for gender I've to show
only the first character i.e. m/f/u and for salary I've to sum the total amount from front i.e
by using PHP.

also in my table salary is in varchar would I've to parse it as number?

<pre lang="xml"><?php
        include('conn.php');
        $result =mysql_query("select * from test")
            or die(mysql_error());
 
        echo "<table cellpadding = '10'>";
        echo "<tr>
                <th>name:</th>
                <th>gender:</th>
                <th>total salary:</th>
            </tr>";
 
        while($row = mysql_fetch_array($result))
        {
            echo "<tr>";
            echo '<td>'.$row['name'].'</td>';
            echo '<td>'.$row['gender'].'</td>';
            echo '<td>'.$row['salary'].'</td>';
        }
    ?>
Posted 15-Jan-13 23:48pm
Edited 20-Jan-13 22:05pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

in gender you can use
echo '<table><tbody><tr><td>'.substr($row['gender'],0,1).'</td></tr></tbody></table>';
instead  of echo '<table><tbody><tr><td>'.$row['gender'].'</td></tr></tbody></table>';
and you don't have to change it to number or double. php will auto convert it
  Permalink  
Comments
nischalinn at 20-Jan-13 22:58pm
   
Thank you. What about getting the total sum of salary??
Kislay Raj at 23-Jan-13 14:15pm
   
mean what you have all names are unique, each have unique salary value it's not monthly or any other period you have describe so they will give the values what you have entered if you want to get all sum value of salary you can use Sum

SELECT SUM(salary) FROM test

if you want to sum in php then set a variable like
$a=0;
before while and in while loop use
$a+=$row['salary'];

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 DamithSL 258
1 Sergey Alexandrovich Kryukov 230
2 jyo.net 180
3 OriginalGriff 170
4 Afzaal Ahmad Zeeshan 155
0 Mika Wendelius 485
1 DamithSL 457
2 OriginalGriff 365
3 Afzaal Ahmad Zeeshan 258
4 Sergey Alexandrovich Kryukov 254


Advertise | Privacy | Mobile
Web04 | 2.8.150731.1 | Last Updated 21 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100