PDA

View Full Version : PHP is doing my head in


Pauly
December 5th, 2004, 17:34
[syntax:bd918c17f6="php"]
// BELOW IS THE START / END OF THE LEFT SECTION - REQUIRED IN HEADER
if (THIS_SCRIPT == 'website_articles') {
$showimage = '<img src="http://www.opaqueice.com/images/blue/misc/articles.gif" alt="Articles" border="0" />';
}
elseif (THIS_SCRIPT == 'arcade') {
$showimage = '<img src="http://www.opaqueice.com/images/blue/misc/arcade.gif" alt="Games Arcade" border="0" />';
}
else {
$showimage = '<img src="http://www.opaqueice.com/images/blue/misc/oi_boards_top.gif" alt="$vboptions[bbtitle]" border="0" />';
}
[/syntax:bd918c17f6]

Doesn't work when I'm on the arcade script page so I'm assuming it's something wrong with the elseif bit. Help is much appreciated!

websterworld
December 5th, 2004, 17:39
Try a 'case' statement maybe.

Pauly
December 5th, 2004, 17:45
I don't know how lol, I don't know PHP at all :(

websterworld
December 5th, 2004, 18:00
Sorry I have C on my mind, heh.
Ok, I believe what your doing wrong is the boolean expression in the if statement.

if (THIS_SCRIPT == 'website_articles') << doesnt seem right to me.


It should be: if( $x > $y ), and x and y have to have values in order to compare. Did you declare 'THIS_SCRIPT' as a var of sorts? or 'website_articles'?

You should have something like:

$THIS_SCRIPT = 1;
$website_articles = 1;
$arcade =2;

And (re)set the value each time.


I havnt touched PHP for some time, but I think I'm right. :)

ethicaldesign
December 5th, 2004, 18:08
[syntax:7cd79cba07="php"]
switch (THIS_SCRIPT)
{
case 'website_articles':
$showimage = 'articles.gif';
break;

case 'arcade':
$showimage='arcade.gif';
break;

default:
$showimage='oi_boards_top.gif';
}

$showimage='<img src="http://www.opaqueice.com/images/blue/misc/'.$showimage.'" alt="'.$vboptions[bbtitle].'" border="0" />';
[/syntax:7cd79cba07]

This should work faster than your if/else and be more flexible. If you want to add in other sections, just add in more cases above the "default:" line.

For more info about switch:

http://uk2.php.net/switch

Best to get into the habit of doing things that way wherever you want to test for more than one condition and an else. Your code will execute faster, be easier to read, and be easier to maintain.

Good luck :)

ethicaldesign
December 5th, 2004, 18:11
websterworld: I think THIS_SCRIPT is a constant that's defined somewhere else in the script that paul's using. It'll be a string variable (judging by the code sample posted above).

websterworld
December 5th, 2004, 18:16
websterworld: I think THIS_SCRIPT is a constant that's defined somewhere else in the script that paul's using. It'll be a string variable (judging by the code sample posted above).

Righty-o, so the case method it is. I was right the first time. :)

ethicaldesign
December 5th, 2004, 18:16
P.S. I just edited the last line of that. Spotted that you were trying to evaluate a variable within double quotes that was itself within single quotes ($vboptions[bbtitle])

Things within double quotes are only evaluated if they aren't surrounded by single quotes (otherwise the double quotes are read by the parser as straightforward characters rather than a request to evaluate what's inside of them).

You need to change that to $vboptions['bbtitle'] aswell! Not sure if you're allowed to reference an associative array like you have there without single quotes. Could be wrong though. If the above code doesn't work you could try changing the $vboptions array to include the single quotes.

sonicgroup
December 5th, 2004, 18:33
Oh, by the way, the initial error was elseif. It should be two separate words: else if.

To expand on ethical's last reply: variables inside single quotes - i.e. your last else statement, will not be parsed by PHP because anything in single quotes in taken as a literal string.

So, if you want the string to be in single quotes, then you need to break the string and concatenate the variable onto it, then concatenate the rest of the string:

[syntax:6056770d98="php"]
$showimage = '<img src="http://www.opaqueice.com/images/blue/misc/oi_boards_top.gif" alt="'.$vboptions['bbtitle'].'" border="0" />';
[/syntax:6056770d98]

ethicaldesign
December 5th, 2004, 18:42
Yeah, I must have updated it while you were posting :)

I think it's better to join the filename onto the string at the end of the evaluation though (whichever way it's done) because it should be more efficient doing it that way, and more flexible if editing is required later (no need to change multiple definitions if the website or path changes).

I notice that you've also changed the associative array to include single quotes (didn't do this in my example but put a note in). I notice it wasn't like this in pauls original code however.

Do you know if you have to always quote what comes in the square brackets when referencing an associative array? That's the way I would always do it myself but I wasn't 100% sure if it was absolutely required (I seem to remember seeing other code that doesn't do that aswell).

ethicaldesign
December 5th, 2004, 18:44
Just had a thought! If 'bbtitle' is actually a constant that's defined somewhere else (like THIS_SCRIPT), then you will need to do it without the single quotes in the square brackets!

That might be why it's written that way to begin with.

sonicgroup
December 5th, 2004, 22:16
I don't know the exact rules for quoting indexes for arrays, but I make a habit of doing it (unless of course it's a numerical index), just to avoid problems, and to make things clearer. However, PHP is case sensitive, and most people use all caps to signify constants, so in this case, bbtitle would not get replaced by a constant.

Pauly
December 6th, 2004, 19:14
Doesn't work :( Any ideas?

And yes the script is defined ;)

ethicaldesign
December 6th, 2004, 20:20
What didn't work? The script I posted above or one of the other examples?

