Forum Moderators: coopster

Message Too Old, No Replies

PHP n00b -- Code almost complete -- need slight code fixing!

         

kilanw

7:56 pm on Jun 23, 2009 (gmt 0)

10+ Year Member



Hi guys I'm trying to set up a wordpress.org site and switch out header images for different categories. I am a PHP n00b so I know there is something simple and stupid here screwing up my PHP code, I just don't know what.

<div id="main_content">

<a href="<?php bloginfo('url'); ?>"><div id="header">

<?php if ( is_category(america-wants-peace) ) { ;?>
<div id="headercat01">
<?php } elseif ( is_category(world-peace) ){ ;?>
<div id="headercat02">
<?php } elseif ( is_category(religion-peace) ) { ;?>
<div id="headercat03">
<?php } elseif ( is_category(wise-want-peace) ) { ;?>
<div id="headercat04">
<?php } elseif ( is_category(non-violence-solves-problems) ) { ;?>
<div id="headercat05">
<?php } elseif ( is_category(american-militarization) ) { ;?>
<div id="headercat06">
<?php } elseif ( is_category(global-militarization) ) { ;?>
<div id="headercat07">
<?php } elseif ( is_category(war-propaganda) ) { ;?>
<div id="headercat08">
<?php } elseif ( is_category(war-economy-fails) ) { ;?>
<div id="headercat09">
<?php } elseif ( is_category(war-is-hell) ) { ;?>
<div id="headercat10">
<?php } else { ;?>
<div id="header">
<?php } ;?>

<h1></h1>
<div class="description"></div>

</div></a>

the error message is:

Parse error: syntax error, unexpected T_GLOBAL, expecting ')' in /home/peacepai/public_html/test/wp-content/themes/autumn-almanac/header.php on line 38

line 38 is:

