Selasa, 01 Mei 2012 - 20:20:20 WIB
Teknik Looping Didalam Looping Disertai Studi Kasusnya
Diposting oleh : Wiro Sableng
Kategori: Teknologi - Dibaca: 560032 kali

Dalam logika pemrograman, looping (perulangan) digunakan untuk mengulang suatu proses. Namun, dalam tutorial kali ini, saya tidak akan membahas dasar-dasar looping (perulangan), jadi saya langsung aja ke permasalahannya mengapa diperlukan looping didalam looping? Dalam kasus tertentu, looping saja tidaklah cukup, misalnya membuat rekap data yang kompleks dari beberapa tabel dan menampilkan masing-masing berita dalam suatu kategori. Dalam kedua kasus tersebut diperlukan looping didalam looping (while didalam while).

Agar tidak tambah bingung, saya akan berikan contohnya secara bertahap, biasanya dalam menampilkan data dalam suatu tabel kategori hanya diperlukan satu looping, contoh skripnya sebagai berikut:

<?php
include "koneksi.php";

echo "<h3>Daftar Kategori</h3>";

$kategori=mysql_query("select * from kategori");

while($k=mysql_fetch_array($kategori)){
  echo "<li>$k[nama_kategori]</li>";
}
?>

Apabila dijalankan di browser, maka hasilnya dapat dilihat pada gambar berikut:

Selanjutnya, kita akan menampilkan beberapa berita didalam masing-masing kategori. Logikanya, setelah looping (while) untuk menampilkan kategori, maka perlu diselipkan satu looping lagi untuk menampilkan berita didalam looping kategori. Dan tentu saja, berita yang tampil haruslah berhubungan dengan kategorinya.

Untuk itu, coba perhatikan dulu relasi antara tabel kategori dengan tabel berita pada gambar berikut:

Kemudian baru kita bikin skripnya sebagai berikut:

<?php
include "koneksi.php";

$kategori=mysql_query("select * from kategori");

// looping untuk menampilkan nama kategori
while($k=mysql_fetch_array($kategori)){
  echo "<h3>$k[nama_kategori]</h3>";
  
  $berita=mysql_query("select * from 
  kategori, berita 
  where kategori.id_kategori=berita.id_kategori 
  and berita.id_kategori='$k[id_kategori]'");

// looping untuk menampilkan judul berita 
// yang berhubungan dengan kategori masing-masing
  while($b=mysql_fetch_array($berita)){
    echo "<li>$b[judul]</li>";
  } // end while looping berita
} // end while looping kategori
?>

Sekarang coba jalankan di browser, maka hasilnya dapat dilihat pada gambar berikut:

Saya juga telah mengimplementasikan teknik looping didalam looping di CMS Lokomedia pada templates eL jQuery ala Yahoo, silahkan buka file kiri.php pada folder templates/eljquery-yahoo. Hasilnya dapat dilihat pada gambar berikut:

Oke, demikianlah sekilas kegunaan teknik looping dalam looping. Untuk mendownload skrip dan database latihannya, silahkan Klik Disini.




27422 Komentar :

weight loss goals
15 Maret 2019 - 06:14:52 WIB

For most recent information you have to pay
a quick visit web and on web I found this web page as a best website for most recent updates.
안전한놀이터
15 Maret 2019 - 06:26:27 WIB

When someone writes an paragraph he/she retains the idea of a
user in his/her mind that how a user can understand it.
Therefore that's why this post is outstdanding. Thanks!
Rodrigo
15 Maret 2019 - 06:36:46 WIB

I know this weebsitе gives quality based articⅼes aand additіߋnal stuff, is there any other websіte whiсh provides these kinds of information in quality?
안전놀이터추천
15 Maret 2019 - 06:41:28 WIB

I was suggested this blog by my cousin. I'm not
sure whether this post is written by him as no one else know such
detailed about my trouble. You're amazing! Thanks!
https://www.floridareviews.co/
15 Maret 2019 - 06:45:38 WIB

Hi there just wanted to give you a quick heads up. The text in your
post seem to be running off the screen in Ie. I'm not sure if this
is a formatting issue or something to do with web browser compatibility but I thought I'd post
to let you know. The design and style look great though! Hope
you get the problem fixed soon. Kudos
http://www.heyttu.com/wiki/Index.php?title=world_cup_trophy_broken_throughout_german_celebration
15 Maret 2019 - 06:49:36 WIB

Superdry men's Germany Trophy Sequence t-shirt.
안전토토사이트
15 Maret 2019 - 06:54:31 WIB

Today, I went to the beach with my children. I found a sea shell and
gave it to my 4 year old daughter and said "You can hear the ocean if you put this to your ear." She placed the shell to her ear and screamed.
There was a hermit crab inside and it pinched her ear.
She never wants to go back! LoL I know this is completely off topic but I had to
tell someone!
오바마카지노
15 Maret 2019 - 06:54:36 WIB

Whats up very cool site!! Guy .. Beautiful ..
Wonderful .. I'll bookmark your site and take the feeds also?
I'm satisfied to seek out a lot of useful info here within the submit, we need work out more techniques in this regard, thanks for sharing.
. . . . .
zcash
15 Maret 2019 - 06:56:37 WIB

I аm actually happy to read thіs web site poosts which
contains tons of valuable іnformation, thanks for providing
these kinds of data.
https://www.reuters.com/
15 Maret 2019 - 07:12:45 WIB

obviously like your website but you need to test the spelling on quite a few of your posts.
A number of them are rife with spelling problems and I find it
very bothersome to inform the reality however I'll surely come again again.
<< First | < Prev | ... 1551 | 1552 | 1553 | 1554 | 1555 | ... | 2743 | Next > | Last >>
Isi Komentar :
Nama :
Website :
Komentar
 
 (Masukkan 6 kode diatas)