Having trouble

From: Date: Sun, 27 Aug 2000 17:35:17 +0000
Subject: Having trouble
Groups: php.general 
Request: Send a blank email to php-general+get-13849@lists.php.net to get a copy of this message
Hi, I'm having trouble with a bit of PHP code. I'm trying to make a web based bookmark manager. Initially I'm just trying to make it work with one "folder", but I'd like to be able to make it work with multiple categories, or folders. I figured the easiest way to make it work would be to maintain a data structure containing the links, and then just write it to the disk using serialize() and retrieve it using unserialize(). (By the way, I specifically chose not to use a real database for this.) Right now I'm able to add links ok, but I'm having a lot of trouble deleting them. I have a bunch of debugging information enabled so you'll be able to see what's going on. Functions that aren't used are commented out. If you want to try out the code, add a few links (make sure you create a file called plinks3.dat in the same directory as the file and set the permissions to allow writing) and them try deleting them in various orders, and see what happens. Any help would really be appreciated. Thanks! Keith The entire code is pasted below. Unfortunately it's about 200 lines. :( ----------------------------------------------------------------------- <!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <style type="text/css"> .bad {background-color: red; color: black} .good {background-color: green; color: white} </style> <title>Links</title> </head> <body> <?php /*$default_category_name = "default"; $address = "address"; $text = "description"; */ function create_links(){ global $links; $links = array("default" => array()); } function add_link($address, $description){ global $links; //echo "Address was $address and description was $description<br><br>"; if(!$links){ create_links(); } $templink = array("address" => $address, "description" => $description); $links["default"][] = $templink; } /*function add_category($category){ global $links; if(isset($links[$category])){ return 0; } else{ $links[$category] = array(); return 1; } } function add_sub_link ($category, $address, $description){ global $links; $links[$category][]["address"] = $address; $links[$category][]["description"] = $description; }*/ function print_link($address, $description){ echo "<a href=\"$address\">$description</a>"; } function delete_link($index){ global $links; unset($links["default"][$index]); } function save_links(){ global $links; $fp = fopen("plinks3.dat", "w"); fputs($fp, serialize($links)); fclose($fp); } function load_links(){ global $links, $link_count; $linksfile = file("plinks3.dat"); $links = unserialize($linksfile[0]); if(!$links){ create_links(); } $link_count = count($links["default"]); } function dump_links(){ echo "<pre>"; var_dump($GLOBALS["links"]); echo "</pre>"; } function print_links(){ //Precondition: $links is already populated global $links; echo "<h2>Default</h2>\n"; echo "<ul>\n"; $delete_number = 0; while(list($index, $link) = each($links["default"])){ echo '<li><input type="checkbox" name="to_delete[' . $delete_number++ . ']" value="' . $index . '"> '; print_link($link["address"], $link["description"]); echo '</li>'; } echo "</ul>\n"; } function reset_links(){ global $links; unset($links); } //-------------------------------------------------------- if($action == "add_link"){ if(!$address || !$description){ echo '<p class="bad">A link must have both an address and a description</p>'; } else{ load_links(); add_link($address, $description); echo '<p class="good">Added link: '; print_link($address, $description); echo "</p>"; save_links(); } } elseif($action == "reset_links"){ echo '<p class="good">Resetting all links</p>'; reset_links(); save_links(); } elseif($action == "delete_link"){ $num = count($to_delete); if($num){ load_links(); echo '<p class="good">Deleted links: </p><ul>'; for($n = 0; $n < $num; $n++){ echo "<li>"; if($debug){dump_links();} echo "Trying to print number $n: "; print_link($links["default"][$n]["address"], $links["default"][$n]["description"]); if($debug){dump_links();} echo "</li>"; delete_link($to_delete[$n]); if($debug){dump_links();} } echo "</ul>"; save_links(); } else{ echo '<p class="bad">Nothing to delete!</p>'; } } elseif($action == "convert_old_links"){} //-------------------------------------------------------- $debug = 1; load_links(); if($debug) { dump_links(); } if($link_count){ echo '<form method="get" action="' . $GLOBALS["PHP_SELF"] . "\">\n"; print_links(); ?> <input type="hidden" name="action" value="delete_link"> <input type="submit" value="Delete selected links"> </form> <?php } else{ echo "<h1>No links</h1>"; } ?> <form method="get" action="<?php echo $PHP_SELF?>"> <table> <tr><td>Description:</td><td><input type="text" name="description"></td></tr> <tr><td>Address:</td><td><input type="text" name="address"></td></tr> </table> <input type="hidden" name="action" value="add_link"> <input type="submit" value="Add a link to this URL"> </form> <?php if($debug){ ?> <form method="get" action="<?php echo $PHP_SELF?>"> <input type="submit" name="action" value="reset_links"> </form> <?php } ?> </body> </html>

« previous php.general (#13849) next »