SitePoint Sponsor

User Tag List

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

    Question Single quote trouble inserting into js script

    Hi, I'm a newbie having a couple problems with my event calendar.

    I can put entries into my database with single quotes, but it screws everything up once I recall it from the database and place it into my events.js script

    Here is the code:

    PHP Code:
    function get_JS(){

    require_once (
    'connectto.php'); // Connect to the db.
    $query "SELECT event_date, event_description, event_link, image, width, height FROM table";
    $result = @mysql_query ($query); // Run the query.

    if ($result) { // If it ran OK, display the records.

    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);
    // This keeps adding events to the string
    $events .= "DefineEvent('$date', '$event', '$link', '$image', $width$height);\n";

    }
    // end while loop

    return $events;

    //end if result loop
    else {
    return;
    }
    }
    //end Function

    if ($js get_JS())
    file_put_contents('events.js'$js);
    else
    echo 
    'error'
    The way it appears in events.js now is...and single quotes appear to be a problem

    DefineEvent('20071122', 'Thanksgiving', '', '', 0, 0);


    I'm under time pressure to finish this...does anyone have a quick solution?

    Help 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

  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)
    Code:
    DefineEvent('20071122', 'Thanksgiving', '', '', 0, 0);
    Javascript should have no problem with that. Perhaps the problem is that it is expecting an integer for the first parameter? If that's the case, remove the quotes.

    Other than that, it is difficult to help you much more because it is impossible to tell what the actual error is. A working page would be better.

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

    Question Calendar doesn't work with single quotes

    If I put enter someone's birthday into the form with a single quote...it enters the script like:

    DefineEvent('20071108', 'Kevin's birthday', '', '', 0, 0);

    Then the whole calendar doesn't work because of the single quote in "Kevin's"

    but it works fine for the other example I showed...

    DefineEvent('20071122', 'Thanksgiving', '', '', 0, 0);

    Does anyone have a working solution to correct this using the original code above? Help!!
    Apache version 1.3.37 (Unix)
    PHP version 5.1.6
    MySQL version 5.0.27-standard-log
    Operating system Linux

  4. #4
    SitePoint Enthusiast newb40's Avatar
    Join Date
    Jul 2007
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Someone earlier recommended:

    $format = 'DefineEvent("%s", "%s", "%s", "%s", %d, %d);'
    $events .= sprintf($format, $date, $event, $link, $image, $width, $height);

    But all I get is a parse error on the $events . line...I looked up the format in the PHP manual, but can't find the mistake...

    I'm stuck....
    Apache version 1.3.37 (Unix)
    PHP version 5.1.6
    MySQL version 5.0.27-standard-log
    Operating system Linux

  5. #5
    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)
    Ah I see. The problem is that if there is a single quote, Javascript will think that quote is the end of the string, and what comes after it is something else, so it causes an error. Just replace instances of ' in the event variable with \':
    PHP Code:
    function get_JS(){



    require_once (
    'connectto.php'); // Connect to the db.

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

    $result = @mysql_query ($query); // Run the query.



    if ($result) { // If it ran OK, display the records.



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



    $height $row['height'];

    $width $row['width'];

    $image $row['image'];

    $link $row['event_link'];

    $event str_replace("'""\\'"$row['event_description']);

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

    $date implode(''$date);

    // This keeps adding events to the string

    $events .= "DefineEvent('$date', '$event', '$link', '$image', $width$height);\n";



    }
    // end while loop



    return $events;



    //end if result loop

    else {

    return;

    }

    }
    //end Function



    if ($js get_JS())

    file_put_contents('events.js'$js);

    else

    echo 
    'error'

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

    Smile It works perfectly...Thanks so much!

    It worked perfectly.

    Thanks Raffles...you rock!

    Till next time....and have a great day...your help is so very much appreciated!
    Apache version 1.3.37 (Unix)
    PHP version 5.1.6
    MySQL version 5.0.27-standard-log
    Operating system Linux


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
  •