doakra Posted December 8, 2006 Share Posted December 8, 2006 I want to have an area on my site that displays a subject with updates about that subject listed below it. Something like this:Subject 1 Date 1 - Update 1 Date 2 - Update 2 Date 3 - Update 3Subject 2 Date 1 - Update 1 Date 2 - Update 2and so on.I want to be able to add and remove subjects and updates at will. Is this possible with php and mysql? If so, any ideas for a tutorial or example code to get me going? I have looked at threaded discussion tutorials, but no luck. Quote Link to comment Share on other sites More sharing options...
craygo Posted December 8, 2006 Share Posted December 8, 2006 It is entirely possible to do what you want. You could do it with 2-3 tables. Here is something quick with a few fields for each.Users table (optional)id, username, password, name, blah, blah, blahSubject tablesub_id, user_id, subject, description, sub_dateSubUpdates tables_up_id, sub_id, user_id, update, up_dateNow you can link everything together through queries and group them the way you like[code]<?php// mysql connection here$sql = "SELECT * FROM SubjectLEFT JOIN SubUpdates ON Subject.sub_id = SubUpdates.sub_idJOIN Users ON Subject.user_id = Users.user_id ORDER BY sub_date, up_date DESC";?>[/code]this is a basic statement and has not been tested but will give you a startRay Quote Link to comment Share on other sites More sharing options...
doakra Posted December 9, 2006 Author Share Posted December 9, 2006 Thanks for the reply. I'm trying to learn php, but i'm not very good with it yet. I must admit, you lost me pretty quickly there. If possible, could you give me a little more information? Thanks.Donnie Quote Link to comment Share on other sites More sharing options...
craygo Posted December 10, 2006 Share Posted December 10, 2006 Well I guess the first thing would be to ask, How versed are you? alsoDo you have a server with mysql and php on it??If yes, do you know how to create databases and tables?Do you know any html???I would be glad to help you out just let me know. You can catch me on AIM Mon - Fri 8:30-4:30 EST my screen name is craygo69LaterRay Quote Link to comment Share on other sites More sharing options...
doakra Posted December 11, 2006 Author Share Posted December 11, 2006 Thanks, Ray. I appreciate the fact your willing to work with me.I am not very well versed. I am self taught. I know html pretty well. I know some php and some mysql. I have server space with php and mysql and I have them both running locally for testing purposes. I can, and have created my database and tables. I did research and I think I understand the join command now . . . at least partially.Here is what I have so far:2 Tables in 1 database - no user ids necessary.subject table: 4 fields - subject_id, subject_date, subject, description (description will not be used most of the time, but I want to display the field if there is anything in it.)updates table: 4 fields - up_id, subject_id, update, up_dateIn the php file:I connect to and select the database:[code]<?php$db_host = 'localhost';$db_user = '';$db_pwd = '';$db_name = 'threads';mysql_connect($db_host, $db_user, $db_pwd) or die(mysql_error());mysql_select_db($db_name);?>[/code]I then have some html . . . Then . . .[code]<?php$sql = "SELECT * FROM subject LEFT JOIN updates ON subject.subject_id = updates.subject_id ORDER BY subject_id, up_date DESC";$result = mysql_query($sql);while ($row = mysql_fetch_array($result)) {$subject_id = $row['subject_id'];$subject_date = $row['subject_date'];$subject = $row['subject'];$description = stripslashes($row['description']);$up_id = $row['up_id'];$update = stripslashes($row['update']);$up_date = $row['up_date'];$display_block .= "<p><strong>$subject</strong><br>$up_date<br>$update</p>";}echo "$display_block";?>[/code]I am getting the following error:Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\path\file.php on line 64I might try to install AIM after lunch and chat with you a little.Thanks again! Quote Link to comment Share on other sites More sharing options...
craygo Posted December 11, 2006 Share Posted December 11, 2006 While developing, at the least, add some debugging to help you out.[code]$result = mysql_query($sql) or die (mysql_error());[/code]this will let you know what error you have. Usually the mysql_fetch_array comes from an error in the sql statement returning nothing which means it is wrong syntax.Ray Quote Link to comment Share on other sites More sharing options...
doakra Posted December 11, 2006 Author Share Posted December 11, 2006 Well, the debug helped . . . .I think. I'm just not sure what it told me.This is the error I got:Column: 'subject_id' in order clause is ambiguous Quote Link to comment Share on other sites More sharing options...
craygo Posted December 11, 2006 Share Posted December 11, 2006 Since subject_id is in both tables it does not know which one to use so add the table before it[code] ORDER BY subjects.subject_id, up_date[/code]Ray Quote Link to comment Share on other sites More sharing options...
doakra Posted December 11, 2006 Author Share Posted December 11, 2006 OK . . .I figured that out. The query works. I changed ORDER BY subject_id to ORDER BY subject.Now I have to work on the format of the display.What I get now is:subject 1 Date Update 1subject 1 Date Update 2subject 2 Date Update 1and so on.I want:Subject 1 - Description (If Applicable) Date - Update 1 Date - Update 2Subject 2 - Description (If Applicable) Date - Update 1 Date - Update 2Also, I am using timestamp for the date in the database. Is there a way to display this as MM/DD/YYYY or something similar?Thanks. Quote Link to comment Share on other sites More sharing options...
craygo Posted December 11, 2006 Share Posted December 11, 2006 Couple things you need to do for this. You have to group the results back and look for a change in the subject and you can easily change the date format using php in your loop.[code]<?php// Set group value$lastsub = "";while ($row = mysql_fetch_array($result)) { $subject_id = $row['subject_id']; $subject_date = date("m/d/Y", strtotime($row['subject_date'])); $subject = $row['subject']; $description = stripslashes($row['description']); $up_id = $row['up_id']; $update = stripslashes($row['update']); $up_date = date("m/d/Y", strtotime($row['up_date'])); // Check if group has changed if($subject_id != $lastsub){ // Print group header echo "<p><strong>$subject</strong> description: $description Started: $subject_date</p><br>"; } // Print update rows echo "<p>$update $up_date</p><br>"; $lastsub = $subject_id;}?>[/code]Try that out. not tested but will get you going if a problemRay Quote Link to comment Share on other sites More sharing options...
doakra Posted December 12, 2006 Author Share Posted December 12, 2006 I'm having trouble with this line:$up_date = date("m/d/Y", strtotime($row['up_date'])); (This is line 74)It gives this error:Warning: unexpected error in date() in c:\path\file.php on line 74If I remove the date formating and just use:$up_date = $row['up_date'];It works well. 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.