开发者

Choosing one of two files to include in PHP

I do apologize for asking this stupid question but I am wondering if you can help me..

i want insert among file $cat1 or $cat2 to in page.php... the code i was create e.g.

<? //config.php
$cat_all = ($cat开发者_运维百科2 or $cat1);
$cat1 = "cat.php" ;
$cat2 = "../back-up/cat.php"; //default anyway
if(file_exists($cat1)){
    require_once $cat1;
}else{
    require_once $cat2;
}
?>

<? //page.php
require "config.php";
function ShowIndexProd($number){
    $ar = file($cat_all); // <------- input here //
    $sizear = count($ar)-$number;
    $data = explode("#",$ar[$sizear]);
    echo "
    <td class='BoxIndex' width='30%'><a href='prod_show.php?id=$sizear'><IMG SRC='img/$data[5]' width=150 border=0></a> <BR /><BR />
        <B><FONT COLOR='red'> $deti[2] </FONT></B> <BR />
        <B>Price:</B>$rp $data[3] $usd<BR />
        <a href='prodshow.php?id=$sizear'><b><FONT COLOR='#FF6600'> more details... </FONT></b></a>
    </td>";
}

but the result: file $cat1 or $cat2 printed to html page, that i want them as file configuration.. any help would much appreciate and thank you


You need to have <? ?> tags in files @cat1 and @cat2.


When you include or require a file it needs to have the PHP tags if you want to run PHP in the included file. I see you are using <? (known as short tags) so that needs to be in the file where the code needs to run.

You might, on a side note, consider moving to long tags <?php as this is more portable but that is a minor consideration at this stage.

Now once you have included cat.php it is going to run there and then. So any output will be above your current scripts output. If that is the expected behaviour then good. If it is not then you might want to think about where the output is wanted and include there.

I am not sure what you are trying to do with $cat_all = ($cat2 or $cat1); as you appear not to have defined the contents of those values until the next few lines. Also you do not appear to be using $cat_all anywhere after you define it.

If the result of including the files is not as you had imagined it would be that is a second issue and I would recommend posting another question with the contents of one or both cat.php files as well as the one above. Remember to tell us the expected and actual results so we can see where things are going wrong for you.

I suspect given the nature of your question and the fact that you fell for a beginners mistake that there might be other questions you will need to ask as you learn more about PHP. Even if we don't admit it we were all once beginners too.


Try include'config.php' ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