How can I make those dates line up with start date and end date or selectively echo the dates rather than have the while loop echo them?
For example if I can echo "Start Date" then the actual date then the "end date" and then the date.
Basically how may I echo the dates from mySQL selectively rather than using the loop to echo them?
for example if I want to echo row 2 can I do echo $row'date' (to echo the 2nd row)
On HTML Page PHP CODE:
Start Date:
End Date:
<?php
mysql_connect("host","username","password") or die(mysql_error());
mysql_select_db("UserLogins") or die(mysql_error());
$query = "SELECT * FROM Date1";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo "<li>".$row['date']."</li>";
echo "<br />";
}
?>
It sounds like to you need to constrain your query results. If you only want certain records from your database then you should filter out the ones you don't want right in the SQL.
$query = "SELECT * FROM Date1 WHERE date >= ? AND date <= ?";
If you use prepared statements you can plug in values for the two questions marks.
first solution
$arr = Array();
while($row = mysql_fetch_array($result)){
$arr[] = $row;
}
echo $arr[0]['date']; // first
echo $arr[3]['date'];
echo $arr[7]['date'];
echo $arr[count($arr)-1]['date']; // last
second solution
$first = #mysql_fetch_assoc(mysql_query("SELECT * FROM Date1 ORDER BY date ASC LIMIT1"));
$last = #mysql_fetch_assoc(mysql_query("SELECT * FROM Date1 ORDER BY date DESC LIMIT1"));
echo $first['date'].'<br>';
echo $last['date'];
if you want to use second solution remember to put index on date column
also...
<?php
mysql_connect("host","username","password") or die(mysql_error());
mysql_select_db("UserLogins") or die(mysql_error());
$query = "SELECT min(date) as startdate, max(date) as enddate FROM Date1";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
if($row) {
echo "Start Date: " . $row['startdate'] . "<br />\n";
echo "End Date: " . $row['enddate'] . "<br />\n";
}
?>
???
err...
<?php
mysql_connect("host","username","password") or die(mysql_error());
mysql_select_db("UserLogins") or die(mysql_error());
$query = "SELECT * FROM Date1";
$result = mysql_query($query) or die(mysql_error());
echo "<table border=\"0\">\n<tr><th>Start Date</th><th>End Date</th></tr>";
$startRow = true;
while($row = mysql_fetch_array($result)) {
if($startRow) {
echo "\n<tr><td>";
} else {
echo "<td>";
}
echo $row['date'];
if($startRow) {
echo "</td>";
} else {
echo "</td></tr>";
}
$startRow = !$startRow;
}
if(!$startRow) {
echo "</tr>";
}
echo "\n</table>\n";
?>
???
Related
$query="SELECT * FROM cse_routine WHERE batch ='$batch' and section='$section' order by 'day'";
$post = $db->select($query);
?>
<?php
if ($post) {
while ($result = $post->fetch_assoc()) {
$var = "select * from cse_routine where day = '".$result['day'] ."'";
print $result['day']."<br>";
if ($results=$db->select($var))
{
while ($rows = mysqli_fetch_assoc($results))
{
print $rows['t_slot']. " " . $rows['room']. " " . $rows['day']. " " . $rows['c_code'];
echo "<br>";
}
}
echo "<br>";
}
}
echo "<br>";
?>
Running this code; I found this:
It's ok. But actually, I want output that not print the same date repeatedly. See here Saturday, Sunday print 3 each of them but I want that Saturday print 1 and Sunday print for 1.
You just change you query to
$query="SELECT * FROM cse_routine WHERE batch ='$batch' and section='$section'
GROUP by 'day'";
You could try using DISTINCT constraint (http://www.mysqltutorial.org/mysql-distinct.aspx) in first SQL:
$query="SELECT DISTINCT day FROM cse_routine WHERE batch ='$batch' and section='$section' order by 'day'";
So you will have Saturday and Sunday only once each.
I have an index.php with various SQL queries which helps me find the balances of the respective accounts. This is a TRIAL BALANCE so I need to SUM all the amount in the Debit Column and SUM all the amount in the Credit Column so that I can Tally both of them. Please help me with the SUM. All values are echoed to a PHP table.
index.php
<?php
echo "<table border='1'>";
echo "<tr><th width='150'>Account</th><th width='200'>Debit</th><th width='200'>Credit</th></tr>";
echo "<tr><th align='left'>Cash & Bank</th></tr>";
//List the Banks first with their respective balances.
$bank_sql = "SELECT * FROM bank";
$bank_query = mysqli_query($conn, $bank_sql);
while ($bank_result = mysqli_fetch_array($bank_query)){
$bank_name = $bank_result['name'];
$sql= "SELECT SUM(amount) FROM account WHERE (mode='$bank_name' AND status='completed') AND (type='p' OR type='r')";
$sql_query = mysqli_query($conn, $sql);
while($sql_result = mysqli_fetch_array($sql_query)){
$sql_value = $sql_result['SUM(amount)'];
}
$sql1 = "SELECT SUM(amount) FROM account WHERE (mode='$bank_name' AND status='completed') AND (type='s' OR type='pa')";
$sql1_query = mysqli_query($conn, $sql1);
while($sql1_result = mysqli_fetch_array($sql1_query)){
$sql1_value = $sql1_result['SUM(amount)'];
}
echo "<tr><td>".$bank_name."</td>";
if ($sql_value > $sql1_value){
echo "<td align='right'>AED " . number_format(($sql_value - $sql1_value),2) . "</td><td> </td>";
}
elseif ($sql1_value > $sql_value) {
echo "<td> </td><td align='right'>AED " . number_format(($sql1_value - $sql_value),2) . "</td>";
}
else {
echo "<td> </td><td> </td>";
}
}
echo "</tr></table>";
}
?>
Try the below query
SELECT SUM(amount) FROM ht_account WHERE mode='$bank_name' AND status='completed' AND type IN('sale','purchase','reciept','payment')
A quick way to do it would be.
$sql3="select column1,column2 from yourtable where ..condition";
$sumofcolumn1;
$sumofcolumn2;
while($sql3_result = mysqli_fetch_array($sql3))
{
$sumofcolumn2+=$sql3_result['column2'];
$sumofcolumn1+=$sql3_result['column1'];
}
echo $sumofcolumn1.' '.$sumofcolumn2
I have had a long road to get to this last question. Everything is my code is working now, but I can't get this last little issue. Right now I have:
$sql = "SELECT phonenumber,email, dataplan AS currentplan, SUM(datamb) AS
value_sum FROM maindata GROUP BY phonenumber, dataplan";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$val = $row["value_sum"];
$plan = $row["currentplan"];
$remain = $plan - $val;
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
It only subtracts the first value as opposed to the values for all. displayed like this:
while ($row = mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td>".$row['phonenumber'] . "</td> ";
echo "<td>".$row['currentplan'] . "</td> ";
echo "<td>".ROUND ($row["value_sum"],2) . "MB</td> ";
echo "<td>".$remain . " MB</td> ";
echo "<td>".$row['email'] . "</td></tr>";
}
So my goal is to subtract all value_sums from all dataplans, but what I have now, gives me the first value for all columns. Thank you!
mysql_fetch_assoc() will always get one row. You can use it in loop, or better use PDO, eg. like this:
$sql = "SELECT phonenumber,email, dataplan AS currentplan, SUM(datamb) AS
value_sum FROM maindata GROUP BY phonenumber, dataplan";
$results = $pdo->query($sql);
You can read about creating PDO connections here http://www.php.net/manual/en/book.pdo.php
Hi I have trying to learn php by writing little web app for showing me sales data. I have got a query which i now works as i have tested it but i want it to echo the datematched and the number of rows/results found with that date. This is what I have so far
<?php
$con=mysqli_connect("host","user","password","database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM matched WHERE datematched IN (
SELECT datematched FROM matched GROUP BY datematched HAVING count(*) > 1");
while($row = mysqli_fetch_array($result))
{
echo "['";
echo "" . $date['datematched'] . "', ";
echo "" . $num_rows . "],";
}
mysqli_close($con);
?>
I know i am doing something wrong here. ryan
EDIT:
<?php
$con=mysqli_connect("host","user","password","database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM matched WHERE datematched IN (
SELECT datematched FROM matched GROUP BY datematched HAVING count(*) > 1");
echo "['";
echo " 16/08/2013 ', ";
echo "12345}],";
mysqli_close($con);
?>
Okay i have just checked my echo and they work i put in some data so all i need is to find a way of getting the information of the datematched that has been found and then the number of rows that has been found with that. Thanks Ryan
first of all you need to make an adjustment to your query, so that it has the number of rows your expecting.
$result = mysqli_query($con,"SELECT datematched, COUNT(*) as num_rows "
. "FROM matched GROUP BY datematched HAVING num_rows > 0");
then you can display the data as follows
while($row = mysqli_fetch_array($result))
{
echo $row['datematched'] . ",";
echo $row['num_rows'];
}
if your sql query is perfect then you should write like this wayt
while($row = mysqli_fetch_array($result))
{
echo "['";
echo "" . $row['datematched'] . "', ";
echo "" . $row['num_rows'] . "', ";
}
please set your column as you got in your mysql query.
<?php
$query=mysqli_query($con,"SELECT datematched FROM matched GROUP BY datematched");
$num=mysqli_num_rows($query);
if($num>1)
{
$result = mysqli_query($con,"SELECT * FROM matched");
$num_rows=mysqli_num_rows($result);
while($row = mysqli_fetch_array($result))
{
echo '['; echo $row['datematched']; echo $num_rows; echo ']';
}
}
Im trying to display the total for each project group. The Problem is that all the values for that field are recorded like so:
0h 06m
The sum is displaying as:
0
with the following code. How do i display the total result in proper HH:MM format
<?
$query = "SELECT taskname, SUM(tasktime) FROM tictoc WHERE uid = '6' GROUP BY taskname";
$result = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result)) {
echo "<br />";
echo "Total ". $row['taskname']. " = <strong>". $row['SUM(tasktime)']."</strong>";
echo "<br />";
}
?>
If tasktime is a timestamp data-type, then you can use DATE_FORMAT(tasktime,'%Hh %im').