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
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post

    Trying to move data from php array to js array

    The goal here is to use a login page to post data to a php script that creates the info needed to make a calendar month that sends ajax buttons to a users browser, so the user can interact with the calendar by simple point and click, while always staying on the same page. Those ajax/js buttons are created by js functions. Those js functions are executed by calls that use js arrays to hold data, received from corresponding php arrays, which get their data from a php for loop. The js functions use that js array data to create html element id's that allow the calendar to work. Each button uses it's id to know which month, day and time it corresponds to. The js functions need to read the js array elements clearly to know what id's to assign each button they create. Below I wrote small bits of code (not the entire script) to try to demonstrate what I'm trying to do.


    It all starts with a php for loop that places calendar day data (as elements of the array) inside two previously initialized empty arrays:
    PHP Code:
    <?php
    $phpid8AM 
    = array();
    $id8AMuserid = array();  

    $days_in_month date("t"); //The number of days in the current month    
    $M3 date("M"); //Jan, Feb, etc
    $Y date("Y"); //Finds todays year(20xx)

    //Fill empty php arrays with calendar data
    for($d 1$d <= $days_in_month$d++)
    {
                   
    $phpid8AM[$d] = "'".$M3.$d.$Y.'8AM'."'";

                   
    $id8AMuserid[$d] = "'".$M3.$d.$Y.'8AM_user_id'."'";
    }
    ?>

    The next step is to take these two newly filled php arrays and use a loop to put that data into two corresponding js arrays. This step is where I'm stuck. As you read this, please remember this is all happening inside the same php script. I learned that the loop above needs to complete before any process that fills js arrays with that same data can begin.


    I tried using a php heredoc to do this, but I get the errors described further down the page:
    PHP Code:
      print  <<<BOBCAT
    <script type="text/javascript">

    id8AM = [];
    id8AMuserid = [];

    for(i=1; i <= 
    {$days_in_month}; $i++)
    {
     id8AM[i] = 
    {$phpid8AM[i]};
    id8AMuserid[i] = 
    {$id8AMuserid[i]};
    }

    </script>
    BOBCAT; 

    I get this js error:
    Code:
    id8AM is not defined

    I get these php errors:
    PHP Code:
    "use of undefined constant i" and "undefined index i" both in this line:
     
    id8AM[i] = {$phpid8AM[i]};

    The very same errors above apply to this line:
    id8AM[i] = {$phpid8AM[i]}; 

    DrDOS gave me this code as an example awhile back (thank you). The BOBCAT heredoc above is my attempt to use it, but I'm not sure what parts I need, or if I need it at all. This entire script I'm writing is php with js tucked into it, so js has to be put in heredocs. The script below (thanks DrDOS) is php nested inside js <script> tags:
    PHP Code:
    <script type="text/javascript">
    myarray = [];
    <?php
    for($i 0$icount($mystyle); $i++)
    {
         echo 
    "myarray.push('".$mystyle[$i]."');\n";
    }
    ?>
    for (i = 0; i = myarray.length; i++)
    {
       the data[i].value = myarray[i];
    }
    </script>

    The final step will be putting these newly created js array elements (created by the loop above I'm having trouble with) in a js loop that tells the browser to call these functions.
    PHP Code:
       print  <<<SALLY
    </script type="text/javascript">
    for(s=1; s <= 
    {$days_in_month}; s++)
    {
    firstFunctionCall(id8AM[s], id8AMuserid[s]);
    secondFunctionCall(id8AM[s], id8AMuserid[s]);
    }
    </script>
    SALLY; 

    I wrote the example below to show that I'm using function arguments correctly.The example below is just an example. It is not one of the actual js functions. The actual functions are sent to the browser in a php heredoc that is not in a loop. The heredoc places the js functions in the head of the html document the browser creates:
    Code:
    function firstFunctionCall(firstargument, secondargument)
    document.write(firstargument);
    document.write(secondargument);
    As far as I can tell, the middle step, where php array element data is transferred to js array elements is where I need help.

    Thank you
    Last edited by milesdriven; 08-13-2012 at 10:50 PM.

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,046
    Thanks
    15
    Thanked 241 Times in 241 Posts
    I don't think you can use a loop inside a heredoc, in any case the heredoc is superfluous, if you do it just way I showed you it will work.

  • #3
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by DrDOS View Post
    I don't think you can use a loop inside a heredoc, in any case the heredoc is superfluous, if you do it just way I showed you it will work.
    Part of your push example:
    PHP Code:
    myarray = [];
    <?php
    for($i 0$i count($mystyle); $i++)
    {
        echo 
    "myarray.push('".$mystyle[$i]."');\n";
    }
    ?>
    The variable $i in the loop starts at zero. The first day of the month is 1.


    Is there a way to write:
    PHP Code:
    echo "myarray.push('".$mystyle[$i]."');\n"
    so it ignores zero and starts at 1?

    I know if I write a line like this:
    PHP Code:
    id8AM[] = some data 


    ...and put it in a loop, the first index of id8AM will be zero. I don't know if using the push example above will also start the first index at zero. I have to be sure the number the for loop counter starts at is the same the array index starts at. I'd rather have 1 than zero:

    Thanks very much for your help.

  • #4
    New Coder
    Join Date
    Aug 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Use method:
    PHP Code:
    <?php

    $php_array
    =array("text","demo");

    $js_array=json_encode($php_array);

    echo 
    $js_array;

    ?>


  •  

    Posting Permissions

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