php - Undefined offset - php

How can I fix this undefined offset error.
I get: Undefined offset 0 - 44, on line 234.
Line 234 looks like this:
$mysql['invo_mysql_where'] .= ( $x > 0 ? " OR " : '' )."`id` = {$mysql['inactive'][$x]}";
The entire part of the code, looks like this:
shuffle($mysql['inactive']);
for($x = 0, $l = $mysql['inactive_amount']; $x < $l; $x++){
$mysql['invo_mysql_where'] .= ( $x > 0 ? " OR " : '' )."`id` = {$mysql['inactive'][$x]}";
}
$mysql_inactive:
$mysql['inactive'] = array();
if($ile == 10){
$mysql['inactive_amount'] = mt_rand(0, 1);
} else if($ile == 20){
$mysql['inactive_amount'] = mt_rand(0, 2);
} else if($ile == 30){
$mysql['inactive_amount'] = mt_rand(0, 3);
} else if($ile == 40){
$mysql['inactive_amount'] = mt_rand(0, 4);
} else if($ile == 50){
$mysql['inactive_amount'] = mt_rand(0, 4);
} else if($ile == 60){
$mysql['inactive_amount'] = mt_rand(1, 3);
} else if($ile == 70){
$mysql['inactive_amount'] = mt_rand(2, 3);
} else if($ile == 80){
$mysql['inactive_amount'] = mt_rand(3, 4);
} else if($ile == 90){
$mysql['inactive_amount'] = mt_rand(4, 4);
} else if($ile == 100){
$mysql['inactive_amount'] = mt_rand(5, 5);
} else if($ile == 200){
$mysql['inactive_amount'] = mt_rand(6, 5);
} else {
$mysql['inactive_amount'] = mt_rand(20, 50);
}
$mysql['invo_mysql_where'] = '';

The problem is that you define $mysql['inactive'] as an empty array here:
$mysql['inactive'] = array();
Then never add elements to it. Later, you try to use it as a populated array here:
$mysql['inactive'][$x]
The error occurs because no indexes are defined for the $mysql['inactive'] array. In order to fix this, you'll need to populate it with some data.

Related

Generate random number again if it doesn't equal x

I want to generate a number between 1 and 100, but I want it to keep regenerating that number until it equals 50, once it equals 50, then echo it out. How would I do this?
My Function:
function create() {
$production_line = mt_rand(0, 3);
$random1 = mt_rand(0, 9);
$random2 = mt_rand(0, 9);
$random3 = mt_rand(0, 9);
$random4 = mt_rand(0, 9);
$random5 = mt_rand(0, 9);
$random6 = mt_rand(0, 9);
$production_year = mt_rand(3, 4);
$week1 = 4;
$week2 = 8;
$factory1 = 4;
$factory2 = 8;
if ($production_line + $random1 + $random2 + $random3 + $random4 + $random5 + $random6 + $production_year + $week1 + $week2 + $factory1 + $factory2 == 55) {
return $production_line.$random1.$random2.$random3.$random4.$random5.$random6.$production_year.$week1.$week2.$factory1.$factory2;
}
}
Use a simple loop:
$i = 0;
while ($rand = mt_rand(0,100)) {
$i++;
if ($rand == 50) {
// found 50, so break out of the loop
break;
}
}
echo "It took $i iterations to find 50";
But that's a bit pointless, right? If you're just going to output 50 all the time, then why do you need to generate a random number? Just echo 50 instead. Also note that this could be a slow operation if the larger limit is a bigger number.
Generate and test all the random numbers inside a while loop.
function create() {
$week1 = 4;
$week2 = 8;
$factory1 = 4;
$factory2 = 8;
while (true) {
$production_line = mt_rand(0, 3);
$random1 = mt_rand(0, 9);
$random2 = mt_rand(0, 9);
$random3 = mt_rand(0, 9);
$random4 = mt_rand(0, 9);
$random5 = mt_rand(0, 9);
$random6 = mt_rand(0, 9);
$production_year = mt_rand(3, 4);
if ($production_line + $random1 + $random2 + $random3 + $random4 + $random5 + $random6 + $production_year + $week1 + $week2 + $factory1 + $factory2 == 55) {
return $production_line.$random1.$random2.$random3.$random4.$random5.$random6.$production_year.$week1.$week2.$factory1.$factory2;
}
}
}
I've rearranged bits of the code to make it more cohesive:
function create() {
do {
$arr = [
mt_rand(0, 3), // line
mt_rand(0, 9),
mt_rand(0, 9),
mt_rand(0, 9),
mt_rand(0, 9),
mt_rand(0, 9),
mt_rand(0, 9),
mt_rand(3, 4), // year
4, // weeks
8,
4, // factories
8,
];
} while (array_sum($arr) != 55);
return join('', $arr);
}
$number = 0;
$try = 0;
while ($number != 50) {
$try++;
$number = rand(1,100);
}
echo "found $number after $try cycles";

