Dymond
Dymond

Reputation: 2277

create dynamic link from Mysql table

Im trying to create dynamic links from some post inside a database table, but i cant figure out how to create the link, when the user is already logged in.

I think something like this.

    <?php $articles = new Articles();

foreach($articles->fetch_user_article($_GET['uid']) as $article) :?>
 <a href="edit_articles.php?uid=<?php echo $_SESSION['id']?>&article=<?php echo $article['id'];?>"><?php echo $article['title'];?></a>

    <?php endforeach ?>

This gives me a link that looks like this

edit_articles.php?uid=5&article=213

The article id:s are correct from the DB table.

Now my edit_articles.php file

$articles = new Articles();
$article = $articles->fetch_user_article($_GET['uid']); 
echo $article['text'];

But when im reach the edit_articles.php file i get

Undefined index: text

And my function

function fetch_user_article($uid){
        $uid = (int)$uid;
        $query = $this->link->query ("SELECT id, title,text FROM blog WHERE user_id = '{$uid}' ");
        $tweet = array();
        while(($row = $query->fetch(PDO::FETCH_ASSOC)) !== FALSE) { 
            $tweet[] = $row;
        }
        return $tweet;
    }

Upvotes: 0

Views: 1208

Answers (1)

LorenzoR
LorenzoR

Reputation: 421

Your function fetch_user_article is returning more than one article. Use like this to fecth all articles.

$articles = $articles->fetch_user_article($_GET['uid']);

foreach ( $articles AS $article ) {
    echo $article['text'];
}

If you want fetch_user_article to return only one article, then the field user_id of table blog should be unique.

Or you will have to rewrite the query

 SELECT id, title,text FROM blog WHERE user_id = '{$uid}'

so it gives you only one result, something like:

      $article_id = $_GET['article'];
      SELECT id, title,text FROM blog WHERE id = '{$article_id}'

Upvotes: 1

Related Questions