SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Enthusiast newb40's Avatar
    Join Date
    Jul 2007
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Can you access php/mysql before javascript?

    Hi I'm a newbie that is trying to make an event calendar.

    I found a free one on the web that works great...but I was wondering if I could modify it to access my own database for the events on the calendar (instead of manually changing the html or javascript code every time)

    Additionally this feature would hopefully enable others to enter data on a form so that they could insert events on the calendar as well.

    Here is the code for my html page:


    <script src="calendar.js"></script>

    <script language="JavaScript">

    <!--
    DefineEvent(20031130, "Mom and Dad's Anniversary", "", "", 0, 0);
    Calendar( );
    // -->
    </script>

    (Where DefineEvent is an array describing "date", "event in text", "hyperlink", "image", "image height", "image width")

    I realize that PHP/MySQL is server side and JavaScript is client side (I don't know any JavaScript)..but can you access the database first...and subsequently access the query variables using JavaScript?

    If so...how would you do it...and where would it be written in relation to the code above?

    Anyone have helpful thoughts?
    Apache version 1.3.37 (Unix)
    PHP version 5.1.6
    MySQL version 5.0.27-standard-log
    Operating system Linux

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You have two options here: on the server side, using your data from the database create an array for all the possible events that the user might want to access. Or perhaps only for a month before and after the current date, having to refresh the page for dates after that, because doing it for a huge amount of events could mean the arrays are enormous and thus make your Javascript enormous and slow to download.

    The other, more modern and better solution is to use Ajax. Thus you could load only the current month's events and when the user clicks "next month", you make an Ajax call to a script on your server which queries the database and returns the next month's events.

    There appear to be a few tutorials and scripts that help you do this:
    http://www.google.co.uk/search?q=ajax+php+calendar
    http://www.google.co.uk/search?q=aja...mysql+calendar

  3. #3
    SitePoint Enthusiast newb40's Avatar
    Join Date
    Jul 2007
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How about the old fashioned way?

    Thanks for the links!

    So if I were to use your first solution (the "old fashioned and slow" way) of creating a PHP/MySQL array of the variables that I need to access and stick them into the JavaScript...would it work like...

    <? Form validation code here...

    $query = SELECT date, event FROM table

    $result = mysql_query($query)

    while ($row=mysql_fetch_array($result)) {


    <script src="calendar.js"></script>

    <script language="JavaScript">

    <!--
    DefineEvent($row[date], $row[event]);
    Calendar( );
    // -->
    </script>

    }

    Form code down here...

    ?>


    This is obviously a total guess....and I assume its probably not correct...

    Can someone show me a brief outline or skeleton of how the code should be structured?
    Apache version 1.3.37 (Unix)
    PHP version 5.1.6
    MySQL version 5.0.27-standard-log
    Operating system Linux

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yeah, something like that. Though instead of having loads of function calls to DefineEvent, it would be better to create one single associative array, like this (it's actually an object literal, when it's with curly braces {}):
    PHP Code:
    <?php
    echo "var calEvents = {\n";
    while (
    $row=mysql_fetch_array($result)) {
      echo 
    $row['date'] . ': \'' $row['event'] . "';\n";
    }
    echo 
    '}';
    ?>
    which should create something like:
    Code Javascript:
    var calEvents = {
      '1-11-07': 'Mike\'s birthday';
      '2-11-07': 'Dave\'s birthday';
      '3-11-07': 'Jane\'s birthday';
    }
    You obviously have to add code do escape the apostrophes such as ' in "Mike's" into \' so you don't get javascript errors.

    Then you could simply output this object into a separate javascript file, or one you're already creating dynamically, using PHP's file_put_contents(). Say you call it "calevents.js", then just put this in your <head>:
    HTML Code:
    <script type="text/javascript" src="calevents.js"></script>
    Nice and unobtrusive and not a horrible mess in the middle of your HTML form.

  5. #5
    SitePoint Enthusiast newb40's Avatar
    Join Date
    Jul 2007
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Help with parsing

    Thanks for your help!

    I almost had it...but now I'm stuck on syntax and parsing errors.

    I'm trying to make my src="calevents.js" page look exactly like...

    DefineEvent(20071101, "Parents Night and Book Fair", "", "", 0, 0);
    DefineEvent(20071103, "Book Fair", "", "", 0, 0);

    because if this is printed verbatim in calevents.js ...I can watch it work perfectly in action...

    but the date that I retrieve has dashes...2007-11-01, which doesn't work, and now I'm also getting a parse error that I can't get out of...

    here is my code...

    $query = "SELECT event_date, event_description, event_link, image, width, height FROM whatevertable";

    $result = @mysql_query ($query); // Run the query.
    if ($result) { // If it ran OK, display the records.
    while ($row=mysql_fetch_array($result)) {

    echo " DefineEvent '(' . $row['event_date'] . ', \'' . '"' . $row['event_description'] . '", \'' . '"' . $row['event_link'] . '", \'' . '"' . $row['image'] . '", \'' . $row['width'] . ', \'' . $row['height'] . ");\n";
    }


    Can someone help me with the syntax to make it print exactly like

    DefineEvent(20071101, "Parents Night and Book Fair", "", "", 0, 0);
    DefineEvent(20071103, "Book Fair", "", "", 0, 0);

    Helpful suggestions would be very much appreciated.
    Apache version 1.3.37 (Unix)
    PHP version 5.1.6
    MySQL version 5.0.27-standard-log
    Operating system Linux

  6. #6
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Well, you obviously need to put the date you retrieve through something to take the dashes away:
    PHP Code:
    while ($row=mysql_fetch_array($result)) {

    $date explode('-'$row['event_date']);
    $date implode(''$date);
    echo 
    " DefineEvent '(' . $date . ', \'' . '"' . $row['event_description'] . '", \'' . '"' . $row['event_link'] . '", \'' . '"' . $row['image'] . '", \'' . $row['width'] . ', \'' . $row['height'] . ");\n";


  7. #7
    SitePoint Enthusiast newb40's Avatar
    Join Date
    Jul 2007
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Parsing

    Awesome! Thanks...

    but now I get this error...

    Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' on line 28

    code for line 28:

    echo "DefineEvent '(' . $date . ', \'' . '"' . $row['event_description'] . '", \'' . '"' . $row['event_link'] . '", \'' . '"' . $row['image'] . '", \'' . $row['width'] . ', \'' . $row['height'] . ");\n";


    Goal is to make it look like..

    DefineEvent(20071101, "Parents Night and Book Fair", "", "", 0, 0);
    DefineEvent(20071103, "Book Fair", "", "", 0, 0);

    I feel stupid, but I really don't know where the error is because as a newbie, my knowledge of proper syntax is not great...can someone help?
    Apache version 1.3.37 (Unix)
    PHP version 5.1.6
    MySQL version 5.0.27-standard-log
    Operating system Linux

  8. #8
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You're mixing up double and single quotes. Check that you're handling them properly, that when you open one type it then gets closed properly and that any quotes in between of the same type are escaped. If they're of the other type they don't need escaping. A text editor with syntax highlighting can be very helpful with this.

  9. #9
    SitePoint Enthusiast newb40's Avatar
    Join Date
    Jul 2007
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Using the code:

    $date = explode('-', $row['event_date']);
    $date = implode('', $date);

    echo $row[$date] . ', \'' . $row['event_date'] . "';\n";


    I get:

    , '2007-08-08'; , '2007-10-10';


    I'm confused....

    1) Why don't the dashes go away?
    2) Why are there ' marks around the date? (I want to get rid of them)
    3) Why does a comma precede everything?

    Any thoughts?
    Apache version 1.3.37 (Unix)
    PHP version 5.1.6
    MySQL version 5.0.27-standard-log
    Operating system Linux

  10. #10
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You need the ' because javascript needs to understand this. Look at my previous post again, you're not supposed to echo $row[$date] but $date itself, and then obviously you don't need to echo $row['event_date'] as well.

  11. #11
    SitePoint Enthusiast newb40's Avatar
    Join Date
    Jul 2007
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How do I get this into calEvents.js?

    Well I was able to print out what I needed using...

    while ($row=mysql_fetch_array($result)) {

    $height = $row['height'];
    $width = $row['width'];
    $image = $row['image'];
    $link = $row['event_link'];
    $event = $row['event_description'];
    $date = explode('-', $row['event_date']);
    $date = implode('', $date);

    echo "DefineEvent", "(", $date, ", ", '"', $event, '", ', '"', $link, '", ', '"', $image, '", ', $width, ', ', $height, ');', "<br />";

    }

    which printed out what I wanted to eventually put into src=calEvents.js...

    but how do I get this printed text over to that file? I tried putting all the PHP stuff in there and it didn't work...

    Any helpful suggestions?
    Apache version 1.3.37 (Unix)
    PHP version 5.1.6
    MySQL version 5.0.27-standard-log
    Operating system Linux

  12. #12
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Please use syntax highlighting when you post code.

    I mentioned file_put_contents() before. Click on the link and it will show you what to do. Note that it's a PHP5-only function, so if you're on PHP4 (which you shouldn't be any more really) you'll have to use fopen, fwrite and fclose in that order.


Bookmarks

Posting Permissions

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