orgenize a database results to the format morris chart expects

hi
i make a query to the database and have the right results. now i am stuck with orgenizing it to an array in the format morris charts expect.
i get this from the database results:
array(2) {
  [0]=>
  array(2) {
    ["loadname"]=>
    string(5) "peres"
    ["V"]=>
    string(5) "16777"
  }
  [1]=>
  array(2) {
    ["loadname"]=>
    string(5) "peres"
    ["P"]=>
    string(5) "16575"
  }
}

Open in new window


i need it in this format (tested it manually first, so this works for the chart):
$linevalues = array(
            array( 'loadname' => "peres" ,  'V' => 16777, 'P' => 16575) ,//each inner array define a point
            array( 'loadname' => "peres" ,  'V' => 400, 'P' => 600 ),
        );

Open in new window


i need to build the inner arrays to insert into the bigger array.

help?!!!
LVL 1
derridaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Can you post your DB query - the results are difficult to interpret as a var_dump
0
derridaAuthor Commented:
$sql = "SELECT * FROM loadlines WHERE chart_num = 1 AND load_name = '$loadname' ";
0
Ray PaseurCommented:
SELECT * will retrieve all of the columns from the database.  If you use SELECT * and the only columns you get are "loadname" and "V" then your table does not have "p" defined.  Where will you get the value for "p" ?
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

derridaAuthor Commented:
hi
the machines names are atored as serialized in the database. so before inserting them i serialize and when after the select i unserialize them:

$thelines = unserialize($thedata[0]->linesforchart);

Open in new window

0
derridaAuthor Commented:
i have all the data i need , my issue is how to prepare them in the array format morris chart work well with
0
Ray PaseurCommented:
have all the data i need
Then please show us the data, as you have it, and we can help you format it correctly.
0
Julian HansenCommented:
Please post all the relevant code for accessing and retrieving data from the database including where you unserialize.

Your sample data does not tell us much and the remaining posts only give us glimpses. For instance - in your sample the var_dump shows one line of the array you show in your second code snippet - however there are two lines in the second snippet - could there be more?

Can we assume that the data will always be loadname => V followed by a loadname => P AND that the V and P in the latter are related?
0
derridaAuthor Commented:
ok this is the whole geting the data out of the database:

$chartdata = new Green_model_loadlines();
$sql = "SELECT * FROM loadlines WHERE chart_num = 1 AND load_name = '$loadname' ";
$thedata = $chartdata->readbysql($sql);
//var_dump($thedata);

$thelines = unserialize($thedata[0]->linesforchart);

$linevalues = array();
foreach ($thelines as $key => $value) {
    $exploded = explode('-', $value);
    $theegauge = $exploded[1];
    $thevalue = $exploded[0];
    $linevalues[] = array('loadname' => $thedata[0]->load_name , $theegauge => $thevalue) ;

}

Open in new window


in the database the lines for the charts is stored serialized like so:
a:2:{i:0;s:7:"16777-V";i:1;s:7:"16575-P";}

Open in new window

i explode them, cause the first part is the value and the second is the machine it comes from. that is the way they want it.
i need to take it and format it to the array i shared in my original question.

hope now it is more clear, and sorry if i wan't clear enogh earlier.
0
Ray PaseurCommented:
It would be very helpful to see a bit more of the data - For example, if you could run the query, and then use var_dump() to print out a couple of rows of the results set (this appears currently commented out).  We do not really need to see the code, just the data.
0
derridaAuthor Commented:
ray, the first array in my original question is that var_dump
0
Julian HansenCommented:
With Ray on this - still don't have enough pieces.

Looking at your code and serialized data though it seems you could be doing something like this
$linevalues = array();
$thelines = unserialize($thedata[0]->linesforchart);
// V-values are in the first element and P-values in the second so
// Split the strings here and save the values
list($vvalue, $vkey) = explode('-',$thelines[0]);
list($pvalue, $pkey) = explode('-',$thelines[1]);

// Now make the array line with both the V and P values
$linevalues[] = array('loadname' => $thedata[0]->load_name, "V" => $vvalue, "P" => $pvalue);

Open in new window


Can't say the above is correct - you have not provided enough data but that is probably along the lines of what you want.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
This may get us a bit closer, but I would still feel more comfortable seeing more of the data set.  Nobody would put serialized data into a data base this way -- it's just not done like that.  So there may be other anti-patterns lurking in the data and if we can discern those, or rule them out, it might be helpful.

Also, PHP serialize() / unserialize() has some serious design-flaw problems.  I believe that JSON encoding is a much better approach to this sort of thing.  See this link for details.
http://php.net/manual/en/function.unserialize.php#112823

Here's a try: http://iconoun.com/demo/temp_derrida.php
<?php // demo/temp_derrida.php

/**
 * http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28675055.html
 * http://php.net/manual/en/function.unserialize.php
 * http://php.net/manual/en/function.strpos.php
 */
error_reporting(E_ALL);
echo '<pre>';

// A DATA FRAGMENT
$str = 'a:2:{i:0;s:7:"16777-V";i:1;s:7:"16575-P";}';

// WHAT DOES THIS LOOK LIKE?
$arr = unserialize($str);
var_dump($arr);

// TRY TO DECONSTRUCT THE DATA FRAGMENT
$p = '-P';
$v = '-V';
foreach ($arr as $key => $val)
{
    if (strpos($val, $p))
    {
        $val = str_replace($p, NULL, $val);
        echo PHP_EOL . "THE P-VALUE IS $val";
    }

    if (strpos($val, $v))
    {
        $val = str_replace($v, NULL, $val);
        echo PHP_EOL . "THE V-VALUE IS $val";
    }
}

Open in new window

0
Julian HansenCommented:
@Ray - "Nobody would put serialized data into a data base this way " - not sure I agree with you on this. I prefer JSON but personally make extensive use of saving non-searching / non-relationship data in JSON strings. Not strictly relational data but makes coding a lot easier and if the data is not required for searching / joining then there is really no benefit to BCNF'ing the data just to satisfy an ideal.
0
Ray PaseurCommented:
@juilanH: it's not about an abstract concept or an ideal.  It's a security issue and it's a use-of-data issue.  Even the PHP online manual warns us away from these functions.  It's just one of the bad things in PHP, so I recommend that it be avoided.  These are PHP's own words of warning.
http://php.net/manual/en/function.unserialize.php

FALSE is returned both in the case of an error and if unserializing the serialized FALSE value. It is possible to catch this special case by comparing str with serialize(false) or by catching the issued E_NOTICE.

Do not pass untrusted user input to unserialize(). Unserialization can result in code being loaded and executed due to object instantiation and autoloading, and a malicious user may be able to exploit this. Use a safe, standard data interchange format such as JSON (via json_decode() and json_encode()) if you need to pass serialized data to the user.
0
Julian HansenCommented:
... which is why I said I prefer JSON. The point is about how data is stored - relational or serialized (JSON or otherwise).

Proper data filtering is a fundamental requirement in any sane online application - if you are not santizing your data then you are rolling a red carpet out to all and sundry who want a go at your server. Therefore, irrespective of the security flaws in the serialize function (which is a bit of a straw man in this discussion) - the requirement to ensure data is valid before passing it to whatever function will process it - is a requirement.
0
derridaAuthor Commented:
that works. the data is sent like this from machines. althouth i belive i will try to manipulate it after getting it. but that worked for now. thanks all
0
derridaAuthor Commented:
also thanks for this great discussion.
0
Julian HansenCommented:
You are welcome - good luck with it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.