I'm trying to get the total time duration of all time variables from the database. But I don't seem to get the right total or result. This is my code...
$len = count($mids);
foreach($mids as $key => $val)
{
$result=mysql_query("SELECT * FROM tbltempmovies WHERE mid='".$val."'");
while($row=mysql_fetch_array($result))
{
$dur += strtotime($row['duration']);
}
}
$duration = date('g:i:s', $dur);
PS: I know I'm not using mysqli or PDO. I'll fix that once I'm done with this.
try this
$duration = date('g:i:s', strtotime($dur));
Related
I am a novice developer running a server.
Recently, I am running a server using php and mysql.
However, the server keeps hitting and checking, and mysql is eating all the cpu.
I think it is a problem to put the sql query in the loop statement because of poor skill.
I looked around, but it couldn't be solved, but I saw a post that told me to solve it with a join statement rather than repeatedly calling the sql statement.
If you think about it, it is definitely a problem by repeatedly calling the sql statement, so I think it will improve if it can be processed as a single query.
But I don't know how to change it right now...
I am using a code similar to the one below, not the code that is actually used. Please help me on how to change this.
It's a little embarrassing to use this code...
I want to print the sum.
I have 24 hours worth of data.
This code takes the number of records in 1 minute and adds up the value divided by 100.
$sum = 0;
for ($i = 0; $i < 1440; $i++){
$time1 = date("Y/m/d H:i",(strtotime($basetime) - 72000 + ($i * 60))).":00";
$time2 = date("Y/m/d H:i",(strtotime($basetime) - 60 + ($i * 60))).":59";
$sql = "select count(*) as cnt from mytable where start_time between '$time1' and '$time2'";
$query_result = mysqli_query($sql);
$sum += 100 / $query_result['cnt']);
}
echo $sum;
$sql = "select count(*) as cnt from mining where start_time between '$time1' and '$time2'";
$query_result = sql_fetch($sql);
$sum = 0;
for ($i = 0; $i < 1440; $i++){
$time1 = date("Y/m/d H:i",(strtotime($basetime) - 72000 + ($i * 60))).":00";
$time2 = date("Y/m/d H:i",(strtotime($basetime) - 60 + ($i * 60))).":59";
$sum += $query_result['cnt']);
}
I am trying to update all of the rows in a table in my database with a different value. I am trying to while loop the query to do this. Here's what I have...
<?php
$rand = rand(100,150000);
$start = 1;
$start += $start;
$start2 = $start +1;
echo $start;
while($start =< 686) {
echo "UPDATE table_video SET total_view = $rand WHERE id BETWEEN $start AND $start2;";
};
?>
I am sure most of you should be able to look at my code and understand what I am trying to accomplish. I would like the assistance. Thank You very much!
Use the mySQL RAND() function, min and max your range for the random values. I belive on this case are (100,150000)
UPDATE table_video
SET total_view = ROUND( (RAND() * (max-min)) +min )
WHERE id BETWEEN 1 AND 686;
Sorry if this has been asked before however I am having trouble finding the answer to my problem.
I am trying to build a calendar system and schedule system for my web application in PHP and having difficulty with one particular area.
I have a "for" statement where it will draw up the times of the day starting at 12:00AM and finishing at 11:30PM
Inside this for loop, I have a foreach which i want to echo out the objects in an array that match a particular time.
Everything I have tried which includes using for,while and foreach statements don't show what I am after which is the events lining up next to the time.
here is my code
<?php
$tStart = strtotime($start_time);
$tEnd = strtotime($end_time);
$tNow = $tStart;
while($items = mysql_fetch_object($result)){
$events[] = $items;
}
for($tNow=$tStart; $tNow<$tEnd; $tNow=strtotime('+30 minutes',$tNow)){
// Time to color the rows to make it easier to read
if(!isset($day_row)){
$day_row = "0";
}
if(isset($day_row) && $day_row >= "2"){
$day_row--;
}
else{ $day_row++;
}
//This bit draws the first column.
echo "<tr><td class=\"day_row".$day_row."\" width=\"70px\">".date("h:i A",$tNow)."</td>";
// MySQL stuff is now here
foreach($events as $e => $item){
if($item->apnt_start == $tnow){
$rowspan = ((strtotime($item->apnt_finish)-strtotime($item->apnt_start))/"1800");
echo "<td class=\"day_row_apnt\" rowspan=\"$rowspan\">".$item->apnt_start."-".$item->apnt_finish." ".$item->apnt_brief."</td></tr>";
}
}
}
?>
at present i am given a page with
12:00 AM
12:30 AM
01:00 AM
01:30 AM
02:00 AM
02:30 AM
03:00 AM
03:30 AM
04:00 AM
04:30 AM
05:00 AM
05:30 AM
Next to the time I want the appointment with matching time.
I am trying to achieve something similar to http://mrbs.sourceforge.net/
I can't use their system however as I can't integrate it properly and I have tried looking at their code and it seems to be pointing at many files and i am having trouble trying to understand the function i am after.
Please let me know if this is not clear enough and will try to explain further.
You need to define and set value for variables below:
$start_time = "09:00 AM";
$end_time = "11:30 PM";
Also you need to add query and database connection (above while($items = mysql_fetch_object($result)){ statement):
mysql_connect("hostname", "user", "password");
mysql_select_db("mydb");
$result = mysql_query("select * from mytable");
EDIT:
You should use while mysql_fetch_assoc instead of mysql_fetch_object.
Replace
while($items = mysql_fetch_object($result)){
with
while($items = mysql_fetch_assoc($result)){
Delete: $events[] = $items;
Ensure your while statement above ends after all code is executed (code you listed in your question) - closing bracket }.
Okay, this should work for you.
<?php
$tStart = strtotime($start_time);
$tEnd = strtotime($end_time);
$tNow = $tStart;
echo '<table>';
while($events = mysql_fetch_assoc($result)){
for($tNow=$tStart; $tNow<$tEnd; $tNow=strtotime('+30 minutes',$tNow)){
// Time to color the rows to make it easier to read
if(!isset($day_row)){
$day_row = "0";
}
if(isset($day_row) && $day_row >= "2"){
$day_row--;
}
else{ $day_row++;
}
//This bit draws the first column.
echo "<tr><td class=\"day_row".$day_row."\" width=\"70px\">".date("h:i A",$tNow)."</td>";
if(strtotime($events['apnt_start']) == $tNow) {
$rowspan = ((strtotime($events->apnt_finish)-strtotime($events->apnt_start))/"1800");
echo "<td class=\"day_row_apnt\" rowspan=\"$rowspan\">".$events->apnt_start."-".$events->apnt_finish." ".$events->apnt_brief."</td></tr>";
}
} //end for
} //end while
echo '</table>';
?>
I would like to thank everyone that provided advice on this issue, I have finally got it working with added another variable and using a method of storing an array in an array.
The final code looks like
<?php
$tStart = strtotime($start_time);
$tEnd = strtotime($end_time);
$tNow = $tStart;
$events = array();
$eas = "0"; // eas stands for Event Array Start. this will be used to cycle through the events in the array.
while($items = mysql_fetch_assoc($result)){
$events[] = $items;
}
for($tNow=$tStart; $tNow<$tEnd; $tNow=strtotime('+30 minutes',$tNow)){
// Time to color the rows to make it easier to read
if(!isset($day_row)){
$day_row = "0";
}
if(isset($day_row) && $day_row >= "2"){
$day_row--;
}
else{ $day_row++;
}
//This bit draws the first column.
echo "<tr><td class=\"day_row".$day_row."\" width=\"70px\">".date("h:i A",$tNow)."</td>";
if(strtotime($events[$eas]['apnt_start']) == $tNow) {
$rowspan = ((strtotime($events[$eas]['apnt_finish'])-strtotime($events[$eas]['apnt_start']))/"1800");
echo "<td class=\"day_row_apnt\" rowspan=\"$rowspan\" >".$events[$eas]['apnt_start']."-".$events[$eas]['apnt_finish']." ".$events[$eas]['apnt_brief']."</td></tr>";
$eas++;
}
else{
echo "<td class=\"day_row".$day_row."\"></td>";
}
} //end for
?>
By using the variable $eas I was able to then control which number it would start at by setting it to 0 initially and then when it found an entry with a matching time it went through the if statement where at the end of the if statement it was given $eas++ to increment.
This then proved that if there was no appointment the $eas would not run and it would not increment thus remaining on the last incremented $eas.
Thanks again for everyone's help.
<?php
// initial value
$week = 50;
$year = 2001;
$store = array();
do
{
$week++;
$result = "$week/$year";
array_push($store,$result);
if($week == 53){
$week = 0;
$year++;//increment year by 1
}
continue;
}
// End of Loop
while ($result !== "2/2002");
?>
print_r($store);
result want return will be
array("51/2001", "52/2001", "01/2002", "02/2002");
What is my problems by using while using do..while ,continue?
Your arguments to array_push are the wrong way around. Read the manual entry for functions you use. Turn on warnings on your server; running this in codepad showed me the problem immediately. [Edit: You have now quietly fixed that in your question.]
You also have a typo: $i instead of $week.
Finally, you test against "02/2002", but for that month the string will be "2/2002".
Fixed code (live demo):
<?php
// initial value
$week = 50;
$year = 2001;
$store = array();
do
{
$week++;
$result = "$week/$year";
array_push($store, $result);
if($week == 53){
$week = 0;
$year++;//increment year by 1
}
continue;
}
// End of Loop
while ($result !== "2/2002");
?>
In general, I'd recommend against loops like this. As you've discovered, your code is very fragile because you're testing for just one very specific value, and if that value is not precisely correct you get an infinite loop.
Instead, consider comparing $week and $year separately and numerically:
while ($week < 2 && $year <= 2002)
Next time please include in your question the output that you are seeing, as well as the output that you want to see. It'll save us time in reproducing your problem.
I may not be understanding this correctly... If you could explain a bit more that'd help.
Try turning the loop into a function, and turn the while(..) to check the functions variable.
then just call it 4 times to fill your array.
I'm trying to do something relatively simple here. Basically I have a table with a bunch of rows in it marked with a timestamp (format: 2009-05-30 00:14:57).
What I'm wanting to is do is a query which pulls out all of the rows, and splits them by the month so I'm left with a final result like:
February
rowID name order date
rowID name order date
rowID name order date
January
rowID name order date
rowID name order date
rowID name order date
etc.
I have a few vague ideas how to do this - they just seem long winded.
One of the ways would be to do a query for each month. I'd derive what the current month is in PHP then construct a for() which goes back a certain number of months.
like:
$currentmonth = 8;
$last6months = $currentmonth - 6;
for($i = $currentmonth; $i == $last6months; $i--) {
$sql = 'SELECT * FROM reports WHERE MONTH(reports.when) = $currentmonth ';
$res = mysql_query($sql);
// something would go here to convert the month numeral into a month name $currentmonthname
echo $currentmonthname;
while($row = mysql_fetch_array($res)) {
// print out the rows for this month here
}
}
Is there a better way to do this?
It's better to fetch all data once,ordered by month..
Then while fetching with php you can store your current month in a variable (for example $curMonth) and if there is a change in the month, you echo "New Month"...
Executing a query is slow, it's better to minimize your "conversations" with the db..
Don't forget that you have to deal with years aswell. If you have two records, one for January '09 and one for January '08, your results may be skewed.
Best to follow Svetlozar's advice and fetch all data at once. Once you have it in memory, use PHP to segment it into something usefull:
$monthData = array();
$queryResult = mysql_query("
SELECT
*,
DATE_FORMAT('%m-%Y', when) AS monthID
FROM
reports
WHERE
YEAR(when) = 2009 AND
MONTH(when) BETWEEN 5 and 11
");
while ($row = mysql_fetch_assoc($queryResult))
{
if (!isset($monthData[$row['monthID']]))
$monthData[$row['monthID']] = array();
$monthData[$row['monthID']][] = $row;
}
mysql_free_result($queryResult);
foreach($monthData as $monthID => $rows)
{
echo '<h2>Data for ', $monthID, '</h2>';
echo '<ul>';
foreach($rows as $row)
{
echo '<li>', $row['someColumn'], '</li>';
}
echo '</ul>';
}
You could change your SQL query to get your entire report. This is much more efficient than querying the database in a loop.
select
monthname(reports.when) as currentmonth,
other,
fields,
go,
here
from reports
order by
reports.when asc
You could then use this loop to created a nested report:
var $currentMonth = '';
while($row = mysql_fetch_array($res)) {
if($currentMonth !== $row['currentMonth']) {
$currentMonth = $row['currentMonth']);
echo('Month: ' . $currentMonth);
}
//Display report detail for month here
}
*Note: Untested, but you get the general gist of it I'm sure.
This is the SQL script:
SELECT*, DATE_FORMAT(fieldname,'%Y-%m') AS report FROM bukukecil_soval WHERE MONTH(fieldname) = 11 AND YEAR(fieldname)=2011
I hope you know where you should put this code :D