Can someone explain a parsing error when trying to adapt BBC Options For News?

Started by Grammy, May 04, 2016, 10:10:39 PM

Previous topic - Next topic

Grammy

Hi,

I'm new to SMF and to this forum, and I've been looking all through modifications without finding what I'm needing until I ran across this thread:  http://www.simplemachines.org/community/index.php?topic=337912.msg2276542#msg2276542  This seems to be exactly what I want but I guess the template the guy was working with is outdated?  My forum is 2.0.11, default theme.

I don't know much at all about PHP but I've spent the last few days trying to adapt his code to fit my ManageNews.template.php.  I've had some success with a great deal of it, as far as not throwing parsing errors, but there are a few similar blocks of code that are throwing errors, until I remove a specific line.  But I'm sure these lines are necessary, surely!  A sample of a block of code is:


// Begin size dropdown

echo '
<select onchange="surroundText('[size='   this.options[this.selectedIndex].value.toLowerCase()   ']', '[/size]', eval(box)); this.selectedIndex = 0; eval(box).focus(eval(box));" style="margin-bottom: 1ex;">
<option value="" selected="selected">', $txt['font_size'], '</option>
<option value="1">8pt</option>
<option value="2">10pt</option>
<option value="3">12pt</option>
<option value="4">14pt</option>
<option value="5">18pt</option>
<option value="6">24pt</option>
<option value="7">36pt</option>
</select><br />';


This block throws parsing errors, unless I remove this:

'[size='   this.options[this.selectedIndex].value.toLowerCase()   ']', '[/size]',

Then it's fine.  But surely this is important?

The issue is the same with the color drop down and font drop down.  In the color menu, the page loads without a parsing error if I omit

'[color='   this.options[this.selectedIndex].value.toLowerCase()   ']', '[/color]',

and with the font menu, it loads without an error if I omit

'[font='   this.options[this.selectedIndex].value.toLowerCase()   ']', '[/font]',

These all seem similar to me but, as I said, I don't understand PHP and I've been running it through a PHP validator and Googling all night, but I guess I'm tired.  It's over my head.  Can anyone point out why including these bits of code throw the error?

With the full block in the validator, the error is "unexpected '=', expecting ']' in your code " and if I put only the part I omit into the validator, the error is "unexpected 'this' (T_STRING) in your code" (as in this.options).

I do have one more small piece of code causing an issue, but if I can first learn about this bit, I expect I shouldn't muddy the waters with something else.

The Source files and Language files were easily edited with no issues.  I would so appreciate any help.  I'm attaching a small image of what the news posting area looks like, so far, with the bit of code I've managed to adapt.  The issue is that the text is unchanged after selection (but with bits of code omitted, what else should I expect?).

Thanks so much for any help.


Editing to add that I was thinking about something else when I mentioned the source files editing easily.  This doesn't use source files.   :)

qc

How about this code?
echo '
<select onchange="surroundText(\'[size=\'   this.options[this.selectedIndex].value.toLowerCase()   \']\', \'[/size]\', eval(box)); this.selectedIndex = 0; eval(box).focus(eval(box));" style="margin-bottom: 1ex;">
<option value="" selected="selected">', $txt['font_size'], '</option>
<option value="1">8pt</option>
<option value="2">10pt</option>
<option value="3">12pt</option>
<option value="4">14pt</option>
<option value="5">18pt</option>
<option value="6">24pt</option>
<option value="7">36pt</option>
</select><br />';


A single quote within an echo '...'; statement must be escaped with a leading "\".
Playing quizduell? Having quizduell questions? Our german quizduell forum quizcommunity.de is looking for quiz freaks to come and play quizduell with us :)

Grammy

Oh, thank you, qc!  That loads beautifully!  Thanks for explaining about the single quotes.  I'm so hoping to learn, if I hang out here enough.  It's still not allowing the changes because, when I highlight (select) the text then use the pulldown, using the pulldown results in the text becoming un-selected.  I expect this might be because of the last remaining bit of code I haven't been able to upload, which is

foreach ($context['bbc_tags'] as $image => $tag) {
echo '
<a href="javascript:void(0);" onclick="surroundText('', $tag['before'], '', '', $tag['after'], '', eval(box)); return false;">'.
'<img onmouseover="bbc_highlight(this, true);" onmouseout="if (window.bbc_highlight) bbc_highlight(this, false);" src="', $settings['images_url'], '/bbc/', $image, '.gif" align="bottom" width="23" height="22" alt="', $tag['description'], '" title="', $tag['description'], '" style="background-image: url(', $settings['images_url'], '/bbc/bbc_bg.gif); margin: 1px 2px 1px 1px;" /></a>';
}


