tommr Posted December 16, 2010 Share Posted December 16, 2010 I use strftime('%b %d, %Y', strtotime($row['start_date'])); in several places and it works fine. When I take this code { echo "<a href=\"/show_submits/show_detail.php?id=" $row['id'] "\">Details</a>\n"; echo $row['venue_state'] " " $row['start_date'] " " $row['show_name'] " " $row['venue_city'] "<br>\n"; } and change it to this code { echo "<a href=\"/show_submits/show_detail.php?id=" $row['id'] "\">Details</a>\n"; echo $row['venue_state'] " " strftime('%b %d, %Y', strtotime($row['start_date'])); " " $row['show_name'] " " $row['venue_city'] "<br>\n"; } I get this error. Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in /home/arts/public_html/shows/index.html on line 194 The only change I made was for the $row['show_name'] Am I making an obvious mistake? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 16, 2010 Share Posted December 16, 2010 Both of those code fragments have parse errors caused by improper string concatenation, and/or quoting 'malfunctions'. Quote Link to comment Share on other sites More sharing options...
tommr Posted December 16, 2010 Author Share Posted December 16, 2010 Can you be a little more specific? When I use the first instance it works fine. I just want to change the format of the date. I am new to this and am trying to learn. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 16, 2010 Share Posted December 16, 2010 Both code snippets should give you errors, since you're not concatenating the string elements. Your original code: <?php { echo "<a href=\"/show_submits/show_detail.php?id=" $row['id'] "\">Details</a>\n"; echo $row['venue_state'] " " $row['start_date'] " " $row['show_name'] " " $row['venue_city'] "<br>\n"; } ?> can be written as <?php { echo "<a href='/show_submits/show_detail.php?id={$row['id']}'>Details</a>\n"; echo $row['venue_state'] . " {$row['start_date']} {$row['show_name']} {$row['venue_city']}<br>\n"; } ?>[code] Ken Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 16, 2010 Share Posted December 16, 2010 For example, the first line echo "<a href=\"/show_submits/show_detail.php?id=" $row['id'] "\">Details</a>\n"; should be written echo "<a href=\"/show_submits/show_detail.php?id=\"{$row['id']}\">Details</a>\n"; You can't simply toss a quote into the middle of a string and insert a variable. The quotes within the same type of quotes have to be escaped, and variables aren't interpolated within single quoted strings. Very briefly: $variable = 'another string'; echo "This is a string with $variable inside"; echo 'This is a string with ' . $variable . ' inside'; Both of the above will return This is a string with another string inside echo 'This is a string with $variable inside'; That will return This is a string with $variable inside Array elements are a bit different. To use them in a double quoted string, they need to be enclosed in curly braces {}, or concatenated. echo "This is an {$array['element']} in a string."; OR echo "This is an " . $array['element'] . "in a string". As for quotes, to echo a double quote from within a double quoted string, or a single quote from within a single uoted string, it must be escaped with a backslash. echo "This \"string\" is valid"; echo 'This "string" is valid'; echo "This "string" is not valid"; echo 'This \'string\' is valid'; echo "This 'string' is valid"; echo 'this 'string' is not valid'; Quote Link to comment Share on other sites More sharing options...
tommr Posted December 16, 2010 Author Share Posted December 16, 2010 Thanks again for the quick reply. That helps make it clear. I am still having trouble with the date but I will endeavor to persevere. Quote Link to comment Share on other sites More sharing options...
tommr Posted December 16, 2010 Author Share Posted December 16, 2010 Is this correct then? strftime('%b %d, %Y', strtotime($row['start_date'])) I am having nothing but trouble with this. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 16, 2010 Share Posted December 16, 2010 What are you trying to do with it, and in what format is the date stored in the database? Quote Link to comment Share on other sites More sharing options...
tommr Posted December 16, 2010 Author Share Posted December 16, 2010 The date is stored as 2010-12-10 I want to display the date as Dec 10, 2010 I used the strftime('%b %d, %Y', strtotime($row['start_date'])) on other parts of the site and it worked with out noticeable trouble. Here is an example... http://www.artsandcraftsnetwork.com/show_submits/6ny.php Now if I go to http://www.artsandcraftsnetwork.com/shows/ and search for ny I get the list but with the 2010-12-10 The database version is too confusing. The whole blasted thing is a Frankenstein of parts and pieces I bought, wrote and had other help with. I tried to find an events script that would do what I wanted but none was available and I do not have the funds to commission one so there you go. Any help would be appreciated. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 16, 2010 Share Posted December 16, 2010 SELECT DATE_FORMAT(`start_date`, '%b %e, %Y') AS s_date Then you can simply echo $row['s_date'] along with the rest of the data from the query, and you'll have the date already formatted. Much simpler than what you're trying to do now. Quote Link to comment Share on other sites More sharing options...
tommr Posted December 16, 2010 Author Share Posted December 16, 2010 No errors but nothing is printing for date. I am using... $sql = "select * from craft_shows DATE_FORMAT(`start_date`, '%b %e, %Y') AS s_date "; echo "<a href='/show_submits/show_detail.php?id={$row['id']}'>Details</a>\n"; echo $row['venue_state'] . " {$row['s_date']} {$row['show_name']} {$row['venue_city']}<br>\n"; Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 16, 2010 Share Posted December 16, 2010 You aren't actually executing the query in that code, and since the s_date is derived FROM the table, it needs to be before the FROM parameter in the query string. $sql = "SELECT *, DATE_FORMAT(`start_date`, '%b %e, %Y') AS s_date FROM craft_shows"; $result = mysql_query($sql); while( $row = mysql_fetch_assoc($result) ) { echo "<a href='/show_submits/show_detail.php?id={$row['id']}'>Details</a>\n"; echo $row['venue_state'] . " {$row['s_date']} {$row['show_name']} {$row['venue_city']}<br>\n"; } Quote Link to comment Share on other sites More sharing options...
tommr Posted December 16, 2010 Author Share Posted December 16, 2010 Thank you thank you. It works but by the looks of the results I need to sort by date! One of the problem of working from a book is that if I have questions there is no one to help. Thanks to your help I now understand the concept of FROM. This is a big help in getting that detail nailed down but I really appreciate your taking the time to show what I am doing wrong. I think I will spend some time browsing the forum for more ideas of how to make the site work better. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 16, 2010 Share Posted December 16, 2010 You can add an ORDER BY clause to the query as well. $sql = "SELECT *, DATE_FORMAT(`start_date`, '%b %e, %Y') AS s_date FROM craft_shows ORDER BY start_date DESC"; MySQL has some very good native date/time manipulation functions, it's just a matter of knowing they exist, and how to use them. Quote Link to comment Share on other sites More sharing options...
tommr Posted December 17, 2010 Author Share Posted December 17, 2010 I can figure this out. I thought it was working but for what ever reason this code calls all the results no matter I select from the drop down menu. If I take the date and code fix out it works on but the date is not displayed as I want. I have tried 6 ways to Sunday and no dice. <?php $find = trim($_GET['find']); $field = $_GET['field']; if($find && $field) { // we have search form submitted // first of all we need to check for values to prevent sql injection $valid_fields = array("venue_state", "venue_city", "start_date"); if(!in_array($field, $valid_fields)) die("Error: Invalid field!"); // connect to our Database mysql_connect("localhost", "arts_cshow", "My Password") or die(mysql_error()); mysql_select_db("arts_shows") or die(mysql_error()); echo "<h6>Search Results for $find </h6>\n"; $find = addslashes($find); $result = mysql_query("SELECT * FROM craft_shows WHERE $field LIKE '%$find%'"); if(mysql_num_rows($result) == 0) { echo "<p>0 matches found.</p>"; } else { $id = $row['id']; echo "<table class=\"stshows\"><tr><td class=\"stshows\">"; while($row = mysql_fetch_array($result)) $sql = "SELECT *, DATE_FORMAT(`start_date`, '%b %e, %Y') AS s_date FROM craft_shows"; $result = mysql_query($sql); while( $row = mysql_fetch_assoc($result) ) { echo "<a href='/show_submits/show_detail.php?id={$row['id']}'>Details</a>\n"; echo $row['venue_state'] . " {$row['s_date']} {$row['show_name']} {$row['venue_city']}<br>\n"; } echo "</td></tr></table>\n"; } } ?> 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.