PHP and Phpmyadmin multiplication - php

I want to hit 2 values with the same id on PhpMyAdmin. According to the codes below, only the aggregation is running in the outputs I receive. I get a value of 0 when I multiply instead of adding.
<?php
$a = 0;
$oran = new sorgu;
$k_id = $kuponlar->veri->id;
$oran->select("*","matches","kid=$k_id");
while($oran->verioku()){
$a = $a+ floatval($oran->veri->oran);
}
echo $a;
?>
Screenshots:
It is necessary to multiply the "oran" values in there. But those with the same "kid" value are at least struck for that table.
That's the code that's running
Action Required:
1,25 x 1,5 x 1,3 x 1,55

Related

Calculating Size Dimensions in PHP

I'm trying to put together a simple calculation.
I have a size dimension of 100 x 100 x 100 which is a box.
I have individual items of 50 x 50 x 50.
If I keep adding items, I want to work out how many boxes is required to hold those items.
For example:
If I have 1 item of (50x50x50) then I will need 1 box to hold that item.
If I have 2 items of (50x50x50) then I will need 1 box to hold those 2 items.
If I have 3 items of (50x50x50) then I will need 2 boxes to hold those 3 items.
Any help would be greatly appreciated.
<?php
function GetItemsInBoxQuantity ($boxsize = array(100, 100,100), $verifiable_item = array(50,50,50)) { # here we pass default values of box size and verifable items
$boxspace = array_product($boxsize);
$verifiable_item_space = array_product($verifiable_item);
$count_boxes_in_boxspace = floor($boxspace / $verifiable_item_space);
return $count_boxes_in_boxspace;
}
# Now we putting new values of box size and size of items
$box = array(200,100,100);
$verifiable_item= array(40,40,40);
$result = GetItemsInBoxQuantity($box, $verifiable_item);
echo 'We can put into box '.$result.' items.';
?>
Output:
We can put into box 31 items.

Challenge: display php array with aggregated result

