+ Post New Thread
Results 1 to 5 of 5
Coding Thread, [PHP] - Add an array result of a MySQL query to a String in Coding and Web Development; I need some help. Playing with doing something in PHP where the resultant query needs to be an array that ...
  1. #1

    Join Date
    Feb 2008
    Location
    Wiltshire
    Posts
    896
    Thank Post
    282
    Thanked 140 Times in 113 Posts
    Blog Entries
    28
    Rep Power
    42

    [PHP] - Add an array result of a MySQL query to a String

    I need some help.

    Playing with doing something in PHP where the resultant query needs to be an array that I'm then going to throw out to Google Charts API to produce some charts.

    I'm using a mixture of Dreamweaver and adding bits as I fumble along (I'm not a programmer), but so far the following when used in my page does output to the browser the array of data I'm after, however I want to put this into another string so I can output it as a string elsewhere.

    Code:
    <?php $ResultChartX = mysql_query($query_rsSensorData);
    
    while ($row = mysql_fetch_array($ResultChartX, MYSQL_NUM)) {
        printf("%s,", $row[4]);  
    }
    
    ?>
    As I said, the above bit works as the printf is showing the data I want, but I want that output to go into another string (shall we say $chartx_values) not the screen.

    Hope that makes sense, thanks in advance!

    Pete

  2. #2

    webman's Avatar
    Join Date
    Nov 2005
    Location
    North East England
    Posts
    8,413
    Thank Post
    642
    Thanked 964 Times in 664 Posts
    Blog Entries
    2
    Rep Power
    327
    This is one way of doing it:

    Code:
    <?php
    $ResultChartX = mysql_query($query_rsSensorData);
    // Create a new array for the values
    $_values = array();
    while ($row = mysql_fetch_array($ResultChartX, MYSQL_NUM)) {
        // Add value from DB row to the array
        $_values[] = $row[4];
    }
    
    // Glue all the array values together with a comma - $chartx_values is your string.
    $chartx_values = implode(',', $chartx_values);
    ?>

  3. Thanks to webman from:

    FragglePete (20th November 2011)

  4. #3

    Join Date
    Feb 2008
    Location
    Wiltshire
    Posts
    896
    Thank Post
    282
    Thanked 140 Times in 113 Posts
    Blog Entries
    28
    Rep Power
    42
    Didn't work, but noticed the error. However, you put me in the right direction, so many many thanks. Works now:

    Code:
    // Glue all the array values together with a comma - $chartx_values is your string.
     $chartx_values = implode(',', $_values>
    Again, really appreciate the help - put me back on track now.

    This is my first attempt at putting something together using PHP; have to say though, do like the way it goes together.

    Pete
    Last edited by FragglePete; 20th November 2011 at 08:22 AM.

  5. #4

    webman's Avatar
    Join Date
    Nov 2005
    Location
    North East England
    Posts
    8,413
    Thank Post
    642
    Thanked 964 Times in 664 Posts
    Blog Entries
    2
    Rep Power
    327
    Ah yes, sorry - I changed the name of the variable but forgot about that final instance of it. Glad you got it working

  6. #5

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,866
    Thank Post
    412
    Thanked 777 Times in 650 Posts
    Rep Power
    182
    For reference the answer to your original question is to use sprintf(), which returns the string for assignment instead of just outputting it.

    PHP Code:
    <?php
      $monkeys 
    1;
      
    $string sprintf("%d monkeys ready for powdarring"$monkeys);
      echo 
    $string;
    ?>

  7. Thanks to powdarrmonkey from:

    FragglePete (22nd November 2011)

SHARE:
+ Post New Thread

Similar Threads

  1. Add an Article Images
    By drtech in forum EduGeek Joomla 1.5 Package
    Replies: 0
    Last Post: 30th November 2008, 08:32 PM
  2. unable to add an additional dc
    By simongrahamuk in forum Windows
    Replies: 2
    Last Post: 25th February 2008, 02:22 PM
  3. Add an IP address to local host
    By FN-GM in forum Windows
    Replies: 12
    Last Post: 22nd January 2008, 08:29 PM
  4. Thats an.... interesting way of fixing it
    By _Bat_ in forum General Chat
    Replies: 9
    Last Post: 31st March 2007, 05:22 PM
  5. add an application
    By colin in forum Learning Network Manager
    Replies: 6
    Last Post: 20th December 2006, 11:57 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •