Eroare paginare loop

Ai o întrebare legată de PHP? Incercăm să îi găsim soluţie. Sau poate doar vrei să publici un cod interesant.

Moderatori: Zamolxe, Moderatori

invatphp
PHPRomania Supporter
Mesaje: 23
Membru din: Dum Iul 15, 2012 12:53 pm

Eroare paginare loop

Mesajde invatphp » Dum Iul 15, 2012 1:30 pm

Cod: Selectaţi tot

 $skip = array(
  'adresa1.ro' => 'Adresa 1',
  'adresa2.ro' => 'Adresa 2',
  'adresa3.ro' => 'Adresa 3',
  'adresa4.ro' => 'Adresa 4',
  'adresa5.ro' => 'Adresa 5',
  'adresa6.ro' => 'Adresa 6',
  'adresa7.ro' => 'Adresa 7',
  'adresa8.ro' => 'Adresa 8',
  'adresa9.ro' => 'Adresa 9' ) ;
 $totl = count( $skip ) ;
 $pags = ceil( $totl / 3 ) ;
 $page = intval( $_GET["page"] ) ? $_GET["page"] : 1 ;
 $page = ( int )$page ;
 if ( $page > $pags ) {
  $page = $pags ;
 }
 if ( $page < 1 ) {
  $page = 1 ;
 }
 if ( $page && $pags ) {
  $start = ( $page - 1 ) * 3 ;
  $finis = ( $page * 3 ) - 1 ;
  if ( $finis > $totl - 1 ) {
   $finis = ( $totl - 1 ) ;
  }
  foreach ( $skip as $id => $value ) {
   for ( $i = $start; $i <= $finis; $i++ ) {
    echo "<p>\n" ;
    echo "<b><a href=\"" . $id . "\">" . $value . "</a></b>\n" ;
    echo "</p>\n" ;
   }
  }
 }
 if ( $page > 2 ) {
  echo "<a href=\"" . htmlentities( $_SERVER["PHP_SELF"] ) . "?page=1\">1</a>\n" ;
 }
 if ( $page > 1 ) {
  echo "<a href=\"" . htmlentities( $_SERVER["PHP_SELF"] ) . "?page=" . ( $page - 1 ) . "\">" . ( $page - 1 ) . "</a>\n" ;
 }
 if ( $pags > 1 ) {
  echo $page . "\n" ;
 }
 if ( $page < $pags ) {
  echo "<a href=\"" . htmlentities( $_SERVER["PHP_SELF"] ) . "?page=" . ( $page + 1 ) . "\">" . ( $page + 1 ) . "</a>\n" ;
 }
 if ( $page < $pags - 1 ) {
  echo "<a href=\"" . htmlentities( $_SERVER["PHP_SELF"] ) . "?page=" . $pags . "\">" . $pags . "</a>\n" ;
 }

Eroarea pe care o intampin este ca nu-mi afiseaza cate 3 linkuri per pagina ci un sir lung de linii. Cine imi spune unde am gresit in cod si ma poate ajuta cu solutie?



Avatar utilizator
Birkoff
Senior Member
Mesaje: 6380
Membru din: Joi Mar 18, 2004 2:34 pm
Localitate: Bucuresti
Contact:

Mesajde Birkoff » Dum Iul 15, 2012 2:41 pm

cred ca ai o eroare aici

Cod: Selectaţi tot

foreach ( $skip as $id => $value ) {
   for ( $i = $start; $i <= $finis; $i++ ) {
    echo "<p>\n" ;
    echo "<b><a href=\"" . $id . "\">" . $value . "</a></b>\n" ;
    echo "</p>\n" ;
   }
  }


corect cred ca ar trebui

Cod: Selectaţi tot

$nr = 1;
foreach ( $skip as $id => $value ) {
  if ( $nr >= $start && $nr <= $finis) {
    echo "<p>\n" ;
    echo "<b><a href=\"" . $id . "\">" . $value . "</a></b>\n" ;
    echo "</p>\n" ;
    echo '<br>';
   }

   $nr++;
  }


recomandare: vezi pe site la mine tutoriale de paginare (foloseste cautarea de pe site)
1) CMS, ERP, CRM, etc... (doar pentru clienti))
2) Portofoliu, servicii, contact, blog
3) Folositi aceasta clasa sql in proiectele voastre (open source)
4) Vrei un magazin virtual la cheie, usor de folosit, cu api-uri incluse pentru maximizarea vanzarilor si multe alte facilitati? Da un semn si discutam.

