• Over the weekend I tried using my nearly non-existant knowledge of php to write a little piece of code to repeat something down the page for me, albeit with slight variations.

    I tried uploading it in a new Page template. I couldn’t get it to work. I heard that a plugin was needed before PHP would work in posts/pages. So I downloaded one, enabled it and still the page wouldn’t work (I just get what is, effectively, a blank page).

    I tried uploading the most basic piece of code I could think of, namely a piecve to print ‘Hello World’. It worked, making me think that the problem was my coding rather than the plugin not working.

    The really confusing part is that I then tried deactivating the plugin, just to double check, and the printing of ‘Hello World’ still worked. So I’m thoroughly confused. Do I need to use a plugin to run PHP in a Page or not?

Viewing 10 replies - 1 through 10 (of 10 total)
  • within a page yes you do – but if it’s used within a template then no. what i mean by template is a custom file that ends with .php such as underpants.php. this would be a template containing a layout for the underwear page for example.

    hello world would work with and without the php, i’m guessing that wp ignored the php and simply printed the text part of the entry. you would need runphp to execute php within a page.

    If you put PHP code in the Page Content text box when you are writing the Page, that code will not be executed without a plugin. PHP code that is “hard-coded” into a specific Page Template will execute; you don’t need a plugin.

    Thread Starter greymullet

    (@greymullet)

    Thanks very much ??

    I probably didn’t explain that too well. When I said about uploading the code, I meant a template file containing the PHP code. All of the PHP I talked about had been hardcoded. That at leat explains why the ‘Hello World’ ran without the plugin enabled.

    Now I just need to find out what’s wrong with the piece of PHP code that I wrote over the weekend.

    I’ll put it in the next post for clarity’s sake:

    Thread Starter greymullet

    (@greymullet)

    Right, I’ve found the code. Sorry about making this 2 posts, but I had to find the code and my computer was sounding like it could crash at any minute. I didn’t want to lose what I’d just typed.

    If anyone wants to have a look through this and point out anything that I might be doing wrong I’d very much appreciate it. The code’s intended to display the HTML code as many times as there are rows in the array, with the values from the array filled in. When I tried to use this hard-coded into a template the page wouldn’t even display. The source of what I got when I tried accessing it was just “<html><body></body></html>”. Any help would be great.

    <?php
    $Photoset = array(
    “Party 1” => “Smarch 2003”,
    “Party 2 => “Smarch 2004”,
    );

    foreach ($Photoset as $Party => $Date){
    echo
    <div class=”widetopbox”></div>

    <div class=”textbox”>
    <img src=”sunthb.jpg” class=”alignleft” />
    <div class=”setinfo”><h3>$Party</h3><br/>
    $Date
    </div>
    <div class=”newset”></div>
    </div>
    <div class=”widebottombox”></div>

    ;
    }

    ?>

    Try:

    echo <<<END
    <div class="widetopbox"></div>

    <div class="textbox">
    <img src="sunthb.jpg" class="alignleft" />
    <div class="setinfo"><h3>$Party</h3><br />
    $Date
    </div>
    <div class="newset"></div>
    </div>
    <div class="widebottombox"></div>
    END;

    OR break up your PHP into multiple segments:
    <?php
    $Photoset = array(
    "Party 1" => "Smarch 2003",
    "Party 2 => "Smarch 2004",
    );

    foreach ($Photoset as $Party => $Date) {
    ?>
    <div class="widetopbox"></div>

    <div class="textbox">
    <img src="sunthb.jpg" class="alignleft" />
    <div class="setinfo"><h3><?php echo $Party ?></h3><br/>
    <?php echo $Date ?>
    </div>
    <div class="newset"></div>
    </div>
    <div class="widebottombox"></div>

    <?php
    }
    ?>

    if it’s empty, that’s an error in parsing/compiling the php code. I’d try the second example above. You should also NOT have a comma after “Smarch 2004”, though I don’t recall if php has a problem with ‘extra’ commas.

    -d

    Thread Starter greymullet

    (@greymullet)

    Thanks, I’ll try cutting out the commas to start with. If that doesn’t work then I’ll try mdawaffe’s suggestion.

    With mdawaffe’s code will the:

    <div class=”widetopbox”></div>

    <div class=”textbox”>
    <img src=”sunthb.jpg” class=”alignleft” />
    <div class=”setinfo”><h3><?php echo $Party ?></h3><br/>
    <?php echo $Date ?>
    </div>
    <div class=”newset”></div>
    </div>
    <div class=”widebottombox”></div>

    repeat as many times as there are rows inthe array?

    Thread Starter greymullet

    (@greymullet)

    I tried it and still no luck. Any other suggestios would be really helpful.

    What I need is code that will repeat the HTML down the page but change those two variables. Any alternative method would be appreciated. The most important thing is that it works which, sadly, this isn’t doing at the moment. My original code, and all of the variations on it, are just making the server send a blank page. Not even any of the non PHP stuff is getting through, the page just comes out completely blank.

    Thread Starter greymullet

    (@greymullet)

    A friend of mine’s sorted it out. His suggestion was pretty much like mdawaffe’s except with one variation. He spotted what none of us seemed to have, namely me missing out a ” in the second line of the array.

    Whoops…

    Thread Starter greymullet

    (@greymullet)

    If anyone’s interested, the final code came out like this:

    <?php
    $Photoset = array(
    “Party 1” => “Smarch 2003”,
    “Party 2” => “Smarch 2004”,
    );

    foreach ($Photoset as $Party => $Date){
    ?>

    <div class=”widetopbox”></div>
    <div class=”textbox”>
    <img src=”<?php bloginfo(‘stylesheet_directory’); ?>/sunthb.jpg” alt=”image” class=”alignleft” />
    <div class=”setinfo”><h3><? echo $Party; ?></h3><br/>
    <? echo $Date; ?>
    </div>
    <div class=”newset”></div>
    </div>
    <div class=”widebottombox”></div>

    <?
    ;
    }

    ?>

    Once again, thanks for all the help ??

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Yet another problem with including PHP’ is closed to new replies.