Page 1 of 1
Delete Arrays
Posted: Fri Aug 02, 2002 5:10 am
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

Posted: Fri Aug 02, 2002 5:47 am
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
Posted: Fri Aug 02, 2002 6:59 am
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.
Posted: Fri Aug 02, 2002 7:08 am
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
Posted: Fri Aug 02, 2002 7:33 am
by hob_goblin
basically you'll do something like:
Code: Select all
$file = file("file.txt");
foreach($file as $value){
$v_array = explode("|", $value);
foreach($v_array as $key => $info){
echo 'Key: '.$key."<br />\n";
echo 'Value: '.$value."<br /><br />\n";
}
echo "<hr />";
}
Posted: Fri Aug 02, 2002 7:42 am
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їbeskrivelse] </td>
<td align = "default"> $valїpris] </td>
<td align = "default"> $valї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

Posted: Fri Aug 02, 2002 7:50 am
by twigletmac
It won't work because you're using fopen() and not file(). I'd read hob_goblin's example again.
Mac
Posted: Fri Aug 02, 2002 9:45 am
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?

Posted: Fri Aug 02, 2002 9:46 am
by twigletmac
So what does your script look like now? What doesn't work? Do you get any error messages?
Mac
Posted: Fri Aug 02, 2002 10:21 am
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.
Posted: Fri Aug 02, 2002 11:38 am
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
Posted: Fri Aug 02, 2002 2:18 pm
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.
Posted: Fri Aug 02, 2002 6:59 pm
by Zeceer
Problem solved. Thanx everbody for your help
