1

I am trying to get specific information from database and specific for each user. I want to loop through an object and get a specific information (string) from 2 different field in database. Each 2 field combine will create a link to download a file.(those file will be located inside project folder)

I need to loop through the database get the field and display it, of course database will containe many link-file.

Here the database structure:

  • file_id (int)
  • file_name (name)
  • file_extension (pdf)
  • user_id (int)

Here is class file to get full info from user:

    public $file = array();
    public $pdo = '';

    public function __construct($id)
    {
        $this->pdo = new PDO('mysql:host=127.0.0.1;dbname=****', '***', '***', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
        $sql = 'SELECT `name`, `file_name`, `file_extention` FROM `users`'
                 .'JOIN `file`'
                 .'ON users.`user_id` = file.`user_id`'
                 .'WHERE users.`user_id` = :id';
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute(array(':id' => $id));
        while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $this->file[] = $row;
        }
    }

Here is view class display :

   public function displayFile($link) 
   {
    $output = '';
    $output .= '<p><a href="#"><i class="fa fa-folder"></i>&nbsp&nbsp'. $link.'</a></p><br />';
    return $output;
   }

I need to get a link like :

$link = [file_name].'.'.[file_extension];

Final result for public should be :

  • link to file 01
  • link to file 02
  • link to file 03
  • etc....
2
  • 1
    Could you clarify where you are stuck, please? Did you write this code yourself or did someone pass it off to you? Commented May 13, 2015 at 13:27
  • I wrote this code myself. When i construct the link this always take the last link and skip the previous one. Commented May 13, 2015 at 13:30

3 Answers 3

1

You almost got it, inside your loop, you create a link like this:

while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    ...
    $link = $row['file_name'] . '.' . $row['file_extension'];

    // then you can store it in an array:
    $links[] = $link;
    // or call the display function directly:
    $output .= displayFile($link);
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

Instead of adding the row to your array, build the link and add it.

   while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                $this->file[] = $row['file_name'].'.'.$row['file_extension'];
    }

Comments

0

Working code incase someone need it : Thanks to Honza Haering

 public function __construct($id)

{
    public $file = array();
    public $pdo = '';
    $this->pdo = new PDO('mysql:host=127.0.0.1;dbname=project001', 'root', '', array(PDO::ATTR_ERRMODE => PDO::
ERRMODE_EXCEPTION));
    $sql = 'SELECT `name`, `file_name`, `file_extention` FROM `users`'
             .'JOIN `file`'
             .'ON users.`user_id` = file.`user_id`'
             .'WHERE users.`user_id` = :id';
    $stmt = $this->pdo->prepare($sql);
    $stmt->execute(array(':id' => $id));
    while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $this->file[] = $row['file_name'] . '.' . $row['file_extention'];
    }
}
public function displayFile() 
{
    $link = $this->file;
    $output = '';
    foreach($link as $row) {
    $output .= '<p><a href="#"><i class="fa fa-folder"></i>&nbsp&nbsp'. $row.'</a></p><br />';
    }
    return $output;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.