Question

I´m trying to get into fat-free php framework, which works like a charme. there are a view things I can´t figure out. To make a long story short find here a simple example :

http://barbatti.de/f3/

Questions:

  1. When one klicks on a record the GET /view/@id route is triggered. How can I realize a Navigation in the following single view for next article and prev article. The links should not be visible if there´s no next article and the Nr. of the current article should be namend. (I´ve read the how to about cursors but can´t figure this out)

  2. How can I fill the select Box in single view with all found articles?

  3. How do I have to change the route for single view that it also listens on the post from the select box drop down from question 2?

My Setup:

index.php (I kicked the GET / route in the code below)

$f3 = require('lib/base.php');

$f3->set('DB',
  new DB\SQL(
    'mysql:host=localhost;port=3306;dbname=f3',
    'root',
    'root'
  )
);

$f3->route('GET /view/@id',
  function($f3) {
    $id = $f3->get('PARAMS.id');
    $article=new DB\SQL\Mapper($f3->get('DB'),'test');    
    $f3->set('list',$article->find(array('id=?',$id)));
    echo template::instance()->render('single.htm');
  }
);

$f3->run();

Template single view:

<repeat group="{{ @list }}" value="{{ @item }}">
    <p>
      {{ @item.name }}
      {{ @item.text }}
      {{ @item.age }}
    </p>
</repeat>

<hr>
<a href="view/@item">prev</a> | <a href="view/@item">next</a> | <small>this is entry XX from XX</small>
<p> <small>Select Entry:</small></p>
<form action="/view" method="post">
   <select name="goto">
      <repeat group="{{ @list }}" value="{{ @item }}">
        <option value="{{ @item.id }}">{{ @item.name }}</option>
      </repeat>
   </select>
<input type="submit">
</form>

SQL:

CREATE TABLE `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` text NOT NULL,
  `text` text NOT NULL,
  `age` text NOT NULL,
  `rel` int(11) NOT NULL,
  KEY `id` (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;

INSERT INTO `test` (`id`, `name`, `text`, `age`, `rel`) VALUES
   (1, 'a', 'hi', '20', 1),
   (3, 'b', 'yes', '25', 1),
   (4, 'c', 'why not', '30', 1),
   (5, '1', 'why', '15', 2),
   (6, '2', 'Ahoi', '35', 2),
   (7, '3', 'ade', '40', 2);

Thanks a lot!

Was it helpful?

Solution

Why do you use find() with an id condition in its where criteria, which probably just returns 1 result and then loop through the whole list? This doesn't make much sense to me. Use a findone() or load() method... Then use $article->next()->dry(); to determine if there is another record... Same for prev() / skip(-2)

OTHER TIPS

to 1: This way is quite dirty, but you could to two extra queries, to look for articles with a higher or lower id. If it returns another result, you could add the prev or next link. You should find a few more examples by look for "pagination" on Stackoverflow.

to 2: you have to do another query with all entries from the database to fill that box.

to 3: either you create a new route for that form action, or your change your route to something like: GET|POST /view/@id But this means, you also have to change the action in your form to the exact route. So, the action has to look like: action="{{(int) @PARAM.id}}" (I added the (int) prevent cross site scripting)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top