Home > Archive > PHP Language > August 2005 > New to PHP, what is the error in this file?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
New to PHP, what is the error in this file?
|
|
| Frank Stallone 2005-08-19, 4:13 pm |
| I'm fairly new to PHP. Because I've done some programming years ago I
was hoping I could sort of jump into it without going to the absolute
basics but I'm thinking that's not the case.
What I want is to have a PHP page read data values off of a xml
document and put them into html.
I'm using PHP-dev. The problem is when I do a syntax check it doesn't
take me to the line where the problem is.
So I uploaded the files and IE is telling me "Parse error: parse
error, unexpected $ in
/usr/home/web/users/a0018893/html/uploads/review.php on line 125
"
which is the ?> at the end. That doesn't make no sense because there
is no $ in that line.
What I did was take a tutorial file and try to tailor fit it to what I
want and if I can get it to work then I want to code my own PHP file
to write the XML so I don't have to hand type text into the xml file,
upload it, etc, etc. I read up on syntax, etc, etc and I thought I had
the gist of it and if I could see it in read mode then I could take
what I learned about write mode and work with my combined knowledge to
made the "write" page and like I said it's not going well.
The XML file(review.xml) looks like this:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<review>
<id>1</id>
<title>Zelda</title>
<body>sweet</body>
<score>5 Stars</score>
</review>
The PHP file (review.php) looks like this:
<?php
// the xml file to parse
$xmlSource="review.xml";
//defined variables
$id="";
$title="";
$body="";
$score="";
$currentElement=""; //holds the name of the current element
$shows=array(); //array to hold all the data
function endElement($parser,$name){
$elements=array('id','title','body','sco
re');
/*After parsing a movie we reset the rest of the globals.*/
if(strcmp($name,"review")==0){
$GLOBALS['id']="";
$GLOBALS['title']="";
$GLOBALS['body']="";
$GLOBALS['score']="";
}
}//end endElement()
/* The character data Handler
* Depending on what the currentElement is,
* the handler assigns the value to the appropriate variable
*/
function characterData($parser, $data) {
$elements = array (
'id','title', 'body', 'score');
foreach ($elements as $element) {
if ($GLOBALS["currentElement"] == $element) {
$GLOBALS[$element] .= $data;
}
}
}
/* This is where the actual parsing is going on.
* parseFile() parses the xml document and return an array
* with the data we asked for.
*/
function parseFile(){
global $xmlSource,$shows;
/*Creating the xml parser*/
$xml_parser=xml_parser_create();
/*Register the handlers*/
xml_set_element_handler($xml_parser,"startElement","endElement");
xml_set_character_data_handler($xml_pars
er,"characterData");
/*Disables case-folding. Needed for this example*/
xml_parser_set_option($xml_parser,XML_OP
TION_CASE_FOLDING,false);
/*Open the xml file and feed it to the parser in 4k blocks*/
if(!($fp=fopen($xmlSource,"r"))){
die("Cannot open $xmlSource ");
}
while(($data=fread($fp,4096))){
if(!xml_parse($xml_parser,$data,feof($fp
))){
die(sprintf("XML error at line %d column %d ",
xml_get_current_line_number($xml_parser)
,
xml_get_current_column_number($xml_parse
r)));
}
}
/*Finish ! we free the parser and returns the array*/
xml_parser_free($xml_parser);
return $shows;
}//end parseFile()
/ ****************************************
****************************************
*******************
Calling the parseFile() and getting the result out in
a simple html-table
****************************************
****************************************
*******************/
$result=parseFile();
print '<table border="1">';
foreach($result as $arr){
/*check on movieId to see if we reached a new movie.
* If so we print out the movieName
*/
if(strcmp($id,$arr["id"])!=0){
print '
<tr>
<td colspan="3"><b>'.$arr["title"].'</b></td>
</tr>
<tr>
<td colspan="3">'.$arr["body"].'</td>
</tr>
<tr>
<td colspan="3">'.$arr["score"].'</td>
</tr>';
$id=$arr["id"];
}
print '</table>';
?>
I would definitely appreciate any help I receive.
| |
| Ken Robinson 2005-08-19, 4:13 pm |
| Frank Stallone <faux_ask_me@lollerskates.com> wrote in
news:2secg1dste502b31vstafl3rp96n9lgrb1@
4ax.com (in part):
>
> So I uploaded the files and IE is telling me "Parse error: parse
> error, unexpected $ in
> /usr/home/web/users/a0018893/html/uploads/review.php on line 125
> "
> which is the ?> at the end. That doesn't make no sense because there
> is no $ in that line.
Usually you get that error when you're missing a ending brace "}"
[BIG Snip}
>
> print '<table border="1">';
> foreach($result as $arr){
>
> /*check on movieId to see if we reached a new movie.
> * If so we print out the movieName
> */
> if(strcmp($id,$arr["id"])!=0){
> print '
> <tr>
> <td colspan="3"><b>'.$arr["title"].'</b></td>
> </tr>
> <tr>
> <td colspan="3">'.$arr["body"].'</td>
> </tr>
> <tr>
> <td colspan="3">'.$arr["score"].'</td>
> </tr>';
>
> $id=$arr["id"];
>
>
> }
>
> print '</table>';
> ?>
Notice, you have an opening brace after the foreach() and if()
statements, but you only have one closing brace. You can safely remove
the opening brace after the if() statement, since you don't need them if
only one statement is being excuted as a result of the if().
Ken
| |
| Frank Stallone 2005-08-22, 6:58 pm |
|
Thank you.
Clearly I need to get back into to the habit of looking over code and
being able to spot things like that. Unfortuantely I'm probably
farther away from that point than I'd like. ;)
On Fri, 19 Aug 2005 20:24:56 GMT, Ken Robinson
<sendspamhere@rbnsn.com> wrote:
>Frank Stallone <faux_ask_me@lollerskates.com> wrote in
> news:2secg1dste502b31vstafl3rp96n9lgrb1@
4ax.com (in part):
>
>
>Usually you get that error when you're missing a ending brace "}"
>
>[BIG Snip}
>
>
>
>Notice, you have an opening brace after the foreach() and if()
>statements, but you only have one closing brace. You can safely remove
>the opening brace after the if() statement, since you don't need them if
>only one statement is being excuted as a result of the if().
>
>Ken
|
|
|
|
|