Using PHP to grab a simplified colour palette from an image?

So far I have found examples on how to grab all the RGB values of each pixel in an image but I want something that will break down an image and give me a simplified color palette.
Is there a way to use imagetruecolortopalette to somehow spit out the reduced palette colours, or to break an image into 25 x 25 blocks and then grab the average value of that block?
Maybe there is an alternative I'm missing? I basically just want to be able to find the most common colors within an image.
Thanks in advance.
Hmm, well I have had created something like this for a client. The screenshot is below
The complete code is as follows
$microTime = microtime(true);
function textColor($R1, $G1, $B1) {
$a = (($R1 * 299) + ($G1 * 587 ) + ($B1 * 114 )) / 1000;
if ($a < 128)
return 'white';
else
return 'black';
}
function rgb2html($r, $g = -1, $b = -1) {
$hex = "#";
$hex.= str_pad(dechex($r), 2, "0", STR_PAD_LEFT);
$hex.= str_pad(dechex($g), 2, "0", STR_PAD_LEFT);
$hex.= str_pad(dechex($b), 2, "0", STR_PAD_LEFT);
return $hex;
if (is_array($r) && sizeof($r) == 3)
list($r, $g, $b) = $r;
$r = intval($r);
$g = intval($g);
$b = intval($b);
$r = dechex($r < 0 ? 0 : ($r > 255 ? 255 : $r));
$g = dechex($g < 0 ? 0 : ($g > 255 ? 255 : $g));
$b = dechex($b < 0 ? 0 : ($b > 255 ? 255 : $b));
$color = (strlen($r) < 2 ? '0' : '') . $r;
$color .= (strlen($g) < 2 ? '0' : '') . $g;
$color .= (strlen($b) < 2 ? '0' : '') . $b;
return '#' . $color;
}
function colorPalette($imageFile, $colorJump, $granularity = 5) {
$granularity = max(1, abs((int) $granularity));
$colors = array();
$ratio = array();
$wastageCount = array();
$occurrenceSCount = array();
$occurrenceMCount = array();
$size = #getimagesize($imageFile);
if ($size === false) {
return false;
}
$img = #imagecreatefromstring(file_get_contents($imageFile));
if (!$img) {
user_error("Unable to open image file");
return false;
}
for ($y = 0; $y < $size[1]; $y += $granularity) {
$lastColor = NULL;
$lastX = -1;
for ($x = 0; $x < $size[0]; $x += $granularity) {
$thisColor = imagecolorat($img, $x, $y);
$rgb = imagecolorsforindex($img, $thisColor);
$red = round(round(($rgb['red'] / $colorJump)) * $colorJump);
$green = round(round(($rgb['green'] / $colorJump)) * $colorJump);
$blue = round(round(($rgb['blue'] / $colorJump)) * $colorJump);
$thisRGB = $red . ',' . $green . ',' . $blue;
if ($lastColor != $thisRGB) {
if (array_key_exists($thisRGB, $wastageCount)) {
$wastageCount[$thisRGB]++;
} else {
$wastageCount[$thisRGB] = 1;
}
if ($lastX + 1 == $x) {
if (array_key_exists($lastColor, $occurrenceSCount)) {
$occurrenceSCount[$lastColor]++;
} else {
$occurrenceSCount[$lastColor] = 1;
}
}
if ($lastX + 1 != $x) {
if (array_key_exists($lastColor, $occurrenceMCount)) {
$occurrenceMCount[$lastColor]++;
} else {
$occurrenceMCount[$lastColor] = 1;
}
}
$lastColor = $thisRGB;
$lastX = $x;
}
if (array_key_exists($thisRGB, $colors)) {
$colors[$thisRGB]++;
} else {
$colors[$thisRGB] = 1;
}
}
}
$totalPixels = array_sum($colors);
foreach ($colors as $k => $v) {
$ratio[$k] = round(($v / $totalPixels ) * 100, 2);
}
return array($ratio, $wastageCount, $colors, $occurrenceSCount, $occurrenceMCount);
}
usage
$colorJump = 1;
$pixelJump = 1;
$paletteR = colorPalette($dbImgFile_dir, $colorJump, $pixelJump);
$palette = $paletteR[0];
$wastage = $paletteR[1];
$colorsFound = $paletteR[2];
$occSArray = $paletteR[3];
$occMArray = $paletteR[4];
$totalPixels = array_sum($colorsFound);
$totalTime = abs(microtime(true) - $microTime);
the looping around is more complex, as I have to get the pallet from the DB and to match the colors with them, and also the template parser is used which is full custom code, and will not help you.
Just ignore the Required Weight column from this, the single occurrences and multiple occurrences are calculated, if there is a single pixel, for example RED, RED, BLUE, RED, RED it will be 1 single occurrence and 2 multiple occurrence

