sum all value keep same id - php

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

Related

Loop from array not executed

I am working on an android app that should create some records on a MySQL database.
This is the PHP file that receives the POST values from the app.
As you may see, there are three arrays that collect specific POST.
The first and second loops are working fine and executing the related guardar_post_media() function.
But the third loop is not executed and there are real values on the variables inside the third array.
May be there is something wrong that I canĀ“t detect, may be you can.
<?php
require_once '../mis_php_functions/funciones_basicas.php';
if($_SERVER['REQUEST_METHOD']=='POST'){
$val39 = $_POST['val39'];
$val40 = $_POST['val40'];
$val46 = $_POST['val46'];
$val48 = $_POST['val48'];
$val50 = $_POST['val50'];
$val52 = $_POST['val52'];
$val54 = $_POST['val54'];
$val56 = $_POST['val56'];
$val58 = $_POST['val58'];
$val60 = $_POST['val60'];
$val62 = $_POST['val62'];
$val64 = $_POST['val64'];
$val65 = $_POST['val65'];
$val67 = $_POST['val67'];
$val69 = $_POST['val69'];
$val71 = $_POST['val71'];
$val73 = $_POST['val73'];
$val75 = $_POST['val75'];
$val77 = $_POST['val77'];
$val79 = $_POST['val79'];
$val81 = $_POST['val81'];
$val82 = $_POST['val82'];
$val83 = $_POST['val83'];
$val84 = $_POST['val84'];
$val85 = $_POST['val85'];
$val86 = $_POST['val86'];
$val87 = $_POST['val87'];
$val88 = $_POST['val88'];
$val89 = $_POST['val89'];
$val100 = $_POST['val100'];
$val101 = $_POST['val101'];
$val102 = $_POST['val102'];
$val103 = $_POST['val103'];
$val104 = $_POST['val104'];
$status = 1;
$post = guardar_post($val40,$val39,$val100,$val102,$status,$val103);
if ($post != false) {
$fotos = array($val48,$val50,$val52,$val54,$val56,$val58,$val60,$val62,$val64);
$arrayLength = count($fotos);
echo "Numero de fotos ".$arrayLength;
$i = 0;
while ($i < $arrayLength)
{
if ($fotos[$i] == 0){
}
else{
guardar_post_media(1,$fotos[$i],$val102,$val100,$post);
}
echo "<br />".$fotos[$i] ."<br />";
$i++;
}
$videos = array($val67,$val69,$val71,$val73,$val75,$val77,$val79,$val81,$val83);
$arrayLength2 = count($videos);
echo "Numero de videos ".$arrayLength2;
$i = 0;
while ($i < $arrayLength2)
{
if ($videos[$i] == 0){
}
else{
guardar_post_media(2,$videos[$i],$val102,$val100,$post);
}
echo "<br />".$videos[$i] ."<br />";
$i++;
}
$youtube = array($val85,$val86,$val87,$val88,$val89);
$arrayLength3 = count($youtube);
echo "Numero de youtube ".$arrayLength3;
$i = 0;
while ($i < $arrayLength3)
{
if ($youtube[$i] == 0){
}
else{
guardar_post_media(3,$youtube[$i],$val102,$val100,$post);
}
echo "<br />".$youtube[$i] ."<br />";
$i++;
}
sendMessageNuevoPost($val39,$val102,$val103,$val104); // envio de push
}
else{
echo 'error';
}
}
?>
You have:
if ($youtube[$i] == 0){
}
else {
}
But POST vars are all strings. Change to:
if ($youtube[$i] == "0"){
}
else {
}
In otherwords, an equality on a string to numerical 0 will be true in your cases. And thus your else never executes.
*** Edit. PROOF
$test1 = "filename.dat";
$test2 = "2939";
$test3 = "some useful data";
$test4 = "0";
if ($test1 == 0) {
// Dont do anything
}
else {
echo "Do Work 1.";
}
if ($test2 == 0) {
// Dont do anything
}
else {
echo "Do Work 2.";
}
if ($test3 == 0) {
// Dont do anything
}
else {
echo "Do Work 3.";
}
if ($test4 == 0) {
// Dont do anything
}
else {
echo "Do Work 4.";
}
Only echoes out Do Work 2.. All other echo()s are not run because the equality of a string to a number 0 will return true except in those cases where the string also is numerical, and then the interpreter will compare the numerical values.
As how this relates to the OP's question: It can be inferred that the OP's POST vars must contain some non-numerical data because the OP insists that the 3rd array is populated:
But the third loop is not executed and there are real values on the variables inside the third array.
I will add likely the loop is executed, but not giving the expected the results due the reasons so mentioned.
Tested on PHP 5 and 7.

