PHP & MySQL - How to remove first value if it is (,) - php

I have a flowing in my little project to display amount and the date from two different columns. One thing to note is that it stores multiple values in those columns which are separated by (,):
$apaidArray = explode(',', $apaid);
$daterArray = explode(',', $dater);
for ( $i = 0; $i < count($apaidArray); $i++ ){
echo $apaidArray[$i].' repaid on ' .$daterArray[$i].'<br>';
}
As the result of the (,) which is placed initially before the repayment amount is entered this would display the following if there are only two entries into db:
apaid = , 80, 20
dater = , 12.07.2017, 13.07.2017
Result looks like:
repaid on
80 repaid on 12.07.2017
20 repaid on 13.07.2017
So my question is is there a way of removing the (,) before displaying result only if it is the first value in that column so that the resulting would be:
80 repaid on 12.07.2017
20 repaid on 13.07.2017
Any help is greatly appreciated.

Run trim on both values, with the character mask set to ','.

for ( $i = 0; $i < count($apaidArray); $i++ ) {
if (!empty($apaidArray[$i])) {
echo $apaidArray[$i].' repaid on ' .$daterArray[$i].'<br>';
}
}

<?php
$apaid = ', 80, 20';
$apaid = ltrim($apaid,','); //$apaid = ltrim($apaid,', ');
?>

Related

exclude array numbers for mysql

i have in mysql
id = 1
id_bets = 3
id_user = 3
numbers_sell = 4,7,9,1
I need in my php select all number for bets and exclue numbers sell
ex: bets have 50 numbers i nedd show
2 3 5 8 10 11 12 etc.....
exlude - 4,7,9,1
my code
for ($i=1; $i<=50; $i++) {
$exclude = array(4,7,9,1);
if(in_array($i, $exclude)) continue;
echo $i;
}
no work, array not accept while my sql
$exclude = array($row['numbers_sell');
no work
Use explode() to split up the value from the table column.
$exclude = explode(',', $row['numbers_sell']);

Get average difference between all unique rows

I have a bantracker which logs to MySQL each ban, I would like to get the average difference between each unix time so I can estimate based on the data when the next ban will happen.
My thinking would be Last entry + average? Maybe I am completely wrong and somebody has a better idea.
The Table structure looks like this:
Each apikey is unique.
Any help / ideas are much appreciated I don't mind if it's a MySQL query or PHP code.
Thanks!
You're trying to calculate a moving average:
function moving_average( $array ) {
$z = sizeof( $array );
for ( $i = 1; $i < $z; $i++ ) {
$result[] = $array[ $i ] - $array[ $i-1 ];
}
return array_sum( $result ) / count( $result );
}
Hat tip to: Calculating the average increase from array values in PHP
You can use(COUNT,SUM) to find average
$sql = "SELECT COUNT(time) as count,SUM(time) as sum,MAX(time) as sum FROM table_name";
$avg=$row["sum"]/$row["count"];

sum of 2D array in php

I have a situation where I need to generate total sales of each month.
I can show total sales of 12 month in a certain area but I need to show total sales of any month of a certain area. i.e: total sales of June at Head Office area.
my sample code is as follows:
while($area = mysql_fetch_array($exe_area))
{
for ($m=1; $m<=12; $m++) {
.........
echo $total_sold;
$sales_of_year+= $total_sold;
} // end of month listing....
echo $sales_of_year;
} // end of area listing...
U may try this !!!
$tmp = explode( '|', $str );
$data = array();
foreach ( $tmp as $k => $v )
{
$data[] = explode( ',', $v );
}
It looks like your data is being retrieved from the database, in which case you can extract the total sum with your query:
SELECT SUM(column_name) FROM table_name;
Or, to analyse a particular month, depending on your database schema, you can do:
SELECT SUM(column_name) FROM table_name WHERE month = 'June';

Increment Price by Hundred, Thousand, Ten-Thousand, etc

I'm trying to make a select list of prices in my system. The prices are stored as integers. I'm able to get the lowest price and highest price but I want to display them in a select list. I don't want the select list to increment slowly but by 100 or 10,000 or, 100,000 depending on what my starting number is what where i'm at in my incrementation.
For example, say I have these 2 prices:
500000
12345689
I'm trying to increment them by 100,000 Then when I get to 1,000,000 I want to increment by that. It will look something like this:
500000
600000
700000
800000
900000
1000000
2000000
I'm using a custom function and a bit of formatting to get all my prices and get my start price and end price:
$prices = my_custom_function(); // Pulls All Prices in a random order
if(!empty($prices)){
sort($prices); // Sort Prices Lowest to Highest
$price_low = $prices[0];
$price_high = $prices[count($prices)-1];
$price_start = intval( $price_low[0].str_repeat( '0', strlen( $price_low ) - 1 ) );
$price_end = intval( ( $price_high[0] + 1 ).str_repeat( '0', strlen( $price_high ) -1 ) );
}
Using the same example above, my start price and end price will be:
$price_start = 500000
$price_end = 20000000
Now it's at the loop where I run into trouble incrementing it by the values I want. I'm trying to use a while loop and determine where I am in my incrementer:
<?php $i = $price_start; $x = 0; while($x < 10) : ?>
<option value="<?php echo $i; ?>"><?php echo format_price($i); ?></option>
<?php
if(1000 % $i == 0)
$i+=1000;
else if(10000 % $i == 0)
$i+=10000;
else if(100000 % $i == 0)
$i+=100000;
else if(1000000 % $i == 0)
$i+=1000000;
else
$i+=10000000;
$x++;
endwhile;
?>
I ended up adding in the x variable because I kept running into infinite loop problems but theoretically it should be while($i <= $price_end). Can somebody point me in the right direction on how to get the expected output please? I feel like I'm close but not quite there yet and there's probably a better / faster way to go about it. Any help would be great.
I guess a simplified way of looking at it is:
1 -> +1
10 -> +10
100 -> +100
1000 -> +1000
10000 -> +10000
and so forth.
Get power of 10: log10(1234); // 3.09131
Round down: floor(log10(1234)); // 3
Re-raise as power of 10: pow(10,floor(log10(1234))); // 1000
???
Profit.
If someone needs the full solution here it is:
$price = 100; // Starting Price
$priceEnd = 10000; // Ending Price
while($price <= $priceEnd) {
echo $price . "<br/>";
$increase = pow(10,floor(log10($price)));
$price = $price + $increase;
}

How to check matching array values in php?

Hi i have a repeating dates in array values i wan to count the number of repeating dates from the array value. I tried this but am not sure to do it correctly and am getting error Undefined offset: 0
<?php $array = array('2013-11-28','2013-11-28','2013-11-28','2013-11-29','2013-11-29','2013-11-30');
$len = sizeof($array);
$len = $len-1;
$day = array();
for($i=0; $i<=$len; $i++)
{
for($j=0; $j<=$len; $j++)
{
if($array[$i] == $array[$j])
{
if($day[0] == '')
{
$co = 1;
$day[] = $co;
}
else {
$day[$i] = $co++;
}
}
}
echo 'day'.$i.' '.$day[$i].' ';
}
?>
From the date values i should get 3 for 2013-11-28, 2 for 2013-11-29 and 1 for 2013-11-30 as you can see 2013-11-28 is presented 3 times , 2013-11-29 is presented 2 times and
2013-11-30 is presented one time.
I can understand that i am wrongly calculating because in the second loop i am again starting from first index so increasing the count.
I want to know the count of same dates. How to do this. Any other way to count this? Any help please?
Use array_count_values().
$dupesCount = array_count_values($array);
This will give you an array where the value is the key and the new value is the repetition count.

Categories