Results 1 to 5 of 5

Thread: write php code using fwrite

  1. #1
    Join Date
    Dec 2008
    Posts
    48
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default write php code using fwrite

    hi, i am trying to update a file every so often with a couple of variables. having some troubles tho.

    PHP Code:
    $strsql = "select tip_text from tips order by rand() limit 1";
    $resulttip=query($strsql);

    $tip_text = $resulttip[0][tip_text];

    $myFile = "../../includes/siteglobal_cache.php";
    $fh = fopen($myFile, 'w') or die("can't open file");

    $fileData = '<? ';
    $fileData .= ' 
    $site_tip_text $tip_text';

    $fileData .= ' 
    ?>';

    fwrite($fh, $fileData);
    fclose($fh);


    what i want the siteglobal_cache.php to be saved is for example:

    PHP Code:
    <?  $site_tip_text "never eat too many <a href="cookies.php">cookies</a>";  ?>
    as you can see there is a problem with all the " and '... i am a bit lost, can someone clarify on how i should edit this code please? cheers
    Last edited by nicmo; 04-23-2009 at 04:05 PM.

  2. #2
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    "" allows variables to be in it, while '' is purely textual, no variables. If you take the variable out of quotes it should work:
    PHP Code:
    $fileData .= ' $site_tip_text = ' $tip_text '; '
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@ /\/\@|{
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  3. #3
    Join Date
    Dec 2008
    Posts
    48
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default

    that would come up as

    PHP Code:
    <?  $site_tip_text never eat too many <a href="cookies.php">cookies</a>;  ?>
    wich doesnt work

    PHP Code:
    $fileData .= ' $site_tip_text = "' $tip_text '"; '
    works on tips that dont have html... in the example above it would not work because of the HTML's ""

    what to do?

  4. #4
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    This is sort of confusing me with all the variables and different single quotes and double quotes but this:

    PHP Code:
    <?  $site_tip_text "never eat too many <a href="cookies.php">cookies</a>";  ?>
    Could be changed to :

    PHP Code:
    <?  $site_tip_text "never eat too many <a href=\"cookies.php\">cookies</a>";  ?>
    This now escapes the double quote and instead of terminating the string outputs the literal double quote.

    This may not be what you're looking for but I'm still sort of confused by what you've put. Just tell me if what I've said helps you in any way.

  5. #5
    Join Date
    Dec 2008
    Posts
    48
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default

    ok, did the following and seems to be working!

    PHP Code:
    $strsql = "select tip_text from tips order by rand() limit 1";
    $resulttip=query($strsql);

    $tip_text = str_replace(array('"'), '\"',$resulttip[0][tip_text]);

    $myFile = "../../includes/siteglobal_cache.php";
    $fh = fopen($myFile, 'w') or die("can't open file");


    $fileData = '<? ';
    $fileData .= ' 
    $site_tip_text "' . $tip_text . '"'; 

    $fileData .= ' 
    ?>';

    fwrite($fh, $fileData);
    fclose($fh);

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
  •