It does seem as if that bit is the "works".  Is that also a case of needing leading slashes?  Putting it into the validator gives me "unexpected '', $tag['' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';' in your code".

Thanks so much for your help!

qc

Quote from: Grammy on May 04, 2016, 10:35:36 PM
Is that also a case of needing leading slashes?
Yes. You need to add a slash in front of all single quotes within an echo '...'; statement. PHP is very simple minded: if it encounters an echo-statement, it will look for the first quote, then print everything until it encounters another quote. Then it will stop and expect a semicolon to indicate the end of the echo statement.

This works well for e.g. echo 'Hello';

But if you want to print a quote, you have to tell PHP not to stop but continue printing by adding a slash: echo 'Peter\'s shirt is blue';

By the way: If you use double quotes instead of single quotes for your echo "..."; statement, you don't have to escape single quotes but double quotes within the ... string that should be printed.
Playing quizduell? Having quizduell questions? Our german quizduell forum quizcommunity.de is looking for quiz freaks to come and play quizduell with us :)

Grammy

Quote from: qc on May 04, 2016, 10:45:30 PM

Yes. You need to add a slash in front of all single quotes within an echo '...'; statement. PHP is very simple minded: if it encounters an echo-statement, it will look for the first quote, then print everything until it encounters another quote. Then it will stop and expect a semicolon to indicate the end of the echo statement.

This works well for e.g. echo 'Hello';

But if you want to print a quote, you have to tell PHP not to stop but continue printing by adding a slash: echo 'Peter\'s shirt is blue';

By the way: If you use double quotes instead of single quotes for your echo "..."; statement, you don't have to escape single quotes but double quotes within the ... string that should be printed.

I'll give it a go, thanks!  I was wondering...  when you see something like src="', must that second (single) quote have a leading slash?  Also, when you see two together, but spaced, as in '', eval(box));?  Must that be \' \' ?

qc

Actually, I was a bit imprecise in my previous answer. PHP does not only accept a semicolon after the closing quote, but also a simple comma:

echo 'Say ', 'hi ', 'to ', 'you!'; // This prints "Say hi to you!"

which is identical to

echo 'Say '; echo 'hi '; echo 'to '; echo 'you!'; // This prints "Say hi to you!"

You can see that a comma is just a shortcut to avoid writing echo many times. Instead, just write echo once, and give it a comma separated list of quoted strings to print.

Also, if you are unsure of how PHP executes some code, just try it out! If you don't have PHP installed on your local computer, you can play around at e.g. http://sandbox.onlinephpfunctions.com/

Learning by doing :)
Playing quizduell? Having quizduell questions? Our german quizduell forum quizcommunity.de is looking for quiz freaks to come and play quizduell with us :)

Grammy

Thanks, qc!

I'll tuck it in for the night, but I'll hit the bricks tomorrow and see if I can sort it.  Thanks for taking the time to explain things; you've been a huge help!  I'll get this sorted yet! 

(I can't understand why the project on that thread was never turned into a mod and it looks as if it's been abandoned, but I think people would love it.)  But it will take better minds than mine, ha! 

Thanks again!   :)

Grammy

Well...   I'm getting close, in that the page is loading without error, but part of the code is visible on the page.  So...  I "printed" it?  I'm attaching an image of it.  And I'm guessing this is why it's still not allowing me to kept text selected when using the pulldown, so that it can be affected by a BBC choice.   :-\

I was wondering...  in the initial block of code that worked after the edits, there are still single quotes.  Here is an example after that first edit:

// Begin size dropdown
echo '
<select onchange="surroundText(\'[size=\'   this.options[this.selectedIndex].value.toLowerCase()   \']\', \'[/size]\', eval(box)); this.selectedIndex = 0; eval(box).focus(eval(box));" style="margin-bottom: 1ex;">
<option value="" selected="selected">', $txt['font_size'], '</option>
<option value="1">8pt</option>
<option value="2">10pt</option>
<option value="3">12pt</option>
<option value="4">14pt</option>
<option value="5">18pt</option>
<option value="6">24pt</option>
<option value="7">36pt</option>
</select><br />';


