Current location: Hot Scripts Forums » Programming Languages » PHP » [SOLVED] Need help with parse error


[SOLVED] Need help with parse error

Reply
  #1 (permalink)  
Old 07-13-08, 03:14 PM
tricks22 tricks22 is offline
New Member
 
Join Date: Jul 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Need help with parse error

Ok heres my code snippet.
PHP Code:

$myfile = "images.php";
            $fh = fopen($myfile, 'w') or die("can't open file");
            
            
            $images = "<?php \";
            
            //Runs a loop that sets 
$images to be a huge long string containing alot of image info for up to 10 images
            for(
$i=1; $i <=$c$i++)
            {
                
$images .= "$img = array(); \n $img[\'filename\'] = \"" . $filename[$i] . "\";\n$img[\'displayname\'] = \"" . $displayname[$i] . "\"; \n $images[] = $img; \n";
            }
            
            $images .="?>";
            
            
            //Writes to images.php
            fwrite($fh, $images);

and im getting this error: T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING


on line 71, which is this line : }

the line before it is probably the problem, its:
PHP Code:

$images .= "$img = array(); \n $img[\'filename\'] = \"" $filename[$i] . "\";\n$img[\'displayname\'] = \"" $displayname[$i] . "\"; \n $images[] = $img; \n"

The purpose of the script is to write:
PHP Code:

<?php
$img 
= array();
$img['filename'] = "a";
            
$img['displayname'] = "a";
            
$images[] = $img;
?>
Into images.php. Can anyone figure out where the error is coming from?

Thanks in advance, really appreciate the help.

Last edited by tricks22; 07-13-08 at 03:16 PM.
Reply With Quote
  #2 (permalink)  
Old 07-13-08, 05:01 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
Instead of writing out the information in an array, you could use a CSV (comma separated values) file - like so:

filename1,displayname1
filename2,displayname2

You can read the file with fgetcsv and write it with fputcsv.

http://us2.php.net/manual/en/function.fgetcsv.php
http://us2.php.net/manual/en/function.fputcsv.php

This is also potentially more secure, as the generated file is not executeable.
Reply With Quote
  #3 (permalink)  
Old 07-13-08, 05:03 PM
tricks22 tricks22 is offline
New Member
 
Join Date: Jul 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
I appreciate the advise, but I'm doing work for somebody else, and they want it in a particular format (using arrays). Also security is not an issue.
Reply With Quote
  #4 (permalink)  
Old 07-13-08, 09:53 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
PHP Code:

$images .= "$img = array(); \n $img[\'filename\'] = \"" $filename[$i] . "\";\n$img[\'displayname\'] = \"" $displayname[$i] . "\"; \n $images[] = $img; \n"
should be able to be rewritten as:

PHP Code:

$images .= "$img = array();\n$img['filename'] = '{$filename[$i]}';\n$img['displayname'] = '{$displayname[$i]}';\n$images[] = $img;\n"
... which should solve the parser error.
Reply With Quote
  #5 (permalink)  
Old 07-14-08, 06:59 PM
tricks22 tricks22 is offline
New Member
 
Join Date: Jul 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
That solves 1 problem. I shouldn't of escaped single quotes.

Also I should of escaped my $ signs that are in double quotes.

Thanks thou.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ and MSSQL tutorials? scott2500uk C/C++ 8 05-11-09 02:33 AM
Syntax Error Nikas Database 4 05-15-08 10:48 AM
I cannot find what i messed up. Parse error: syntax error, unexpected T_CONSTANT_ENCA bilicek.com PHP 3 01-31-08 04:54 PM
Parse error: parse error, unexpected T_VARIABLE aussietogo PHP 4 08-16-05 08:14 PM
[php error] parse error | fatal error xeoHosting PHP 1 01-03-04 08:12 PM


All times are GMT -5. The time now is 07:50 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.