Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Angry Beginner: LOST and frustrated , please help!

    Here's my issue.
    I have a php document where I am keeping my variables.
    I "include" that document in my main page so I can call those variables.
    I want to set a variable equal to multiple variables, BUT I want a conditional statement which resets a certain variable to blank ("").

    For instance:
    <---articles.php ---->
    <?php
    $newsArticles= $article1.$article2.$article3;

    <--each article variable is already defined with the string including the text of the article-->

    ?>



    <---article1.php-->
    <?php include 'articles.php';

    $article1="";

    print $newsArticles;
    <--I want to be able to echo all the articles except the article that I cleared.-->

    ?>


    forgot to mention: I've tried an if statement but it doesn't seem to work. Is it because my variables are defined in a separate file?
    Last edited by rosiedee; 01-15-2012 at 01:17 AM. Reason: adding additional info

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,471
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Typically, what you're describing is a very simple CMS (content management system).
    But I'm not really sure what you're doing ... it seems like something that won't work ...

    How about this thought ...

    You create your articles using Notepad. Type them all up and save them
    with names like, art1.txt, art2.txt, art3.txt (plain simple text files).

    Then upload all of them into a directory ... you can call the directory "articles".

    In your webpage, perhaps you call the page "index.php", you include those .txt
    files. But you only include them if they exist. So, if you delete one of them, it
    won't show up. Create as many of them as you wish.

    PHP Code:
    <html>
    <head><title> This is my web page </title>
    </head>
    <body>

    <?php
    if(file_exists("articles/art1.txt")){
    include(
    "articles/art1.txt");
    }
    ?>

    <br /><br />

    <?php
    if(file_exists("articles/art2.txt")){
    include(
    "articles/art2.txt");
    }
    ?>

    </body>
    </html>

    The next level of scripting would be to do a loop, where the PHP script
    reads the entire directory (articles) and displays whatever is in there.
    But that's a different lesson. You have to start small and work your way up.



    .
    Last edited by mlseim; 01-15-2012 at 01:43 AM.

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Ah! Thank you for your reply. I didn't know you can do that. That will come in handy.
    But is it possible to reset the value of a variable within a variable?

    For instance, $newsArticles=$article1.$article2.$article3;

    then, on the page where I want all news articles except for the main article to appear, I want to clear the value so that it doesn't print anything.
    But when I try <? $article1=""; ?> and then <? print $newsArticles; ?>
    All of the articles still print. How can I exclude one, or is that not possible unless I have them in separate files like you described?

  • #4
    12k
    12k is offline
    New Coder
    Join Date
    Jan 2012
    Posts
    29
    Thanks
    0
    Thanked 6 Times in 6 Posts
    Try to look up arrays. I think this is what you are looking for.

    PHP Code:
    $arr['hey'] = "Hello There";
    $arr['bye'] = "Cya Later";

    $arr['bye'] = "";
    //or
    unset($arr['bye']); 

  • Users who have thanked 12k for this post:

    rosiedee (01-15-2012)

  • #5
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,226
    Thanks
    10
    Thanked 269 Times in 268 Posts
    Try it this way:
    Code:
    <---article1.php-->
    
    $article1="";
    <?php include 'articles.php';
    
    print $newsArticles;
    <--I want to be able to echo all the articles except the article that I cleared.-->
    
    ?>
    You've got to clear out the value of $article1 *before* you glue all the articles together.

    Dave

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,471
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    $article1="";
    print $article1.$article2.$article3;

    Don't combine them into one variable when you print them ... easy as that.

  • #7
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,313
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Quote Originally Posted by rosiedee View Post
    Ah! Thank you for your reply. I didn't know you can do that. That will come in handy.
    But is it possible to reset the value of a variable within a variable?

    For instance, $newsArticles=$article1.$article2.$article3;

    then, on the page where I want all news articles except for the main article to appear, I want to clear the value so that it doesn't print anything.
    But when I try <? $article1=""; ?> and then <? print $newsArticles; ?>
    All of the articles still print. How can I exclude one, or is that not possible unless I have them in separate files like you described?
    What you're trying to do can not be done.

    As msleim says, print them seperately where and when you need to or use an array and print the required array elements.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    rosiedee (01-16-2012)

  • #8
    Regular Coder
    Join Date
    Jan 2010
    Posts
    101
    Thanks
    17
    Thanked 5 Times in 5 Posts
    I think everything that can be said has been said, but just to reiterate in simple terms:

    When you create a variable (say $newsArticles = $article1.$article2.$article3) the $newsArticles variable is created with the values of the variables at that point in time. So if all the variables you are combining are empty, the $newsArticles variable will be empty as well, unless you redefine it.

    The best way to avoid this is to not assign the variable until you are sure all component variables have been defined. If you wanted to continue the script the way you have done up until this point, I could recommend you create a function which you can put into your variables PHP document:

    PHP Code:
    function newsArticles(){
         return 
    $article1.$article2.$article3;

    That way you can contain your variables (and now functions) all in one place, while still retaining the structure of the code.

    Here's now what you'd have to do:
    PHP Code:
    <---article1.php-->
    <?php include 'articles.php'

    $article1=""

    print 
    newsArticles();
    <--
    I want to be able to echo all the articles except the article that I cleared.-->

    ?>
    Notice the difference in the line that prints the variable.

  • Users who have thanked iLochie for this post:

    rosiedee (01-16-2012)

  • #9
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hi, your suggestion sounds EXACTLY like what I would like to achieve.
    However, the print otherArticles(); doesn't return anything even though I 'include' the php file containing the defined function:
    PHP Code:
    <?php 

    function otherArticles() 
                {    return
                    
    $article1.$article2.$article3 ;
                }
    ?>
    Am I missing something?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •