Page 1 of 1

List Array Values from TXT that have a Common Variable

Posted: Wed Jul 29, 2009 8:17 pm
by jfc3
Hello. Below is a list I have that is using a common variable throughout every array.

Code: Select all

$myvar[value_a] = array('title'=> 'Title of Value_a', 'description' => 'Description for Value_a');
$myvar[value_b] = array('title'=> 'Title of Value_b', 'description' => 'Description for Value_b');
$myvar[value_c] = array('title'=> 'Title of Value_c', 'description' => 'Description for Value_c');
$myvar[value_d] = array('title'=> 'Title of Value_d', 'description' => 'Description for Value_d');
$myvar[value_e] = array('title'=> 'Title of Value_e', 'description' => 'Description for Value_e');
$myvar[value_f] = array('title'=> 'Title of Value_f', 'description' => 'Description for Value_f');

This is for a project where depending on what value the code calls for, it inserts the appropriate title and description. So my arrays need to keep the same variable ($myvar) for every line like listed above. Now this functions perfectly, but is not relevant to my question. I only mention it to mate not that I cannot use different varabile names for each line.

Okay, my question... In another page on the site I am wanting to have a list that will show all lines of the txt file. For example, it would appear like:

Title of Value_a Description for Value_a
Title of Value_b Description for Value_b
...and so on.

In my mind, I am thinking the code would be sometihng like this:

Code: Select all

$allarrays = "thearrayfile.txt";
$lines = file($allarrays);
foreach ($lines as $line) 
{
echo ($myvar[]['title'].$myvar[]['description'].'<br />');
}
...so that it would echo the title and description of line 1 plus a <br />, then echo the title and description of line 2 plus a <br />, and continue. However, it does not work. Also note that this text file will constantly have new lines of arrays added weekly so the PHP cannot simply say "echo the data on line 1, then line 2" specifically. It has to do it in the way that it simply says pull the top line and continues down the list until it completes the list regardless of whether it totals 10 lines or 100 lines.

Would someone more knowledgeable of PHP be able to see a quick fix to this solution? If you need further info, let me know, and thanks in advance.

Re: List Array Values from TXT that have a Common Variable

Posted: Wed Jul 29, 2009 11:45 pm
by jfc3
Oh, okay. I can see how using fgetcsv would work in this scenario. Your solution does exactly as I was theorizing, and leads me in a direction that I was looking for. Thank you.

Okay, I do have a second question about using the data.txt for a different section on the site. Is it possible to add an id to each line at the beginning. For example the data.txt would read:

Code: Select all

ID_A Title A Description A
ID_B Title B Description B
ID_C Title C Description C
ID_D Title D Description D
Then in a completely isolated section of PHP it could explode or convert this file to create a common set of arrays that would equal:

Code: Select all

$item[ID_A] = array('title'=> 'Title A', 'description' => 'Description A');
$item[ID_B] = array('title'=> 'Title B', 'description' => 'Description B');
$item[ID_C] = array('title'=> 'Title C', 'description' => 'Description C');
$item[ID_D] = array('title'=> 'Title D', 'description' => 'Description D');
After which I could have a line that says:

Code: Select all

$id= $_GET['id'];
echo $item[$id]['title'], '<br />', $item[$id]['description'];
So if i pass a variable in the URL like "read.php?id=ID_C", the output would simply read:

Code: Select all

Title C
Description C
I definitely know how to do this ONCE the full arrays are built, I'm just having trouble on how to convert each line of data.txt into a single array as shown above.

Re: List Array Values from TXT that have a Common Variable

Posted: Thu Jul 30, 2009 12:19 am
by jfc3
That makes perfect sense and will function in the way I was looking for. I figured it was a simple step forward from your previous code you created. I also completely understand how a repeating ID will result in the final occurrence taking priority. I think that wraps up any questions or confusion I was having. I really appreciate you taking the time to help me out on this situation. Thank you again. :)