Piechart creation in pdf file using fpdf in php

I want to create piechart in my pdf file created using fpdf. already i had created pdf with fpdf . then i want to create pie chart in that using same table data, is there any option to create pie chart using fpdf ?
Please Help.
Thanks in advance
Try this make changes in code as per your requirements:
You can display following view file on your pdf using pdf helper.
you can use dom pdf helper download it from following link.
http://code.google.com/p/dompdf/downloads/detail?name=dompdf_0-6-0_beta3.zip
<?php
$show_label = true; // true = show label, false = don't show label.
$show_percent = true; // true = show percentage, false = don't show percentage.
$show_text = true; // true = show text, false = don't show text.
$show_parts = false; // true = show parts, false = don't show parts.
$label_form = 'square'; // 'square' or 'round' label.
$width = 199;
$background_color = 'FFFFFF'; // background-color of the chart...
$text_color = '000000'; // text-color.
$colors = array('003366', 'CCD6E0', '7F99B2','F7EFC6', 'C6BE8C', 'CC6600','990000','520000','BFBFC1','808080'); // colors of the slices.
$shadow_height = 16; // Height on shadown.
$shadow_dark = true; // true = darker shadow, false = lighter shadow...
// DON'T CHANGE ANYTHING BELOW THIS LINE...
$data = $_GET["data"];
$label = $_GET["label"];
$height = $width/2;
$data = explode('*',$data);
if ($label != '') $label = explode('*',$label);
for ($i = 0; $i < count($label); $i++)
{
if ($data[$i]/array_sum($data) < 0.1) $number[$i] = ' '.number_format(($data[$i]/array_sum($data))*100,1,',','.').'%';
else $number[$i] = number_format(($data[$i]/array_sum($data))*100,1,',','.').'%';
if (strlen($label[$i]) > $text_length) $text_length = strlen($label[$i]);
}
if (is_array($label))
{
$antal_label = count($label);
$xtra = (5+15*$antal_label)-($height+ceil($shadow_height));
if ($xtra > 0) $xtra_height = (5+15*$antal_label)-($height+ceil($shadow_height));
$xtra_width = 5;
if ($show_label) $xtra_width += 20;
if ($show_percent) $xtra_width += 45;
if ($show_text) $xtra_width += $text_length*8;
if ($show_parts) $xtra_width += 35;
}
$img = ImageCreateTrueColor($width+$xtra_width, $height+ceil($shadow_height)+$xtra_height);
ImageFill($img, 0, 0, colorHex($img, $background_color));
foreach ($colors as $colorkode)
{
$fill_color[] = colorHex($img, $colorkode);
$shadow_color[] = colorHexshadow($img, $colorkode, $shadow_dark);
}
$label_place = 5;
if (is_array($label))
{
for ($i = 0; $i < count($label); $i++)
{
if ($label_form == 'round' && $show_label && $data[$i] > 0)
{
imagefilledellipse($img,$width+11,$label_place+5,10,10,colorHex($img, $colors[$i % count($colors)]));
imageellipse($img,$width+11,$label_place+5,10,10,colorHex($img, $text_color));
}
else if ($label_form == 'square' && $show_label && $data[$i] > 0)
{
imagefilledrectangle($img,$width+6,$label_place,$width+16,$label_place+10,colorHex($img, $colors[$i % count($colors)]));
imagerectangle($img,$width+6,$label_place,$width+16,$label_place+10,colorHex($img, $text_color));
}
if ($data[$i] > 0)
{
if ($show_percent) $label_output = $number[$i].' ';
if ($show_text) $label_output = $label_output.$label[$i].' ';
if ($show_parts) $label_output = $label_output.$data[$i];
imagestring($img,'2',$width+20,$label_place,$label_output,colorHex($img, $text_color));
$label_output = '';
$label_place = $label_place + 15;
}
}
}
$centerX = round($width/2);
$centerY = round($height/2);
$diameterX = $width-4;
$diameterY = $height-4;
$data_sum = array_sum($data);
$start = 270;
for ($i = 0; $i < count($data); $i++)
{
$value += $data[$i];
$end = ceil(($value/$data_sum)*360) + 270;
$slice[] = array($start, $end, $shadow_color[$value_counter % count($shadow_color)], $fill_color[$value_counter % count($fill_color)]);
$start = $end;
$value_counter++;
}
for ($i=$centerY+$shadow_height; $i>$centerY; $i--)
{
for ($j = 0; $j < count($slice); $j++)
{
if ($slice[$j][0] != $slice[$j][1]) ImageFilledArc($img, $centerX, $i, $diameterX, $diameterY, $slice[$j][0], $slice[$j][1], $slice[$j][2], IMG_ARC_PIE);
}
}
for ($j = 0; $j < count($slice); $j++)
{
if ($slice[$j][0] != $slice[$j][1]) ImageFilledArc($img, $centerX, $centerY, $diameterX, $diameterY, $slice[$j][0], $slice[$j][1], $slice[$j][3], IMG_ARC_PIE);
}
OutputImage($img);
ImageDestroy($img);
function colorHex($img, $HexColorString)
{
$R = hexdec(substr($HexColorString, 0, 2));
$G = hexdec(substr($HexColorString, 2, 2));
$B = hexdec(substr($HexColorString, 4, 2));
return ImageColorAllocate($img, $R, $G, $B);
}
function colorHexshadow($img, $HexColorString, $mork)
{
$R = hexdec(substr($HexColorString, 0, 2));
$G = hexdec(substr($HexColorString, 2, 2));
$B = hexdec(substr($HexColorString, 4, 2));
if ($mork)
{
($R > 99) ? $R -= 100 : $R = 0;
($G > 99) ? $G -= 100 : $G = 0;
($B > 99) ? $B -= 100 : $B = 0;
}
else
{
($R < 220) ? $R += 35 : $R = 255;
($G < 220) ? $G += 35 : $G = 255;
($B < 220) ? $B += 35 : $B = 255;
}
return ImageColorAllocate($img, $R, $G, $B);
}
function OutputImage($img)
{
header('Content-type: image/jpg');
ImageJPEG($img,NULL,100);
}
?>
Hope this will help you... :)

