Closed Thread Icon

Topic awaiting preservation: Adding to a 2-Dimensional Array? (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=23557" title="Pages that link to Topic awaiting preservation: Adding to a 2-Dimensional Array? (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Adding to a 2-Dimensional Array? <span class="small">(Page 1 of 1)</span>\

 
Jimmy
Obsessive-Compulsive (I) Inmate

From: Alderaan
Insane since: Sep 2004

posted posted 10-06-2004 01:36

I've been learning PHP for a little more then two weeks now. With PHP I found a decent way of organizing my links. As you can see from the code below I created a bunch of arrays (1,2, ...) and then inside them I created an array where I have the link information and catagory. The while loops sorts through my arrays and checks the catagory. If the catagory equals $selection then it prints it out. For my purpose here I just set $selection to something but in the actual use of it I use a form to submit which section I want.

This works fine for me but my link collection is well over 200 links and I add things to it daily. I'm curious if theres a way I can create another php file that allows me to add to the $links array a new add. I've been reading about push and pop but I've been having problems implementing it. My PHP knowledge is limited but I'm learning more each day. Also, I don't know anything about MySQL databases so that isn't really an option.

Does anyone have any ideas?


code:
<?php
$links = array(
"1" => array(
"&#187; <a href=\"#\" title=\"Link 1\">Link 1</a>",
"First"
),
"2" => array(
"&#187; <a href=\"#\" title=\"Link 2\">Link 2</a>",
"Second"
),
"3" => array(
"&#187; <a href=\"#\" title=\"Link 3\">Link 3</a>",
"Third"
),
"4" => array(
"&#187; <a href=\"#\" title=\"Link 4\">Link 4</a>",
"Fourth"
),
);

$selection = "Fourth";
$counter = 1;

while($counter <= sizeof($links))
{
if($links["$counter"][1] == "$selection")
{
print ($links["$counter"][0]);
print ("<br>");
}
$counter++;
}
?>



Jim

PaulBM
Nervous Wreck (II) Inmate

From: East Anglia, England.
Insane since: Sep 2003

posted posted 10-06-2004 10:22

If you want to stay away from mySQL, which would be the best solution, I think a text file would be second best.
Design a format for a text file, say 1 link per line, comma delimited and enter a few links.
Alter the php routine to read lines from the text file and analyse the link data, printing it if required.
Then you'd be able to edit the text file and not have to change the php code.

The next step would be to make a form to read and write the text file to avoid having to upload the edited version everytime.

But, if you have mySQL available with your web host, I'd look into that route. A simple table with a few fields for each link would be a good way to get into mySQL. You won't regret it once you get past the initial starting point.

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 10-06-2004 10:24

well... I can see serveral ways, the easiest once storing your array into a file, using php->serialise() and php->unserialize() as well as php->fopen() ,php->fwrite() and php->fread() (not to mention php->fclose() ;-) ).

holler if you need more hints,

->Tyberius Prime

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 10-06-2004 11:17

You could even look at the parse_ini_file function.
Anyway, you could remove all the HTML markup of your array.

Hebedee
Paranoid (IV) Inmate

From: Maryland, USA
Insane since: Jan 2001

posted posted 10-11-2004 02:04

I did not look too hard at this, so don't be angry if I am irrelevent.

If you have two dimensions in your array, to add a new value you can have:

code:
$myArray[setValue][]

where the setValue is the first dimension, and you want to add a new value to the second dimension.

Jimmy
Nervous Wreck (II) Inmate

From: Alderaan
Insane since: Sep 2004

posted posted 10-11-2004 22:59

Thanks for all the help guys.

I'm going to try reading up on the things you've suggested and experimenting with a few of them. I'm also going to attempt getting started with MySQL w/ Dreamhost. Does anyone have any suggestions? I believe I have unlimited databases but I don't have a clue where to begin. Off to Google I go!

Jim

Jimmy
Nervous Wreck (II) Inmate

From: Alderaan
Insane since: Sep 2004

posted posted 10-12-2004 01:14

Could you explain the serialize function a bit more TP? I attempted to simply open, read, and close a seperate text file like I would in C++ but that doesn't appear to work the way I planned. I'll post what I'm working with though:

code:
<?php

$file = fopen("data.txt", "r");
$counter = 0;
$counter1 = 0;
$number[] = '';

while (!feof($file))
{
$number[$counter] = fread($file, 10000);
$counter ++;
}
fclose($file);

while ($counter1 < sizeof($number)
print ("$number[$counter1] <br>");
?>



data.txt is just a simple text file in the same directory as the PHP script. I was attempting to open the file, read it in, and then close it. Then I wanted to print it to see what happens.

When I run that script, I get the following error: Parse error: parse error, unexpected T_PRINT in /home/.waikiki/seib3184/domain.com/php/openfile.php on line 25, which happens to be the print statement.

Any ideas?

Edit: If any of my code is confusing, please ask and I'll clearify. Also, if anyone is aware of any good mySQL tutorials/books, I'd appreciate a little info.

Thanks

Jim

(Edited by Jimmy on 10-12-2004 01:16)

(Edited by Jimmy on 10-12-2004 01:18)

Hebedee
Paranoid (IV) Inmate

From: Maryland, USA
Insane since: Jan 2001

posted posted 10-13-2004 00:18

The problem is that in your while loop you have the incorrect number of parentheses. It should be:

code:
while($counter1 < sizeof($number))



It's also a good idea to use brackets in loops and conditionals, even when there is only one line inside - it allows for enhanced readability. TB will tell you that.

I think I used a pocket guide for mySQL for a long time. I haven't seen it for a while. I usually use the SQL tutorial on www.w3schools.com - it is concise and informative.

« BackwardsOnwards »

Show Forum Drop Down Menu