Is it throwing up an error or just something isn't happening as you would expect it would? If it's throwing up an error can you post the error message.

Without details about exactly what's happening its hard to see what the problem might be, because I think the code is correct (at least as syntactically).

Did you try including quotes in the:

$vboptions['bbtitle']

part if you were using my example?

Pauly
December 6th, 2004, 21:02
I was using your example and yes I used the quotes :)

What isn't happening is the image isn't showing. Infact it ceases loading the rest of the script after this point.

For the meantime I have reverted back to the script I was originally using. The else bit works and shows the oi_boards_top.gif image when the THIS_SCRIPT isn't defined. For some reason the if and elseif isn't working.

Pauly
December 7th, 2004, 00:09
Removing the brackets {} helped the original script work :) Thanks for the help all.

ethicaldesign
December 7th, 2004, 01:15
Sorry for the late reply (got into a bit of a discussion with the other paul about something).

I'm really not sure why the code I posted wouldn't work in the same way as the else if method, because it's essentially doing the same thing (only faster).

What was the html output that was produced for the image when you view the source of the resulting page? Perhaps it's just a simple mistake in the last line where the value you set to $showimage is set to include the img tag.

You should get into the habit of using switch statements in this way when you have more than a single condition to check as it's better. If it doesn't work you're doing something wrong.

ethicaldesign
December 7th, 2004, 01:31
Just did a quick test of my suggestion and it works perfectly. The only thing I can see is that the images in the path you're specifying don't exist.

This is what I tried:



<?

define ('THIS_SCRIPT','other');

$alttag='this is some alt tag text';

switch (THIS_SCRIPT)
{
case 'website_articles':
$showimage = 'articles.gif';
break;

case 'arcade':
$showimage='arcade.gif';
break;

default:
$showimage='oi_boards_top.gif';
}

$showimage='<img src="http://www.opaqueice.com/images/blue/misc/'.$showimage.'" alt="'.$alttag.'" border="0" />';

echo $showimage;

?>


this is the resulting html (as expected):

<img src="http://www.opaqueice.com/images/blue/misc/oi_boards_top.gif" alt="this is some alt tag text" border="0" />

The only thing that's different there is that I've used a variable called $alttext rather than the associative array that you're using for that and that I've defined 'THIS_SCRIPT' myself because I don't have access to that definition elsewhere in your script.

Though none of the images should be showing up except for the default one as far as I can tell, because they don't exist.

Hope this is some help anyway. I know you've already got it working using the original method, but that realy isn't the best way to handle that. Worth getting your head around the switch/case method now because you'll find it invaluable later and your scripts will run faster and be easier to maintain.

ethicaldesign
December 7th, 2004, 01:38
clicking:

http://www.opaqueice.com/images/blue/misc/articles.gif

at the time of writing for example produces:

Not Found
The requested URL /images/blue/misc/articles.gif was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Apache/1.3.33 Server at www.opaqueice.com Port 80

That's why your images aren't showing up. If they are showing up with the original script you posted which uses that path, then I can't see why that would happen. Did you change to a different path for the other images apart from the oi_boards_top.gif image (that one does show up - it's a picture of a ladies leg - the others are missing though).

Pauly
December 7th, 2004, 12:03
No no, you see the image nor alt text showed up first, but removing the curly brackets from the original made it show, I also renamed the image files, I'm a bit of an organise freak when it comes to websites :)

ethicaldesign
December 7th, 2004, 12:09
No no, you see the image nor alt text showed up first, but removing the curly brackets from the original made it show, I also renamed the image files, I'm a bit of an organise freak when it comes to websites :)

Then if you change the image filenames (or paths) in the switch example, it will work. The only reason it wasn't working as far as I can see is that it was pointing to files (from your original example) that you've renamed.

You shouldn't be removing curly brackets from conditional statements really even if you use that method (though I really can't see how doing that would make the script suddenly work - you must have changed something else at the same time).

I won't go on about this because I can see you've somehow got it working, just something to bear in mind for the future.

Pauly
December 7th, 2004, 12:11
TakingHold cleaned up the code above it too (Code I didn't post), maybe that had some effect?

ethicaldesign
December 7th, 2004, 12:22
I'm not sure about the cleaned up version? I can't see that here. Regardless for what you're trying to do there, it's better to use a switch than a series of if elseif and else statements. It's also more efficient and will make your code more easily maintainable to separate the filename from the rest of the string you're building (assuming the path and other html output stays the same and it's only the image name that changes).

The example I posted originally will work if you replace the image filenames to point at the actual images you want them to (assuming they don't all have seperate paths now aswell - though that's easily taken care of aswell). Proof of that is posted above.

Removing the curly brackets isn't a good way to go imho. That's quite sloppy - at least if the eventual code that you're using is anything like the original code you posted in this thread.

I don't want to pester you about this - just point it out because I hate to see you picking up bad habits like this when you're learning because they'll be harder to break later.

Pauly
December 7th, 2004, 14:55
Bad habits or not, the 'correct' way of doing things just doesn't work for me, so sloppy will do ;)

TakingHold
December 7th, 2004, 21:10
It was fairly sloppy actually :)

Just glad i could help

Pauly
December 8th, 2004, 13:38
Oh and I see how the switch thing works now :D Wow I was slow yesterday. Thanks again :)

ethicaldesign
December 8th, 2004, 13:58
No worries :) Glad you got it sorted in the end. Sorry to be a nag aswell (just trying to help you to avoid the pitfalls that most of us fall into when using a new language, only to discover the right way later).

Pauly
December 8th, 2004, 13:59
It's ok, keep it coming that way it helps everyone here learn :)