issue in printing output

i am using below code to print the id3 tags of MP2 files but the output is coming in this form
Array ( [FileName] => 4.mp3 [TAG] => ID3 [Version] => 3.0 [Title] => &Bahut Khubsurat Ghazal (mr-Jatt.Com) [Album] => 1Loverz Choice (A Khubsurat Ghazal) (www.mzc.in) [Author] => DJ Badboy (mr-Jatt.Com) [Track] => (www.mzc.in) )
but i want plain output like this
FileName 4.mp3 Title Bahut Khubsurat Ghazal (mr-Jatt.Com) Album 1Loverz Choice (A Khubsurat Ghazal) (www.mzc.in) Author DJ Badboy (mr-Jatt.Com) Track (www.mzc.in)
my code is
print_r(tagReader("4.mp3"));
// ------------------------------
function tagReader($file) {
$id3v23 = array("TIT2","TALB","TPE1","TRCK","TDRC","TLEN","USLT");
$id3v22 = array("TT2","TAL","TP1","TRK","TYE","TLE","ULT");
$fsize = filesize($file);
$fd = fopen($file, "r");
$tag = fread($fd, $fsize);
$tmp = "";
fclose($fd);
if (substr($tag, 0, 3) == "ID3") {
$result['FileName'] = $file;
$result['TAG'] = substr($tag, 0, 3);
$result['Version'] = hexdec(bin2hex(substr($tag, 3, 1))) . "." . hexdec(bin2hex(substr($tag, 4, 1)));
}
if ($result['Version'] == "4.0" || $result['Version'] == "3.0") {
for($i = 0; $i < count($id3v23); $i ++) {
if (strpos($tag, $id3v23[$i] . chr(0)) != FALSE) {
$pos = strpos($tag, $id3v23[$i] . chr(0));
$len = hexdec(bin2hex(substr($tag, ($pos + 5), 3)));
$data = substr($tag, $pos, 9 + $len);
for($a = 0; $a < strlen($data); $a ++) {
$char = substr($data, $a, 1);
if ($char >= " " && $char <= "~")
$tmp .= $char;
}
if (substr($tmp, 0, 4) == "TIT2")
$result['Title'] = substr($tmp, 4);
if (substr($tmp, 0, 4) == "TALB")
$result['Album'] = substr($tmp, 4);
if (substr($tmp, 0, 4) == "TPE1")
$result['Author'] = substr($tmp, 4);
if (substr($tmp, 0, 4) == "TRCK")
$result['Track'] = substr($tmp, 4);
if (substr($tmp, 0, 4) == "TDRC")
$result['Year'] = substr($tmp, 4);
if (substr($tmp, 0, 4) == "TLEN")
$result['Lenght'] = substr($tmp, 4);
if (substr($tmp, 0, 4) == "USLT")
$result['Lyric'] = substr($tmp, 7);
$tmp = "";
}
}
}
if ($result['Version'] == "2.0") {
for($i = 0; $i < count($id3v22); $i ++) {
if (strpos($tag, $id3v22[$i] . chr(0)) != FALSE) {
$pos = strpos($tag, $id3v22[$i] . chr(0));
$len = hexdec(bin2hex(substr($tag, ($pos + 3), 3)));
$data = substr($tag, $pos, 6 + $len);
for($a = 0; $a < strlen($data); $a ++) {
$char = substr($data, $a, 1);
if ($char >= " " && $char <= "~")
$tmp .= $char;
}
if (substr($tmp, 0, 3) == "TT2")
$result['Title'] = substr($tmp, 3);
if (substr($tmp, 0, 3) == "TAL")
$result['Album'] = substr($tmp, 3);
if (substr($tmp, 0, 3) == "TP1")
$result['Author'] = substr($tmp, 3);
if (substr($tmp, 0, 3) == "TRK")
$result['Track'] = substr($tmp, 3);
if (substr($tmp, 0, 3) == "TYE")
$result['Year'] = substr($tmp, 3);
if (substr($tmp, 0, 3) == "TLE")
$result['Lenght'] = substr($tmp, 3);
if (substr($tmp, 0, 3) == "ULT")
$result['Lyric'] = substr($tmp, 6);
$tmp = "";
}
}
}
return $result;
}
You can try
foreach ( tagReader("4.mp3") as $name => $value ) {
printf("<p>%s:%s</p>", $name, $value);
}
This is because you are returning an array. If you want to print out the contents of the array's elements, you will have to loop through the array and print each element instead of returning the whole array.
Something like:
foreach ($results as $key => $value)
echo $key . " " . $value;

