Postingan lainnya
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.
4 Jawaban:
Jawaban Terpilih
kasarannya gini gan <pre> <?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 &gt; 1) ? ($page*$perPage)-$perPage : 0;
$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);
$return['jml'] = ceil($total/$perPage);
$return['query'] = $articles;
return $return;
} ?>
<?php
$pages = pagination($_GET["halaman"]); $q2 = mysqli_query($link, $pages['query']); while($row = mysqli_fetch_assoc($q2)) { ?> <p> <?=$row["Nama"];?> </p> <br> <?php }?>
<div class = ""> <?php for($i = 1; $i<=$pages['jml']; $i++) {?> <a href="?halaman=<?=$i?>"> <?=$i?> </a> <?php } ?> </div> </pre>
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!
bisa email ke dianarifr@gmail.com gan coba ini <pre> <?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 &gt; 1) ? ($page*$perPage)-$perPage : 0;
$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);
$return['jml'] = ceil($total/$perPage);
$return['query'] = $articles;
return $return;
} ?>
<?php if(isset($_GET["halaman"])) $pages = pagination($_GET["halaman"]); $q2 = mysqli_query($link, $pages['query']); while($row = mysqli_fetch_assoc($q2)) { ?> <p> <?=$row["Nama"];?> </p> <br> <?php }?>
<div class = ""> <?php for($i = 1; $i<=$pages['jml']; $i++) {?> <a href="?halaman=<?=$i?>"> <?=$i?> </a> <?php } ?> </div> </pre>