1

I have a homework where I have to call all the rows from the database. When an id of each row is clicked, there should be only this that row displaying.
There are three rows which contain text and two columns: id INT NOT NULL AUTO_INCREMENT and content VARCHAR(255).

I have this code:

file: db.php

<?php

    define("dbserver", "127.0.0.1");

    define("dbuser", "root");

    define("dbpass", "");

    define("dbname", "dbtesting");




 $db = new PDO(

 "mysql:host=" .dbserver. ";dbname=" .dbname,dbuser,dbpass,

 array(

 PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",

 PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET utf8"

 )

 );

 ?>

file: index.php

<?php

include "db.php";

function articleList($get = 0) {

    global $db;

    if (empty($get["p"])) {
        $sql = "SELECT * FROM dbtesting.articles";
    }
    else {
        $get2 = $get["p"];
        $sql = "SELECT * FROM dbtesting.articles WHERE id = '$get2'";
    }

    $result = $db->query($sql);

    foreach ($result as $value) {
        echo "<div style='background-color:#dedede;margin-top:10px'>";
        echo "<a href='index.php?p=".$value["id"]."'>"."ID"."</a>";
        echo " - ";
        echo $value["content"];
        echo "</div>"."<br>";
    }

}

articleList();

?>

The target is to make SQL to display only one article whom "ID" link has been clicked as shown in the picture below. No JavaScript or other languages are allowed. I have no idea how this should work.
This code has been written by my teacher and there is something I should fix for this to work.
I only remember him telling that this should be done by using get parameter or something. I didn't really understand. Could you please help to make it work?
Rows (articles) are displayed. When I click on the ID link there is no effect except the URL change. But content isn't changing.

All articles are displayed - working
all articles


Only clicked article should be displayed (?p=id in URL) - not working Clicked

4
  • We are not supposed to do your homework you are Commented Apr 16, 2016 at 17:30
  • It's not actual homework. It's just an exercise which is not going to be checked for those who didn't have enough time to finish it at school. And I'm not asking to do my homework, but to help me to understand this. How am I supposed to do it if there is no information about this and my teacher can't explain this in the way I can understand? Commented Apr 16, 2016 at 19:17
  • Kick the teacher, if that is the correct term for him/her Commented Apr 16, 2016 at 19:19
  • Yeah, I know that he must explain this. He does but it's difficult to understand him. He believes that we can find any code online if we don't now something. It's true but the only thing I was able to find out today is what I got more questions than answers :( Commented Apr 16, 2016 at 19:28

1 Answer 1

1

try use $_GET["p"] not $get...

Sign up to request clarification or add additional context in comments.

Comments

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.