How to format numbers in php 1,000 to 1k

Im trying to format the output of numbers in php. I have an amount of posts that show up, and next to each user is the total of posts. But it shows that actual amount, i want it to show it in a shorter format, actually, just like they do here at SO with reputation
any ideas?
<?
$numbers = array(100,1000,15141,3421);
function format_number($number) {
if($number >= 1000) {
return $number/1000 . "k"; // NB: you will want to round this
}
else {
return $number;
}
}
foreach($numbers as $number) {
echo $number . " : " . format_number($number);
echo "\n";
}
function count_format($n, $point='.', $sep=',') {
if ($n < 0) {
return 0;
}
if ($n < 10000) {
return number_format($n, 0, $point, $sep);
}
$d = $n < 1000000 ? 1000 : 1000000;
$f = round($n / $d, 1);
return number_format($f, $f - intval($f) ? 1 : 0, $point, $sep) . ($d == 1000 ? 'k' : 'M');
}
Use This
Shorten long numbers to K/M/B?
function number_format_short( $n, $precision = 1 ) {
if ($n < 900) {
// 0 - 900
$n_format = number_format($n, $precision);
$suffix = '';
} else if ($n < 900000) {
// 0.9k-850k
$n_format = number_format($n / 1000, $precision);
$suffix = 'K';
} else if ($n < 900000000) {
// 0.9m-850m
$n_format = number_format($n / 1000000, $precision);
$suffix = 'M';
} else if ($n < 900000000000) {
// 0.9b-850b
$n_format = number_format($n / 1000000000, $precision);
$suffix = 'B';
} else {
// 0.9t+
$n_format = number_format($n / 1000000000000, $precision);
$suffix = 'T';
}

Categories