invatphp
PHPRomania Supporter
Mesaje: 23
Membru din: Dum Iul 15, 2012 12:53 pm

Mesajde invatphp » Dum Iul 15, 2012 4:02 pm

Multumesc Birkoff, raspunsul tau este solutia potrivita.

invatphp
PHPRomania Supporter
Mesaje: 23
Membru din: Dum Iul 15, 2012 12:53 pm

Mesajde invatphp » Dum Aug 05, 2012 6:07 pm

Salut din nou, cine ma poate ajuta cu corectarea codului adica unde am gresit:

Cod: Selectaţi tot

  $test = array(
   'A' => array(
    'adresa.ro' => 'Site.Ro',
    'adresa.com' => 'Site.Com',
    ),
   'B' => array(
    'adresa.info' => 'Site.Info',
    'adresa.biz' => 'Site.Biz',
    ),
   ) ;
  echo "<pre>" ;
  print_r( $test ) ;
  echo "</pre>" ;

  foreach ( $test as $link => $name ) {
   //Cum fac sa extrag si din A si/sau din B toate rezultatele? Adica ori doar din A sau B ori din amandoua, daca se poate.
  }

C0zy
PHPRomania Supporter
Mesaje: 21
Membru din: Dum Iul 08, 2012 9:38 pm
Contact:

Mesajde C0zy » Dum Aug 05, 2012 9:44 pm

din moment ce ai un array , incearca
print_r( $test['A']) ;
pentru a afisa datele din A
$rezultat = $test['A'].$test['B'];
print_r( $rezultat );

Reventon
Average Member
Mesaje: 68
Membru din: Lun Ian 31, 2011 5:07 pm

re

Mesajde Reventon » Dum Aug 05, 2012 9:50 pm

ai pus niste virgule in plus si ai stricat sintaxa.
ti-am editat eu mai jos codul.

tu ai array in array, ceea ce inseamna ca ai matrice multidimensionala.
in cazul de fata, este o matrice multidimensionala pe 2 nivele, iar
pentru asta trebuie sa faci 2 loop-uri, unu in altul.

foloseste print_r ca in modelul de mai jos, pentru a putea vedea toata matricea

<?php

$test = array(
'A' => array('adresa.ro' => 'Site.Ro', 'adresa.com' => 'Site.Com'),
'B' => array('adresa.info' => 'Site.Info', 'adresa.biz' => 'Site.Biz')
) ;

echo '<pre>' . print_r($test, 1) . '</pre>';

foreach ($test as $line) {
foreach ($line as $link => $name) {
echo "Link: $link / Name: $name <br />";
}
}

invatphp
PHPRomania Supporter
Mesaje: 23
Membru din: Dum Iul 15, 2012 12:53 pm

Mesajde invatphp » Lun Aug 06, 2012 1:00 pm

Reventon este ok dar de unde scot sa imi afiseze A si B-ul pe langa celelalte rezultate? :)

Reventon
Average Member
Mesaje: 68
Membru din: Lun Ian 31, 2011 5:07 pm

Mesajde Reventon » Lun Aug 06, 2012 1:08 pm

poti folosi asa :

<?php

$test = array(
'A' => array('adresa.ro' => 'Site.Ro', 'adresa.com' => 'Site.Com'),
'B' => array('adresa.info' => 'Site.Info', 'adresa.biz' => 'Site.Biz')
) ;

echo '<pre>' . print_r($test, 1) . '</pre>';

foreach ($test as $key => $line) {
echo "$key : <br />";
foreach ($line as $link => $name) {
echo "Link: $link / Name: $name <br />";
}
}

invatphp
PHPRomania Supporter
Mesaje: 23
Membru din: Dum Iul 15, 2012 12:53 pm

Mesajde invatphp » Lun Aug 06, 2012 6:28 pm

Multumesc functioneaza :)

laurentiu42
New Member
Mesaje: 3
Membru din: Mar Sep 13, 2016 7:05 pm

paginare video tube

Mesajde laurentiu42 » Mie Sep 14, 2016 1:51 am

Salutare,am thema video tube,stie cineva cum se face paginarea la acesta thema?
Untitled.jpg
Untitled.jpg (219.48 KiB) Vizualizat de 5984 ori


Înapoi la “Cod PHP”

Cine este conectat

Utilizatori ce ce navighează pe acest forum: Niciun utilizator înregistrat și 36 vizitatori