0

Building a simple jQuery/PHP setup where for example the PHP will store 5 variables each containing a simple string. These variables are all separate and not in an array. The jQuery used is just a simple fadeIn/fadeOut function which fades out the 1st string and fades in the 2nd string. Got all that working.

However this is for a client who doesn't really even know what a variable is and has asked that the 5 strings be "changable" so I'm creating a seperate PHP file that contains the strings so that the client can just open that file and change the strings within there and possibly add/delete strings from the file.

What I want the php to do is "count" how many variables there are then echo each string depending on how many variables there are. Variables are named like so

$text_0 = "Its a sentence";
$text_1 = "Its another sentence";
$text_2 = "Its a final sentence";

so obviously need a for statement

for(i=0;i<WHATGOESHERE?;i++){
echo $text_[i];
}

Thanks for any help.

4
  • 2
    You should use an array... Commented Dec 6, 2013 at 10:00
  • 1
    Use an array. Or, if it needs to be editable by a non-techie, a flat text file with one entry per line. Commented Dec 6, 2013 at 10:01
  • I can use an array as counting indexes within an array is easy as pie but yet again this is for an absolute non-techie. How hard could it be for them to edit it? very hard to people that ask "why is the dollar sign in that? its words not money" @deceze so use a function to import the text file contents into an array and divide the array up every /n? Commented Dec 6, 2013 at 10:11
  • Using the .txt method at the moment. Only problem i've got is that my client will want it so its "obvious" what he has to do/no confusing. My text file reads so every odd number index is the author of the comment and every even number is the comment. However this means that there can't be "seperator" lines between each comment. Essentially how do I remove the line break from the text before it passes to the php as each line break is being added to the array as an empty index. Commented Dec 6, 2013 at 11:44

3 Answers 3

3

If you want it to be editable for a non techguy, then dont even use the variables as its to easy to forget a ; or a $ or whatever. Just create a plain text file and in PHP use it as an array.

An added bonus is that the none tech guy is not able to inject PHP code to your system.

file.txt:

Its a sentence
Its another sentence
Its a final sentence

show.php:

<?php
$lines = file('file.txt');

//below is just an example, obviously it should be your jquery code
foreach ($lines as $line_num => $line) {
  echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
}

//length would be count($lines) and upper bound count($lines)-1
?>
Sign up to request clarification or add additional context in comments.

Comments

2

Give this to your non-techie:

Its a sentence
Its another sentence
Its a final sentence

Convert it into an array with:

$sentences = file('sentences.txt', FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);

Done.

Comments

0
<?php

$text_0 = 'a';
$text_1 = 'b';
$text_2 = 'c';

$vars = preg_grep('#^text_\d+$#', array_keys(get_defined_vars()));

var_dump($vars);

?>

It's better to create array $text..

3 Comments

Although you are correct, that is massive overkill and realy bad practice. I would almost give negative points, just to prevent some new coder thinking "Hey, that is a good idea. Thanks!"
Cmon, I just answered the question. You should reference your ideas to author.
People often dont ask what they want to accomplish, but how to implement what they think is the answer. If you want to help people you have to figure out what they realy want to do and help them do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.