I am about to bleed with this challenge....anybody can help me with understanding how to solve please. I am missing the logic. I will spare you the details and give you the input variables to work with, which are dynamically generated. The below records are Male, Female, Male, Female....
$data=array(1,5,10,4,3,4,1,1,1,'2);
$indexOfSubTotalColumns=array(3,6,9,12,15,18,21);
//This means on the 3, 6th ...column of the HTML output, a summary is to happen. That is 1+5 will be 6 on column 3. 10 +4 will be 14 on column 6.
$groupTotalColumns=array(9,21);
//the 9th column will display sum of all columns before it. and 21 from col 10 on words, excluding the subtotals calculated or if you want.
With the following code,I have gotten to calculate the subtotals of each but can't put the dynamic grand total. First, add dumb data to the data array so it becomes equal to the number of HTML columns.
$previousValues=array();
foreach($data as $value){
if(in_array($i,$indexOfSubTotalColumns)){
$rows.="<td>".array_sum($previousValues)."</td>";
$rows.="<td>$value</td>";
$previousValues=array();
array_push($previousValues,$value);
$i++;
//
}
else{//not a subtotal
$rows.="<td>$value</td>";
array_push($previousValues,$value);
}
$i++;
//if $i reaches number of columns i.e. 21, close current row and open new
}
echo $rows;
The above code outputs me this layout:
I am failing to calculate the Totals.

Memcached::increment() increases value by 2 in PHP for appengine

The following code prints a number incremented by 2 every time I reload the page.
<?php
//Memcahce
$memcache = new Memcached;
$memcache->increment('hits');
echo "hitcount = ".$memcache->get('hits');
output
hitcount = 2
Expected : It should increment by 1.
I tried setting it to 2 and 5 but still it increments by 4 and 10 respectively.

How to retrieve 1 record at a time from db

I have a table comment as follow:
Comment
comment_id cmt followupid
1 Hello 3
2 hi 4
3 Hey 2
4 wassup 1
My query is that I want to echo "Hello", "hi", "hey" , "Wassup" and other (the record continues) individualy, I have used
$comment = mysql_result($runQuery, $i,"cmt");
echo $comment;
which works fine but the problem is that it echoes all the comments at once, what I want is to echo all the comment but one at a time. the comment are in a div tag such that each the div appears only after 1 second the page is loaded. I want each comment to appear after different time interval
for e.g:
Hello to appear at 5pm (not necessarily the corect time it can be just an int)
hi 5.10 pm
hey 6.30 pm
Please Help!
The following code should give you some hints.
$result = mysql_query($runquery);
while($row=mysql_fetch_assoc($result)){
// $row contains a single row.
echo $row['cmt'], $row['comment_id']
}
Create another variable storing time divisions(or number of rows). So that different output at different time can be fetched. For eg. If your table has 24 rows(or items), then this variable shall have a value 24. Use it to divide you output times(As in 24 hours, each hour a different value).
Now, the PHP part(I am not much familiar with date and time functions in PHP, so you can totally ignore the paragraph above.
$result = mysql_query($runquery);
$j = 0;
$i = rand( 0, mysql_num_rows($result) );
while($row=mysql_fetch_assoc($result)){
// $row contains a single row.
if( $j++ = $i )
echo $row['cmt'], $row['comment_id'];
}
This will fetch one random row from the table, but not depending upon the server time.

Weird jQuery data() issue - Help?

jquery 1.6.2 / Firefox 6.0.1
OK I'm working on this shipment manager interface and when the page is loaded, each table row tr is assigned an id "shipment_XXXXX" where XXXXX is the id of the shipment from the database.
All the data regarding the shipment is, in PHP set to a multidimensional associative array which contains "shipmentItems" and "pkgs" among other things which are irrelevant. The shipmentItems object is a regular numeric array, where each element has several associative values such as "name", "qty", "price" so for example:
$shipmentItems[0]["name"] = "item 1";
$shipmentItems[0]["qty"] = 5;
$shipmentItems[0]["price"] = 20.00;
$shipmentItems[1]["name"] = "item 2";
$shipmentItems[1]["qty"] = 3;
$shipmentItems[1]["price"] = 5.00;
This array indicates all of the items that are part of this shipment in whole.
The other array pkgs is a list of each package in the shipment and each package has a packing_slip object/associative array. Example:
PACKAGE #1
$pkgs[0]['packing_slip'][0]['name'] = "item 1";
$pkgs[0]['packing_slip'][0]['qty'] = 3;
$pkgs[0]['packing_slip'][0]['price'] = 20.00;
$pkgs[0]['packing_slip'][1]['name'] = "item 2";
$pkgs[0]['packing_slip'][1]['qty'] = 1;
$pkgs[0]['packing_slip'][1]['price'] = 50.00;
PACKAGE #2
$pkgs[1]['packing_slip'][0]['name'] = "item 1";
$pkgs[1]['packing_slip'][0]['qty'] = 2;
$pkgs[1]['packing_slip'][0]['price'] = 20.00;
$pkgs[1]['packing_slip'][1]['name'] = "item 2";
$pkgs[1]['packing_slip'][1]['qty'] = 2;
$pkgs[1]['packing_slip'][1]['price'] = 50.00;
You'll see that the pkg array has the full shipment item list for each packing slip for each package. if you add the 0index qty from both packages you'll see that it adds up to the full shipment qty for that item line.
This set of data gets converted to a JSON string by php and tucked into a hidden form element within its corrosponding row.
After the page is loaded, jquery goes through each hidden json element, parses the json string to an object, the attaches the object to the TR.data('shipmentItems') and TR.data('pkgs') for each shipment in the list.
This is where things get funky...
I'm doing a function where the user can add a new package to the shipment. When they do this they are prompted to specify which package has how many quantities of each item in the whole shipment. They essentially lay out the packing slips.
The function they execute after they have mapped out the quantities, recreates the pkgs array(object) on the fly, which it gets from its rows .data('pkgs') container - and then re-attaches the pkgs object back to the data('pkgs') container.
I have logged the output of this function heavily and the quantities are all being assigned to the proper values see here:
var shipmentItems = $('#shipment_'+shipid).data('shipmentItems');
var pkgs = $('#shipment_'+shipid).data('pkgs');
var pkgnum = pkgs.length; // always returns one higher than last index.
// add new pkg to array
pkgs[pkgnum] = new Object();
pkgs[pkgnum].weight = weight;
console.log("("+pkgnum+") pkgs length: " + pkgs.length);
// overwrite packing slip data.
for(var x = 0; x < pkgs.length; x++) {
var curPS = new Array();
var curins = 0;
for(var y = 0; y < shipmentItems.length; y++) {
var curqty = parseInt($('#pkgqty-'+y+'-'+x).val());
curins += curqty * shipmentItems[y]['price'];
curPS.push(shipmentItems[y]);
console.log("["+y+"] before: " + curPS[y]['qty']);
curPS[y]['qty'] = curqty;
console.log("["+y+"] after: " + curPS[y]['qty']);
}
console.log(curPS[0]['qty'] + ' - ' + curPS[1]['qty']);
pkgs[x].packing_slip = curPS;
pkgs[x].insurance = curins;
}
// write pkgs data()
$('#shipment_'+shipid).removeData('pkgs');
$('#shipment_'+shipid).data('pkgs', pkgs);
The log output of the above is as follows:
(1) pkgs length: 2
[0] before: 3
[0] after: 2
[1] before: 4
[1] after: 3
2 - 3 // value of curPS[0]['qty'] and curPS[1]['qty'] for pkg#1 - pkgs[0] is set to curPS at this point.
[0] before: 2
[0] after: 1
[1] before: 3
[1] after: 1
1 - 1 // value of curPS[0]['qty'] and curPS[1]['qty'] for pkg#2 - pkgs[1] is set to curPS at this point.
This looks like it worked, right? Wrong. After the function completes I have a button I can push that prints out all the data() vars for a row. Not only are the qty value of every single pkg['packing_slip'][x] item set to 1, but if I look at the shipmentItems from this same object log, the qty values for all of the shipmentItems have also been reset to 1. Which is odd because at no point in the code does shipmentItems ever get overwritten and should still be the exact same as it was when the page loaded...
Anyone have any ideas whats going on here?
maybe it's because you pass shipmentItems by Reference - curPS.push(shipmentItems[y]);
Try to pass it by Value - curPS.push(shipmentItems[y].slice());
OK I ended up getting this working thanks to your (Alon) suggestion pointing me in the right direction. The slice() method did not work outright because the array elements I'm slicing out contained objects and so the deeper objects were still being passed as references rather than being copied. After some searching around I found the jQuery.extend() method was what I needed to copy the object arrays! Thanks again!

Categories