Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Need help with named variable & include statement

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Need help with named variable & include statement

Postby kennyharrill » Wed Jul 17, 2013 8:13 am

Below is a partial bit of the code I am using in a php file. This particular file is called emails.php. This used to work for me:

<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="emails.shtml">Back to the main e-mails page.</a>
<br><br>

<?php
$emno = "emails/$em.php";
include "$emno";
?>

I am declaring a variable called emno to reflect the emails directory & respective files, then a statement to include the numbered php file in the emails directory. This particular php code has worked for me for a long time. All of a sudden when I try to open one of the numbered files in the emails directory online, all I see is the "Back to the main e-mails page" message. Can anyone recommend any kind of modification I can do to the php script to get this working? I edit my html and php pages for a particular website using Dreamweaver. I have switched from Windows XP to Windows 8; could this be an issue here?

Any help is appreciated. I want the site visitors to be able to see the contents of the email files themselves, not just the statement that says return to the main emails page.
kennyharrill
New php-forum User
New php-forum User
 
Posts: 9
Joined: Mon Apr 08, 2013 12:38 pm

Re: Need help with named variable & include statement

Postby johnj » Wed Jul 17, 2013 7:47 pm

try to display $emno using echo $emno; statement and check if this has a value. I think $emno has no value in it.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Need help with named variable & include statement

Postby kennyharrill » Thu Jul 18, 2013 4:11 am

<?php
echo $emno
$emno = "emails/$em.php";
include "$emno";
?>

I'm assuming you meant this. I tried this and now all I get is a blank page.
kennyharrill
New php-forum User
New php-forum User
 
Posts: 9
Joined: Mon Apr 08, 2013 12:38 pm

Re: Need help with named variable & include statement

Postby johnj » Thu Jul 18, 2013 8:41 pm

Use this code
Code: Select all
<?php

$emno = "emails/$em.php";
echo $emno;
include "$emno";
?>


it is also good to rewrite $emno = "emails/$em.php"; as $emno = "emails/".$em.".php";
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Need help with named variable & include statement

Postby kennyharrill » Fri Jul 19, 2013 3:54 am

Sorry, but I still just get a blank page when I click the link. Adding the "echo" didn't help.
kennyharrill
New php-forum User
New php-forum User
 
Posts: 9
Joined: Mon Apr 08, 2013 12:38 pm

Re: Need help with named variable & include statement

Postby johnj » Fri Jul 19, 2013 5:01 am

That means there is nothing in $emno. When there is nothing in what will it include? You need to debug and see why $emno does not have a value. you need to first check whether $em has a value.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Need help with named variable & include statement

Postby kennyharrill » Fri Jul 19, 2013 5:32 am

$emno refers to separate php files in a directory called emails. The php file with this code is used in connection with an shtml file called "emails.shtml". The files in the emails directory are consecutively numbered. Each individual email is referred to by a link, i.e., "emails.php?em=001". Don't know how much this will help.
kennyharrill
New php-forum User
New php-forum User
 
Posts: 9
Joined: Mon Apr 08, 2013 12:38 pm

Re: Need help with named variable & include statement

Postby johnj » Fri Jul 19, 2013 5:39 am

I don't understand what you are talking and what you have coded. whenever you want to include dynamically (as you have did), the final output has to be a file name, like include "abcd.php" or include "abcd/efgh.php" etc
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Need help with named variable & include statement

Postby kennyharrill » Fri Jul 19, 2013 5:56 am

It is. In this example, the first would be emails/001.php, then 002 and so on. Sorry for the confusion.

Maybe this will help:

Our file emails.php:

<?php
$emno = "emails/$em.php";
include "$emno";
?>

Our file emails.shtml:

<a href="emails.php?em=001">Open Letter</a>
kennyharrill
New php-forum User
New php-forum User
 
Posts: 9
Joined: Mon Apr 08, 2013 12:38 pm

Re: Need help with named variable & include statement

Postby johnj » Sat Jul 20, 2013 12:31 am

It looks like you are not collecting the "em" value. Try this code:
Code: Select all
if (isset($_GET["em"]))
{
    $em = $_GET["em"];
}
else
{
    echo "<br/> Unable to get value for the variable 'em'";
}
$emno = "emails/$em.php";
include "$emno";
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Need help with named variable & include statement

Postby kennyharrill » Mon Jul 22, 2013 3:59 am

Awesome! That worked! Thanks so much!! Appreciate it!!
kennyharrill
New php-forum User
New php-forum User
 
Posts: 9
Joined: Mon Apr 08, 2013 12:38 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Bing [Bot] and 3 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron