1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

2 level array, trying to bring data from database

Discussion in 'PHP' started by ameerulislam43, Dec 14, 2016.

  1. #1
    I feel like banging my head now. I can't solve it.. doesn't make sense.

    $sql = "SELECT x,y FROM datapoints ";
    $result = $conn->query($sql) ;
    
    if ($result->num_rows > 0) {
        // output data of each row
    
       
            $dataPoints = array(
         
             while($row = $result->fetch_assoc()) {
                array("x" => $row["x"], "y" => $row["y"])   
    if  ($result->num_rows > 1) 
                    { echo ",";}
    
           }  
            );
       
      
    } else {
        echo "0 results";
    }
    Code (markup):
    I'm getting this error for this code

    My expected outcome is somthing like this

     
    ameerulislam43, Dec 14, 2016 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,507
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #2
    Sometimes it can be good to use a "tidy" or "format" tool to show you where you've gone wrong. I don't use netbeans for day to day editing but it's perfect for the kind of thing you're doing here (and you'll kick yourself when you see it).

    My real comment is more about how you are trying to create that javascript array. What you have as your expected array isn't going to play nicely. Then again you could be doing something else altogether and I've got the wrong end of the stick.

    Try this instead - untested but should be more or less right

    <?php
    $sql = "SELECT x,y FROM datapoints ";
    $result = $conn->query($sql) ;
    
    if ($result->num_rows > 0) {
        // output data of each row
            $dataPoints = array();
      
             while($row = $result->fetch_assoc()) {
                $dataPoints[] = "{$row['x']} : {$row['y']}";
            );
        echo 'var dataPoints = {'.implode(' , ', $dataPoints).'}';
    } else {
        echo "0 results";
    }
    PHP:
     
    sarahk, Dec 14, 2016 IP
  3. ameerulislam43

    ameerulislam43 Active Member

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    56
    #3
    Thanks but this one didn't work..


    however this code worked that I got from someone..

       if ($result->num_rows > 0) {
            // output data of each row
            while ($row = $result->fetch_assoc()) {
                $dataPoints[] = array("x" => $row["x"], "y" => $row["y"]);
            }
        } else {
            echo "0 results";
        }
    Code (markup):
    I appreciate your help. :)
     
    ameerulislam43, Dec 14, 2016 IP
  4. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #4
    ThePHPMaster, Dec 14, 2016 IP
  5. sarahk

    sarahk iTamer Staff

    Messages:
    28,507
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #5
    lol, yeah, that would do it. I thought you needed to echo out the values...
     
    sarahk, Dec 14, 2016 IP
  6. ameerulislam43

    ameerulislam43 Active Member

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    56
    #6
    I know the basics sir! just have less practice. and I think this wasn't that simple syntax question.
     
    ameerulislam43, Dec 14, 2016 IP
  7. sarahk

    sarahk iTamer Staff

    Messages:
    28,507
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #7
    It was, you had brackets in all the wrong places.
     
    sarahk, Dec 14, 2016 IP
  8. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #8
    Also, you tried to directly assign array-values with a while-loop inside an array-declaration... So no, you don't know basic syntax.
     
    PoPSiCLe, Dec 14, 2016 IP