how to draw statistics line chart in my admin panel using php? - php

i want to show my chart in my admin panel using GD library using php ..
i get date from my database but when i use any library to show this result in line chart it did not work & it did not show any errors.i can not get what i did wrong?
can anyone help me?
<?php
include ("../setting.php");
include("../classes/phpgraphlib.php");
$result = mysql_query("SELECT DATE_FORMAT(log_date, '%Y-%m-%d') as Date,count(*) as Count FROM log
where operation_type='Add' and user_id='82' and faculty_code='0' group by log_date order by log_date") or die(mysql_error());
$MultiDimArray = array();
$data = array();
while( $row = mysql_fetch_array($result , MYSQL_BOTH) )
{
$MultiDimArray[] = array ( 'Date' => $row['Date'], 'Count' => $row['Count'] );
}
foreach($MultiDimArray as $value)
{
if ( !array_key_exists($value['Date'], $data) ) {
$data[$value['Date']] = $value['Count'];
}
else{ $data[$value['Date']] += (string)$value['Count'];}
}
$data = array_map('strval', $data);
$graph = new PHPGraphLib(400,300);
$graph->addData($data);
$graph->setTitle("Site Statistics");
$graph->setBars(false);
$graph->setLine(true);
$graph->setDataPoints(true);
$graph->setDataPointColor("maroon");
$graph->setDatadata(true);
$graph->setDataValueColor("maroon");
$graph->setGoalLine(.0025);
$graph->setGoalLineColor("red");
$graph->setXdataHorizontal(true);
$graph->createGraph();
?> `
the output of $data array is like that:
array'2013-05-13' => string '7' (length=1)'2013-05-15' => string '3' (length=1)'2013-05-16' => string '5' (length=1)'2013-05-18' => string '8' (length=1)'2013-05-19' => string '2' (length=1)'2013-05-21' => string '2' (length=1)'2013-05-22' => string '10' (length=2)'2013-05-23' => string '2' (length=1)'2013-05-25' => string '1' (length=1)'2013-05-26' => string '8' (length=1)'2013-05-27' => string '19' (length=2)'2013-05-28' => string '7' (length=1)'2013-05-29' => string '3' (length=1)'2013-06-02' => string '11' (length=2)'2013-06-03' => string '2' (length=1)'2013-06-04' => string '7' (length=1)'2013-06-05' => string '3' (length=1)'2013-06-06' => string '2' (length=1)'2013-06-09' => string '15' (length=2)'2013-06-10' => string '5' (length=1)'2013-06-11' => string '1' (length=1)'2013-06-12' => string '3' (length=1)'2013-06-15' => string '10' (length=2)'2013-06-16' => string '3' (length=1)'2013-06-19' => string '2' (length=1)

I'd suggest using JS for that, feeding it with data from PHP. Take a look at Flot or something similar.

Related

Merge inner PHP arrays into one multidimensional array - No duplicates basing on Key value

I have tried a number of PHP functions like array_unique to merge the arrays I have but that does not work since these are not strings.
The starting output would be this on var_dump( $country_cities );
array (size=3)
0 =>
array (size=1)
'BH' =>
array (size=4)
'post_id' => int 7886
'country' => string 'BH' (length=2)
'city_name_eng' => string 'Laurence' (length=8)
'city_name_arabic' => string '3684hdfpfwbhisf' (length=15)
1 =>
array (size=1)
'BH' =>
array (size=4)
'post_id' => int 7885
'country' => string 'BH' (length=2)
'city_name_eng' => string 'Bahrain City' (length=12)
'city_name_arabic' => string 'vgdg824762' (length=10)
2 =>
array (size=2)
'BH' =>
array (size=4)
'post_id' => int 7885
'country' => string 'BH' (length=2)
'city_name_eng' => string 'Bahrain City' (length=12)
'city_name_arabic' => string 'vgdg824762' (length=10)
'KW' =>
array (size=4)
'post_id' => int 7841
'country' => string 'KW' (length=2)
'city_name_eng' => string 'Kuwait City' (length=11)
'city_name_arabic' => string ' مدينة الكويت' (length=24)
The Code below merges the different arrays above.
<?php
// Make the cities unique. Remove duplicates form the inner array.
$uniques = [];
foreach($country_cities as $arr ) {
foreach( $arr as $v) {
if( !in_array($v, $uniques, false) ) {
$uniques[$v['country']] = $v;
}
}
}
var_dump($uniques);
Results logged show the code cuts out some of the arrays.
/Users/..... on line php:74:
array (size=2)
'BH' =>
array (size=4)
'post_id' => int 7885
'country' => string 'BH' (length=2)
'city_name_eng' => string 'Bahrain City' (length=12)
'city_name_arabic' => string 'vgdg824762' (length=10)
'KW' =>
array (size=4)
'post_id' => int 7841
'country' => string 'KW' (length=2)
'city_name_eng' => string 'Kuwait City' (length=11)
'city_name_arabic' => string ' مدينة الكويت' (length=24)
Please help figure out my error or suggest a better way to fix it.
If I understood you correctly, this should be the result you wanted to achieve.
Each country code will be included once, while the cities (based on post_id) will only be added once.
<?php
$result = [];
$processedIds = [];
foreach ($country_cities as $ccs) {
foreach ($ccs as $cc => $details) {
// If the country has not yet been added to
// the result we create an outer array.
if (!key_exists($cc, $result)) {
$result[$cc] = [];
}
$postId = $details['post_id'];
if (!in_array($postId, $processedIds)) {
// Add the unique city to country's collection
$result[$cc][] = $details;
// Keep track of the post_id key.
$processedIds[] = $postId;
}
}
}
print_r($result);
<?php
//...
$uniques = [];
foreach($country_cities as $data ) {
foreach( $data as $countryCode => $item) {
if( !array_key_exists($countryCode, $uniques) ) {
$uniques[$countryCode] = []; //Create an empty array
}
$targetArray = &$uniques[$countryCode];
if( !array_key_exists($item['post_id'], $targetArray) ) {
$targetArray[$item['post_id']] = $item; //Create an empty array
}
}
}
Maybe this code will help?

Write on $_POST an array inside DB like result

I have a problem on $weight_class_title.
When the data is saved it wrote in the database array.
Tk.
the result of var_dump($sql_data_array);
array (size=4)
'weight_class_title' =>
array (size=1)
'weight_class_title' => string 'tt' (length=2)
'weight_class_key' => string 'pound' (length=5)
'weight_class_id' => int 5
'language_id' => int 1
array (size=4)
'weight_class_title' =>
array (size=1)
'weight_class_title' => string 'tt' (length=2)
'weight_class_key' => string 'pound' (length=5)
'weight_class_id' => int 5
'language_id' => int 2
public function execute() {
$CLICSHOPPING_Language = Registry::get('Language');
$CLICSHOPPING_Db = Registry::get('Db');
$languages = $CLICSHOPPING_Language->getLanguages();
$QlastId = $CLICSHOPPING_Db->prepare('select weight_class_id
from :table_weight_classes
order by weight_class_id desc
limit 1');
$QlastId->execute();
$weight_class_id = $QlastId->valueInt('weight_class_id') + 1;
$weight_class_key = $_POST['weight_class_key'];
for ($i=0, $n=count($languages); $i<$n; $i++) {
$weight_class_title_array = $_POST['weight_class_title'];
$language_id = $languages[$i]['id'];
$weight_class_title_array = ['weight_class_title' => HTML::sanitize($weight_class_title_array[$language_id])];
$sql_data_array = ['weight_class_title' => $weight_class_title_array];
$insert_sql_data = ['weight_class_key' => $weight_class_key,
'weight_class_id' => $weight_class_id,
'language_id' => $languages[$i]['id']
];
$sql_data_array = array_merge($sql_data_array, $insert_sql_data);
$this->app->db->save('weight_classes', $sql_data_array);
}

Variable to = specific parts of array

I have 36 check boxes... I call them with this.
Labeled like this.
<input type='checkbox' value='1' name='ck[]'>
Called like this
$checks = $_POST['ck']
$vars = array(
'ck1' => 'Demo',
'ck2' => 'Demo2',
'ck3' => 'Demo3',
'ck4' => 'Demo4',
'ck5' => 'Demo5',
'ck6' => 'Demo6',
'ck7' => 'Demo7',
'ck8' => 'Demo8',
'ck9' => 'Demo9',
'ck10' => 'Demo10',
);
foreach($vars as $key=>$default) {
$checks[$key] = !empty($checks[$key]) ? $default : '';
}
How can I now make it where 2 new variables = specifics from that post.
For instance,
$CH1 = checks[1] - checks[6];
$ch2 = checks[6] - checks[10];
And then,
How can I make it where if there are numbers in $CH1 it will list and break.. like this
$checks[1] . '<br>' . $checks[2] and so on....
UPDATE***
$vars = array(
'1' => 'Desktop',
'2' => 'Laptop Only',
'3' => 'Laptop / Dock',
'4' => 'Laptop Case',
'5' => 'Desk Phone',
'6' => 'Monitor',
'7' => 'Printer Access',
'8' => 'Printer - Personal',
'9' => 'Email(phone)',
'10' => 'Office 365',
'11' => 'Sharepoint',
'12' => 'Fax Fwd',
'13' => 'Adobe DC',
'14' => 'Apacheta',
'15' => 'Brightree',
'16' => 'TeamDME',
'17' => 'DirectView',
'18' => 'RingCentral',
'19' => 'Faxage',
'20' => 'Google Docs',
'21' => 'Badge Access',
'22' => 'Title Plate',
'23' => 'Bis Cards',
'24' => 'Travel',
'25' => 'Exp Reports',
'26' => 'Order Pads',
'27' => 'Demo Trilogy',
'28' => 'Stickers',
'29' => 'APS',
'30' => 'CBSS',
'31' => 'Conexis',
'32' => 'Evenfy',
'33' => 'Inventory',
'34' => 'AMEX',
'35' => 'Fuel Card',
'36' => 'Vehicle'
);
foreach($vars as $key=>$default) {
$checks[$key] = !empty($checks[$key]) ? $default : '';
}
When I echo $checks, it just says array.. does not show any numbers names that were checked.
If you are trying to dinamically read the values, you can construct the key value with the name you already have:
$num_a = 1;
$num_b = 6;
$CH1 = checks['ck' . $num_a] - checks['ck' . $num_b];
You might want to use array_slice:
$CH1 = array_slice($checks, 0, 6);
Now $CH1 will be an array with the first 6 checks only. To get the next 10 (for instance):
$CH2 = array_slice($checks, 6, 10);
...etc.
Then you can loop through them just like you would do with another array:
foreach($CH1 as $chk) {
//
}
If you want to check if any of those is numeric, use is_numeric:
foreach($CH1 as $chk) {
if (is_numeric($chk)) echo $chk . "<br>";
}
You can also filter an array based on a condition. Let's say you want to eliminate the empty elements from the $checks array:
$filtered = array_filter($checks, function ($chk) { return !empty($chk); });
... and now output that filtered array with each entry on a separate line:
echo implode('<br>', $filtered);
Finally, you can use array_intersect_key to get out of your $vars array only those elements that have been checked, as follows:
$result = array_intersect_key($vars, array_flip($_POST['ck']));
So, if the user had selected checkboxes with value 1, 5 and 10, the $results array will look like this:
array (
1 => 'Desktop',
5 => 'Desk Phone',
20 => 'Google Docs',
)
See it on eval.in.
I hope these ideas will all be helpful and of use to you.

Multiple where condition codeigniter active record

I want to make a multiple where query with AND and OR. Suppose my array is:
array (size=8)
0 => array (size=2)
'timeline.type' => string 'videos' (length=6)
'timeline.sourceId' => string '7' (length=1)
1 => array (size=2)
'timeline.type' => string 'loadshedding' (length=12)
'timeline.sourceId' => string '5' (length=1)
2 => array (size=2)
'timeline.type' => string 'news' (length=4)
'timeline.sourceId' => string '3' (length=1)
3 => array (size=2)
'timeline.type' => string 'news' (length=4)
'timeline.sourceId' => string '5' (length=1)
The above array could be dynamic. I want to make active record query like:
(timeline.type = 'videos' AND timeline.sourceId = 7) OR (timeline.type = 'loadshedding' AND timeline.sourceId = 5) OR (timeline.type = 'news' AND timeline.sourceId = 3) // and so on.
I tried :
$this->db->select('timeline.id as postid, timeline.*, sources.*, subscription.*')->from('timeline');
$this->db->join('sources', 'sources.type = timeline.type and timeline.sourceId = sources.id');
$this->db->join('subscription', 'subscription.type = timeline.type and timeline.sourceId = subscription.sourceId');
foreach($sources as $source){ //$sources is an array like given above
$this->db->or_where($source);
}
$this->db->order_by("timeline.id", "DESC");
$this->db->limit(15);
But when I echo $this->db->last_query(); to see the query. It returns SELECT * FROM timeline only. What might be the problem. Thank you.
Your Code Should be like
$sources = array (
0 => array (
'timeline.type' => 'videos',
'timeline.sourceId' => '7' ),
1 => array (
'timeline.type' => 'loadshedding',
'timeline.sourceId' => '5' ) ,
2 => array (
'timeline.type' => 'news',
'timeline.sourceId' => '3' ),
3 => array (
'timeline.type' => 'news',
'timeline.sourceId' => '5')
);
end($sources); // move the internal pointer to the end of the array
$lastkey = key($sources);
$pr = "";
foreach($sources as $key=>$source)
{
if($key != $lastkey)
{
$pr.="( `timeline.type` = '". $source["timeline.type"] ."' and `timeline.sourceId` = " . (int) $source["timeline.sourceId"] . ") OR ";
}
else
{
$pr.="( `timeline.type` = '". $source["timeline.type"] ."' and `timeline.sourceId` = " . (int) $source["timeline.sourceId"] . ")";
}
}
$this->db->select('timeline.id as postid, timeline.*, sources.*, subscription.*')->from('timeline');
$this->db->join('sources', 'sources.type = timeline.type and timeline.sourceId = sources.id');
$this->db->join('subscription', 'subscription.type = timeline.type and timeline.sourceId = subscription.sourceId');
$this->db->where($pr);
$this->db->order_by("timeline.id", "DESC");
$this->db->limit(15);

Why does assigning a new value to an array fail in PHP?

Consider:
array (size=1)
0 =>
array (size=5)
'name' => string '15268459735 Farming and Projects XXXXX' (length=38)
'region' => string '2' (length=1)
'entitynumber' => string '2012/002086/24' (length=14)
'ownership' => string '6' (length=1)
'id' => string '26249' (length=5)
Why does the following still return the same array without the owner element?
foreach ($result as $row)
{
$row['owner'] = 1;
}
Try passing the array by reference:
foreach ($result as &$row) {
$row['owner'] = 1;
}
See also: What's the & for, anyway?

Categories