PHP PDO Pagination not refreshed

im quite new to PHP and im trying to make a pagination, the code works! but i need to click on the button twice to make the page refreshed/change. how do i fix this?
view/main/user/userlist.php
<?php
$_SESSION['pagelim'] = 10;
$_SESSION['page'] = $_GET['halaman'];
if ($_SESSION['page'] == '') {
$_SESSION['pos'] = 0;
$_SESSION['page'] = 1;
} else {
$_SESSION['pos'] = ($_SESSION['page']- 1) * $_SESSION['pagelim'];
}
$itemcount = listpetugas::getlisted();
$pagecount = ceil(listpetugas::getlisted()/$_SESSION['pagelim']);
for ($i=1;$i<=$pagecount;$i++)
{ ?>
<?php
if ($i!=$pagecount){
echo " <ul class='pagination'><li><a href='?controller=main&action=userlist&halaman=$i' onclick='myFunction()'>$i</a></li></ul>";?>
<?php }
else{
echo "$i";
} ?>
<?php } ?>
model/userdb.php
public static function listemup()
{
if ($_SESSION['pos'] =='' && $_SESSION['pagelim'])
{
$pos = 0;
$lim = 10;
}
else {
$pos = $_SESSION['pos'];
$lim = $_SESSION['pagelim'];
}
$list = [];
$db = FirstFire::StartConnection();
$req = $db->query("SELECT * FROM randomity LIMIT $pos,$lim");
$rowcount = $req->rowCount();
foreach ($req->fetchAll() as $post) {
$list[] = new listpetugas($post['name'],$post['email'],$post['adress'],$post['wow']);
}
return $list;
}
JS
<script>
function myFunction() {
location.reload();
}
</script>
sorry for the messy code.

search every word in string with mysql and php

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);
}

How to get the sum from the loop? (php)

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;

set maximum of items that you can make in function

How can I say in/with the follow (see below) function that you can make max 3 items? And if you will make more than three items the function will stop and you get a warning?
function addSection() {
global $compid;
$sectionOb = new Item();
$sectionOb->i_id_pk = $sectionOb->newId();
$sectionOb->i_mod_comp_id_fk = $compid;
$sectionOb->c_titel = '';
$sectionOb->c_content = '';
$sectionOb->i_sort = $sectionOb->newSort($compid);
$sectionOb->insert();
}
if($action == 'add') {
addSection();
}
<a href="<?php echo $_SERVER['REQUEST_URI'] ?>&action=add" />new section</a>
Use a static counter variable:
function limited() {
static $invocationcount = 0;
++$invocationcount;
if($invocationcount <= 3) {
echo "You have called this function $invocationcount times.";
}
else {
echo "Stop doing that!";
}
}
See it in action.
If you are not too concerned about URL spoofing, I would add a counter variable in the URL which is passed to the addSection() method like so:
function addSection($count) {
if ($count >= 3) { return $count; }
global $compid;
$sectionOb = new Item();
$sectionOb->i_id_pk = $sectionOb->newId();
$sectionOb->i_mod_comp_id_fk = $compid;
$sectionOb->c_titel = '';
$sectionOb->c_content = '';
$sectionOb->i_sort = $sectionOb->newSort($compid);
$sectionOb->insert();
// Return incremented count
return $count + 1;
}
// Retrieve the last count from the URL
$count = isset($_GET['count']) ? intval($_GET['count']) : 0;
// Increment the count if the action is add and the addSection method suceedes
if($action == 'add') {
$count = addSection($count);
}
// Add count to the URL so we know what it is
<a href="<?php echo $_SERVER['REQUEST_URI'] ?>&action=add&count=<?php echo $count; ?>" />new section</a>
Thanks a lot for help!! I have do it as follow:
function limit() {
global $compid;
$i = 0;
++$i;
$a_all = page1::page2Id($compid);
$i = sizeof($a_all);
if($i <= 2) {
$sectionOb = section();
$sectionOb->i_id_pk = $sectionOb->newId();
$sectionOb->i_mod_comp_id_fk = $compid;
$sectionOb->c_titel = '';
$sectionOb->c_content = '';
$sectionOb->i_sort = $sectionOb->newSort($compid);
$sectionOb->insert();
return true;
}
else {
return false;
}
}

Categories