0

I have a PHP loop generating a dataset for a chart. It queries two fields I have in a database table.

<?php

    $server = "myserver:1234";
    $user="dbuser";
    $password="userpass";  
    $database = "dbname";

    $connection = mysql_connect($server,$user,$password);
    $db = mysql_select_db($database,$connection);

    $query = "SELECT X, Y FROM dbtable";
    $result = mysql_query($query);        

    while($row = mysql_fetch_assoc($result))
    {
        $dataset1[] = array($row['X'],$row['Y']);
    }
    $final = json_encode($dataset1,JSON_NUMERIC_CHECK);

?>  

Currently data in X is meaningless (it's actually an ID column), and a number increment of 1 starting at 1 will be more useful. This is because I only want to plot a line chart, where X values are fixed. ID would be fine but the values put distracting axis labels on my chart.

How can I use PHP to generate this instead of the database select that I currently have?

Many thanks:)

3

1 Answer 1

2

Declare a variable outside your loop and increment it on each iteration:

$i = 1;
while($row = mysql_fetch_assoc($result))
{
    $dataset1[] = array($i, $row['Y']);
    $i++;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.