Can anybody help me to get the sum or total of the $total_h in the below loop?
I just need to display the sum of $total_h after the loop has finished so I'll know how many hours I've worked.
<?php
$get_time = mysql_query("select * from tbl_timekeep where emp_id =
'OJT0125' order by date desc");
while ($fect_time = mysql_fetch_array($get_time))
{
if ($fect_time[3] == '----' || $fect_time[3] == 'No Out')
{
$out = '17:00:00';
}
else
{
$out = $fect_time[3];
}
$from = new DateTime($fect_time[2]);
$to = new DateTime($out);
$total_h = $from->diff($to)->format('%h.%i');``
echo $fect_time[2].'to'.$fect_time[3]." = [ $total_h ]<br>";
}
?>
<?php
$get_time = mysql_query("select * from tbl_timekeep where emp_id =
'OJT0125' order by date desc");
$sum_total_h = 0;
while ($fect_time = mysql_fetch_array($get_time))
{
if ($fect_time[3] == '----' || $fect_time[3] == 'No Out')
{
$out = '17:00:00';
}
else
{
$out = $fect_time[3];
}
$from = new DateTime($fect_time[2]);
$to = new DateTime($out);
$total_h = $from->diff($to)->format('%h.%i');``
$sum_total_h +=$total_h;
echo $fect_time[2].'to'.$fect_time[3]." = [ $total_h ]<br>";
}
echo $sum_total_h;
?>
add a code to sum up the $total_h;
$sum_total_h = 0;//initialize
then after you get the $total_h. you have to add this code.
$sum_total_h +=$total_h;
Then after the loop is done you can echo the sum of $total_h
echo $sum_total_h;
Related
I have a block of PHP code where I'm using two counters (one specifically for a particular key of featured if the value is 1.
So far, the counter works by putting any records where featured == 1 in $cnt1 and everything else in $cnt2. However, this doesn't do anything for my sorting.
How can I make this so that the final array puts all featured ($cnt1) records at the top of the array/the first results?
if(count($myresults)>0){
foreach ($myresults as $key => $value) {
$id = $value->ID;
if(get_post_meta($id,'_awpcp_extra_field[36]',true) !='' && get_post_meta($id,'_awpcp_extra_field[37]',true) != ''){
$lat = get_post_meta($id,'_awpcp_extra_field[36]',true);
$lon = get_post_meta($id,'_awpcp_extra_field[37]',true);
$area = get_post_meta($id,'_awpcp_extra_field[38]',true);
$amnt = get_post_meta($id,'_awpcp_price',true)/100;
$ttle = get_the_title($id);
$link = get_permalink($id);
$desc = get_the_excerpt($id);
$date = get_the_time('m/d/Y',$id);
$is_featured = get_post_meta($id,'_awpcp_is_featured',true);
$imgpath = get_the_post_thumbnail_url($id,'dailymag-featured');
$counter_name = ($is_featured == 1) ? 'cnt1' : 'cnt2';
$ress[$$counter_name]['featured'] = $is_featured;
$ress[$$counter_name]['lat'] = $lat;
$ress[$$counter_name]['lon'] = $lon;
$ress[$$counter_name]['area'] = $area;
$ress[$$counter_name]['amnt'] = $amnt;
$ress[$$counter_name]['ttle'] = $ttle;
$ress[$$counter_name]['link'] = $link;
$ress[$$counter_name]['desc'] = $desc;
$ress[$$counter_name]['date'] = $date;
$ress[$$counter_name]['count'] = $counter_name;
if($imgpath){
$ress[$$counter_name]['imgs'] = $imgpath;
}else{
$ress[$$counter_name]['imgs'] = '/wp-content/uploads/2022/03/fairfax-4670d9c9.jpeg';
}
$$counter_name++;
}
}
I have following:
if($broj_podstanica != "" && $broj_podstanica != 0) {
$uzmi_podstanice = "SELECT * FROM objekt WHERE vrsta_objekta = '2' ORDER BY sifra ASC LIMIT $broj_podstanica";
$pronasao_sve_podstanice = $db->query($uzmi_podstanice);
while($sifrePodstanica = $pronasao_sve_podstanice->fetch_assoc()) {
$sifreIzbrojane = $sifrePodstanica['sifra'] . ",";
$izbaci_zarez = explode(",", $sifreIzbrojane);
if (!isset($izbaci_zarez[0])) {
$izbaci_zarez[0] = "";
$pods0 = "";
} else {
$pods0 = $izbaci_zarez[0];
}
if (!isset($izbaci_zarez[1])) {
$izbaci_zarez[1] = "";
$pods1 = "";
} else {
$pods1 = $izbaci_zarez[1];
}
if (!isset($izbaci_zarez[2])) {
$izbaci_zarez[2] = "";
$pods2 = "";
} else {
$pods2 = $izbaci_zarez[2];
}
}
echo "1:" . $pods0;
echo "2:" . $pods1;
echo "3:" . $pods2;
echo "4:" . $pods3;
echo "5:" . $pods4;
}
Query gives me results: 30313233.
After while loop I tried to control variables $pods0, $pods1, $pods2, $pods3 and $pods4 but It gives me result for first variable only; $pods0 is 30..
Is it possible to get other values from variables?
You have some problems with your code, first of all, you do not need use isset() function, it always returns true because of the variable is exists. Secondly, if $izbaci_zarez[1] is empty you do not need to set it again with an empty value. The last thing, store the data in an array instead of a variable and it will not limit your variables count (Because of it will hard to follow) and set the values to their correct index.
if($broj_podstanica != "" && $broj_podstanica != 0) {
$uzmi_podstanice = "SELECT * FROM objekt WHERE vrsta_objekta = '2' ORDER BY sifra ASC LIMIT $broj_podstanica";
$pronasao_sve_podstanice = $db->query($uzmi_podstanice);
$i = 1;
$pods = array();
while($sifrePodstanica = $pronasao_sve_podstanice->fetch_assoc()) {
$sifreIzbrojane = $sifrePodstanica['sifra'] . ",";
$izbaci_zarez = explode(",", $sifreIzbrojane);
$pods[$i] = $izbaci_zarez[0];
$i++;
}
echo "1:" . $pods[1];
echo "2:" . $pods[2];
echo "3:" . $pods[3];
echo "4:" . $pods[4];
echo "5:" . $pods[5];
// and etc...
}
My problem is I have some value using explode, so if I want display value in database using explode, I must use foreach. The problem is I don't want all showing up, because I want sum all the value to keep same id.
$customer_sql_query = "SELECT * FROM winner_delivery ";
$customer_sql_result = mysqli_query($connection,$customer_sql_query);
while ($customer = mysqli_fetch_array($customer_sql_result)){
$ecommercelist = "";
$snacklist = "";
if ($customer['ecommercelist'] != "") {
$ecommercelist = $customer['ecommercelist'];
}
else if ($customer['status'] == "delivered") {
$snacklist = $customer['deliverysnacklist'];
}
else {
$snacklist = $customer['usersnacklist'];
}
if ($ecommercelist){
$ecommercelist_array = explode('~',$ecommercelist);
$i = 0;
foreach ($ecommercelist_array as $ecommercelist_row) {
if ($i % 2 == "1"){
$alternatecolor = "#EEEEEE";
}
else {
$alternatecolor = "#FFFFFF";
}
$selection_array = explode('|',$ecommercelist_row);
$selection_entry = $selection_array[0];
$temp_qty = $selection_array[1];
$temp_producttitle = $product_ob[$selection_entry]['producttitle'];
$temp_imageurl = $product_ob[$selection_entry]['imageurl'];
$temp_weighttitle = $product_ob[$selection_entry]['weighttitle'];
$temp_productid = $product_ob[$selection_entry]['productid'];
$count=$count_qty[$selection_entry] += $temp_qty;
$i++;
?>
<li>
<div class="picture"><?=$temp_productid ?></div>
<div class="weighttitle" nowrap>x<?=$count ?></div>
</li>
<?
}
}
}
Actually I have many time adjust to make better, but is the result!
btw this my database for ecommerlist
I need to search every word of an string in the database. If an word exist it needs to be highlighted. The current script works, but needs a lot of memory for my server. I don't know how i make it easier, but maybe do you?
<?php
$total_messages = $_POST['total'] - 1;
for($x = 0; $x <= $total_messages; $x++)
{
//Search highlights
$result = $mysqli->query("SELECT * FROM highlights WHERE enabled=1")
while($row = $result->fetch_assoc())
{
//Vars for highlights
$highlight_txt = $row['value'];
$highlight_type = $row['type'];
$highlight_color = "black";
$highlight_title = null;
//If the text isnt empty
if($highlight_txt != null || $highlight_txt != "")
{
//Type highlights
if($highlight_type == "tree") //Tree type
{
$highlight_type = "18"; //Category number
$highlight_background = "pink"; //Background
if($row['option1'] != null)
{
$highlight_title = htmlentities($row['option1']);
}
}
else
{
$highlight_background = "yellow"; //Background
}
//Add highlight
$message = preg_replace("/\b($highlight_txt)\b/i", "<span class='bc_highlight' highlight-type='$highlight_type' highlight-value='$highlight_txt' style='background: $highlight_background; color: $highlight_color;' title=''>$highlight_txt</span>", $message);
}
}
echo $message; //Display the message
}
Why not doing something like this?
$messages = ["This is my message1","This is my message2"];
$highlights = [];
$result = $mysqli->query("SELECT * FROM highlights WHERE enabled=1")
while($row = $result->fetch_assoc()) {
$highlights[$row["value"]] = [
"type" => $row["type"],
"color" => $row["color"]
];
}
$callback = function($matches) use ($highlights) {
$word = $matches[1];
if(isset($highlights[$word])) {
$highlight = $highlights[$word];
return sprintf('<span style="color:%s">%s</span>',$highlight["color"],$word);
} else {
return $word;
}
};
foreach($messages as &$message) {
$message = preg_replace_callback("/(\w+)/i",$message,$callback);
}
I´m pretty much entirely new to PHP, so please bear with me.
I´m trying to build a website running on a cms called Core. I'm trying to make it so that the previous/next buttons cycle through tags rather than entries. Tags are stored in a database as core_tags. Each tag has it own tag_id, which is a number. I've tried changing the excisting code for thep previous/next buttons, but it keeps giving me 'Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in /home/core/functions/get_entry.php on line 50'.'
Any help would be greatly appreciated.
Get_entry.php:
<?php
$b = $_SERVER['REQUEST_URI'];
if($entry) {
$b = substr($b,0,strrpos($b,"/")) . "/core/";
$id = $entry;
$isPerma = true;
} else {
$b = substr($b,0,mb_strrpos($b,"/core/")+6);
$id = $_REQUEST["id"];
}
$root = $_SERVER['DOCUMENT_ROOT'] . $b;
$http = "http://" . $_SERVER['HTTP_HOST'] . substr($b,0,strlen($b)-5);
require_once($root . "user/configuration.php");
require_once($root . "themes/".$theme."/configuration.php");
require_once($root . "functions/session.php");
if(is_numeric($id)) {
$type = "entry";
} else {
$type = "page";
}
$id = secure($id);
if($type == "page") {
$data = mysql_query("SELECT p.* FROM core_pages p WHERE p.page_title = \"$id\"");
$page_clicks = 0;
while($p = mysql_fetch_array($data)) {
$url = $p["page_url"];
$path = $root . "user/pages/" . $url;
$page_clicks = $p['hits']+1;
require($path);
}
mysql_query("UPDATE core_pages p SET
p.hits = $page_clicks
WHERE p.page_title = $id");
}
if($type == "entry") {
// queries the dbase
$data_tags = mysql_query("SELECT entry_id,entry_title FROM core_entries WHERE entry_show = 1 ORDER BY entry_position DESC") or die(mysql_error());
$navArr=array();
while($tmparray = mysql_fetch_array($data_entries,MYSQL_ASSOC)){
array_push($navArr,$tmparray['entry_id']);
}
function array_next_previous($array, $value) {
$index = array_search($value,$array);
//if user clicked to view the very first entry
if($value == reset($array)){
$return['prev'] = end($array);
$return['next'] = $array[$index + 1];
//if user clicked to view the very last entry
}else if($value == end($array)){
$return['prev'] = $array[$index - 1];
reset($array);
$return['next'] = current($array);
}else{
$return['next'] = $array[$index + 1];
$return['prev'] = $array[$index - 1];
}
return $return;
}
$data = mysql_query("SELECT e.* FROM core_entries e WHERE e.entry_id = $id AND e.entry_show = 1");
$entry_clicks = 0;
if(#mysql_num_rows($data) < 1) {
die("Invalid id, no entry to be shown");
}
while($e = mysql_fetch_array($data)) {
$nextPrevProject = array_next_previous($navArr,$id);
$entry_id = $e['entry_id'];
$entry_title = $e['entry_title'];
// DATE
$t = $e["entry_date"];
$y = substr($t,0,4);
$m = substr($t,5,2);
$d = substr($t,8,2);
$entry_date = date($date_format,mktime(0,0,0,$m,$d,$y));
$entry_text = $e['entry_text'];
$entry_extra1 = $e['entry_extra1'];
$entry_extra2 = $e['entry_extra2'];
$entry_client = $e['entry_client'];
$entry_position = $e['entry_position'];
$entry_hits = $e['hits']+1;
$entry_new = $e['entry_new'];
if($entry_new == 1) {
$isNew = true;
} else {
$isNew = false;
}
if($nice_permalinks) {
$entry_perma = "$http".$entry_id;
} else {
$entry_perma = "$http"."?entry=$entry_id";
}
$data_e2t = #mysql_query("SELECT e2t.tag_id FROM core_entry2tag e2t WHERE e2t.entry_id = $entry_id");
$tag_str = "";
while($e2t = #mysql_fetch_array($data_e2t)) {
$tag_id = $e2t["tag_id"];
$data_tags = #mysql_query("SELECT t.tag_text FROM core_tags t WHERE t.tag_id = $tag_id");
while($t = #mysql_fetch_array($data_tags)) {
$tag_text = $t["tag_text"];
$tag_str = $tag_str . "<a class=\"tag-link\" name=\"tag".$tag_id."\" href=\"#tag-"._encode($tag_text)."\">".$tag_text."</a>".$separator_tags;
}
}
$entry_tags = substr($tag_str,0,strlen($tag_str)-strlen($separator_tags));
$layout_path = $root . "user/uploads/" . treat_string($entry_title) . "/layout.php";
if(is_file($layout_path) && (#filesize($layout_path) > 0)) {
require($layout_path);
} else {
require($theme_path . "parts/entry.php");
}
}
mysql_query("UPDATE core_entries e SET
e.hits = $entry_hits
WHERE e.entry_id = $id");
}
if($isPerma) {
echo "<a class=\"index-link\" href=\"$http\">back to index</a>";
}
?>
You have not defined $data_entries, before using it here:
while($tmparray = mysql_fetch_array($data_entries,MYSQL_ASSOC)){
array_push($navArr,$tmparray['entry_id']);
}
That is why you get the very descriptive error message.
Did you mean to use $data_tags?
Use: "SELECT p.* FROM core_pages p WHERE p.page_title = '".$id."'
Note: mysql_connect is not sql-injection save. If you use mysql_connect, change to PDO.
$data_entries is not defined on line 50, then mysql_fetch_array return an exception of null value given.
Try to change $tmparray = mysql_fetch_array($data_entries,MYSQL_ASSOC) to $tmparray = mysql_fetch_array($data_tags,MYSQL_ASSOC).
Hope this help!