Click here to Skip to main content
12,956,234 members (61,063 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
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
Updated 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 20-Jan-13 22:58pm
   
Thank you. What about getting the total sum of salary??
Kislay Raj 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
Top Experts
Last 24hrsThis month
OriginalGriff 6,869
CHill60 3,550
Maciej Los 3,243
ppolymorphe 2,070
Jochen Arndt 1,973


Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 21 Jan 2013
Copyright © CodeProject, 1999-2017
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