• Mr. X

    ok, I thought it was a good idea but I’m starting to wonder…
    I created 2 files, one news.php and one calendar.php with a short version of the regular index.php code plus a
    $cat = x;
    on top of each file with the x replaced by the “news” and “calendar” category IDs respectively. I get the desired effect when I access each file individually through my browser: one shows all the “news” and the other all the “calendar” events…
    Only problem is, what I wanted to do was to include both file in a home.php page with a <?php include 'directory/news.php'; ?> type thing.
    What happens is that the first include is just fine, but the second outputs the “Sorry, no posts matched your criteria.” part of the conditional statement.
    I don’t know much about programming but I was guessing that maybe the if ($posts) conditional always returns false because the variables have still values from the previous include so I tried resetting those variables but my syntax might be wrong or I’m just missing the point… Not sure.
    Any PHP guru in the house yo?! ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • TechGnome

    (@techgnome)

    I take it that what you are trying to do is show the posts from those two categories from the main screen, right?
    Instead, try using $cat = ‘x,y’; where X is the cat # for news and y is the cat # for calendar. And take out the includes you have. Also, $cat should be treated as a string, and not just a number, hence the single ticks around the x,y…..
    TG

    misterx

    (@misterx)

    Thx TG! Actually, to do what I wanted (totally separated sections in the design for each cat), the solution was slightly different from your suggestion but you totally put me back into the right track. Instead of trying to include 2 different files with the $cat = x embedded on top, all I had to do was to use a scheme like that for each PHP include: <?php include 'https://www.blablabla.net/path/to/blog/index.php?cat=x';?>
    For some reason, it doesn’t work with relative URLs, I have to put in the absolute one for it to work. If someone knows why, I’d love to die slightly more intelligent/less stupid than I am now… ??

    When you do something like include 'directory/news.php';, you are pulling the file from the file system into the currently executing PHP code.
    When you give a full url like include 'https://www.blablabla.net/path/to/blog/index.php?cat=x';, PHP is requesting the file over the network, which means that it’s executing under a separate web request, then the results of that execution are included.
    Check your web server logs, and you’ll see those extra requests showing up.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘PHP variable scope headache’ is closed to new replies.