1

I am looking for some help using the PHP function. At the moment my website is structured like this:

index.php:

<?php   
require_once( 'page_elements.php' );
?>
<body>

<?php echo content();?>

</body>

page_elements.php:

<?php
function content() {
?>
<div class='main'> 
  <img class='main' src="<?=$ImgName?>"> </img> 
</div>
<?php
} ?>

if statement:

if (isset($_SESSION['basket_total']))
{

$basket_total = $_SESSION['basket_total'];

if($basket_total !== '0')
{

    $ImgName = 'img/basket.php';    

}
else
{

    $ImgName = 'img/basket_empty.php';

}
}

What I want to do is to be able to define $ImgName in an if statement that isn't involved in the function content() but if i include it in another file, include 'if_statement.php' or in another function then it doesn't recognise the variable.

Sorry, its my first time structuring a website like this and I am finding it a bit confusing.

Cheers in advance

4
  • You should look at a framework, you're going down the wrong path. I would recommend CodeIgniter for you Commented Nov 1, 2012 at 14:52
  • Yii MVC is better; pls paste the code with your variable; i will help you see get the code you ask; i did not understand the details; Commented Nov 1, 2012 at 14:53
  • 1
    Ya, everyone has their preferences. I think Kohana is better, but I wasn't arguing which was best, I recommended one that has the best documentation to make it as easy as possible for this guy. YII MVC IS BETTER! YA! Commented Nov 1, 2012 at 14:55
  • 1
    I would disagree recommending a frameworks straight away. He would be better served figuring out the basics first before adding on the learning curve of a framework. Commented Nov 1, 2012 at 14:59

2 Answers 2

2

First of all, you don't close an an "img" tag with another "img" tag ...

function content(){
    echo'
    <div class="main">
        <img class="main" src="'.$imgname.'" alt="" title="">
    </div>
    ';
}

is the proper way of doing things. Now as to your question, I'm having trouble understanding your goal, but do you perhaps mean something a.la ...

function content(){
    $imgname = include "file.php";
    echo'
    <div class="main">
        <img class="main" src="'.$imgname.'" alt="" title="">
    </div>
    ';
}

and the if_statement.php would be something like ...

if(isset($_SESSION['basket_total'])){
    return $_SESSION['basket_total'];
}else{
    return "img/basket.php";
}
Sign up to request clarification or add additional context in comments.

2 Comments

or pass the value as argument
@ChrisGastrell thank you very much for your answer. I can see it working but can you please give me more details of what the file.php would include given the if statement i gave in my original question? Cheers
0

This will get around the current issue you are having, but I would do like Ionut Flavius Pogacian suggested above and look into an MVC

<?php   
    require_once( 'page_elements.php' );
    $image_name = "batman.jpg";
?>
<body>
    <?php echo content($image_name);?>
</body>

page_elements.php:

<?php
function content($image_name) {
?>
<div class='main'> 
  <img class='main' src="<?=$image_name?>" /> 
</div>
<?php
} ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.