That now works just fine, even though this bit still has single quotes within the echo:

<option value="" selected="selected">', $txt['font_size'], '</option>

So, I guess my question is, am I showing code on my page, now, after editing that last bit of code I asked about, because I "slashed" it to death?  Is that why some is printing on the page?  Here is the original:

foreach ($context['bbc_tags'] as $image => $tag) {
echo '
<a href="javascript:void(0);" onclick="surroundText('', $tag['before'], '', '', $tag['after'], '', eval(box)); return false;">'.
'<img onmouseover="bbc_highlight(this, true);" onmouseout="if (window.bbc_highlight) bbc_highlight(this, false);" src="', $settings['images_url'], '/bbc/', $image, '.gif" align="bottom" width="23" height="22" alt="', $tag['description'], '" title="', $tag['description'], '" style="background-image: url(', $settings['images_url'], '/bbc/bbc_bg.gif); margin: 1px 2px 1px 1px;" /></a>';
}


And here it is, by the time I got through with it:

foreach ($context['bbc_tags'] as $image => $tag) {
echo '
<a href="javascript:void(0);" onclick="surroundText(\'\', $tag[\'before\'], \'\', \'\', $tag[\'after\'], \'\', eval(box)); return false;">\'.
\'<img onmouseover="bbc_highlight(this, true);" onmouseout="if (window.bbc_highlight) bbc_highlight(this, false);" src="\', $settings[\'images_url\'], \'/bbc/\', $image, \'.gif" align="bottom" width="23" height="22" alt="\', $tag[\'description\'], \'" title="\', $tag[\'description\'], \'" style="background-image: url(\', $settings[\'images_url\'], \'/bbc/bbc_bg.gif); margin: 1px 2px 1px 1px;" /></a>';
}


And you can see by the attached pic, I pretty much took a machete to it.   :(

I'll still keep at it, but just dropping by to let you know how I've botched it, so far.  I'll take your advice and mess with it over at the Sandbox link.  Thanks!

Kindred

<option value="" selected="selected">', $txt['font_size'], '</option>

this is fine... see....
<option value="" selected="selected">' (this means stop the echo)

, (this means concatenate the following)

$txt['font_size'] (add this CODE to the echo (e.g. since the $txt array is an array of strings, read out the string)

, (this means concatenate the following)

' (restart echoing normal text or html)

</option> (yup, here's more html which will be echoed)


you have to escape single quotes WITHIN an echo
e.g. when including javascript which needs to send the single quotes as part of its own code
or
when sendin an apostrophe, like "he's going to the store"
in code, this would be
echo 'he\'s going to the store';
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

Grammy

Wouldn't you know that my first stab at editing PHP would involve a bit of code with every conceivable combination of quotes and single quotes you could dream of?  That bit of code that resulted in appearing on my page is just chock full of them!   :D

I think part of my problem has been not understanding an echo's actual span of influence (if that's not a weird way to put it).  Start and stop, there, that's better!   :)

Thanks, Kindred! I think I feel encouraged enough to get back in there and take another stab at it. 

Kindred

Code (php) Select

foreach ($context['bbc_tags'] as $image => $tag) {
echo '
<a href="javascript:void(0);" onclick="surroundText(\'', $tag['before'], '\', \'', $tag['after'], '\', eval(box)); return false;">
<img onmouseover="bbc_highlight(this, true);" onmouseout="if (window.bbc_highlight) bbc_highlight(this, false);" src="', $settings['images_url'], '/bbc/', $image, '.gif" align="bottom" width="23" height="22" alt="', $tag['description'], '" title="', $tag['description'], '" style="background-image: url(', $settings['images_url'], '/bbc/bbc_bg.gif); margin: 1px 2px 1px 1px;" /></a>';
}

      
      which would read out as
Code (html) Select

<a href="javascript:void(0);" onclick="surroundText('BEFORE_TAG_TEXT', 'AFTER_TAG_TEXT', eval(box)); return false;"> <img onmouseover="bbc_highlight(this, true);" onmouseout="if (window.bbc_highlight) bbc_highlight(this, false);" src="', $settings['images_url'], '/bbc/', $image, '.gif" align="bottom" width="23" height="22" alt="', $tag['description'], '" title="', $tag['description'], '" style="background-image: url(', $settings['images_url'], '/bbc/bbc_bg.gif); margin: 1px 2px 1px 1px;" /></a>'
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

Grammy

Well....  as far as I can tell, I've adapted all gbsothere's code to my 2.0.11 template and it's not throwing any errors.  It all looks fine, but when I select text in the text box, it becomes un-selected once I use the pulldown, so no change is affected.  (I'm attaching my ManageNews.template.php.)  I'm wondering...  should I have tried to adapt his 1.1.10 edits, rather than his RC1.2 edits?  Other than the template and the language file, there are no other edits and no database involvement.  (I didn't edit my Boardindex.template.php, because I would imagine that, by now, that resizing quirk has been resolved.) He says it was working, so I've obviously botched something.   :(

