Modificare cod HTML in script paginare

Aici prezentati probleme, intrebari, sau orice subiecte legate de scripturi si tutoriale de pe site.
dim
Mesaje: 61

Modificare cod HTML in script paginare

Salut
In scriptul de paginare de la adresa: https://marplo.net/php-mysql/clasa-p ... -php , am modificat sa selecteze si sa afiseza paginarea pentru tabele separate, cu 2 feluri de cod html, adaugand :

Cod: Selectaţi tot

public $table = 'tabel';     // AICI adaugati numele tabelului MySQL
 public $html = 'html1';       // AICI adaugati cod HTML  pt a aranja  diferit acelasi continut din table

.....

Cod: Selectaţi tot

while($row = $resql->fetch_assoc()) {
  if($this->table == 'table1'){ $re_cnt .=  '<h3>'. $row['title']. '</h3>'. $row['id']. '<div class="content">'. $row['content']. '</div>'};
   elseif($this->table == 'table2'){
                 if($this->html == 'html1')
                        $re_cnt .=  '<table width="0" border="0">
                          <tr>
                            <td>'. $row['id']. '</td>
                            <td>'. $row['username']. '</td>
                          </tr>
                        </table>';
                elseif ($this->html == 'html2')
                       $re_cnt .=  '<table width="0" border="0">
                          <tr>
                            <td>'. $row['id']. '</td>
                            <td>'. $row['password']. '</td>
                          </tr>
                        </table>';
                        
             }
} 
Acum intrebarea este daca pot sa folosesc in loc de tot codu ala html pt afisare un template sau o alta pagi asta ca sa nu incarca clasa de afisare cu o gramada de cod html?

MarPlo Mesaje: 4343
Salut
Daca vrei sa folosesti un sistem cu tempate separat pentru codul html de afisare paginare, poti incerca sa adaptezi modelul din acest exemplu (se poate testa separat).

Template: html1.tpl

Cod: Selectaţi tot

<table width="0" border="0">
  <tr>
    <td><!-- ID --></td>
    <td><!-- USERNAME --></td>
  </tr>
</table>
fisier.php :

Cod: Selectaţi tot

<?php
$row['id'] = 'un-id';
$row['username'] = 'Un-nume';
$re_cnt = '';

// array cu chei /cuvinte care sunt adaugate in template
$templ = array('ID'=>$row['id'], 'USERNAME'=>$row['username']);

// preia template-ul
$tpl = file_get_contents('html1.tpl');

// inlocuieste datele in template
$re_cnt .= preg_replace_callback('/<!-- ('. implode('|', array_keys($templ)) .') -->/i', function($m) { GLOBAL $templ; return $templ[$m[1]]; }, $tpl);

echo $re_cnt;       // afiseaza pagina obtinuta
 
- Sau poti adauga codul php cum stii intr-un fisier php separat, apoi il incluzi cu include_once() in functia din acea clasa.

dim Mesaje: 61
Salut
Am facut o pagina separate in care am introdus tabelu.

admin.php:

Cod: Selectaţi tot

<?PHP
$bg = '#EDEDED';

$re_cnt .= '<table width="600" align="center"  cellspacing="0" border="1">
<tr>
  <td width="20" bgcolor="#8FC2F1" class="text_negru_normal"><strong>ID</strong></td>
  <td width="200" bgcolor="#8FC2F1" class="text_negru_normal"><strong>User:</strong></td>
  <td  bgcolor="#8FC2F1" class="text_negru_normal"><strong>Pass:</strong></td>
</tr>
<tr>
  <td bgcolor='.$bg.'>'. $row['id'].'</td>
  <td bgcolor='.$bg.'>'. $row['username'].'</td>
  <td bgcolor='.$bg.'>'.$row['password'].'</td>		  
</tr></table>'
?>
si pagina asta am introduso in class.pagination

class.pagination.php

Cod: Selectaţi tot

elseif($resql->num_rows > 0) {
            while($row = $resql->fetch_assoc()) {
                if($this->table == 'admin'){ 
                     if($this->html == 'html1')
                        $re_cnt .=  '<table width="0" border="0">
                          <tr>
                            <td>'. $row['id']. '</td>
                            <td>'. $row['username']. '</td>
                          </tr>
                        </table>';
                    elseif ($this->html == 'html2')
                       include_once "paginare/admin.php";
                        
             }
              }
            }
 
Problema e ca imi afiseaza daor primul rezultat pe pagian in rest le ascunde si nu prea vad unde e greseala???

MarPlo Mesaje: 4343
Incearca sa nu incluzi repetativ in while(), ci o singura data inainte de while(), si folosesti un singur fisier cu functii pentru template, iar in while() se apeleaza functiile; asa:

htmlpg.php:

Cod: Selectaţi tot

<?php
function html1($id, $username) {
  return '<table width="0" border="0"><tr>
    <td>'. $id. '</td>
    <td>'. $username. '</td>
  </tr></table>';
}

function admin($id, $username, $password) {
  $bg = '#EDEDED';

  return '<table width="600" align="center"  cellspacing="0" border="1"><tr>
    <td width="20" bgcolor="#8FC2F1" class="text_negru_normal"><strong>ID</strong></td>
    <td width="200" bgcolor="#8FC2F1" class="text_negru_normal"><strong>User:</strong></td>
    <td  bgcolor="#8FC2F1" class="text_negru_normal"><strong>Pass:</strong></td>
  </tr><tr>
    <td bgcolor='.$bg.'>'. $id.'</td>
    <td bgcolor='.$bg.'>'. $username.'</td>
    <td bgcolor='.$bg.'>'.$password.'</td>          
  </tr></table>';
}
 
class.pagination.php

Cod: Selectaţi tot

elseif($resql->num_rows > 0) {
  include_once 'paginare/htmlpg.php';

  while($row = $resql->fetch_assoc()) {
    if($this->table == 'admin'){
        if($this->html == 'html1') $re_cnt .=  html1($row['id'], $row['username']);
        else if($this->html == 'html2') $re_cnt .=  admin($row['id'], $row['username'], $row['password']);
    }
  }
}
 

dim Mesaje: 61
Multumesc pentru ajutor acum functioneaza ok si il pot folosi in mai multe pagin

Subiecte similare