|
|
Posted in Computers & Tech / How-To's and Tutorials / Programming / PHP
Author: Archimedes Total-Replies: 6 Alright, some of you might want to display your User's or Members on your site. Notes: 1.This is to fit in with Feelay's register and Log-in scripts you can find in the tutorial section. 2.I made this to show the members of my site who is a member and what their ID is. First off, we must set up a connection to our MySQL Database. CODE<?php$con = mysql_connect("localhost","database_username","database_username_password"); if (!$con) { die('Could not connect: ' . mysql_error()); } What it does: 1.It is starting a PHP document. 2.Next it sets up a connection to the database with the members table. 3.Change 'database_username' to your database username. 4.Change 'database_username_password' to your username's password. Next, we select the database the information is stored at using the 'mysql_select_db() function. CODEmysql_select_db("userbase_name", $con);What it does: 1.All it is doing is selecting a database using the connection in the first part of code. For the next part, we will be selecting the table in which the members info is stored at, whereas the table is named 'members'. And you want to order them by ID. CODE$result = mysql_query("SELECT * FROM members ORDER BY ID");What it does: 1.The '$result', is just a variable, so you don't have to type the function again. 2.It is selecting everything '*', from the table members and ordering it by the user's ID. For the third part, we will be creating a HTML table to display the user information. CODEecho "<table border='0'><tr> <th>UserName</th> <th>ID</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['UserName'] . "</td>"; echo "<td>" . $row['ID'] . "</td>"; echo "</tr>"; } echo "</table>"; What it does: 1.You can can change the '0' in the <table border=''> tag to anything you chose. 2.The '<th>User Name</th>'&'<th>ID</th>' tags are the top of the table. 3.The 'while() function is creating an array, whereas '$row' is a variable to shorten your work, instead of typing the mysql_fetch_array() function out again. 4.The 'echo "<td>" . $row['UserName'] . "</td>";' & ' echo "<td>" . $row['ID'] . "</td>";' is just selecting the info from the table to be displayed, whereas the user/members name is stored in the column 'UserName' and the ID is stored under 'ID'. Ending your document. CODEmysql_close($con);?> What it does: 1.It is closing the connection we made in the first part of this tutorial. 2.It is closing the <?php tag in the first part of code of this tutorial. The Output User Name ID User1 1 User2 2 User3 3 User4 4 etc. etc. That's it! Any questions, feel free to ask. This is my first tutorial on Astahost forums.
Sat Jul 26, 2008
Reply New Discussion
Posted in Computers & Tech / Databases
Author: suicide Total-Replies: 1 I'm currently designing an Intranet but one section needs to provide a search on around 5000 documents (word and pdf). Rather than list them all on a page with links to the documents (take a long time to setup and maintain)I was hoping to include a search facility. This search would look at filenames within directorys and display a list of the files found. A user could then select the document they require to look at Any ideas how I might achieve this? Many thanks.
Fri Sep 10, 2004
Reply New Discussion
Posted in Computers & Tech / Programming / Scripting / PHP
Author: vujsa Total-Replies: 7 What you need here is the header function. Like so: CODE<? phpif($_POST['username'] == 'Nuwan'){ header('Location: http://www.example.com/authorized.php'); } else{ header('Location: http://www.example.com/login.php'); } ?> Of course, you'll need a way to compare your username and password to a database or list of users but you didn't ask that question so I'll assume for now that you either don't need it yet or have already figured that out. Hope this helps. vujsa
Thu Sep 13, 2007
Reply New Discussion
Posted in Computers & Tech / Programming / Scripting / PHP
Author: Mordent Total-Replies: 1 I'm curious as to the best methods of letting users submit data to a MySQL database, displaying that data, and removing any unwanted tags etc. from it. Currently, there's a handful of PHP functions that I know of to help with this:[list] QUOTE (php)...which prepends backslashes to the following characters: \x00, \n, \r, \, ', " and \x1aI like to think I made a pretty good attempt at finding out what \x00 and \x1a are, but I can't find anywhere that will simply tell me. I'd assume that one of them is a hyphen (-), as that has special significance in SQL? QUOTE (php)This function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.Anyone care to comment on how that would handle a string differently from htmlspecialchars()? QUOTE (php)The PHP directive magic_quotes_gpc is on by default, and it essentially runs addslashes() on all GET, POST, and COOKIE data. Do not use addslashes() on strings that have already been escaped with magic_quotes_gpc as you'll then do double escaping. The function get_magic_quotes_gpc() may come in handy for checking this.[/list]So, given the function arsenal above, what can we get out of it? Clearly we could apply a lot of overkill to some strings, which would be unnecessary, but what's the minimum that needs to be done to make user-inputted data secure and still output what you want? Let's say we have a textarea which the user can input whatever they like in to, and in all cases the data will be stored in a MySQL database and can be displayed exactly as typed (mainly because that's the bit I'm working on Take this forum, as an example. I can quite happily type things such as "<b>foobar</b>" and they display exactly as entered. The quotation marks are left in, the bold tags are displayed, but not carried out. All formatting such as using bold text is done on the user's side with BB Code, which uses square brackets. For now, however, I want to leave this additional formatting alone, and just show precisely what's typed in. So, back to the textarea idea, let's say we have a form as below: CODE<fieldset><legend>Update Text</legend> <form action="update_text.php" method="post"> <textarea cols="100" rows="10" name="text"></textarea><br /> <input type="submit" name="update" value="Update" /> </form> </fieldset> So whatever the user types in is sent (via POST) to the script update_text.php. In that file we want to store it in a MySQL database. Given that we have a method of identifying the user by an ID (via sessions, most likely), and that the required file connects to the database. CODE...// process input $text = $_POST['text']; // access database require('includes/db.php'); mysql_query('UPDATE members SET text = "' . mysql_real_escape_string($text) . '" WHERE id = "' . mysql_real_escape_string($id) . '"') or die(mysql_error()); ... So, correct me if I'm wrong, but that would store the text so it can be recovered as entered? Newlines ("\n") would be put in, naturally, and any relevant characters would be escaped so that they're stored in MySQL correctly, and the possibility of SQL injection here would be low, right? The data would now be stored, theoretically exactly as inputted. If we want to get that data back out, so that it's shown by default in the form we could do so as shown below: CODE...// access database require('includes/db.php'); $getMember = mysql_query('SELECT text FROM members WHERE id = "' . mysql_real_escape_string($id) . '"') or die(mysql_error()); if (mysql_num_rows($getMember) == 1) { // member found $row = mysql_fetch_array($getMember); $currentText = htmlspecialchars($row['text']); } ... ...and then echo $currentText between the textarea tags in the form? htmlspecialchars() would need to be used, I believe, to stop people from closing the textarea early themselves and going on to do anything else they want. I'm pretty sure no other functions in the list above need to be used, but I'd like to confirm that. Then, when displaying the text (i.e. not in the textarea), I assume something like this could be used: CODE...// access database require('includes/db.php'); $getMember = mysql_query('SELECT text FROM members WHERE id = "' . mysql_real_escape_string($id) . '"') or die(mysql_error()); if (mysql_num_rows($getMember) == 1) { // member found $row = mysql_fetch_array($getMember); $text = nl2br(htmlspecialchars($row['text'])); } ... ...which is identical to the previous method except for the use of nl2br() as well. Note that it's used after htmlspecialchars(), as otherwise the "<br />" tags would else be converted to "<br />" afterwards. Would any other functions need to be used, or would that simply do the job to a high enough level of security and still give the desired result? Thanks in advance for any feedback or comments, Mordent
Sun Mar 16, 2008
Reply New Discussion
Posted in Computers & Tech / Programming / Scripting / PHP
Author: TavoxPeru Total-Replies: 6 QUOTE (Chesso)Iv'e done some searching and found this default_timezone_set() function for PHP but it doesn't tell me how to find what I should give as the paramater for my particular timezone (I'm in Sydney so GMT+10, EST, I think). Anyone know how to use this function? P.S. I use the date() function and just store it as a string in the mysql database. Link: view Post: 83872 Do you mean date_default_timezone_set() function??? well, if it is, you can't use this function with the Asta's server because this function only works with PHP 5+ and as you know the Asta's server works with PHP 4.4.2. Now, the parameter that this function use is a timezone identifier and for your case it is Australia/Sydney, you can find a complete list of timezone identifiers in the appendix H of the php manual. Best regards,
Wed Aug 2, 2006
Reply New Discussion
Posted in General Discussion / Computer Talk
Author: livingston Total-Replies: 4 Skype is changing the world of communication by allowing users to talk from one PC to another PC any where in the world and connected to Internet, free using internet telephony. Only limitation is that you need a PC with internet connection to make calls, now a new product from Netgear is taking the whole Skype experience to a new level. The Skype WiFi phone utilizes tha wireless internet networks to allow users to make free calls to other Skype users anywhere in the world, anytime there is WiFi access, withour a PC The Skype WiFi phone allows you to make calls to other ordinary phones too. The phone's display lists all the online users in the user's contact list. Using this phone is just like any other ordinary mobile phone. Truly this is a great gadget for the increasing internet telephony communications. It will be of great use for people in cities with public WiFi access to make calls to others anywhere in the world for free or cheaper prices. Get to know more about this phone from http://www.netgear.com/products/details/SPH101.php
Fri Jun 29, 2007
Reply New Discussion
Posted in Computers & Tech / Databases
Author: Ahsaniqbal111 Total-Replies: 2 Wordpress runs on apache and mysql. The databse connection capabilites of wordpress are not known to most of wordpress users. The reason is that every thing is sort of automated and a user almost never needs to know how wordpress connects to the database and how it fetches data. I have ran into a problem where I need to create additonal entries into the wordpress database (entries that are not created by wordpress automatically) and then fetch data from these entries from within wordpress. The plan is to include a short summary of some terms that would be displayed when these terms are clicked. When a user clicks on a word (included in the list of words in the database), a connection to database would be made and a query be executed whereby corresponding information would be fetched and displayed to the user. In theory this looks simple but there are few glitches that need to be resolved. The most important of these is the securtiy issue created by connecting to the databse through a method not approved by wordpress. For instance I can simply creat a connection by the following using "<?php mysql_connect(server, username, password)?>" and then use this connection to perfrom queries and fetch data. But this would not be the secure way as the password can then be viewed easily and then can be used for ill things. So I am looking for more secure ways of connecting to the database from within wordpress structure (I mean without having the need to go to custom made pages specifically created for this purpose). I did some search on this topic and found that there is a function called "wpdb()" that exists specifically for fetching data from the database from within wordpress. The problem is that I don't know how to use this methos and whether it is secure enough or not. That is why I have created this topic to get more information from experts out there.
Tue Feb 7, 2012
Reply New Discussion
Posted in Computers & Tech / How-To's and Tutorials / Programming / PHP
Author: 8ennett Total-Replies: 6 Before starting on this tutorial be sure to run through the first 3. Part 1: Advanced User Account System Part 2: Welcome Home Part 3: Administrative ########### Part 4: Profiles ########### Download Example4.rar In this tutorial we are going to create several things. [list] [/list] Our contact list can be found on the updated menu under 'contacts'. This also contains the users blocked list as well at the bottom of the page. ![]() As you can see from the above image it simply contains the name of the person on either the contact list or blocked list and also the action link which will simply say remove. You can choose to remove people off either after putting them there. Users who are on your blocked list are unable to message you, and users who are on your contacts list will be eligible to message you when you turn the 'receive messages from contacts only' option on in your profile settings. Currently there is no messaging system or profile setting for user only messages but we will be creating that in the next tutorial. The other new item on our menu is the Contacts link. Clicking this menu link will take us to the module page modules/profile/profile.php. Now profile.php determines what users profile you are looking at through the $_GET variable USER. Eg. http://www.mysite.com/home.php?l=profile&user=1 will display the profile for who ever has the user id 1. ![]() As you can see on the above image it shows certain information about the user such as name, gender, account type, health stats, cash on hand, level and current status (Online, Offline, In Prison, In Hospital, Banned). Also at the bottom of the profile page is the players signature. This is an area which will only be displayed if the user has input something in to their signature. Between the signature and the players information is the control panel. Now this is obviously different depending on wether you are looking at your own profile or another players. When viewing your own profile there are four different options in our profile control panel. The first is the choice to edit your profile. ![]() As you can see the user can change their name, email, password and signature. If you look through the code for modules/profile/edit.php you will see how we process the form data as all the lines are commented in full as usual. We also ensure that the user is actually changing certain data and only adjust the database data where necessary to save on server load. We have also added a new feature to the players signature. As well as being able to write any text they want in there, they can also insert BB Code. This is specialised code that allows users to format their text in different ways (alignment, bold, italic, underlined, size etc.) as well as being able to insert images and youtube videos. If you look through the new BBCode() function in our lib/functions.php file you can see how simple it is to add more bbcode tags of your own and the outputted html each tag results in. There is also another feature and new function in our functions file where we check to see the size of an image a user is inputting in to their signature, and if it is too wide then we rescale the image so it won't mess up the formatting of our page. Remember, all our modules can be no more than 600px wide otherwise our page starts to look distorted and mishapen. This adds another level of professionalism to your site while maintaining aesthetic integrity. The avatar on the users profile is also subject to the same rescaling except it measures both height and width and rescales to a maximum of 150px for either height or width. Next up on the profile control panel we have the inbox on/off switch. When the inbox is turned off then the player will not receive messages from anyone but admins. We will also be creating a 'only contacts can message' button as well but that's for the next tutorial. After the inbox on/off switch comes our avatar upload button. This loads up the page modules/profile/avatar.php. It's a very simple and effective script where the user can upload an image that is less than 100kb in size (we limit size to save server space) and can only have the extension GIF, JPG, JPEG or PNG. We have to limit the file extensions to prevent upload of malicious software and scripts. ![]() Finally on the profile control there is the delete account button however there is still too much work to be done and unknown future table names that need to be considered so we won't be programming this yet, however there is a piece of javascript written in to the link which asks for confirmation of wether the player wants to delete their account or not. This is to prevent people from accidentally clicking it and deleting their account by mistake. If we move on to the other player profile control panel. Start by registering a new account on your game and manually activating it through the sql database by changing the userlist-validated column to 'Yes'. Now take note of this players new id number and go back to your own account. You will notice we have added another new feature to the game and new function to the lib/functions.php file. This function is userName($id); which displays the name of the user id and also makes the name a link which when clicked takes you to the players profile. This is how we are going to display any users name in the game from now on to help make it more accessible. The link generated for the users name also has their account type in the title so when you mouse over the link you can see their account type. Anyway if you click your name in the top left corner it will take you to your profile and in the address bar should be http://www.mysite.com/home.php?l=profile&user=1 or something similar. If you change the 'user' value to the user id of the new account you just created you can see that players profile now and the player profile control panel. ![]() So far we only have 'Add to contacts' and 'Block user' but in the next tutorial we will be working on more things to put in here. When either of these links are clicked it then either adds the user to the contacts list or blocked list and changes the link on the page to either 'Contact' or 'Blocked' which can be viewed in the 'Contacts' link in the menu. Another feature is, if you add someone to your contacts list while they are on the blocked list then they will become unblocked, and the same the other way around. If you click block user when they are in your contacts then they are remove from your contacts. A few other points interest are how we retrieve a users profile avatar, that is set in a custom function. Also there is the findexts() function which basically returns file extensions. We have also added level, signature, inbox, prison and hospital to our userlist table and created the contacts and blocked tables as well. [list] [/list] As usual leave any comments and questions below and I'll do my best to help. And please, can people stop asking me to make their sites for them. Just learn how to do it yourself through these tutorials.
Wed Jan 12, 2011
Reply New Discussion
Posted in Astahost / Announcements
Author: vujsa Total-Replies: 15
Welcome to Astahost Free Web Hosting
You will notice that the term "Free Web Hosting" is used on the site frequently but you will hardly ever see the term "free web space" on this site. This is because there is a difference between the two. Free Web Hosting at AstaHost is the same kind of web hosting that is usually paid for. This means that you will have a complete and fully featured hosting account that will allow the use of any scripts you wish which means you'll have your own CGI-BIN, database creation and usage, multiple email accounts, multiple FTP accounts, multiple sub-domains, domain hosting, an IP address, and you will be the webmaster of your website. There will not be any advertising banners on your website unless you put them there. Free web space on the otherhand is were someone else is using their website to provide their users with a place to store and display web pages and images. Sometimes these accounts come with an email address. Generally speaking, all file uploades are done via a web based file manager and your web pages are altered to display advertising banners and whatnot. Rarely will you ever be able to host any server side scripts such as php or perl on one of the websites. If you are looking for the link to upload you files, then you have misunderstood how AstaHost works. Here is the system at AstaHost:[list=1] [/list]It might be easier to think of AstaHost a a regular full featured Web Host where instead of your credit card being charged each month, you simply have to remain active in the forums. This is commercial quality web hosting free of charge, not a web space. If you don't feel comfortable with the technical nature of the categories in the AstaHost forums, I would suggest using our sister site at Trap17 instead. Trap17 is less technically oriented and may be easier for you to post in. The hosting features are the same there. I hope that this clears up some of the confussion about what service is actually being offer here. Happy Webmastering, vujsa
What AstaHost Is Not!
[list=1][/list]This is a Posting for Hosting system only. All other uses are prohibited. If you feel that this system may not allow for your needs, it probably won't. We are looking for quality forum members that provide quality posts and we reward them with free web hosting. If you have further questions about the system, please see the FAQ. Thank you for your interest. vujsa * Music and data files may be stored on your hosting account when used for the purpose of website creation.
Sun Jun 5, 2005
New Discussion
Posted in Computers & Tech / Software / Content Management Systems (CM.. / Mambo/Joomla CMS
Author: vujsa Total-Replies: 4 To select which pages the module will be displayed on, you'll need to edit the module settings. Administration > Modules > Site Modules Select the module to edit and either click on it's name or check the box and click the edit button. Now on the right side of the module edit screen you'll see the header " Pages / Items " This is a list of every page (component) in your Mambo / Joomla website. If you select "All" (default) the module will show on every page. If you select "None" it won't show anywhere. If you select the individual pages to display on, you have to hold the "control" (ctrl) button to select more than one item and it will only show on those pages. Additionally, you can have different module only disply if the user is registered or if the user in a staff member. This is controlled with the "Access Level" option. I usually set the Admin Menu (Other Menu) to "Special" so it only shows up for me. No reason for everyone to see that link and it adds a second layer of protection to the Admin directory. [highlight=teal][hr=0][/hr][/highlight] If you want a module to display in different places depending on which page in being displayed, you need to create a copy of the module and edit it to do as you want being sure not to overlap a page that already has the original module on it in the other position. So you can have the log in module on the left side on the home page and then have the log in module at the top on every other page. Each module will have a different name but you can use the same display name for as many modules as you would like or even turn the display name off. [highlight=teal][hr=0][/hr][/highlight] You may find that after you get things set the way you want in Mambo / Joomla, the template may not need too much redesigning other than editing the CSS which holds all of the color, font, size, position, and image information for the template. With Mambo / Joomla, two people can use all of the same modifications and templates but have completely different looking websites if the change where their modules are displayed and the graphic / color information. Let us know if you have any more questions, I could talk about Mambo for hours and never get bored. Enjoy you installation of Mambo. vujsa
Mon Dec 5, 2005
Reply New Discussion
Posted in Computers & Tech / Software / Internet & Network Clients..
Author: szupie Total-Replies: 6 Trekkie101 has just informed me of a new testing version of Google Talk. It's not mentioned by Google's blogs, so there's no page on Google's site that links you to the download. You'll have to get it with this direct link. customizetalk.com has a longer review of this new version. It's got screenshots, too The two main features of this version are the avatars and the themes. Google Talk users will be able to select a custom avatar for display. It'll appear on both the contact list and the chat dialogs. It doesn't appear in the logs stored on your gmail account, though. You can select a custom theme to display in the chat dialog. There are five default themes. I think you can download other themes, but I'm not sure where. They are coded with CSS, so you can also create your own. However, I'm unable to find the themes folder on my computer, so I don't know what the code looks like Another useful feature is the new notification dialog that pops up when a friends signs on. And on the contact list, there's a new scroll bar that appears when you have many friends on the list. You can turn it off by clicking the new View button, then select Show One Page. There's a mail icon next to the View button that shows you how many new mails you have in your gmail box. Remember that this is a testing release, so it may have some bugs. EDIT: Fixed the link
Sat Apr 1, 2006
Reply New Discussion
Posted in Computers & Tech / Programming / Programming General / SQL (Structured Query Language..
Author: minnieadkins Total-Replies: 6 The bundle should have installed mysql with the software. One way of checking is to create a new file, and see if everythings set up in your php.ini settings. Simply put this file in the /www/ directory that your localhost (127.0.0.1) points to. CODE<?php phpinfo(); ?> Scroll down through it and see if mysql has a section in there. If so, php is configured. You can also look through your services to see if mysql has been installed on your system. Start->Run->services.msc Scroll through that and see if mysql is running (or look in your processes by using alt+ctrl+delete). After that you're probably in business. It would have asked you for a user name and password for mysql database, just use those when you access it. If you're not very strong in SQL, I would recommend finding a package that has phpMyAdmin with it so you can have a GUI and very friendly environment to work with. Here's a list of other packaged web servers. QUOTEFoxServ http://www.foxserv.net/portal.php -FoxServ is an Apache / mySQL / PHP installer package for Windows. FoxServ features the latest version of all included pacakges, user defined configuration during installation, PHP as a module, PEAR, and the Zend Optimizer Fire Pages http://www.firepages.com.au -Large number of installer packages, always with the latest and greatest version of PHP, Apache, MySQL, phpMyAdmin etc. Usually has news/installing programs for Beta versions as well. PHP Triad For Windows http://sourceforge.net/projects/phptriad -An installer of Apache, MySQL and PHP for Windows AppServ http://academic.cmri.ac.th/appserv/ -An automatic installation of PHP, MySQL, Apache, phpMyAdmin, PHP-Nuke. Taken from http://www.flash-db.com/Board/index.php?bo...y;threadid=2926 If you're wanting to learn sql then I suggest http://www.htmlgoodies.com/beyond/php/ and scroll down to where you see the introductory to databases. There's better resources, but you should be able to understand these tutorials/primers well enough to get the jist of everything. Once you get the hang of it you can try more advanced guides.
Sat Apr 1, 2006
Reply New Discussion
Posted in Computers & Tech / Databases
Author: yordan Total-Replies: 8 QUOTE (khalilov)K thanks i think i got the idea guys =).I have another question, how do you view a the data in the table, i know i can do that in a while loop in a php script but i don't want to do that every time, i searched the data base in localhost and phpmyadmin and i just can't find it O.o, its probably something i missed. I want to view it directly from their. Link: view Post: 126207 If you cannot find the database in phpmyadmin, this means that the database is not where you think it is. If you are able to write the connect string for php, you should be able to provide the same infos to phpmyadmin and display the list of the tables and display a table content.
Sat Jul 26, 2008
Reply New Discussion
Posted in Computers & Tech / Programming / Scripting / PHP
Author: Jeigh Total-Replies: 4 Hm, this is just ideas as I forget how php handles this stuff from server side, but I would imagine you could do something along the lines of sessions or cookies, say for example you have cookies for all logged in users that expire in 5 minutes but are refreshed every time they switch to a new page. This means that you'll have a cookie per user with basic information in it if they are active, and once they are inactive for 5 minutes they are taken from the list. Similarly a script could be used to add active users to a table a log there last page access, similarly scanning this list every so often for users that have 'timed out', meaning surpassed an inactive time limit chosen by you. I'm sure someone will come along with a more precise idea but these just popped into my head so I thought I'd share in case they help. They might not even be viable given the technologies in use.
Mon Sep 8, 2008
Reply New Discussion
Posted in Computers & Tech / Programming / Scripting / PHP
Author: sid.calcutta Total-Replies: 6 Here is a PHP Script which may help you. First of all, Let us create a table named tbl_test in a MySQL Database. The table contains three fields: 1. field_id 2. category 3 description Now here is the CREATE TABLE CODE for you. First of all, we'll drop Table, if it exists. CODEDROP table if exists `tbl_test`;Now we'll create the table tbl_test. CODECREATE TABLE `tbl_test` (`field_id` int(11) NOT NULL auto_increment, `category` char(20) default 'General', `description` char(100) default 'Not Specified', PRIMARY KEY (`field_id`) ) Now insert some test data in the table. Now is the time to play with the following PHP Script. The script starts at this point. CODE<?phpglobal $db_name,$db_host,$db_user_name,$db_pass; $db_name='test'; $db_host='localhost'; $db_username='test'; $db_pass='test'; Connecting to Database: CODE$new_file="myfile.csv";Consider giving a dynamic file name rather than a static one. This is the file that will store the fetched data. Create a file using fwrite(): CODE$fp=fopen($new_file,"w");if(!$fp) die("Error creating file"); It will be used later on to store the fetched records. CODE$link_id=mysql_connect($db_host, $db_username,$db_pass);if(!$link_id) die("connection failed"); Hopefully you can add a more appropriate error handler. Setting the current database: CODE$current_db=mysql_select_db($db_name,$link_id);if(!$current_db) die("error connecting host"); Creating a HTML table CODEecho " <table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\">";echo "<tr>"; The table will hold a form which will display the options ( i.e. category as in my example). Your users will choose a value from the options given. CODEecho "<td><form name=\"form1\" enctype=\"multipart/form-data\" method=\"post\" action=\"$PHP_SELF\">"; echo "<p>Please select a category </p>"; Now selecting the records ( i.e. category) one after another from the category table and displaying it in a list box CODEecho "<select name=\"category\" size=\"1\">";$search_string="SELECT DISTINCT category FROM tbl_test"; $result_of_search_string=mysql_query($search_string); while($data_search_string=mysql_fetch_row($result_of_search_string)) { echo "<option> $data_search_string[0] </option>"; } echo "</select>"; A simple one line instruction for the user: CODEecho "Click here to start :";CODEecho "<input type=\"submit\" name=\"Submit\" value=\"search\">";echo "</form>"; echo"</tr>"; The FORM ends at this point Note the action attribute of the FORM tag set to $PHP_SELF So when the user clicks on the submit botton the same page reloads with a value saved in the $category variable. Now we'll show the record based on the options chosen by the user. CODEecho "<tr><td><h2> Your records </h2></td></tr>";Let us set few global variables. CODEglobal $records_per_page, $cur_page, $PHP_SELF, $search_category, $category;Checking if $category variable stotes any value or not, if it does not, we set it to default : General/ Or any value you prefer, but it must be the one of the values you have entered in the category field of your table. CODEif(empty($_POST["category"]))$search_category="General"; else $search_category=$_POST["category"]; Checking is done. Now we'll show the user, what value he wanted to see. CODEecho "You are searching for $search_category";Now counting the number of records found in the database for that category: CODE$count_query="select count(*) from tbl_test where category = '$search_category' ";CODE$result = mysql_query($count_query);if(!$result) die("error"); $query_data = mysql_fetch_row($result); $total_record = $query_data[0]; if(!$total_record) die('No Record Found!'); Now setting the page number. CODE$page_num = $cur_page + 1;Now setting the maximum number of records to be displayed in one page: CODE$records_per_page=10;You can use another form to allow the user to select the records per page as we have done for selecting the category from the table. Now we are calculating the total number of pages. CODE$total_num_page = $last_page_num= ceil($total_record/$records_per_page); CODEecho "<tr><td><CENTER><H3>$total_record found on $search_category - Displaying the page$page_num out of $last_page_num.</H3></CENTER>\n</tr></td>"; Now set the current page number. To start with it will have a value = 0. CODEif(empty($cur_page)){ $cur_page = 0; } Now we set the limit for the records to be displayed per page. CODE$limit_str = "LIMIT ". $cur_page * $records_per_page .", $records_per_page"; $new_query="SELECT description FROM tbl_test where category LIKE '$search_category'"; $query=$new_query ." ". $limit_str; Again a database query to fetch the records as per the option selected by the user. And display the records in a table. CODE$result = mysql_query($query);if(!$result) die("No record found"); while($data=mysql_fetch_row($result)) { echo " <tr><td> $data[0] </td></tr> "; // this will show records fwrite($fp,$data[0]); // this will write to the file fwrite($fp,","); // This is the ", " seperater } We have finished fetching records and at the same time stroring it in a file. Now we are creating links for the user to nevigate to continuation pages. CODEif($page_num > 1){ $prev_page = $cur_page - 1; echo "<A HREF=\"$PHP_SELF?&cur_page=0\">[Top]</A>"; echo "<A HREF=\"$PHP_SELF?&cur_page=$prev_page\">[Prev]</A> "; } if($page_num < $total_num_page) { $next_page = $cur_page + 1; $last_page = $total_num_page - 1; echo "<A HREF=\"$PHP_SELF?&cur_page=$next_page\">[Next]</A> "; echo "<A HREF=\"$PHP_SELF?&cur_page=$last_page\">[Bottom]</A>"; } Now we'll allow the user to download the file. CODEecho "<tr><td><a href =\"$new_file\">Click here to download</a>";Our task is over. Now some closing HTML tags again. CODEecho" </Td>";echo " </tr>"; echo "</table>"; ?> So far as the present script is concerned, I got a great help from a book entitle BEGINNING PHP4 published by Wrox Press Ltd. Infact page transition part has actually been scripted in that great book. It works nicely. Regards, Sid
Mon Mar 13, 2006
Reply New Discussion
|