Does anyone see anything obvious?

Grammy

Well...   could you maybe just point out the block of code that causes the contents of the textbox to respond to the pulldown, then?  If I at least knew that, maybe I could tinker with it?   :(

Grammy

Quote from: Grammy on May 06, 2016, 08:48:10 PM
Well...   could you maybe just point out the block of code that causes the contents of the textbox to respond to the pulldown, then?  If I at least knew that, maybe I could tinker with it?   :(

Anybody out there?   :)

Grammy

Well.....   none of you are certainly under any obligation to answer that last question, I suppose.  It certainly doesn't fall within Simple Machine's scope of responsibility to assist when a new user tries to resurrect a bit of code that once worked.  I suppose I was a bit thrown off by the title of this particular board and, having come so close, actually getting gbsothere's code to parse without error, feel that I am "just there" if only I could identify the block of code that causes the content of the message area to actually retain its selected status long enough to respond to the pulldown menu.  If I just knew that, I could try to compare it to similar codes found elsewhere or at least know what to Google.  But, if you guys don't know, or are just too busy, I suppose it's time to return the files to their pre-edited state and move on.

(I felt this "mod" would be needed because, although we'd like our news fader to be eye-catching and attractive, not every individual who is responsible enough to be a moderator has a working knowledge of basic HTML.  It would have made their job so much simpler.)

Thanks, anyway, for your previous help with this.

Kindred

The news fader does accept bbc already, in 2.0.x, i thought. You just have to type it
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

Grammy

Quote from: Kindred on May 09, 2016, 04:11:08 PM
The news fader does accept bbc already, in 2.0.x, i thought. You just have to type it


Well....  I guess that's why I'm stumped, I know that it does, if I type it.   I think that if the forum's main board posting areas didn't have BBC buttons, most mainstream posters out there wouldn't know how to manually add them and I thought gbsothere's idea of putting pulldowns in the news area would be helpful to moderators who don't know how to manually add BBC or HTML.  They could just use the pulldowns.  The issue, though, is that when I highlight the text so that the pulldown can affect it, it loses its selected status and the highlight goes away as soon as I use the pulldown.  As a result, the pulldown can't make changes, because no text is selected.  It's a conundrum.   I just don't know enough about coding to understand which part of his codes deal with what's inside the text area and how make to a highlighted portion stay selected.  I know that, even now, while I'm typing this reply to you, I can highlight a few words and they stay selected while I reach up to the pulldown and make the color, say red.     No problem there.  So why the problem in the news area, I guess is my big question? 

I'm sorry to be such a putz about this whole thing. 

Kindred

You are not being a putz...  However, I think that you may be wasting your efforts... I think that it would be easier to teach the few folks who have access to the news section to use basic BBC rather than worry about coding all the editor functions into the news section.
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

qc

@Grammy In our forum, we had similar issues with BBCode and our Shoutbox - it parses BBCode, but there is no editor / buttons for it.

The solution our non-BBCode-experienced members use: use the ordinary post editor to compose the BBCode-enriched message. Then copy the text from there to where you want to have it.
Playing quizduell? Having quizduell questions? Our german quizduell forum quizcommunity.de is looking for quiz freaks to come and play quizduell with us :)

Grammy

Quote from: Kindred on May 09, 2016, 06:13:02 PM
You are not being a putz...  However, I think that you may be wasting your efforts... I think that it would be easier to teach the few folks who have access to the news section to use basic BBC rather than worry about coding all the editor functions into the news section.

You may be right!

Well...  I guess I wanted to beat that code into submission somehow, but perhaps I'd better start with "Hello, World!"   :D

Thanks.

Advertisement: