I would like to show the count value for the mysql result in html table.
For an example "district Name1" is the name of "district name" and "count-value1" shows the total post office count in "district name1".
My output must be like this,
------------------------
District | Post Offices
------------------------
Name1 | Count-Value1
Name2 | Count-Value2
Name3 | Count-Value3
Name4 | Count-Value4
... | ...
------------------------
Anyone can please help me to fix this..
This is my PHP code,
<?php
include('config.php');
$data_content= "";
$qry = "SELECT DISTINCT district_N,state_N FROM pincode_data WHERE state_N ='" . mysql_real_escape_string($_GET['st'])."' ORDER BY district_N ASC";
$result = mysql_query($qry);
while($row = mysql_fetch_array($result))
{
$dist_Value = $row['district_N'];
$state_Value = $row['state_N'];
$data_content.= "<tr><td><a href='pincity.php?dist=".$row['district_N']."'> ".$row['district_N']."</a></td><td>Count Value to be Displayed Here</td></tr>";
}
mysql_close();
?>
This is my HTML code,
<html>
<head>
</head>
<body>
<h1>Pincodes in <?php echo $state_Value; ?></h1>
<div>
<table>
<tr><td>District</td><td>Post Offices</td></tr>
<?php echo $data_content; ?>
</table>
</div>
</body>
</html>
SQL has syntax for counting elements of a group. If you want to count all occurrences of each value for X in a table, the general form is select X, count(X) from <table> GROUP BY X. This will group all equal values of X together, and give you a total count of them. For your particular case, try this:
$qry = "SELECT district_N,state_N, COUNT(district_N) cnt
FROM pincode_data WHERE state_N ='" . mysql_real_escape_string($_GET['st'])."'
GROUP BY district_N ORDER BY district_N ASC";
Also, it is good to see that you are escaping the parameters - but mysql_* is deprecated and will be removed in future versions of PHP. You are best off to use either mysqli_* or PDO. In either case, you should also look into prepared statements and parameter binding. It's good for your health.
Related
PeterM helped me with this question, but i dont want to create new one, thats why im editing this one.
I got another problem, sorry, im just new with sql.
This is my code that output all the information I need from the database.
What i want:
I created table with name "customers" with columns "id and customername".
Then i created simple script with INSERT INTO "customers", yes, its script like admin page.
The idea is: I want to add new orders from a separate page, for example: admin.php so I don't have to do it from a file.
How to easily do this?
I have admin.php file with: INSERT INTO customers (customername) VALUES (customername) and its working, i can add new order to database, when i fill html form, it doesn't show my new added order.
<?php
$output = "SELECT *, SUM(enddate - startdate) AS time FROM employees GROUP
BY id";
$result = mysqli_query($conn, $output);
WHILE ($row = mysqli_fetch_assoc($result)) {
$EMPLOYEE = $row['employee'];
$CUSTOMER = $row['customername'];
$WORKDATE = $row['workdate'];
$WORKTYPE = $row['worktype'];
$DAYHOURS = $row['startdate'];
$ENDDATE = $row['enddate'];
$TOTAL = $row['time'];
echo "
<tr>
<td>$EMPLOYEE</td>
<td>$CUSTOMER</td>
<td>$WORKDATE</td>
<td>$WORKTYPE</td>
<td>$DAYHOURS</td>
<td>$ENDDATE</td>
<td>$TOTAL</td>
</tr>";
}
?>
My html form:
<div class='row'>
<div class='col-25'>
<label for='customers'>Choose OrderName</label>
</div>
<div class='col-75'>
<select name='customername'>
<?php
$customers = "SELECT customername FROM customers";
$result = mysqli_query($conn, $customers);
WHILE ($row = mysqli_fetch_assoc($result)) {
$customer = $row['customername'];
echo "<option value=''>$customer</option>";
}
?>
</select>
</div>
</div>
You should change your query into something like this:
SELECT *, SUM(`enddate` - `startdate`) AS time FROM `employees` GROUP BY `employee`;
First you need to subtract the startdate from the enddate then get the sum of all the grouped results, hence the SUM part.
This should give you a result somewhat similar to this
Results:
| id | employee | startdate | enddate | time |
|----|----------|-----------|---------|------|
| 8 | David | 8 | 22 | 17 |
The time column is the one you need.
I made a fiddle where you can see and test how it works, http://sqlfiddle.com/#!9/40a959/5
I want a loop, that echoes some football matches and my users bets on them, and then the possibillity to change the bets.
I have two tables in my db:
vm_kampe which is the matches where res is the result:
|id |hhold |uhold |res |
|1 |Rusland |Saudi |NULL |
|2 |Egypten |Uruguay |NULL |
... and so on
and vm_kampe, which is the users bets:
|id |resu |
|1 |2-1 |
|2 |1-3 |
... and so on.
My update site is like this:
SELECT * FROM vm_kampe k JOIN vm_207 u ON k.id = u.id
...
foreach($results as $row){
echo '<div class="vm_kupon_row">
<div class="vm_id"><input type="hidden" name="kampids[]" value="'.$row->id.'"></div>
<div class="vm_kampe">'.$row->hhold.' - '.$row->uhold.'</div>
<div class="vm_result"><input class="vm_resultat" type="text" name="resultats['.$row->id.']" placeholder="X-X" value="'.$row->resu.'"></div>
<input type="submit" class="godkend-vm-kupon" name="submit['.$row->id.']" value="Godkend">
My POST site is like this:
$res = $mysqli->real_escape_string($_POST['resultats']);
$id = $mysqli->real_escape_string($_POST['kampids']);
FOREACH ($_POST as $p) {
$gid = $p['id'];
$result = $p['res'];
$sql = "UPDATE vm_207 SET resu = '$result' WHERE id = '$gid'";
};
But if I try editting a bet and press submit, the bet will just stay the same as before. All I want is for me to be able to update every match. Please help :)
UPDATE
I just tried this in the post code:
$id = $_POST['kampids'];
$res = $_POST['resultats'];
foreach ( $id as $key => $k) {
print "The match is " .$k. " and the result is " .$res[$key];
};
I tried writing 2-1 in the first game and the print was:
The match is 1 and the result is 2-1
The match is...
So as I can see, the function works, so now I just need it to update the table..
In your hidden field, you are not setting the index number statically like you are with the other inputs.
<div class="vm_id"><input type="hidden" name="kampids['.$row->id.']" value="'.$row->id.'"></div>
I think this question / answer should help.
How to get form input array into PHP array
I have two tables and i want to echo the total call count once each user logins:
Login
Firstname | Lastname | Login ID
------------------------------
Tyler | Durden | 3
Call Count
Name | Call Count | Open | GrandTotal
------------------------------
Tyler Durden| 100 | 33 | 133
i tried:
<?php
$result = mysqli_query($mysqli, "SELECT * FROM csvdata WHERE Name=".$_SESSION['firstname']. ' ' .$_SESSION['lastname']." ");
while($res = mysqli_fetch_array( $result )) {
echo $res['Open'].' Open Calls';
echo $res['GrandTotal'].' Grand Total Calls';
}
mysqli_close($mysqli);
?>
But its not working, i think i have to join the the two tables to get it to work. What do you think?
Assuming your Call Count table is actually called csvdata, you'll want to format your SQL request string a bit by adding single quotes around the WHERE name = part.
<?php
$result = mysqli_query($mysqli, "SELECT * FROM csvdata WHERE Name='".$_SESSION['firstname']. ' ' .$_SESSION['lastname']."' ");
while($res = mysqli_fetch_array( $result )) {
echo $res['Call Count'].' Call Count';
echo $res['Open'].' Open Calls';
echo $res['GrandTotal'].' Grand Total Calls';
}
mysqli_close($mysqli);
?>
Good practice would require that you use primary keys to facilitate joins between tables and make sure two users with the same name can be differenciated.
In that case you may want to consider replacing the Name column in your Call Count table for your loginID. This way you could get your name from the Login table (as shown below). Also, as bad as it is to have duplicated data like your user's name in both tables, you do not need your GrandTotal column since you can easily get the sum of CallCount and Open to get the exact same number. In the end, your query should look more like this (assuming your tables are called Login and CallCount).
<?php
$result = mysqli_query($mysqli, "SELECT l.FirstName, l.LastName, cc.CallCount, cc.Open, (cc.CallCount + cc.Open) AS GrandTotal FROM Login AS l JOIN CallCount AS cc ON l.LoginID = cc.LoginID WHERE l.FirstName LIKE \"".$_SESSION['firstname']."\" AND l.LastName LIKE \"".$_SESSION['lastname']."\"");
// ...
?>
I have a table named user where I have inserted a value like
($workshop_sales*0.01)+($counter_sales*0.005)+($sub_dealer_sales*0.0075)
in column incentive and value like
if{$workshop_sales>1600000' in column 'open_cond' and value like '}
in close_cond.
Now in my PHP page I have fetched values from that table. How can I use values from my columns open_cond and close_cond because I wish to use it as expression otherwise please help me by giving other way to execute it.I am doing wrong then please give me information whether its possible that I can write 'if statement' in php using it store as a text value in database column value ?? ''
I am getting result as
if($workshop_sales>1600000){2123.085}
but I want to use it as PHP expression in my code.
<?php
$selectalldepartment = "select * from user ";
$selectedalldepartment = mysqli_query($conn,$selectalldepartment);
$selectedalldepartment = mysqli_query($conn,$selectalldepartment);
$depart=1;
while($row10 = mysqli_fetch_assoc($selectedalldepartment))
{
$sqlsel1 = "select * from parameters ";
$sqlsel1q = mysqli_query($conn,$sqlsel1);
$row2 = mysqli_fetch_assoc($sqlsel1q);
//assigned counter sales value
$counter_sales = $row2["counter_sales"];
?>
<tr>
<td style="text-align:center;"><?php echo $depart; ?></td>
<td style="text-align:center;"><?php echo $row10["open_condition"]; eval("echo {$row10['incentive']};");echo $row10["close_condition"];?></td> </tr>
<?php
$depaz++;
$depart++;
}
?>
Here is my table:
<!-- language: lang-none -->
user_id | user_name | incentive |open_cond | close_cond |
1 | Goldy Shahu | ($workshop_sales*0.01)+ | |
| | ($counter_sales*0.005)+ | |
| | ($sub_dealer_sales*0.0075)|if($workshop_sales> |
1600000){ |}
you should not store conditions into your table
need to store data like
$workshop_sales and 1600000 in seporate columns
then you can make condition
like something
if($row10["open_condition"] > $row10["open_condition_value"]){
//your code
}
Say I have the two following tables:
country_table:
---------------------------------
|ID|country_code|country_name |
|01|CA |Canada |
|02|US |United States |
---------------------------------
user_table:
----------------------------------------------
|id|user_id|country_code|email_address |
|01|oswalsh|CA |blah#hotmail.com |
|02|jlong |US |blah2#hotmail.com |
----------------------------------------------
I am creating my user signup/edit account forms, and have a drop down for country. Originally I had simply stored the country_name in the user_table instead of using a country_code, however I have also decided to implement a map script...which is a massive mess of javascript files which make use of the country_code.
Thus I created the country_table so I could match the country_code and the country_name so I could map user interactions...However I am not sure how to go about creating the sql statement for the drop down.
What I have so far:
<?php
include ('../../connect.php');
$account_panel = mysql_query("SELECT * FROM user_table WHERE id=1")
or die(mysql_error());
while($row1 = mysql_fetch_array( $account_panel ))
{
?>
<select name="event_country" id="event_country" required />
<option selected="selected" value="<?php echo $row1['user_country']; ?>"><?php echo $row1['user_country']; ?></option>
<?php
$countries = mysql_query("SELECT * FROM country_table")
or die(mysql_error());
while($row = mysql_fetch_array( $countries ))
{
echo "<option value='". $row['value'] ."'>". $row['name'] ."</option>";
}
echo "</select>";
?>
<?php
}
mysql_close($connection);
?>
So as you should be able to see from above what is happening, is that the list is populated from the country_table, but I am trying to get the selected value from the user table. This does seem to work, but my problem is that the country code is what is stored and being displayed. So say I am grabbing the info for oswalsh...me...the country returned is CA, and I would like it to display Canada.
If I understood you right, you need to familiarize yourself with SQL Joins
To join those two tables and get the country_name along with the user data from the user_table, you'd need an SQL statement like this:
SELECT ut.*, ct.country_name
FROM user_table ut
INNER JOIN country_table ct
ON ut.country_code = ct.country_code
In your case, this would result in the following:
1 oswalsh CA blah#hotmail.com Canada
2 jlong US blah2#hotmail.com United States
Also, you should consider using mysqli_ and stop using mysql_ functions. Here is a discussion on this topic on SA.