...

View Full Version : Beginner: LOST and frustrated , please help!



rosiedee
01-15-2012, 01:11 AM
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?

mlseim
01-15-2012, 01:40 AM
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.



<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.



.

rosiedee
01-15-2012, 01:46 AM
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?

12k
01-15-2012, 02:57 AM
Try to look up arrays. I think this is what you are looking for.



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

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

tracknut
01-15-2012, 03:09 AM
Try it this way:


<---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

mlseim
01-15-2012, 07:05 PM
$article1="";
print $article1.$article2.$article3;

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

tangoforce
01-15-2012, 07:59 PM
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.

iLochie
01-16-2012, 02:57 AM
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:



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:

<---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.

rosiedee
01-17-2012, 07:30 PM
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

function otherArticles()
{ return
$article1.$article2.$article3 ;
}
?>

Am I missing something?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum