Query dalam query

Tempat diskusi tentang PHP buat newbie
kuswaradih
Posts: 1
Joined: Wed Aug 10, 2016 3:30 am

Query dalam query

Postby kuswaradih » Wed Aug 10, 2016 3:35 am

Selamat pagi para suhu, mau tanya nih cara membuat query dalam query di php, agar web tidak jadi down. soalnya selama ini web saya setelah beberapa bulan suka jadi down atau web site busy, dimana entry process nya jadi full 45/45.

berikut isi dari php yg saya buat :

<?php
include_once "/home/olknfkom/public_html/php/koneksi.php";

if(isset($_GET['kode']))
{
$kode=str_replace(" ","%",$_GET['kode']).'%';
$kode2=str_replace(" ","%",$_GET['kode']);
}
else
{
$kode='%';
$kode2='%';
}
$start=0;
$limit=10;

if(isset($_GET['id']))
{
$id=$_GET['id'];
$start=($id-1)*$limit;
}
else
{
$id=1;
$start=($id-1)*$limit;
}

$query=mysql_query("SELECT i.no_urut AS Urut,CONCAT(CONCAT(LEFT(b.kode_barang,LENGTH(b.kode_barang)-4),'%20',IF(SUBSTRING(RIGHT(b.kode_barang,4),1,1)='0',RIGHT(b.kode_barang,3),RIGHT(b.kode_barang,4)))) AS Gambar,CONCAT(LEFT(b.kode_barang,LENGTH(b.kode_barang)-4),' ',IF(SUBSTRING(RIGHT(b.kode_barang,4),1,1)='0',RIGHT(b.kode_barang,3),RIGHT(b.kode_barang,4))) AS Kode,w.ket AS Warna,h.ket AS Bahan,CONCAT(g.berat,' gr') AS Berat,b.kode_barang AS KodeBrg
FROM barang g,barang_detail b,size s,warna w,bahan_barang h,catalog_isi i
WHERE SUBSTRING(b.barcode,12,2)=s.kode_barcode AND SUBSTRING(b.barcode,10,2)=w.kode_barcode AND SUBSTRING(b.barcode,2,1)='0' AND g.kode=b.kode_barang AND g.kode_bahan=h.kode_bahan AND CONCAT(LEFT(b.kode_barang,LENGTH(b.kode_barang)-4),' ',IF(SUBSTRING(RIGHT(b.kode_barang,4),1,1)='0',RIGHT(b.kode_barang,3),RIGHT(b.kode_barang,4))) LIKE '$kode' AND i.barcode_barang=b.barcode AND i.barcode_catalog='25'
GROUP BY g.kode,w.ket
ORDER BY i.no_urut
LIMIT $start,$limit");

while($query2=mysql_fetch_array($query))
{
echo "<div id='fr-gbr'>";
echo "<div id='fr-gbr-produk'>";
echo "<a class='group2' href='images/kode/large/".$query2['Gambar'].".jpg' title='".$query2['Kode']." - ".$query2['Warna']." - ".$query2['Bahan']."'><img class='gbr' src='images/kode/small/".$query2['Gambar'].".jpg' alt='Catenzo ".$query2['Kode']."' /></a>";
echo "</div>";
echo "</div>";
echo "<div id='fr-ket-gbr'>";
echo "<table>";
echo "<tr>";
echo "<td class='label'>No. Urut</td><td class='titikdua'>:</td><td>".$query2['Urut']."</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='label'>Kode</td><td class='titikdua'>:</td><td>".$query2['Kode']."</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='label'>Warna</td><td class='titikdua'>:</td><td>".$query2['Warna']."</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='label'>Bahan</td><td class='titikdua'>:</td><td>".$query2['Bahan']."</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='label'>Berat</td><td class='titikdua'>:</td><td>".$query2['Berat']."</td>";
echo "</tr>";
echo "</table>";
echo "</div>";
echo "<div id='fr-stok'>";
echo "<table id='t01'>";
echo "<tr>";
echo "<th>Size</th><th>Stok</th><th>Pesanan</th><th>Last Update</th>";
echo "</tr>";

$sql='SELECT s.ukuran AS Size,b.stok AS Stok,b.pesanan AS Pesanan,DATE_FORMAT(b.last_update,"%d %b %y, %H:%i:%s") AS Info
FROM barang_detail b,size s,catalog_isi i
WHERE SUBSTRING(b.barcode,12,2)=s.kode_barcode AND b.kode_barang="'.$query2["KodeBrg"].'" AND b.barcode=i.barcode_barang AND i.barcode_catalog="25"
ORDER BY s.kode_barcode';

$query3=mysql_query($sql);
while($query4=mysql_fetch_array($query3))
{
echo "<tr>";
echo "<td>".$query4['Size']."</td><td>".$query4['Stok']."</td><td>".$query4['Pesanan']."</td><td>".$query4['Info']."</td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
}

$query=mysql_query("SELECT DISTINCT(kode_barang)
FROM barang_detail b,catalog_isi i
WHERE SUBSTRING(b.barcode,2,1)='0' AND CONCAT(LEFT(b.kode_barang,LENGTH(b.kode_barang)-4),' ',IF(SUBSTRING(RIGHT(b.kode_barang,4),1,1)='0',RIGHT(b.kode_barang,3),RIGHT(b.kode_barang,4))) LIKE '$kode' AND i.barcode_barang=b.barcode AND i.barcode_catalog='25'");

$rows=mysql_num_rows($query);
$total=ceil($rows/$limit);
mysql_close($koneksi);

echo "<div>";
if ($total>0)
{
if($id>1)
{
echo "<a href='stok.php?id=".($id-1)."&amp;kode=$kode2' class='button'>Previous</a>";
}
if($id!=$total)
{
echo "<a href='stok.php?id=".($id+1)."&amp;kode=$kode2' class='button'>Next</a>";
}

echo "<ul class='page'>";
$adjacents = 3;
$lpm1 = $total - 1;
if ($total < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($i = 1; $i <= $total; $i++)
{
if ($i == $id)
{ echo "<li class='current'>".$i."</li>"; }
else
{ echo "<li><a href='stok.php?id=".$i."&amp;kode=$kode2'>".$i."</a></li>"; }
}
}
elseif($total > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if($id < 1 + ($adjacents * 2))
{
for ($i = 1; $i < 4 + ($adjacents * 2); $i++)
{
if ($i == $id)
{ echo "<li class='current'>".$i."</li>"; }
else
{ echo "<li><a href='stok.php?id=".$i."&amp;kode=$kode2'>".$i."</a></li>"; }
}
echo "<li>...</li>";
echo "<li><a href='stok.php?id=".$lpm1."&amp;kode=$kode2'>".$lpm1."</a></li>";
echo "<li><a href='stok.php?id=".$total."&amp;kode=$kode2'>".$total."</a></li>";
}
//in middle; hide some front and some back
elseif($total - ($adjacents * 2) > $id && $id > ($adjacents * 2))
{
echo "<li><a href='stok.php?id=1&amp;kode=$kode2'>1</a></li>";
echo "<li><a href='stok.php?id=2&amp;kode=$kode2'>2</a></li>";
echo "<li>...</li>";
for ($i = $id - $adjacents; $i <= $id + $adjacents; $i++)
{
if ($i == $id)
{ echo "<li class='current'>".$i."</li>"; }
else
{ echo "<li><a href='stok.php?id=".$i."&amp;kode=$kode2'>".$i."</a></li>"; }
}
echo "<li>...</li>";
echo "<li><a href='stok.php?id=".$lpm1."&amp;kode=$kode2'>".$lpm1."</a></li>";
echo "<li><a href='stok.php?id=".$total."&amp;kode=$kode2'>".$total."</a></li>";
}
//close to end; only hide early pages
else
{
echo "<li><a href='stok.php?id=1&amp;kode=$kode2'>1</a></li>";
echo "<li><a href='stok.php?id=2&amp;kode=$kode2'>2</a></li>";
echo "<li>...</li>";
for ($i = $total - (2 + ($adjacents * 2)); $i <= $total; $i++)
{
if ($i == $id)
{ echo "<li class='current'>".$i."</li>"; }
else
{ echo "<li><a href='stok.php?id=".$i."&amp;kode=$kode2'>".$i."</a></li>"; }
}
}
}
echo "</ul>";
}
else
{
if ($kode2=='%')
echo "<p>Belum ada barang.</p>";
else
echo "<p>Barang dengan kode $kode2 tidak ada.</p>";
}
echo "</div>";
?>

Return to “Diskusi PHP”

Who is online

Users browsing this forum: No registered users and 0 guests