sdet.ro
sdet.ro

Reputation: 327

display elements from db

In order to create a pagination, I will have to store all the paths to image in the database. I created my database as following:

id(auto increment) int250
name varchar 250
link text
type varchar(250)
category int(50)

The name and the types are not quite relevant in this moment. I would like to figure out on how to display the elements from my database. In order to make it easier(not sure about the paths yet) I have stored in the link column a http image. IF i vardump it, I get everything with an array. My code:

<?php
if (DB::connection()->getDatabaseName()) {
$pics = DB::table('pictures')
->orderBy('id', 'desc')
->get();
 var_dump($pics);
 }

?>

My result:


array(3) { [0]=> object(stdClass)#182 (5) { ["id"]=> int(3) ["name"]=> string(1) "2" ["link"]=> string(66) "http://www.planwallpaper.com/static/images/canberra_hero_image.jpg" ["type"]=> string(3) "pic" ["category"]=> int(1) } [1]=> object(stdClass)#183 (5) { ["id"]=> int(2) ["name"]=> string(1) "2" ["link"]=> string(66) "http://www.planwallpaper.com/static/images/canberra_hero_image.jpg" ["type"]=> string(3) "pic" ["category"]=> int(1) } [2]=> object(stdClass)#184 (5) { ["id"]=> int(1) ["name"]=> string(4) "test" ["link"]=> string(66) "http://www.planwallpaper.com/static/images/canberra_hero_image.jpg" ["type"]=> string(5) "test1" ["category"]=> int(1) } } 

How do I get the pictures displayed as pictures?

Sorry, I'm quite a beginner in programming, doing my best to learn. I'm trying to get this done, because I finished a website, and all the pictures were hardcoded. Now I must implement pagination, and in order to do that, the best way would be to store the images link, and display them with limit 10; for example.

Any help would be great!

EDIT: Controler:

    <?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

abstract class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}

Upvotes: 0

Views: 83

Answers (2)

Antony Thompson
Antony Thompson

Reputation: 1638

If your using laravel, in the method your using from your controller add this code

    $pictures = Picture::paginate(20);

    return view('view_name', ['pictures' => $pictures]);

Then in your view add

@foreach ($pictures as $pic)
   <img src="{{$pic->link}}" />
@endforeach

Upvotes: 1

hanshenrik
hanshenrik

Reputation: 21463

foreach($pics as $pica){
if($pica->type!=='pic'){continue;/*not a pic.. ignore?*/}
echo '<img src="'.htmlentities($pica->link, ENT_QUOTES | ENT_HTML401 | ENT_SUBSTITUTE | ENT_DISALLOWED, 'UTF-8', true).'" />';

}

Upvotes: 0

Related Questions