Kairu Posted December 30, 2006 Share Posted December 30, 2006 Alright.... Now that I have, for the most part, finished my dynamic image project. I have moved on to shoutboxes. I hope everyone knows what they are.... If you don't, I can probably find an example of one.What I want to do is have it all in one file. None of the usual multiple file stuff. (I want to be able to call it through htaccess under a different name.)What I need to know first is how to make the php file display different things given different circumstances. Say, initially I display what appears to be pure HTML, a user input field where a username and a message is input. Then submit is hit and the page refreshes, and shows a confirmation, hiding the input field and showing a previously hidden confirmation field or something. When their is an error it displays the error on the page itself without moving on.Is this even possible with PHP? And I am aware that it would be much easier with multiple files, but that is currently not an option. (Plus it makes it a lot easier to transport and sync) Link to comment https://forums.phpfreaks.com/topic/32242-solved-shoutbox/ Share on other sites More sharing options...
trq Posted December 30, 2006 Share Posted December 30, 2006 Its easy enought to do with php. You would simply setup a switch. eg;[code]<?php switch ($_GET['shout']) { case 'edit': edit(); break; case 'del': del(); break; default: view(); }?>[/code]Then just put all your code for editing within an edit() function, all for viewing with view() etc etc....Then if the page is called with a url such as http://yoursite.com/index.php?shout=edit the shoutbox will switch into edit mode. Link to comment https://forums.phpfreaks.com/topic/32242-solved-shoutbox/#findComment-149637 Share on other sites More sharing options...
Kairu Posted December 30, 2006 Author Share Posted December 30, 2006 Yes! Thats exactly what I was looking for! Link to comment https://forums.phpfreaks.com/topic/32242-solved-shoutbox/#findComment-149642 Share on other sites More sharing options...
Kairu Posted December 30, 2006 Author Share Posted December 30, 2006 Alright.... Got the switch working no problem, but a form is required to input the data.... I was thinking something like this, but I'm not sure at all how to get it to work.[code]<?php switch ($_GET['id']) { case 'edit': edit(); break; case 'view': view(); break; default: error(); }function edit(){echo'<html><body><form method="POST"> <p>Username: <input type="text" name="username" size="20"></p> <p>Message: <input type="text" name="Message" size="20"></p> <p><input type="submit" value="Submit" name="Submit"><input type="reset" value="Reset" name="Reset"></p></form></body></html>';}function view(){echo $_POST['Username'] . ' said: ';echo $_POST['Message'];}?>[/code] Link to comment https://forums.phpfreaks.com/topic/32242-solved-shoutbox/#findComment-149652 Share on other sites More sharing options...
trq Posted December 30, 2006 Share Posted December 30, 2006 [quote]I'm not sure at all how to get it to work.[/quote]What do you meen? Whats not working? Link to comment https://forums.phpfreaks.com/topic/32242-solved-shoutbox/#findComment-149654 Share on other sites More sharing options...
Kairu Posted December 30, 2006 Author Share Posted December 30, 2006 Oh! Der. Forgot that part didnt I.I want to be able to submit the form, and have it refresh the page (well, I want it to go to the view function, so I want it to go to New.php?id=view ) with the data in the form available. I'm not quite sure how to do this, having never used a form in HTML or anything before. (I know, odd. But I've never had a use for one in the years I have been using it. ) Link to comment https://forums.phpfreaks.com/topic/32242-solved-shoutbox/#findComment-149655 Share on other sites More sharing options...
trq Posted December 30, 2006 Share Posted December 30, 2006 You need to make your form point to the right location then.[code]<form action="New.php?id=view" method="POST">[/code] Link to comment https://forums.phpfreaks.com/topic/32242-solved-shoutbox/#findComment-149656 Share on other sites More sharing options...
Kairu Posted December 30, 2006 Author Share Posted December 30, 2006 Ugh.... well that makes me feel like a newb to HTML.... I should have known that. Thank you, though I'll probably be back in a bit for more help...... Solved again... for now. Link to comment https://forums.phpfreaks.com/topic/32242-solved-shoutbox/#findComment-149659 Share on other sites More sharing options...
Kairu Posted December 30, 2006 Author Share Posted December 30, 2006 Do you see any reason in that code, (Assuming I have fixed the form action) That would prevent $_POST['username'] from displaying a value? Link to comment https://forums.phpfreaks.com/topic/32242-solved-shoutbox/#findComment-149670 Share on other sites More sharing options...
trq Posted December 30, 2006 Share Posted December 30, 2006 PHP is case sesitive. You form uses username, while you are looking to display $_POST['Username']. Link to comment https://forums.phpfreaks.com/topic/32242-solved-shoutbox/#findComment-149674 Share on other sites More sharing options...
Kairu Posted December 30, 2006 Author Share Posted December 30, 2006 I thought of that....Here is the exact code I have. If I change it to $_POST['Message'] it works, but not $_POST['Username']...[code]<?php switch ($_GET['id']) { case 'edit': edit(); break; case 'view': view(); break; default: error(); }function edit(){echo'<html><body><form action="thedarkrealm.no-ip.org/New.php?id=view" method="POST"> <p>Username: <input type="text" name="Username" size="20"></p> <p>Message: <input type="text" name="Message" size="20"></p> <p><input type="submit" value="Submit" name="Submit"><input type="reset" value="Reset" name="Reset"></p></form></body></html>';}function view(){echo $_POST['Username'] . ' said: ';echo $_POST['Message'];}?>[/code] Link to comment https://forums.phpfreaks.com/topic/32242-solved-shoutbox/#findComment-149680 Share on other sites More sharing options...
trq Posted December 30, 2006 Share Posted December 30, 2006 It should. Also note that if your going to place your domain in the action (not recommended) you'll need the http:// otherwsie your going to have all sorts of issues.Just make your action look like....[code]<form action="New.php?id=view" method="POST">[/code] Link to comment https://forums.phpfreaks.com/topic/32242-solved-shoutbox/#findComment-149686 Share on other sites More sharing options...
Kairu Posted December 30, 2006 Author Share Posted December 30, 2006 Ah, firefox was displaying an old version of the code for some reason...... Link to comment https://forums.phpfreaks.com/topic/32242-solved-shoutbox/#findComment-149695 Share on other sites More sharing options...
Kairu Posted December 30, 2006 Author Share Posted December 30, 2006 Le sigh. I need to query the database... but need the highest ID. So.... mysql_query('SELECT * FROM box WHERE id = [Highest ID]); with the query information that gets the highest ID of course. IS this possible or will I need to create another table to store the highest ID? Link to comment https://forums.phpfreaks.com/topic/32242-solved-shoutbox/#findComment-149702 Share on other sites More sharing options...
trq Posted December 30, 2006 Share Posted December 30, 2006 Dont rely on the id. Put a timestamp field in your database and query against that for your latest record. eg;[code]SELECT * FROM shout ORDER BY timestamp DESC LIMIT 1;[/code] Link to comment https://forums.phpfreaks.com/topic/32242-solved-shoutbox/#findComment-149713 Share on other sites More sharing options...
Kairu Posted December 30, 2006 Author Share Posted December 30, 2006 How would I use the timestamp? Like, how would I input the timestamp into the database? And the format so I can add the first one manually? I had never thought of using a timestamp instead of an ID, thanks! Link to comment https://forums.phpfreaks.com/topic/32242-solved-shoutbox/#findComment-149717 Share on other sites More sharing options...
trq Posted December 30, 2006 Share Posted December 30, 2006 Easy enough, just make a field of the type TIMESTAMP, then, when you insert a record use mysql's NOW() function. eg;[code]INSERT INTO shout (data,stamp) VALUES ('this is a post',NOW());[/code] Link to comment https://forums.phpfreaks.com/topic/32242-solved-shoutbox/#findComment-149726 Share on other sites More sharing options...
Kairu Posted December 30, 2006 Author Share Posted December 30, 2006 It just wont work..... I get a blank screen. If I comment out the query, it works fine, but it does not do what I want, and I just cant figure out what I'm doing wrong.[code]<?php switch ($_GET['id']) { case 'edit': edit(); break; case 'view': view(); break; default: error(); }function edit(){echo'<html><body><form action="New.php?id=view" method="POST"> <p>Username: <input type="text" name="Username" size="20"></p> <p>Message: <input type="text" name="Message" size="20"></p> <p><input type="submit" value="Submit" name="Submit"><input type="reset" value="Reset" name="Reset"></p></form></body></html>';}function view(){ mysql_connect('localhost:3306', 'U', 'P');mysql_select_db('gaia_image');mysql_query("INSERT INTO box (Timestamp,Username,Message) VALUES (NOW(), $_POST['Username'], $_POST['Message'])");}?>[/code]Also, I have manually inserted a row into the table using MyPHPAdmin, but I cant get the data from the cells correctly. Why will this not work?[code]<?phpmysql_connect('localhost:3306', 'U', 'P');mysql_select_db('gaia_image');$result = mysql_query('SELECT * FROM box ORDER BY timestamp DESC LIMIT 1');$row = mysql_fetch_array($result);echo $row[0];?>[/code]It just comes up blank. Link to comment https://forums.phpfreaks.com/topic/32242-solved-shoutbox/#findComment-149735 Share on other sites More sharing options...
Kairu Posted December 30, 2006 Author Share Posted December 30, 2006 Alright, scratch all that. Not sure how fixed it, but it is fixed.But I have a question on how to access the data that comes back.The way I have it set up now (Since their is going to be more then one shoutbox using this table) is with ID's to separate the messages into their individual shoutboxes, and timestamps to order them.This is the code I'm trying to use to access the data that is output.[code]mysql_connect('localhost:3306', 'Gaia', 'Kairu');mysql_select_db('gaia_image');$result = mysql_query('SELECT *FROM `box`WHERE `id` = 1ORDER BY `box`.`Timestamp` DESC');$row = mysql_fetch_array($result);echo $row[2];[/code]In this case, it outputs "Test Text". Now my question is, how do I access the second row of data? Or the third?I know there are three rows in the table with the ID of 1.... Link to comment https://forums.phpfreaks.com/topic/32242-solved-shoutbox/#findComment-149854 Share on other sites More sharing options...
Kairu Posted December 30, 2006 Author Share Posted December 30, 2006 Once again.... it seems to have fixed itself. Perhaps firefox is just using old copies of the pages that are cached. Anyway..... I'm going to post my next problem in a new thread, so this one is solved. ^^ Thank you! Link to comment https://forums.phpfreaks.com/topic/32242-solved-shoutbox/#findComment-149888 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.