• PHP Help
  • Using <a href="index.php?>some thing</a>

I have a question about linking web pages. I have a database that is used to display information on my index page, which is working fine. However, at some point I will be adding information that contains a link to a page within the same database. I can do this with external links, but how do I do this for internal links. For example: assume I have a database entry called Egypt with its own text and images, how do I link to this page in say the 'home page', a page in the database.

I hope that makes sense: I have tried this <a href="index.php?id=4">Egypt</a> which works, that is the information appears on the index page. However, this means manually adding the id for each link.

I have pageheader and pagecontent for the data.

EX:
pageheader pagecontent
Egypt some information.

Any help will be appreciated.

    If all the IDs and names are in the database, then presumably you could just do one query, then loop through the results and display them -- unless I'm missing something?

    $sql = 'SELECT id, title FROM articles ORDER BY title';
    $stmt = $pdo->query($sql);
    echo "<ul id='menu'>";
    while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        echo "<li><a href='index.php?id={$row['id']}'>{$row['title']}</a></li>";
    }
    echo "</ul>";
    

    Just to give an idea...change accordingly for actual table/column names, database extension being used, etc....

      Thanks for the advice, but I may have not made myself clear, sorry about that. What I want is a link on a page - think wikipedia, but that link is in a database field. For example I have a database field called pagecontent and have the following inside the field:This is the home page

      This is a test link:
      <a href="index.php?id=4">Predynastic Egypt</a>

      So, should I use the menu 'home' page link, this link is presented on the page with the text and is not part of any menu. I hope that helps. I tried to upload and image to this reply, but all I got was some code that I could do nothing with.

      steveb63 I hope that helps.

      Not really. 🙁 I'm afraid that all I'm seeing is that you want some HTML with a link to appear somewhere. It's not clear to me what the problem is, and therefore how PHP can help solve it. Seems to me you are going to have to manually add something, whether it's an ID like that, or setting things up to use something like a RESTful link format along the lines of <a href='/articles/Egypt'>Egypt</a>, with the underlying .htaccess config to point to index.php or whatever it would be, and processing in that file to query for the article with title 'Egypt' (or whatever makes sense). However, that still means you need to know what the exact title is in that example, so it doesn't really save you much/any time, plus you need to implement that RESTful stuff (or I guess you could keep it simpler and just do <a href='/index.php?title=Egypt'> and then just tweak the current code to search for title instead of ID?).

        Thanks for the help. I will try the tweak of the code and see what happens. If this fails, than I will just manually ad the id.
        😉

          Write a Reply...