Jump to content

Recommended Posts

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> 

 

Link to comment
https://forums.phpfreaks.com/topic/280544-create-links-on-a-page/
Share on other sites

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.

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/>
 

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 by jaykappy
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.