gckmac Posted April 26, 2009 Share Posted April 26, 2009 I'm having difficulty turning a php variable into an a href link. I am trying to place it here ... above the echo <hr/>: <?php echo '<h1><a href="http://www.maine.info/festivals.php">Maine Events</a> - ' . $month . ' ' . $display_cat . ' </h1>'; echo '<hr />'; The variable is $display_cat (where it could equal "festivals" "music" "theater" etc.) I would like to make the following links (I will list 12 of them for all months): $display_cat in <a href="http://www.maine.info/events/$display_cat/january.php">Jan</a> etc for additional months Thanks in advance! gckmac Quote Link to comment https://forums.phpfreaks.com/topic/155723-solved-php-variable-into-link/ Share on other sites More sharing options...
mikesta707 Posted April 26, 2009 Share Posted April 26, 2009 <a href="http://www.maine.info/events/<? echo $display_cat; ?>/january.php">Jan</a> That should work for what you need to do. Hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/155723-solved-php-variable-into-link/#findComment-819666 Share on other sites More sharing options...
gckmac Posted April 26, 2009 Author Share Posted April 26, 2009 It invokes the following invalid URL, a problem I've been having - http://www.maine.info/events/%3C?%20echo%20$display_cat;%20?%3E/january.php Quote Link to comment https://forums.phpfreaks.com/topic/155723-solved-php-variable-into-link/#findComment-819669 Share on other sites More sharing options...
thebadbad Posted April 26, 2009 Share Posted April 26, 2009 Use full PHP tags, or simply: <?php echo '<a href="http://www.maine.info/events/' . $display_cat . '/january.php">Jan</a>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/155723-solved-php-variable-into-link/#findComment-819673 Share on other sites More sharing options...
gckmac Posted April 26, 2009 Author Share Posted April 26, 2009 Thanks Mike and BadBad ... almost there ... BadBad code is working ... but .. need to lower case the month invoked by display_cat ... for it improperly shows as: http://www.maine.info/events/Festivals.january.php Thoughts? Thanks in advance. You folks are great. gckmac Quote Link to comment https://forums.phpfreaks.com/topic/155723-solved-php-variable-into-link/#findComment-819676 Share on other sites More sharing options...
gckmac Posted April 26, 2009 Author Share Posted April 26, 2009 OK! Fixed it with $display_cat=strtolower($display_cat); Could not have done it without you guys. See below ... (more months to add) <?php $display_cat = strtolower($display_cat); echo '<p><font face="arial" size="1"><a href="http://www.maine.info/events/' . $display_cat . '/january.php">Jan</a> | '; echo '<a href="http://www.maine.info/events/' . $display_cat . '/february.php">Feb</a></font>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/155723-solved-php-variable-into-link/#findComment-819682 Share on other sites More sharing options...
thebadbad Posted April 26, 2009 Share Posted April 26, 2009 Good. Let me show you a way to loop through the months, getting rid of the repetitive code: <?php $display_cat = strtolower($display_cat); $months = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); $links = array(); foreach ($months as $month) { $links[] = '<a href="http://www.maine.info/events/' . $display_cat . '/' . strtolower($month) . '.php">' . substr($month, 0, 3) . '</a>'; } echo '<p><font face="arial" size="1">' . implode(' | ', $links) . '</font></p>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/155723-solved-php-variable-into-link/#findComment-819750 Share on other sites More sharing options...
gckmac Posted April 26, 2009 Author Share Posted April 26, 2009 I will try that looping code. Thank you. My next issue is this: The following code works, but when inserted where I want it breaks the php code below it: I.e., code that works: <?php // $month = date("F") ; $month = strtolower($month); $month1 = ucfirst($month); $janlinkclass = ($month1 == "January") ? "current" : ""; $feblinkclass = ($month1 == "February") ? "current" : ""; $marlinkclass = ($month1 == "March") ? "current" : ""; $aprlinkclass = ($month1 == "April") ? "current" : ""; $maylinkclass = ($month1 == "May") ? "current" : ""; $junlinkclass = ($month1 == "June") ? "current" : ""; $jullinkclass = ($month1 == "July") ? "current" : ""; $auglinkclass = ($month1 == "August") ? "current" : ""; $seplinkclass = ($month1 == "September") ? "current" : ""; $octlinkclass = ($month1 == "October") ? "current" : ""; $novlinkclass = ($month1 == "November") ? "current" : ""; $declinkclass = ($month1 == "December") ? "current" : ""; ?> <li> <strong class ="head selected" >Event Control Panel</strong> <ul> <li>Events in: <select id="scity" name="scity"> <option value="-1">Select City...</option> <?php $query = "select distinct(city) from maine_festivals where city <> '' and state = 'Maine' and (datediff(enddate, current_date) >= 0 or category = 'Festivals') order by city "; $result = mysql_query($query, $link); if (isset($month)) { $cat1 = strtolower($cat); } else { $cat1 = "festivals"; } while ($row = mysql_fetch_assoc($result)) { extract($row); echo "<option value = '$city'"; if ($city == $gcity) echo " selected "; echo ">$city</option>\n" ; } ?> </select> </li> CODE THAT BREAKS WHEN ABOVE IS INSERTED: <?php echo '<hr />'; //$startcat = ""; while ($row = mysql_fetch_assoc($result)) { extract($row); $startdate1_part = substr($startdate1, 0, strlen($startdate1) - 6); $date = ($startdate1 == $enddate1)? $startdate1: $startdate1_part . " thru " . $enddate1; if ($expireddays >= 0) { $mapurl = ' <a href = "http://www.maine.info/sandbox/maps/map2.php?type=000000001&zoom=13&city=' . $city . '"> Map + Directions</a> '; $parastyle = ""; } else { $mapurl = "" ; $parastyle = "expired"; } if ($city == "") { $city = "Multiple Locations"; $mapurl = "" ; } if ($username == ADMIN || $username == $author) { $editlink = " | <a href = 'http://www.maine.info/users/editevent.php?id=$id'>Edit</a>"; } else { $editlink = ""; } $no_title = str_replace(" ", "", $title); $no_title = str_replace("'", "", $no_title); echo "<a name = '$no_title' ></a>"; if (strlen($linkurl) > 5) { echo "<h2 class = '$parastyle'><a href = '$linkurl' class = '$parastyle' >$title</a> "; } else { echo "<h2 class = '$parastyle'>" . $title; } echo <<<ENT </h2> <p class = "$parastyle"><font face="arial" size="2"> <strong>$date. $city.</strong> $description $mapurl <!-- <a href = "http://www.maine.info/users/reporterror.php?id=$id&type=event">Report Update</a> --> $editlink </p></font> ENT; } ?> </div> Any Thoughts? Thanks! gckmac Quote Link to comment https://forums.phpfreaks.com/topic/155723-solved-php-variable-into-link/#findComment-819814 Share on other sites More sharing options...
thebadbad Posted April 26, 2009 Share Posted April 26, 2009 What do you mean 'breaks'? Getting any errors? If yes, show 'em, else describe how it's breaking. Quote Link to comment https://forums.phpfreaks.com/topic/155723-solved-php-variable-into-link/#findComment-819821 Share on other sites More sharing options...
gckmac Posted April 26, 2009 Author Share Posted April 26, 2009 Firstly - the month array works great. (Now I've got to figure out how it does it Next, in terms of the breakage. Yes, when I insert the noted code, it displays the intended function (drop down list of cities), but then the stuff below it no longer displays. Just blank. For what it should look when working, see www.maine.info/cities/freeport/events.php To view it broken (temporarily), see http://www.maine.info/events/festivals/april.php gckmac Quote Link to comment https://forums.phpfreaks.com/topic/155723-solved-php-variable-into-link/#findComment-819846 Share on other sites More sharing options...
gckmac Posted April 26, 2009 Author Share Posted April 26, 2009 Aside - The month array is helpful, but it leaves the current month as December (the last one on the list), so that causes a bit of a problem. Quote Link to comment https://forums.phpfreaks.com/topic/155723-solved-php-variable-into-link/#findComment-819865 Share on other sites More sharing options...
gckmac Posted April 26, 2009 Author Share Posted April 26, 2009 I think the issue causing breakage (nothing displays after /select) may be that <select> and </select> are not within <php> commands. But I don't know how to resolve this. gckmac Quote Link to comment https://forums.phpfreaks.com/topic/155723-solved-php-variable-into-link/#findComment-819896 Share on other sites More sharing options...
thebadbad Posted April 28, 2009 Share Posted April 28, 2009 Can you post the full script that's not working? Quote Link to comment https://forums.phpfreaks.com/topic/155723-solved-php-variable-into-link/#findComment-820951 Share on other sites More sharing options...
gckmac Posted April 28, 2009 Author Share Posted April 28, 2009 Thanks thebadbad. I dealt with it by putting the offending code in another <div>. Better placement anyway. Thank you again. Would like to keep in touch so send your email or AIM address if you will. gckmac Quote Link to comment https://forums.phpfreaks.com/topic/155723-solved-php-variable-into-link/#findComment-820981 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.