<?php } elseif ( is_category(global-militarization) ) { ;?>

although I know whatever is wrong with line 38 is wrong with at least half of the other lines.

StoutFiles

8:13 pm on Jun 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Doesn't solve the problem, but would make your code easier to follow. Instead of constantly stopping and starting the php, just make it all php.

<?php
if ( is_category(america-wants-peace) )
{ echo '<div id="headercat01">'; }
elseif ( is_category(world-peace) )
{ echo '<div id="headercat02">'; }

...and so on.

rainborick

8:38 pm on Jun 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think:

<?php } elseif { ( is_category(global-militarization) ) { ;?>

andrewsmd

9:04 pm on Jun 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have to echo the divs. You are stopping you php if statements so all of those divs are always going to be displayed. try this code also what is is_category? If it's a method I'm gussing it needs to take in a string so you need if (is_category("america-wants-peace")){
//some code here
}
Just a guess. Let me know if you need any more help.
<?php
if ( is_category(america-wants-peace) ) {
echo("<div id=\"headercat01\">");
}
elseif ( is_category(world-peace) ){
echo("<div id=\"headercat02\">");
}
elseif ( is_category(religion-peace) ) {
echo("<div id=\"headercat03\">");
}
elseif ( is_category(wise-want-peace) ) {
echo("<div id=\"headercat04\">");
}
elseif ( is_category(non-violence-solves-problems) ) {
echo("<div id=\"headercat05\">");

}
elseif ( is_category(american-militarization) ) {
echo("<div id=\"headercat06\">");
}
elseif ( is_category(global-militarization) ) {
echo("<div id=\"headercat07\">");
}
elseif ( is_category(war-propaganda) ) {
echo("<div id=\"headercat08\">");
}
elseif ( is_category(war-economy-fails) ) {
echo("<div id=\"headercat09\">");
}
elseif ( is_category(war-is-hell) ) {
echo("<div id=\"headercat10\">");
}
else {
echo("<div id=\"header\">)";
}
?>

[edited by: eelixduppy at 1:53 am (utc) on June 24, 2009]
[edit reason] disabled smileys [/edit]

StoutFiles

9:16 pm on Jun 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are stopping you php if statements so all of those divs are always going to be displayed

You don't HAVE to echo the divs...stopping and starting php is allowed, you should learn that as it's very useful.

If you choose to echo, echo with single quotes if your content has double quotes so you don't have to use escape characters. Why make it messier than you have to?

echo '<div id="headercat01">';

andrewsmd

9:45 pm on Jun 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I didn't mean he was stopping them because of the closing php tags, he was stopping the if statements with the ; terminating an extra execution after the if. If you break down the line
<?php if ( is_category(america-wants-peace) ) { ;?>
<div id="headercat01">
<?php } elseif ( is_category(world-peace) ){ ;?>

into just php you end up with
<?php if ( is_category(america-wants-peace) ) { ; }elseif{...

that is never going to execute anything.

Stopping and starting php is fine for small applications. Personally when I write anything, I have all of my php separated from my html using templates. If you write anything on a large scale it becomes too hard to debug if you always mixing php and html.
Let me know if you still can't get it working kilanw we were all where you are at one point in our lives.

g1smd

12:20 am on Jun 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The code is waaaay too long.

Load the data into an array, and then run a loop X number of times through the array data, writing out one link each time around the loop.

You'll find the list easier to maintain too.

Later on, you might even migrate the list from an array into a database table.

kilanw

3:35 pm on Jun 24, 2009 (gmt 0)

10+ Year Member



Hey guys, still need a little help. I copied Andrewsmd's code exactly and continued to get the error message for the same line (global-militarization) which changed from 38 to 45. Then I tried adding single quotes like Stoutfiles suggested, and it brought my site back, but looked ALL screwed up, and did not achieve the desired effect with the header image.

Afterwards, I changed "is_category" to "the_category" and it brought the error message back. (i've seen both used to call the category from the database).

I also double checked the category "global-militarization". Everything is spelled properly and the category does exist.

The PHP is consistent across the board, I wonder why it's calling specifically THAT LINE, and not one before it?

Andrewsmd+Stoutfiles makes me feel I'm on the right track, but I'd really love to get this figured out. Thanks for your help so far everyone.

andrewsmd

3:40 pm on Jun 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well it sounds like your close. Since your new I'll give you a piece of advice. Always post whatever code you can. Please post what you have now. Also it looks to me like is_category is a function am I wrong? If it is, could you post the code for that function also? Get your code posted and I'll give you some more help. Also, the sinlge quotes double quotes thing won't be a problem if you always use double quotes. I know that it sucks to have to escape double quotes when you want to echo them, but trust me, you can avoid some major debugging issues if you ALWAYS use double quotes.

kilanw

3:58 pm on Jun 24, 2009 (gmt 0)

10+ Year Member



OHHHHH! Epiphany!

(global is some kind of PHP code! that's why error is T_GLOBAL. I changed the slug to world-militarization which takes away that error, then I get an error for unexpected ';' on line 58:

echo("<div id=\"header\">)";
}
?>

So I remove that and it says "unexpected '}' on line 59", so I remove that and it says "unexpected ';' on line 59".
of which there is no semi-colon on line 59.

So my guess is........

else {
echo("<div id=\"header\">)";
}
?>

is wrong in some way shape or form. What I'm trying to tell the PHP is
"if the category is this, call this header image"
but in the last line am trying to tell it
"if it's not one of those ten categories(if it's anything else), call this header image"

So what the hell am I doing wrong?

And to everyone, thanks in advance, I appreciate your help.

andrewsmd

5:31 pm on Jun 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What kind of editor are you using. Global should change colors because it is a reserved word in php. If your editor doesn't highlight it, you may want to try using eclipse. It's free. Post your entire if else section exactly as you have it and I'll take a look at it.

kilanw

6:26 pm on Jun 24, 2009 (gmt 0)

10+ Year Member



Andrewsmd, you're really helping me a lot, thanks again. In NJ 2 day PHP seminars are like 2 grand. They don't teach it at any of the local colleges under the rationale of "we already have a databasing class"

I'm using dreamweaver, and the word global was green, that's what tipped me off. So yes, it was highlighted.

I think category is a function? I'm building a wordpress, So what I'm trying to say is "If the category is this, display this header image" (which is set as background image of that div)

I modified my original code from what I saw here:
wordpress.org/support/topic/196979?replies=7

Here is the whole string, as is, now:

</head>
<body><div id="wrapper">
<div id="main_content">

<a href="<?php bloginfo('url'); ?>"><div id="header">

<?php
if ( is_category(america-wants-peace) ) {
echo('<div id=\"headercat01\">');
}
elseif ( is_category(world-peace) ){
echo('<div id=\"headercat02\">');
}
elseif ( is_category(religion-peace) ) {
echo('<div id=\"headercat03\">');
}
elseif ( is_category(wise-want-peace) ) {
echo('<div id=\"headercat04\">');
}
elseif ( is_category(non-violence-solves-problems) ) {
echo('<div id=\"headercat05\">');
}
elseif ( is_category(american-militarization) ) {
echo('<div id=\"headercat06\">');
}
elseif ( is_category(world-militarization) ) {
echo('<div id=\"headercat07\">');
}
elseif ( is_category(war-propaganda) ) {
echo('<div id=\"headercat08\">');
}
elseif ( is_category(war-economy-fails) ) {
echo('<div id=\"headercat09\">');
}
elseif ( is_category(war-is-hell) ) {
echo('<div id=\"headercat10\">');
}
else {
echo('<div id=\"header\">)';
}
?>

<h1></h1>
<div class="description"></div>

</div></a>

and current error message is:

Parse error: syntax error, unexpected ';' in /home/peacepai/public_html/test/wp-content/themes/autumn-almanac/header.php on line 58

Is it possible it's the last line? cause the top is telling it display <div id="header"> then it's saying if the category is any of these then display this corresponding div, then the line is or else if it's not these categories, display <div id="header">

I'm going to guess that doesn't need to be in there twice...

andrewsmd

6:50 pm on Jun 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



last echo echo('<div id=\"header\">)';
needs to be echo('<div id=\"header\">');
you have your ') mixed up. I threw that into eclipse and saw it right away because of the coloring. You should check it out.

kilanw

8:05 pm on Jun 24, 2009 (gmt 0)

10+ Year Member



Thanks again, I'll be checking out Eclipse.

That got my site showing again. Now the header image appears, but not inline with the rest of the site. The 350 pixel height header that's supposed to show at the top of the screen above everything, now appears behind the navigation and other page elements.

Additionally, the intended effect was still not achieved. If I click on a category on the right side (america wants peace... etc.) it still shows the same header image, not the new header image for that category.

So while I think I can maybe(?) fix the first part of the problem with css, I still can't understand why it's not switching out the images...

I'll keep monkeying with it and come back to the post.

Thanks again for helping.

andrewsmd

8:21 pm on Jun 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should throw some test data into the if statements to see if you are getting the intended results. What I mean is something like this
if ( is_category(america-wants-peace) ) {
echo('<div id=\"headercat01\">');
echo("we should have the header america-wants-peace");
}
elseif ( is_category(world-peace) ){
echo('<div id=\"headercat02\">');
echo("we should have the header world-peace");
}
elseif ( is_category(religion-peace) ) {
echo('<div id=\"headercat03\">');
echo("we should have the header religion-peace");
} etc...

See if that echoes the correct thing on the correct page. Then you will know if it is a php problem or a html/css problem.

kilanw

8:37 pm on Jun 24, 2009 (gmt 0)

10+ Year Member



the second echo works

I also changed "is_category" to "the_category" and it displayed text to navigate to that category page.

I think it's possible is_category is the wrong command?
As I mentioned before, I pulled it from here
wordpress.org/support/topic/196979?replies=7

[edited by: coopster at 9:10 pm (utc) on June 24, 2009]
[edit reason] no personal urls please TOS [webmasterworld.com] [/edit]

kilanw

8:38 pm on Jun 24, 2009 (gmt 0)

10+ Year Member



pulled this from the wordpress codex:

A Category Page

is_category()
When any Category archive page is being displayed.
is_category('9')
When the archive page for Category 9 is being displayed.
is_category('Stinky Cheeses')
When the archive page for the Category with Name "Stinky Cheeses" is being displayed.
is_category('blue-cheese')
When the archive page for the Category with Category Slug "blue-cheese" is being displayed.
is_category(array(9,'blue-cheese','Stinky Cheeses'))
Returns true when the category of posts being displayed is either term_ID 9, or slug "blue-cheese", or name "Stinky Cheeses". Note: the array ability was added at Version 2.5.
in_category('5')
Returns true if the current post is in the specified category id. read more

Note: Be sure to check your spelling when testing, "is" and "in" are a big difference.

See also is_archive() and Category Templates.

kilanw

8:43 pm on Jun 24, 2009 (gmt 0)

10+ Year Member



in the codex they have single quotation marks around the category name, mine don't. But I tried, and it makes no difference.

andrewsmd

8:52 pm on Jun 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That shouldn't matter but you need to do it on more than one page. Because if it is saying is america, you need to make sure it doesn't say is america on every page.

andrewsmd

8:56 pm on Jun 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



echo('<div id=\"headercat01\">');
Here's why my earlier post about using double quotes is true. When you used ' it displays everything literally. That means your are echoing the html
<div id=\"headercat01\">
So the browser is looking for the id \"headercat01\"
You either need to change it to
echo("<div id =\"headercat01\">");
or
echo('<div id = "headercat01">');

kilanw

2:18 pm on Jun 25, 2009 (gmt 0)

10+ Year Member



AndrewsMD thanks again for your help, I think I'm 99% there! I followed your advice on the double quotes and it brought the first header image (headercat01) for every category. Then I changed all the elseif's to if's and it worked! All the categories display different headers. But I do want to make sure that isn't improper coding...

There's something wrong with the final statement, though. I want it to display <div id="header"> (the default header) when it's not on one of those 10 categories. As it stands, it is displaying <div id="headercat10"> when it's on a regular category. Here's the whole code:

<a href="<?php bloginfo('url'); ?>"><div id="header">

<?php
if ( is_category(america-wants-peace) ) {
echo("<div id=\"headercat01\">");
}
if ( is_category(world-peace) ) {
echo("<div id=\"headercat02\">");
}
if ( is_category('religion-peace') ) {
echo("<div id=\"headercat03\">");
}
if ( is_category('wise-want-peace') ) {
echo("<div id=\"headercat04\">");
}
if ( is_category(non-violence-solves-problems) ) {
echo("<div id=\"headercat05\">");
}
if ( is_category(american-militarization) ) {
echo("<div id=\"headercat06\">");
}
if ( is_category(world-militarization) ) {
echo("<div id=\"headercat07\">");
}
if ( is_category(war-propaganda) ) {
echo("<div id=\"headercat08\">");
}
if ( is_category(war-economy-fails) ) {
echo("<div id=\"headercat09\">");
}
if ( is_category(war-is-hell) ) {
echo("<div id=\"headercat10\">");
}
else {
echo("<div id=\"header\">");
}
?>

<h1></h1>
<div class="description"></div>

when it's on a top level category, like just peace or green, it should be displaying green naked lady on the top. (like the homepage).

Thanks again.

[edited by: jatar_k at 8:23 pm (utc) on June 25, 2009]

kilanw

2:28 pm on Jun 25, 2009 (gmt 0)

10+ Year Member



well, those if statements had it going and working 99% of how I wanted to. Now, without changing a thing, it will only show me <div id="headercat10">

andrewsmd

3:35 pm on Jun 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Once again I would try my suggestion earlier to see if your if statements are being executed correctly.

kilanw

4:03 pm on Jun 25, 2009 (gmt 0)

10+ Year Member



could there be a problem with the "elseif" statements? If i take out "else" and make them "if" statements, it will automatically pull the one closest to the bottom and show it on every category page.

Hence why it is showing the first statement. it's the only "if" statement.

same thing with the echo. No matter which category I click, it shows me "this header should be america-wants-peace"

<?php
if ( is_category(america-wants-peace) ) {
echo('<div id="headercat01">');
echo("we should have the header america-wants-peace");
}
elseif ( is_category(world-peace) ) {
echo('<div id="headercat02">');
echo("we should have the header world-peace");
}
elseif ( is_category(religion-peace) ) {
echo('<div id="headercat03">');
echo("we should have the header religion-peace");
}
elseif ( is_category(wise-want-peace) ) {
echo('<div id="headercat04">');
echo("we should have the header wise-want-peace");
}
elseif ( is_category(non-violence-solves-problems) ) {
echo('<div id="headercat05">');
echo("we should have the header non-violence-solves-problems");
}
elseif ( is_category(american-militarization) ) {
echo('<div id="headercat06">');
echo("we should have the header american-militarization");
}
elseif ( is_category(world-militarization) ) {
echo('<div id="headercat07">');
echo("we should have the header world-militarization");
}
elseif ( is_category(war-propaganda) ) {
echo('<div id="headercat08">');
echo("we should have the header war-propaganda");
}
elseif ( is_category(war-economy-fails) ) {
echo('<div id="headercat09">');
echo("we should have the header war-economy-fails");
}
elseif ( is_category(war-is-hell) ) {
echo('<div id="headercat10">');
echo("we should have the header war-is-hell");
}
else {
echo('<div id="header">');
}
?>

<h1></h1>

andrewsmd

9:10 pm on Jun 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you have the code for the is_category function? I think your problem is that function is always returning true no matter what you pass into it.

g1smd

10:13 pm on Jun 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You'll find it a lot easier to have the data in an array; the code will be much simpler.

kilanw

3:15 pm on Jun 29, 2009 (gmt 0)

10+ Year Member



Hey guys,

Thanks again for all the help
I seem to have got something going with this array. HOWEVER, the main header (not one of the ones to appear under a category, but the one to appear when NOT on those categories) takes FOREVER to load. Is there a simple fix here? Should I have the standard <div id="header"> there somewhere?

<a href="index.php"><?php wp_head(); ?></a>
</head>
<body><div id="wrapper">
<div id="main_content">

<a href="<?php bloginfo('url'); ?>">

<?php
$c = get_query_var('cat');
$cat = get_category ($c);

$headers = array(
'america-wants-peace' => 'headercat01',
'world-peace' => 'headercat02',
'religion-peace' => 'headercat03',
'wise-want-peace' => 'headercat04',
'non-violence-solves-problems' => 'headercat05',
'american-militarization' => 'headercat06',
'world-militarization' => 'headercat07',
'war-propaganda' => 'headercat08',
'war-economy-fails' => 'headercat09',
'war-is-hell' => 'headercat10'
);

$divid = isset($headers[$cat->slug]) ? $headers[$cat->slug] : 'header';

?>
<div id="<?php echo $divid ?>">

<h1></h1>
<div class="description"></div>

</div></a>

andrewsmd

5:11 pm on Jun 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't see anything wrong with it but something that will make your code easier to read.
Change
$divid = isset($headers[$cat->slug]) ? $headers[$cat->slug] : 'header';

?>
<div id="<?php echo $divid ?>">

to

$divid = isset($headers[$cat->slug]) ? $headers[$cat->slug] : 'header';

echo('<div id="{$divid}">');

?>