Cara bikin function pagination

Awalnya kodenya gini gan



<?php
    $host   = 'localhost';
    $user   = 'root';
    $pass   = '';
    $db     = 'lrn_page';

    $link   = mysqli_connect($host, $user, $pass, $db) or die(mysqli_error($link));


    $perPage    = 3;//
    $page       = isset($_GET["halaman"]) ? (int)$_GET["halaman"] : 1 ;
    $start      = ($page > 1) ? ($page*$perPage)-$perPage : 0;

    // die('tes...' . $page);

    $articles = "SELECT * FROM page ORDER BY id_hal DESC LIMIT $start, $perPage";
    $result2 = mysqli_query($link, $articles);

    $result = mysqli_query($link, "SELECT * FROM page");
    $total  = mysqli_num_rows($result);

    $pages  = ceil($total/$perPage);
    // die('total..' . $total);
?>

<?php while($row = mysqli_fetch_assoc($result2)) { ?>
    <p> <?=$row["judul"];?> </p> <br>
<?php }?>

<div class = "">
    <?php for($i = 1; $i<=$pages; $i++) {?>
        <a href="?halaman=<?=$i?>"> <?=$i?> </a>
    <?php } ?>
</div>

Diatas itu tutorial belajar dari bang hilman, php pagination.Dan itu sudah work.

Nah, saya mau terapin di blog dan naruh beberapa bagiannya di function aja, supaya ga numpuk di index.php nya gan, terus saya ubah jadi gini.

 <!-- <?php

function panggil($aku){
    echo "saya memanggil " . $aku;
}

panggil('dia');

?> -->

<?php

function pagination(){

    $host   = 'localhost';
    $user   = 'root';
    $pass   = '';
    $db     = 'lrn_page';

    $link   = mysqli_connect($host, $user, $pass, $db) or die(mysqli_error($link));


    $perPage    = 3;//
    $page       = isset($_GET["halaman"]) ? (int)$_GET["halaman"] : 1 ;
    $start      = ($page > 1) ? ($page*$perPage)-$perPage : 0;

    // die('tes...' . $page);

    $articles = "SELECT * FROM page ORDER BY id_hal DESC LIMIT $start, $perPage";
    $result2 = mysqli_query($link, $articles);

    $result = mysqli_query($link, "SELECT * FROM page");
    $total  = mysqli_num_rows($result);

    // die('total..' . $total);

}
?>

<?php

pagination();

    while($row = mysqli_fetch_assoc($result2)) { ?>
    <p> <?=$row["judul"];?> </p> <br>
<?php }?>

<div class = "">
    <?php for($i = 1; $i<=$pages; $i++) {?>
        <a href="?halaman=<?=$i?>"> <?=$i?> </a>
    <?php } ?>
</div>

Hasilnya malah eror jadi gini gan


Notice: Undefined variable: result2 in C:\xampp\htdocs\lrn_page\index.php on line 47

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\lrn_page\index.php on line 47

Notice: Undefined variable: pages in C:\xampp\htdocs\lrn_page\index.php on line 52

Mohon pencerahannya, saya udah panggil fungsinya tapi variabel2nya nggak terpanggil gan.

avatar fachrimubarak
@fachrimubarak

12 Kontribusi 1 Poin

Diperbarui 6 tahun yang lalu

4 Jawaban:

Jawaban Terpilih

kasarannya gini gan <pre> &lt;?php $host = 'localhost'; $user = 'root'; $pass = ''; $db = 'lrn_page';

$link = mysqli_connect($host, $user, $pass, $db) or die(mysqli_error($link));

function pagination($hal){ global $link;

$perPage    = 3;//
$page       = isset($hal) ? (int)$hal : 1 ;
$start      = ($page &amp;gt; 1) ? ($page*$perPage)-$perPage : 0;

$articles = &quot;SELECT * FROM page ORDER BY id_hal DESC LIMIT $start, $perPage&quot;;
$result2 = mysqli_query($link, $articles);

$result = mysqli_query($link, &quot;SELECT * FROM page&quot;);
$total  = mysqli_num_rows($result);
$return[&#039;jml&#039;] = ceil($total/$perPage);
$return[&#039;query&#039;] = $articles;
return $return;  

} ?&gt;

&lt;?php

$pages = pagination($_GET["halaman"]); $q2 = mysqli_query($link, $pages['query']); while($row = mysqli_fetch_assoc($q2)) { ?&gt; &lt;p&gt; &lt;?=$row["Nama"];?&gt; &lt;/p&gt; &lt;br&gt; &lt;?php }?&gt;

&lt;div class = ""&gt; &lt;?php for($i = 1; $i&lt;=$pages['jml']; $i++) {?&gt; &lt;a href="?halaman=&lt;?=$i?&gt;"&gt; &lt;?=$i?&gt; &lt;/a&gt; &lt;?php } ?&gt; &lt;/div&gt; </pre>

avatar dianarifr
@dianarifr

642 Kontribusi 316 Poin

Dipost 6 tahun yang lalu

Makasih gan

avatar fachrimubarak
@fachrimubarak

12 Kontribusi 1 Poin

Dipost 6 tahun yang lalu

gan, @dianarifr

<pre> Notice: Undefined index: halaman in C:\xampp\htdocs\lrn_blog\index.php on line 12 </pre>

kok variabel ini gak jalna ya?

<pre> $page = isset($hal) ? (int)$hal : 1 ; </pre>

itu yang agan kirimin saya, sya kurang paham. Kalau boleh minta kontak agan, ana pengen belajar banyak gan. Terima kasih sebelumnya kalau berkenan gan!

avatar fachrimubarak
@fachrimubarak

12 Kontribusi 1 Poin

Dipost 6 tahun yang lalu

bisa email ke dianarifr@gmail.com gan coba ini <pre> &lt;?php $host = 'localhost'; $user = 'root'; $pass = ''; $db = 'lrn_page';

$link = mysqli_connect($host, $user, $pass, $db) or die(mysqli_error($link));

function pagination($hal){ global $link;

$perPage    = 3;//
$page       = (!empty($hal) ? (int)$hal : 1);
$start      = ($page &amp;gt; 1) ? ($page*$perPage)-$perPage : 0;

$articles = &quot;SELECT * FROM page ORDER BY id_hal DESC LIMIT $start, $perPage&quot;;
$result2 = mysqli_query($link, $articles);

$result = mysqli_query($link, &quot;SELECT * FROM page&quot;);
$total  = mysqli_num_rows($result);
$return[&#039;jml&#039;] = ceil($total/$perPage);
$return[&#039;query&#039;] = $articles;
return $return;  

} ?&gt;

&lt;?php if(isset($_GET["halaman"])) $pages = pagination($_GET["halaman"]); $q2 = mysqli_query($link, $pages['query']); while($row = mysqli_fetch_assoc($q2)) { ?&gt; &lt;p&gt; &lt;?=$row["Nama"];?&gt; &lt;/p&gt; &lt;br&gt; &lt;?php }?&gt;

&lt;div class = ""&gt; &lt;?php for($i = 1; $i&lt;=$pages['jml']; $i++) {?&gt; &lt;a href="?halaman=&lt;?=$i?&gt;"&gt; &lt;?=$i?&gt; &lt;/a&gt; &lt;?php } ?&gt; &lt;/div&gt; </pre>

avatar dianarifr
@dianarifr

642 Kontribusi 316 Poin

Dipost 6 tahun yang lalu

Login untuk ikut Jawaban