Page 1 of 2

proper syntax???

Posted: Sun Jun 27, 2010 3:35 am
by markosjal
I have a piece of code as follows

Code: Select all

<?php
    
    	while ($row=mysql_fetch_array($scatres))
    	{ 
   		echo "<option value=\"$row[subcatid]\">$row[subcatname]</option>\n";
    	}
    
    	?>
I have managed to get this sitre 99% translated via an array that has worked for other drop down menus like the one above by adding only $lang as seen below

Code: Select all

<?php
    
    	while ($row=mysql_fetch_array($scatres))
    	{ 
   		echo "<option value=\"$row[subcatid]\">$lang[$row[subcatname]]</option>\n";
    	}
    
    	?>
however this does not work in this case. I tried defining a new variable as well and applying the $lang array to that, and no luck.


Can anyone give me some insight?

thanks

Re: proper syntax???

Posted: Sun Jun 27, 2010 3:56 am
by internet-solution
Its difficult to understand from the code you have posted. What is in your $lang array?

And its not a syntax issue

Re: proper syntax???

Posted: Sun Jun 27, 2010 6:30 am
by cpetercarter
When you say "this does not work", what actually happens? Do you, for example, get an empty space where you expect to see the translation of $row['subcatname']? Have you got error messaging switched on? If so, do you get an error message (eg on which says that the variable $lang or the offset 'subcatname' are not defined?) And if you don't have error messaging switched on, please switch it on, because it is very difficult to give you any help unless we can see what php is telling you.

Re: proper syntax???

Posted: Sun Jun 27, 2010 8:45 am
by McInfo
This is too ambiguous for the PHP compiler to understand.

Code: Select all

echo "...$lang[$row[subcatname]]..."; // Bad syntax
This clearly tells the comiler where the embedded variable is.

Code: Select all

echo "...{$lang[$row['subcatname']]}..."; // Good syntax

Re: proper syntax???

Posted: Sun Jun 27, 2010 8:51 am
by markosjal
If I had got an empty space, it would be good news. That is why I have assumed it was a syntax issue because when I plug this into the 'live' site it seems to cause the pages to not load. A blank space always indicates to me that the correspondiong entry is not in the $lang array

I am no 'expert' and have no formal php training . Here is what I do know

$lang is an array that comes from a 500 or so line text file.
i believe $row[subcatname[ to come from a database array

since this is a shared hosting account, I have no idea how to turn on the debugging. Does that not require access to php.ini file? Is there another way in cpanel hosting to turn on debuging?

McInfo,
tried your solution abnd it did not work. Had not dawned on my to try triple bracketing though

Re: proper syntax???

Posted: Sun Jun 27, 2010 9:10 am
by McInfo

Code: Select all

<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', true);
PHP Manual: Error Handling and Logging

Re: proper syntax???

Posted: Mon Jun 28, 2010 12:06 am
by markosjal
McInfo

I stand corrected , your solution with the bracketing did work! ON a whim I tried it again , and viola!

Thanks a bunch. I hope it works everywhere I have had this issue.

EDIT-----------------------


I have one remaining issue I can not seem to resolve, and it has to do with nesting PHP in Javascript. Is this possible?

I have (note that the message is in English and Spanish on the first code sample)

Code: Select all

function checkme() // Define function checkme
{
    if (document.forms[0].adtitle.value == "")
    {
    alert("The Title field is empty. El Campo de titulo esta vacio.");
    document.forms[0].adtitle.focus();return(false)
    } 
...
I want to do something like this but it apparently does not work.

Code: Select all

function checkme() // Define function checkme
{
    if (document.forms[0].adtitle.value == "")
    {
    alert(<?php echo $lang['JS_MESSAGE_TITLE'] ; ?>);
    document.forms[0].adtitle.focus();return(false)
    }

Re: proper syntax???

Posted: Mon Jun 28, 2010 1:49 am
by cpetercarter
Javascript runs on the browser, not on the server.

So you need to place your Javascript, between <script></script> tags, as part of the html for your webpage. You can include bits of php within the Javascript, provided that you give the script a .php extension. If you do this, php will run/evaluate sections of the code between <?php ... ?> tags before the page is sent to the browser.

In your example, the "echo" command in the php bit should be removed. You only need the variable name.

Re: proper syntax???

Posted: Mon Jun 28, 2010 10:01 am
by markosjal
I changed the call to the javascript file from where it is called in the index.php , and added the php extension, as well as added the php extension to the filename, and changed one of the java error messages from text to php with no "echo" .

The result is it is as if the javascript does not run at all now.

UPDATE

After clearing browser cache I now see the message displayed as
<domain.com>
(BLANK)

where domaion is the actual domin being accessed. and BLANK is NOTHING or NULL wheree the message should e displayed

Re: proper syntax???

Posted: Mon Jun 28, 2010 11:43 am
by McInfo
You do need echo because you are trying to display something. But you also need quotes in the JavaScript to make what is being echoed into a JavaScript string. Ideally, you would escape any quotes in the string being echoed so those quotes would not interfere with the JavaScript quotes.

Code: Select all

alert('<?php echo $lang['JS_MESSAGE_TITLE']; ?>');

Re: proper syntax???

Posted: Mon Jun 28, 2010 12:43 pm
by markosjal
my last attempt was

Code: Select all

alert('<?php echo $lang['JS_CHECK_TITLE']; ?>');
No joy still

'JS_CHECK_TITLE' does correspond to the $lang array

Re: proper syntax???

Posted: Mon Jun 28, 2010 1:07 pm
by markosjal
I found this page, but not sure what it all means nor how to implement it.

http://www.the-art-of-web.com/php/javascript-escape/

Re: proper syntax???

Posted: Mon Jun 28, 2010 1:51 pm
by McInfo
markosjal wrote:'JS_CHECK_TITLE' does correspond to the $lang array
If that key is not in the array, why are you using it?
markosjal wrote:I found this page, but not sure what it all means nor how to implement it.
We might be getting off-track and making things prematurely complicated by trying to add escaping before addressing the original problem which was echoing a PHP variable into JavaScript.

Do you completely understand this example? If not, is there something specific that you would like me to explain?

example_1.php

Code: Select all

<?php
$message = 'Hello, World.';
?>
<script type="text/javascript">
alert('<?php echo $message; ?>');
</script>

Re: proper syntax???

Posted: Mon Jun 28, 2010 3:22 pm
by markosjal
As I said it DOES correspond to the array!

It IS in the array or however you want to put it.


As for the example, I am only trtying to understand why it does NOT work

Here it is go to this link , and leave the title blank but fill in the other fields

http://banderasnews.pvshopper.com/?view ... cutregion=

That is the code as we have discussed in the title field. If I return it to the text, it works

Thanks

Re: proper syntax???

Posted: Mon Jun 28, 2010 3:50 pm
by Crys
How you set $lang['JS_MESSAGE_TITLE'] ?