Selasa, 01 Mei 2012 - 20:20:20 WIB
Teknik Looping Didalam Looping Disertai Studi Kasusnya
Diposting oleh : Wiro Sableng
Kategori: Teknologi - Dibaca: 29237 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.




1579 Komentar :

click this link.
23 Maret 2019 - 17:27:10 WIB

Superb post however , I was wanting to know if you could write a litte more on this subject?

I'd be very thankful if you could elaborate a little bit further.
Appreciate it!
Simple Super Forskolin Extract
23 Maret 2019 - 17:33:43 WIB

I leave a response whenever I like a post on a website
or if I have something to add to the discussion. It is caused by the fire communicated in the post
I looked at. And on this article Teknik Looping Didalam Looping
Disertai Studi Kasusnya. I was excited enough to create a comment ;) I do have a couple
of questions for you if you don't mind. Could it be simply me or do a few of
these responses appear like they are written by brain dead folks?
:-P And, if you are posting at other online social sites, I'd like
to follow you. Could you make a list all of all
your social sites like your twitter feed, Facebook page
or linkedin profile?
Keto Burn XP - Getting You Into Ketosis Faster? | Review
23 Maret 2019 - 18:55:49 WIB

Of course, what a great website and informative posts, I will bookmark your site.Best Regards!
Keto Rev Pills
23 Maret 2019 - 19:29:23 WIB

I like the efforts you have put in this, thanks for all the
great articles.
Neustyle Body Diet
23 Maret 2019 - 20:59:45 WIB

Hey! Quick question that's entirely off topic. Do you know how to make your site mobile friendly?
My web site looks weird when browsing from my iphone 4.
I'm trying to find a theme or plugin that might be able to resolve
this issue. If you have any suggestions, please share.

Appreciate it!
https://ketoburnxp.org/
23 Maret 2019 - 22:01:56 WIB

Do you have a spam problem on this website; I also am a blogger, and I was wondering your situation;
we have created some nice procedures and we are looking to exchange methods with other folks, be sure to shoot me an e-mail if interested.
water.weather.gov
23 Maret 2019 - 22:18:56 WIB

Just what I was searching for, regards for posting.
Digestive Freedom Plus
23 Maret 2019 - 22:39:10 WIB

Hey! I understand this is sort of off-topic
but I needed to ask. Does operating a well-established
website such as yours require a massive amount work?
I am completely new to blogging but I do write in my
diary daily. I'd like to start a blog so I will be able to share my own experience and thoughts
online. Please let me know if you have any kind of suggestions or tips for new aspiring blog
owners. Thankyou!
canada pharmacies
24 Maret 2019 - 00:51:11 WIB

Awesome things here. I am very glad to see your article.
Thanks so much and I'm having a look forward
to contact you. Will you please drop me a mail?
Neustyle Body Forskolin Review
24 Maret 2019 - 02:43:20 WIB

I do not even know how I stopped up here, but I believed this post was once
great. I don't know who you might be but certainly you are going to a famous blogger for those who aren't already ;) Cheers!
<< First | < Prev | ... 128 | 129 | 130 | 131 | 132 | ... | 158 | Next > | Last >>
Isi Komentar :
Nama :
Website :
Komentar
 
 (Masukkan 6 kode diatas)