Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jul 2011
    Posts
    272
    Thanks
    63
    Thanked 1 Time in 1 Post

    Turn rows into array

    Okay I'm stumped. I have a javascript code that has an array as follows:

    var cnt=0,webpageArray = ["http://youtube.com",
    "http://facebook.com"];
    and I'm trying to change the code so that the links come from a TABLE I have setup. How do I return the array of the following query in order to work in my situation? PDO MYSQL please!

    $query = "SELECT URL FROM path WHERE campaigns LIKE '%-$currentFile-%'";
    Last edited by markman641; 10-18-2012 at 03:16 AM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,986
    Thanks
    79
    Thanked 4,432 Times in 4,397 Posts
    Code:
    <script type="text/javascript">
    var webpageArray = [
    <?php
    $query = "SELECT URL FROM ...."
    $result = mysql_query($query) or die( mysql_error() );
    $delimiter = "";
    while ( $row = mysql_fetch_array($result) )
    {
        echo $delimiter . '"' . $row[0] . '"' ;
        $delimiter = ",\n";
    }
    ?>
        ];
    </script>
    I don't use PHP, but that seems simple enough for even me to do.

    There may be more efficient ways in PHP, but I can't see why that won't work.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    markman641 (10-18-2012)

  • #3
    Regular Coder
    Join Date
    Jul 2011
    Posts
    272
    Thanks
    63
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Old Pedant View Post
    Code:
    <script type="text/javascript">
    var webpageArray = [
    <?php
    $query = "SELECT URL FROM ...."
    $result = mysql_query($query) or die( mysql_error() );
    $delimiter = "";
    while ( $row = mysql_fetch_array($result) )
    {
        echo $delimiter . '"' . $row[0] . '"' ;
        $delimiter = ",\n";
    }
    ?>
        ];
    </script>
    I don't use PHP, but that seems simple enough for even me to do.

    There may be more efficient ways in PHP, but I can't see why that won't work.
    Great job man, worked like a charm Although you forgot a semicolon after the query

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,986
    Thanks
    79
    Thanked 4,432 Times in 4,397 Posts
    Well, the query wouldn't have done anything as written even with a semicolon.

    I was just trying to show you where I was plugging in the code after your original $query line.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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