Nimbuz Posted January 16, 2009 Share Posted January 16, 2009 Hello, I'm an absolute beginner in PHP (while expert in html/css) who just started learning basics through video tutorials and ebooks. Now that I know the php syntax, I'm looking to develop (or use an existing script if available) a very very simple event display script. I want to store events with their "Name" "Location" and "Date" in either mysql or a txt file (preferable) and display its contents on a page, but I also want to do is cross-through passed events (could be done by giving them a css class eg. "event-passed"). I have no clue how to start. I'd greatly appreciate if you someone write the basic code for me that I can then modify for my use. Many thanks. Quote Link to comment https://forums.phpfreaks.com/topic/141034-event-display/ Share on other sites More sharing options...
RussellReal Posted January 16, 2009 Share Posted January 16, 2009 probably will be your only constructive post here.. This isn't for script requests, although you may see us write scripts for people, we write scripts to correct already written code or to help correct a problem with already written code. I'm not trying to come off like a prick, but most of us spend alot of time on this forum and like helping people, and thats what we intend to do, HELP, but give out. I know this is a help request though but many people will most likely disregard this after you have "someone write the basic code for me" in the thread.. unless maybe I'm speaking for myself ofcourse, however, unlikely. I can however point you in the right direction, then when you have some established code we can try to help you with errors you encounter. I recommend you use mysql (#1), you can get the date using date, although for databases you should use the timestamp, that way you can code with it (all languages are more mathematical than anything else). to check for past events, you would use an if statement and compare time with the timestamp in the database, "if timestamp less than time THEN its older OTHERWISE (else) its new" this is actually pretty simple.. hope I help you bro and sorry for sounding snobbish Quote Link to comment https://forums.phpfreaks.com/topic/141034-event-display/#findComment-738184 Share on other sites More sharing options...
chronister Posted January 16, 2009 Share Posted January 16, 2009 I second RussellReal's motion about the whole "write it for me", and for basically the same reasons. You will learn MUCH better if you try it yourself (making mistakes is good because you learn from them.) and then ask for help when you run into trouble. I also agree that you should use MySQL for this as it is much easier to analyze data (i.e. determine if date is in the past) when you pull it from the database. The first steps you should take is go to phpMyAdmin and create your database structure. I will even give ya something you can copy and paste to do it. CREATE TABLE IF NOT EXISTS `events` ( `eventId` int(11) NOT NULL auto_increment, `eventName` varchar(50) NOT NULL, `eventLocation` varchar(255) NOT NULL, `eventDate` datetime NOT NULL, PRIMARY KEY (`eventId`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ; -- -- Dumping data for table `events` -- INSERT INTO `events` (`eventId`, `eventName`, `eventLocation`, `eventDate`) VALUES (1, 'Event1', 'Bob''s Bar', '2009-01-20 22:30:00'), (2, 'Event2', 'Jim''s Saloon', '2009-01-23 01:04:58'), (3, 'Event3', 'Moose Lodge', '2009-01-12 20:00:00'), (4, 'Event4', 'Holiday Inn', '2009-01-17 15:45:00'); As you can see it is 4 fields, the unique event ID, then the name, location and date. I have also created 4 test events to play with. 2 are in the past, 2 are in the future. Are you looking at / learning MySQL with PHP? I suggest you do so. It will make your applications much better and easier to deal with, and picking up MySQL I feel was easier than picking up PHP when I started. Once you have the database created, create a page that will display the events. That will be the first step. Then you will need a page to add/edit events. But, start with trying to create a page to display the events and go from there. There are plenty of people on here that will help ya as you go along. What you are wanting to do is really pretty easy once you get the hang of it, so let's see what you got. nate Quote Link to comment https://forums.phpfreaks.com/topic/141034-event-display/#findComment-738194 Share on other sites More sharing options...
RussellReal Posted January 16, 2009 Share Posted January 16, 2009 chronister you came on here acting so nice you make me feel bad lol! but yeah, mysql really does help handling data ALOT, and its so much faster to let mysql do all the cataloging and greabbing and parsing, all you do is REQUEST the data, and then handle it.. its like effortless, rather than with a text file create a system which allows you to edit, remove, add, grab, parse, from a plain text file, and still hold its place within the file.. save the effort and just get hooked on mySQL practically all my applications I code, use mysql now, unless its a parser or sumfin simplistic like that. Quote Link to comment https://forums.phpfreaks.com/topic/141034-event-display/#findComment-738202 Share on other sites More sharing options...
chronister Posted January 16, 2009 Share Posted January 16, 2009 chronister you came on here acting so nice you make me feel bad lol! RussellReal, your just a big jerk just kidding. I have never used a text file for "database" applications... it is MUCH more challenging to do. MySQL is pretty easy to pick up for the simple things... and even some of the more complicated queries are not too bad once you figure out how to structure them. Nate Quote Link to comment https://forums.phpfreaks.com/topic/141034-event-display/#findComment-738206 Share on other sites More sharing options...
Nimbuz Posted January 16, 2009 Author Share Posted January 16, 2009 Thanks for the quick replies. I haven't used the date function yet, so I guess I have more PHP to explore before I can even ask for help. :-) Thanks anyway. Quote Link to comment https://forums.phpfreaks.com/topic/141034-event-display/#findComment-738251 Share on other sites More sharing options...
chronister Posted January 16, 2009 Share Posted January 16, 2009 The date function is not very difficult to use or understand. I haven't used the date function yet, so I guess I have more PHP to explore before I can even ask for help. :-) Thanks anyway. Geez, you make it sound like we completely denied you any help. We are more than happy to help you and to even help you learn, but we want you to learn and try yourself too. Do you know any mysql? Have you looked at it at all yet? Nate Quote Link to comment https://forums.phpfreaks.com/topic/141034-event-display/#findComment-738326 Share on other sites More sharing options...
Nimbuz Posted January 16, 2009 Author Share Posted January 16, 2009 ah no, I'm sorry if I sounded harsh. I just meant that although I began READ and UNDERSTAND the syntax now, but I can't actually write something useful yet. Yes, I've tried creating a DB, Tables, Fields. I can also understand how the fileds are populated. Quote Link to comment https://forums.phpfreaks.com/topic/141034-event-display/#findComment-738332 Share on other sites More sharing options...
chronister Posted January 16, 2009 Share Posted January 16, 2009 Ok, then take the sql I gave you and go to PHPMyAdmin and go to your events DB and hit the sql tab up top and paste the sql in there. You should then get your db table created and populated. Once you have that created, then create a page called viewEvents.php or something to that effect. On that page, make a connection to the DB server. http://us3.php.net/mysql_connect Then select the db. http://us3.php.net/manual/en/function.mysql-select-db.php Then you will run the query to select the events. http://us3.php.net/manual/en/function.mysql-query.php Then you will loop through them so you can display them. See if you can get these things nailed down and we will go from there. Nate Quote Link to comment https://forums.phpfreaks.com/topic/141034-event-display/#findComment-738385 Share on other sites More sharing options...
Nimbuz Posted January 16, 2009 Author Share Posted January 16, 2009 Thanks heaps for the pointers, Nate. So, my viewEvents.php is this: <?php // DB Connection $connection = mysql_connect("localhost","root","mypass"); if (!$connection) { die("Database connection failed: " . mysql_error()); } // Select DB $db_select = mysql_select_db("hightrek", $connection); if (!$db_select) { die("Database connection failed: " . mysql_error()); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>View Events</title> </head> <body> <?php // Perform Query $result = mysql_query("SELECT * FROM hightrek"), $connection); if (!$result) { die("Database query failed: " . mysql_error()); } // Use Returned Data while ($row = mysql_fetch_array($result)) { echo $row["eventId"]." ".$row["eventName"]." ".$row["eventLocation"]." ".$row["eventDate"]."<br />"; } ?> </body> </html> <?php // Close Connection mysql_close($connection); ?> but it gives a syntax error: Parse error: syntax error, unexpected ',' in /Volumes/disk2/Work/www/php/viewEvents.php on line 23 I know syntax errors are silly errors, but I so dumb that I couldn't find whats wrong although I went through the code twice. Quote Link to comment https://forums.phpfreaks.com/topic/141034-event-display/#findComment-738408 Share on other sites More sharing options...
premiso Posted January 16, 2009 Share Posted January 16, 2009 // Perform Query $result = mysql_query("SELECT * FROM hightrek", $connection); Will fix that error. Quote Link to comment https://forums.phpfreaks.com/topic/141034-event-display/#findComment-738411 Share on other sites More sharing options...
Nimbuz Posted January 16, 2009 Author Share Posted January 16, 2009 Ah great. So with: <?php // Perform Query $result = mysql_query("SELECT * FROM events", $connection); if (!$result) { die("Database query failed: " . mysql_error()); } // Use Returned Data while ($row = mysql_fetch_array($result)) { echo $row["eventId"]." ".$row["eventName"]." ".$row["eventLocation"]." ".$row["eventDate"]."<br />"; } ?> It outputs: 1 Event1 Bob's Bar 2009-01-20 22:30:00 2 Event2 Jim's Saloon 2009-01-23 01:04:58 3 Event3 Moose Lodge 2009-01-12 20:00:00 4 Event4 Holiday Inn 2009-01-17 15:45:00 ..so it worked. All thanks to you! :-D Quote Link to comment https://forums.phpfreaks.com/topic/141034-event-display/#findComment-738416 Share on other sites More sharing options...
Nimbuz Posted January 16, 2009 Author Share Posted January 16, 2009 Now how do I create a page for adding/editing events? Thanks heaps. Quote Link to comment https://forums.phpfreaks.com/topic/141034-event-display/#findComment-738421 Share on other sites More sharing options...
chronister Posted January 16, 2009 Share Posted January 16, 2009 Create a form to input the data. It will be a simple 3 field form. The ID will be auto-generated. Once you have the form (set it's method as post... method="post"), you will use the superglobal $_POST['fieldname'] to get the results. Create your insert page like this... <?php if(isset($_POST['nameOfSubmitButton')) { // process form // initiate another connection // set field data to easy to use vars and run them through mysql_real_escape_string($var) to sanitize data and protect against injection attacks // run query to insert the data } else { ?> Create Form Here <?php } ?> Get that one working and then we can help you use this form again to be the edit form as well. Nate Quote Link to comment https://forums.phpfreaks.com/topic/141034-event-display/#findComment-738447 Share on other sites More sharing options...
Nimbuz Posted January 16, 2009 Author Share Posted January 16, 2009 Yes, I've been working on that. So, I created a form: <form action="insert.php" method="POST"> <fieldset> <legend>Create a Row</legend> <label>Event Name:</label> <input type="text" name="event_name" /> <br /> <label>Event Location:</label> <input type="text" name="event_location" /> <br /> <label>Event Date:</label> <input type="text" name="event_date" /> <br /> <input type="submit" name="create_event" /> </fieldset> </form> and insert.php: <?php // DB Connection $connection = mysql_connect("localhost","root","mypass"); if (!$connection) { die("Database connection failed: " . mysql_error()); } // Select DB $db_select = mysql_select_db("hightrek", $connection); if (!$db_select) { die("Database connection failed: " . mysql_error()); } // CODE BLOCK if(isset($_POST['create_event'])) { // INSERT DATA $insert_sql="INSERT INTO events (eventName, eventLocation, eventDate) VALUES ($_POST[event_name]','$_POST[event_location]','$_POST[event_date]')"; if (!mysql_query($insert_sql, $connection)) { die("Database connection failed: " . mysql_error()); } echo "1 record added"; } else { echo "Sorry, something wrong!"; } ?> <a href="viewEvents.php">Return to previous page</a> <?php // Close Connection mysql_close($connection); ?> but it throws an error: Database connection failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Event 2','My Place 2','anytime 2')' at line 3 Quote Link to comment https://forums.phpfreaks.com/topic/141034-event-display/#findComment-738463 Share on other sites More sharing options...
Nimbuz Posted January 16, 2009 Author Share Posted January 16, 2009 I modified the codeblock to: // CODE BLOCK if(isset($_POST['create_event'])) { // INSERT DATA $insert_sql = "INSERT INTO events (eventId, eventName, eventLocation, eventDate) VALUES ($_POST[event_id]', $_POST[event_name]','$_POST[event_location]','$_POST[event_date]')"; if (!mysql_query($insert_sql, $connection)) { die("Row insert failed: " . mysql_error()); } echo "1 record added"; } else { echo "Sorry, something wrong!"; } and the error is now: Notice: Undefined index: event_id in /Volumes/disk2/Work/www/php/insert.php on line 18 Row insert failed: Column count doesn't match value count at row 1 Quote Link to comment https://forums.phpfreaks.com/topic/141034-event-display/#findComment-738479 Share on other sites More sharing options...
Nimbuz Posted January 16, 2009 Author Share Posted January 16, 2009 oh great, I can now add rows with this: // CODE BLOCK if(isset($_POST['create_event'])) { // INSERT DATA $insert_sql = "INSERT INTO events (eventId, eventName, eventLocation, eventDate) VALUES ('$_POST', '$_POST[event_name]','$_POST[event_location]','$_POST')"; if (!mysql_query($insert_sql, $connection)) { die("Row insert failed: " . mysql_error()); } echo "1 record added"; } else { echo "Sorry, something wrong!"; } but a couple questions: 1. Is this statement correct: ('$_POST', '$_POST[event_name]','$_POST[event_location]','$_POST')"; 2. Date is inserted as 0000-00-00 00:00:00, how do I set it to current date and time? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/141034-event-display/#findComment-738484 Share on other sites More sharing options...
chronister Posted January 16, 2009 Share Posted January 16, 2009 <?php // CODE BLOCK if(isset($_POST['create_event'])) { // INSERT DATA // sanitize the data to protect against injection attacks $eventName = mysql_real_escape_string($_POST['event_name']; $eventLocation = mysql_real_escape_string($_POST['event_location']); // we take what was entered in the date field, and run it through strtotime() (string to time) which gives a unix timestamp // then we run it through the date function and put it in the format we need for mysql. $eventDate = date('Y-m-d h:i:s', strtotime($_POST['event_date']) ); $insert_sql = "INSERT INTO events (eventName, eventLocation, eventDate) VALUES ('$eventName', $eventLocation', '$eventDate')"; $result = mysql_query($insert_sql, $connection) or die("Row insert failed: " . mysql_error()); if(mysql_affected_rows() > 0) { echo "1 record added"; } } else { // the form has not been submitted yet.. no need to do anything here } ?> I made a few minor changes... lemme know if you have any questions about it. Quote Link to comment https://forums.phpfreaks.com/topic/141034-event-display/#findComment-738540 Share on other sites More sharing options...
Nimbuz Posted January 16, 2009 Author Share Posted January 16, 2009 There was a missing ")" but even after fixing that, I get this error: Row insert failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Place 2', '1970-01-01 05:30:00')' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/141034-event-display/#findComment-738551 Share on other sites More sharing options...
chronister Posted January 16, 2009 Share Posted January 16, 2009 Oops... forgot a ' $insert_sql = "INSERT INTO events (eventName, eventLocation, eventDate) VALUES ('$eventName', '$eventLocation', '$eventDate')"; I forgot the ' right before $eventLocation... Try that and see if it works. Nate Quote Link to comment https://forums.phpfreaks.com/topic/141034-event-display/#findComment-738572 Share on other sites More sharing options...
RussellReal Posted January 16, 2009 Share Posted January 16, 2009 just as a side note.. // Select DB $db_select = mysql_select_db("hightrek", $connection); you don't need $db_select = you could just do // Select DB mysql_select_db("hightrek", $connection); Quote Link to comment https://forums.phpfreaks.com/topic/141034-event-display/#findComment-738659 Share on other sites More sharing options...
Nimbuz Posted January 17, 2009 Author Share Posted January 17, 2009 Yes, script runs fine now, but the date is always "1970-01-01 05:30:00", how could that be fixed? Quote Link to comment https://forums.phpfreaks.com/topic/141034-event-display/#findComment-738919 Share on other sites More sharing options...
chronister Posted January 17, 2009 Share Posted January 17, 2009 Basically the strtotime() function cannot parse the date in the format you have it in. How are you entering the dates for that field? You might split the date and time fields up and use a javascript calendar popup script to populate the date, or you could specify it be entered in a certain way and check in the script that it is being entered that way. Check out the following examples.. and you might want to change the following in date('Y-m-d h:i:s'.......) change h to H so you get the 24 hour format (military time) <?php echo date('Y-m-d H:i:s', strtotime('may 30 2009 1:30:00 pm') ); // prints as 2009-05-30 13:30:00 - Good echo date('Y-m-d H:i:s', strtotime('05/30/09 1:35 am') ); // prints as 2009-05-30 01:35:00 - Good echo date('Y-m-d H:i:s', strtotime('05-30-2009 13:51:00') ); // prints as 1969-12-31 18:00:00 - Not Good ?> Nate Quote Link to comment https://forums.phpfreaks.com/topic/141034-event-display/#findComment-738929 Share on other sites More sharing options...
Nimbuz Posted January 17, 2009 Author Share Posted January 17, 2009 Or can I leave the date field blank (like ID) and let mysql add current date and time automatically when row is added? Is that possible? How would I do that? EDIT: Ah no, its not a normal record so the date is certainly required. But I'd sure like to know how to leave date/time blank and let mysql add it automatically. Should be really useful in the future. Quote Link to comment https://forums.phpfreaks.com/topic/141034-event-display/#findComment-739002 Share on other sites More sharing options...
Nimbuz Posted January 17, 2009 Author Share Posted January 17, 2009 Great. So entering date in this format "05/30/09 1:35 am" worked. Now I need a page for modifying/removing records? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/141034-event-display/#findComment-739003 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.