Schlo_50 Posted November 20, 2007 Share Posted November 20, 2007 Hey guys, I am currently working on a project whereby users login to my website and then using a form add new quick links/tabs to their navigation bar. So for instance if the user had 'home/search engines/favourites' as links on their navigation bar and they wanted to add auction sites as a group link they fill out the form which then posts the data into a flat file database. (usertabs.txt) So far i can make the form send the link name, URL and attach the username of the person adding links so to make a id for the link name and URL. So in the flat file the structure is presently: username(unique)|Link name|URL schlo_50|Google|http://www.google.com What i want to do is write some code to say, 'when Joe Bloggs is logged in, display all links uploaded by him by searching usertabs.txt and outputting any link name and URL that follows his username.' So far i have: function displaytab(){ $usertabs = $_SESSION['userName']; $file = file("usertabs.txt"); foreach($file as $key => $val){ $data[$key] = explode("|", $val); $user = $data[$key][0]; $link = $data[$key][1]; $url = $data[$key][2]; } if ($usertabs == $user){ echo $link, $url; } } This is what i have so far, it's just i need something that works better with sessions.. If i log in with one user and upload a new link the script works but if i then log out and login with a different username the links aren't outputted. I've designed that code to search the whole text file for any entries matching the users username and output it but it doesn't do it very well. Help anyone? Maybe its my file search method? Help please, Regards Quote Link to comment Share on other sites More sharing options...
trq Posted November 20, 2007 Share Posted November 20, 2007 I assume you have session_start() at the top of the page? Try... <?php function displaytab(){ $lines = file("usertabs.txt"); foreach ($lines as $line) { $data = explode("|", $line); if (current($data) == $_SESSION['userName']) { echo next($data), next($data); } } } ?> Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted November 20, 2007 Author Share Posted November 20, 2007 I did that which worked perfectly. The thing now, is that i need to arrange any information sent to my .txt file so that it falls on a new line. The code i am using to upload file contents so far is: function addtab($name,$link){ if ($_POST['submit'] == "submit") { $file_name = "usertabs.txt"; $open_file = fopen($file_name, "a+"); $file_contents= $_SESSION['userName'] . '|' . $_POST['name'] . '|' . $_POST['link'] ."|"; fwrite($open_file, $file_contents); fclose($open_file); echo "New Tab successfully uploaded"; } } The code above inputs data in rows separated by '|' but i want new lines. I have a picture to illustrate what i want: Quote Link to comment Share on other sites More sharing options...
trq Posted November 20, 2007 Share Posted November 20, 2007 $file_contents= $_SESSION['userName'] . '|' . $_POST['name'] . '|' . $_POST['link'] ."|\n"; Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted November 20, 2007 Author Share Posted November 20, 2007 Thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.