1

) I have a question:

I have a databse with the following information: Name of report [name] Link to report [link]

The various reports are displayed in tables in my page (using db_tables filed with static, month, week etc)

But i want to generate a link to the report based on "link" and "name"

so i set up a pgsql query :

result = select name, link from db where type = 'week'

This works fine.

To display the above i have:

echo "<div id=\"selMaand\">" ;
    echo "<table id = \"t2\">\n ";
    echo "\t<tr class=\"head\">\n";
    echo "<th colspan=\"2\">Select Maand</th>";
        while ($line = pg_fetch_array($result5, null, PGSQL_ASSOC)) {
    echo "\t<tr>\n";
    foreach ($line as $col_value) {
        echo "\t\t<td width=\"100%\">$col_value</td>\n";
    }
    echo "\t</tr>\n";
}
    echo "</table>\n";
    echo "</div>";

which works fine to.

Now, the link from database is http://192.168.178.1:8080/etc/etc/page.html"onclick humptydumpty>name each time i want to adjust something in either the onlclick or address, i have to change all my links.

What can i insert in my querys so i will be able to build a link like;

echo "<a href = "http://192.168.178.1:8080/etc/etc/**$link**"onlclick humptydumpyu>**$name**</a>

I cant figure out how to break down my array!

Thanks in advance

Sjoerd

2
  • $line['name'] is your name and $line['link'] is your link. Throw them into html link and problem solved. Where is the problem?
    – cen
    Commented Nov 18, 2013 at 16:45
  • The problem was that i had a (foreach) to build a table, but it seems that i dont need that. This wasnt working: foreach ($line as $col_value)
    – Sjoerdjump
    Commented Nov 19, 2013 at 8:54

1 Answer 1

2

If you just need a portion of your URL in the database (i.e. only a portion of it would change for each record), then only store that portion of the URL in the database. You can then generate the remaining portion of the URL (call it the URL base) in your code.

So something like this:

define('URL_BASE', 'http://some.url.base/that/you/can/change/globally/');

Then you get the remaining piece of the URL from the database for each item. So you would have something like this inside your query loop to build the full URL.

$link = $line['link'];
$final_url = URL_BASE . $link;

As far as the onclick stuff goes, I would have that set sepearately as well, since I assume the onclick behavior is not dependent on the individual link (if it is you can make a separate DB column for it).

define('LINK_ONCLICK', 'onclick="some_onclick_function()"');

And in your query loop you can put this together like this:

$name = $line['name'];
$link = $line['link'];
$final_url = URL_BASE . $link;
echo '<a href="' . $final_url . '" ' . LINK_ONCLICK . '>' . $name . '</a>';

Note that it is not really necessary to define the strings for the url base and onclick behavior as constants. I just showed it this way as it is common practice to set globally defined values that do not need to change at run-time ion such a manner.

2
  • Hi and thanks for your reply! I'm going to play around with your idea, see if i can get it to work! Thanks again :)
    – Sjoerdjump
    Commented Nov 19, 2013 at 7:39
  • This is it, Great answer! For some reason i did not have to use the "'. $final_url .'" but i got it to work for me. If i had 15 rep i'd upvote your answer :) [code]echo "\t\t<td width=\"100%\"><a target=\"report\"href=\"$final_url\"$click\">$name</a></td>\n";[/code]
    – Sjoerdjump
    Commented Nov 19, 2013 at 8:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.