Delete Arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Delete Arrays

Post by Zeceer »

Hello,

i have a problem with arrays. Just so i can have something to do i am building a webshop without MySQL or PostgreSQL. The thing is that i need to know have to delete arrays that is keeped in another document.

<?php

$produkter = array(
"Test1" => array("beskrivelse"=>"Fungerer bra", "pris"=>"899kr", "info"=>'<a href="#">Les Mer</a>'),
"Test2" => array("beskrivelse"=>"Stort", "pris"=>"699kr", "info"=>'<a href="#">Les Mer</a>'),
"Test3" => array("beskrivelse"=>"Smaker Godt", "pris"=>"10kr", "info"=>'<a href="#">Les Mer</a>'),
"Test4" => array("beskrivelse"=>"Bra", "pris"=>"998kr", "info"=>'<a href="#">Les Mer</a>')
);

?>

This is a document called products.php. What i need is a file that can be called delete.php or something that can delete on of this array's. delete.php is to be used in the Admin section. Let's say i'm gonna delete "Test2" what to do then? :-). I know about unset() and that but it doesn't seam to work when the array is in another document.

And while i'm here, anybody knows how to add an variable to the document? Let's say i'm gonna add "Test5"?

I'll be gratefull for answers ot tips :-)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

What you need is not a page with an array defined in PHP but a flatfile database (basically a text file) something like:

Code: Select all

id|beskrivelse|pris|info
Test1|Fungerer bra|899kr|<a href="#">Les Mer</a>
Test2|Stort|699kr|<a href="#">Les Mer</a>
Test3|Smaker Godt|10kr|<a href="#">Les Mer</a>
Test4|Bra|998kr|<a href="#">Les Mer</a>
Then you can use PHP's filesystem functions to go through this file when you want to display, add or delete products. For example using file() you can read all of that information into an array.

Mac
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post by Zeceer »

I see, but have is possible to loop thru the "database" to output the info to the browser? I need to output the data in tables.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Well if you used file() it would all be in an array that you could loop through. If you use explode() on the elements in this array you can separate out each part of the individual records.

Check out these posts:
http://www.devnetwork.net/forums/viewto ... plode+file
http://www.devnetwork.net/forums/viewto ... plode+file
http://www.devnetwork.net/forums/viewto ... plode+file

Mac
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

basically you'll do something like:

Code: Select all

$file = file("file.txt");

foreach($file as $value)&#123;

$v_array = explode("|", $value);

 foreach($v_array as $key => $info)&#123;
  echo 'Key: '.$key."<br />\n";
  echo 'Value: '.$value."<br /><br />\n";
  &#125;

echo "<hr />";

&#125;
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post by Zeceer »

I've tried reading about explode() and file but I don't get it. Heres the file that will output the "database" to the browser:

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
      <title>Produkter</title>
</HEAD>
<BODY>

<?php include("style.php"); ?>

<table border=0 cellpadding ="10">
<tr>
<td align="default"> <?php print("Produkt"); ?></td>
<td align="default"> <?php print("Beskrivelse"); ?></td>
<td align="default"> <?php print("Pris"); ?></td>
<td align="default"> <?php print("Info"); ?></td>
</tr>

<?php

$produkter = fopen( "produkter.php", 'r' );

$explode = explode("|", $produkter);

foreach ( $produkter as $key => $val )

        print("
        <tr>
        <td align = "default"> $key </td>
        <td align = "default"> $val&#1111;beskrivelse] </td>
        <td align = "default"> $val&#1111;pris] </td>
        <td align = "default"> $val&#1111;info] </td>
        </tr>
        ");
        
fclose( $produkter );
        
?>

</BODY>
</HTML>
And heres the "produkter" file that is the database.

Code: Select all

Whey|Fungerer bra|899kr|<a href="#">Les Mer</a>
Gain|Stort|699kr|<a href="#">Les Mer</a>
Hard|Smaker Godt|10kr|<a href="#">Les Mer</a>
Soft|Bra|998kr|<a href="#">Les Mer</a>
Could you please make the file that outputs data to the browser work? Cause I can't :(
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

It won't work because you're using fopen() and not file(). I'd read hob_goblin's example again.

Mac
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post by Zeceer »

Well, tried it at it didn't work any better. Could yo please make the script so that it works as it's supposed to? :oops: :oops:
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

So what does your script look like now? What doesn't work? Do you get any error messages?

Mac
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post by Zeceer »

It's like the one i posted earlier today. It' is supposed to get the information from the "produkter.php" file and print it in the browser using tables. The file that handles the data and create the tables is "list_produkter". The both files are seen longer up.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Have you done your code in accordance with the example that hob_goblin posted, ie. have you replaced the fopen() statement with a file() one and deleted the fclose() statement? Have you had a look at the other posts that I linked to which are about doing similar things to what you want to do?

Mac
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post by Zeceer »

Yes, i've read them all and tried them all. The post's you linked i didn't anderstand all of it. I just need a script that can get the information out of the "database" and in to the browser.
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post by Zeceer »

Problem solved. Thanx everbody for your help :-)
Post Reply