jaykappy Posted July 26, 2013 Share Posted July 26, 2013 I am looking to see if there is a way that I can create links via html or php I have a year range say 1940-2013. instead of creating a ton of <a href> in html can I simply set the range and have php or something create these links centered on the page... example year links 1970 1971.....2013 where each year would be a link that I can push into a query and return results fro that given year...I can already do the query but looking to avoid the below repetitive code. Dont want to do the below, although it works. <a class="one" href="/MainPageindexAsbuiltsDisplayYr.php?Dept=Engineering_Asbuilts&Yr=2013">2013</A> <a class="one" href="/MainPageindexAsbuiltsDisplayYr.php?Dept=Engineering_Asbuilts&Yr=2012">2012</A> <a class="one" href="/MainPageindexAsbuiltsDisplayYr.php?Dept=Engineering_Asbuilts&Yr=2011">2011</A> ........ <a class="one" href="/MainPageindexAsbuiltsDisplayYr.php?Dept=Engineering_Asbuilts&Yr=1945">1945</A> Quote Link to comment https://forums.phpfreaks.com/topic/280544-create-links-on-a-page/ Share on other sites More sharing options...
mac_gyver Posted July 26, 2013 Share Posted July 26, 2013 the way to avoid repetitious code, that only varies in a value, is to use a programming loop construct to replace the code. loops are fundamental to all programming languages and would be covered in about the third main section in any book or tutorial. sorry for the cheeky answer but this is really basic stuff that you should have already seen. the most straightforward code would use a for(){} loop to loop over a range of values. you could also produce the range of values in an array (using the range() function), then use a foreach(){} loop to loop over that array of values. Quote Link to comment https://forums.phpfreaks.com/topic/280544-create-links-on-a-page/#findComment-1442291 Share on other sites More sharing options...
jaykappy Posted July 26, 2013 Author Share Posted July 26, 2013 Thanks I did this...one question...and I know this is a green question....we all have to lean somehow... Cant seem to get them to align left to right and at a specific width...so the below might make 4-5 rows of links. AND how do I get them to create the links descending? for ($i = 1964; ; $i++) { if ($i > 2013) { break; } ?> <div style="width=300px text-align=center margin=0 auto;";> <?php echo '<a href="/view_asbuilts_images.php?imageyear=2013&album_id=' . $i . '">'.$i.'</A>'; ?> <div/> Quote Link to comment https://forums.phpfreaks.com/topic/280544-create-links-on-a-page/#findComment-1442299 Share on other sites More sharing options...
jaykappy Posted July 26, 2013 Author Share Posted July 26, 2013 got the backwards with this for($i = 2013; $i >= 1964; $i--){ but still cant get them to line up horizontal and for a couple rows. Quote Link to comment https://forums.phpfreaks.com/topic/280544-create-links-on-a-page/#findComment-1442300 Share on other sites More sharing options...
jaykappy Posted July 26, 2013 Author Share Posted July 26, 2013 (edited) got it..moved the DIV tags outside <div id="asbuiltyear"> <?php $year = strftime("%Y")+1; for($i = $year; $i >= 1964; $i--){ echo '<a id="asbuiltyear" href="/view_asbuilts_images.php?imageyear=' . $i . '&album_id=' . $album_id . '">'.$i.'</A>'; echo ' '; } ?> </div> #asbuiltyear{ width: 75%; margin: 0 auto; } Edited July 26, 2013 by jaykappy Quote Link to comment https://forums.phpfreaks.com/topic/280544-create-links-on-a-page/#findComment-1442308 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.