PDA

View Full Version : [q] Php Problems


mrshiney
09-30-2005, 10:47 AM
Ok I've been working on a new design for my site that is a) self-contained in one script and B) database-based.

I've got most of it working but there are a couple of things stumping me.

PROBLEM 1: Variable Scope Problems

Why doesn't this print out "The username is joe!"? Instead, FOO.PHP doesn't seem to be passed the $username defined in the included file.


// This is the file FOO.PHP ...
global $username;
include('./includes/bar.php');
echo "The username is ".$username."!";

//This is the file BAR.PHP ...
$username = 'joe';



PROBLEM 2: LOGICAL TEST PROBLEMS

What's wrong with my if statement. A friend told me try using boolean true/false instead of 1/0 ... e.g. "$THECODE=true;" ... Will that work?


$THECODE = 1;
if !($THECODE=1) {die('CODE NOT SET!');}
echo "Access granted!";



PROBLEM 3: EMBEDDING PHP CODE IN SQL

The below just echoes the HTML code (i.e. it is not interpreted). How do I make this work so that it interprets the PHP echo command embedded in the result string and display the image?


$imagefile='foo.png';
echo mysql_result( "<img src=\"./images/<? php echo $imagefile ?>\" align=\"center\">", 0, "tbl_images" );

dbmasters
09-30-2005, 10:52 AM
not sure on these but let me try:

echo "The username is ".GLOBAL[$username]."!"; (might be GLOBALS instead of GLOBAL, it's early, I'm not sure...

Not:

if !($THECODE=1) {die('CODE NOT SET!');}

But

if ($THECODE!=1) {die('CODE NOT SET!');}

Note: equals in if statement is == not =

== equals
!= not equals

And if you use boolean you don't need if($variable == true), just if($variable) will work.

#3...why the heck is that in a mysql_result function?

mrshiney
09-30-2005, 02:15 PM
Thanks for the help! Some followup below.

Originally posted by dbmasters+Sep 30 2005, 09:52 AM--><div class='quotetop'>QUOTE(dbmasters @ Sep 30 2005, 09:52 AM)</div><div class='quotemain'>echo "The username is ".GLOBAL[$username]."!"; (might be GLOBALS instead of GLOBAL, it's early, I'm not sure...
Quoted post
[/b]

Ok so a reference to a global variable has to be "GLOBAL[$username]"? So the below would be the correct way to define a global variable, as in...


GLOBAL[$username]='joe';


Originally posted by dbmasters@Sep 30 2005, 09:52 AM
if ($THECODE!=1) {die('CODE NOT SET!');}

Thanks for the clarification. If this bit of code were going to be in an included file, I'd need to use "GLOBAL[$THECODE]" right?

<!--QuoteBegin-dbmasters@Sep 30 2005, 09:52 AM
#3...why the heck is that in a mysql_result function?
Quoted post
[/quote]

This is my first attempt at a db-driven site so it's a little kludgy. On my "list" is to move just content to the db, and leave all formatting in the script and/or css.

Complicating things is that the site needs to be in japanese/english so I have the main content in two parallel-constructed tables (one for english, one for japanese). Then depending upon which language the user has selected, it displays content from one table or another.

Anyway thanks for your help!

dbmasters
10-01-2005, 09:42 AM
no, as I recall (haven't used globals in a bit) you can use any ol' variable as global and need the GLOBAL[] around it when you call it, not when you define it...which is weird...but I think that's the case.

MGrisafi
10-02-2005, 03:51 PM
I'm pretty sure that you wouldn't have to declare it as a global in that situation. It would already be, even from within an include file. It would be a different story if you were declaring a global from within a function or class.

In your case you'd probably just be all right declaring $username = 'Joe'; in the include and echo $username; in the main file.

I noticed a syntax curiosity in your include() call:


include('./includes/bar.php');

// is that period supposed to be there? (before the 1st slash '/')

dbmasters
10-02-2005, 09:35 PM
The period is valid, and common actually...it mean the same as no slash at all.

MGrisafi
10-03-2005, 05:35 PM
I stand corrected...

Hmm, learn something new everyday although I'm not sure I understand its purpose/usefullness

caddickj
10-03-2005, 06:18 PM
OT: MGrisafi... whereabouts in central PA are you? I'm in Lancaster, myself.

MGrisafi
10-04-2005, 05:36 PM
No way! I'm in Ephrata myself. I go into Lancaster quite often, I was just at the Borders next to Park City picking up a couple books yesterday.

In fact, the guy who I'm working on a site for lives in Lancaster. Maybe you heard of him? http://www.treyalexander.net

Wow, you really do learn something everyday. Small world!

caddickj
10-04-2005, 06:12 PM
Originally posted by MGrisafi@Oct 4 2005, 03:36 PM
No way! I'm in Ephrata myself. I go into Lancaster quite often, I was just at the Borders next to Park City picking up a couple books yesterday.

In fact, the guy who I'm working on a site for lives in Lancaster. Maybe you heard of him? http://www.treyalexander.net

Wow, you really do learn something everyday. Small world!
Quoted post

Small world is right. I used to live up in Rothsville and Brownstown for a while. Love Ephrata.

Haven't heard of Trey, but now that I've seen the site I might look into him a bit. :)

MGrisafi
10-04-2005, 07:16 PM
Yeah, Ephrata's not bad now that the little heroin epidemic seems to be waining, and that damn fair's outta here (for this year anyhow :P )

And if you know anyone who wants to learn guitar, Trey's a heck of a teacher and player.