Skip to content

Instantly share code, notes, and snippets.

@amin007
Last active May 22, 2022 06:39
Show Gist options
  • Save amin007/cdee54b8254344445377ce53bb6ce2b5 to your computer and use it in GitHub Desktop.
Save amin007/cdee54b8254344445377ce53bb6ce2b5 to your computer and use it in GitHub Desktop.
cara buat mysqli yang simple sangat
<?php
include 'tatarajah.php';
$search = isset($_GET['search']) ? $_GET['search'] : '';
$rd = "\r\t";
#--------------------------------------------------------------------------------------------------
$conn = mysqli_connect($servername, $dbusername, $dbpassword, $dbname);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}/*
if($conn—>connect_error)
{
die("Connection failed: ". $conn->connect_error);
}//*/
$sql = "select item_pic, item_name, item_quantity from item where item_name Like '%$search%' ";
$result = $conn->query($sql);
#--------------------------------------------------------------------------------------------------
diatas();
#--------------------------------------------------------------------------------------------------
if ($result->num_rows > 0)
{
while ($rows = $result->fetch_assoc())
{
echo "$rd<table border=1 class='table bg-white rounded shadow-sm table-hover'>";
echo "$rd<tr align='center'>";
echo "$rd<th>Food Donation Photo</font></th>";
echo "$rd<th>Food Name</font></th>";
echo "$rd<th>Food Quantity</font> </tho";
echo "$rd<th>Action</font></th>";
echo "$rd</tr>";
echo "$rd<tr>";
echo "$rd<td><img src='img/".$rows['item_pic']."' height='200' width='200'><br></td>";
echo "$rd<td>".$rows['item_name']."<br></td>";
echo "$rd<td>".$rows['item_quantity']."<br></td>";
echo "$rd<td><a href='request_form.php'>Request Food</a></td>";
echo "$rd</tr>";
/*while ($get_info = mysqli_fetch_row($result))
{
print "$rd<tr>";
foreach ($get_info as $field)
print "$rd<td><font face=georgia/>$field</font></td>\n";
print "$rd</tr>\n";
}//*/#endwhile
print "$rd</table>\n";
}#endwhile
};# endif
#--------------------------------------------------------------------------------------------------
$conn->close();
#--------------------------------------------------------------------------------------------------
dibawah();
#--------------------------------------------------------------------------------------------------
#contoh fungsi
#--------------------------------------------------------------------------------------------------
function senaraiCss($pilih = 0)
{
$papar[] = array(
'https://use.fontawesome.com/releases/v5.11.2/css/all.css',
//'https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css',
'https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css'
);
$papar[] = array(
'https://use.fontawesome.com/releases/v5.11.2/css/all.css',
'https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css',
);
return $papar[$pilih];
}
#--------------------------------------------------------------------------------------------------
function ulangCss($listCss)
{
$style = null;
if (isset($listCss) && $listCss != null)
{
foreach ($listCss as $css)
{
$style .= "\n" . '<link rel="stylesheet" type="text/css" href="' . $css . '">';
}
}
return $style;
}
#--------------------------------------------------------------------------------------------------
function metaList()
{
$k01 = 'width=device-width, initial-scale=1,';
$p[] = '<meta charset="utf-8">';
$p[] = '<meta name="viewport" content="' . $k01 . ' shrink-to-fit=no">';
//$p[] = '<meta name="viewport" content="' . $k01 . ' maximum-scale=1">';
$p[] = '<meta name="description" content="">';
//$p[] = '<meta name="keywords" content="derma,Crownfunding,">';
$p[] = '<meta name="author" content="Amin007">';
//$p[] = '';
return $p;
}
#--------------------------------------------------------------------------------------------------
function ulangMeta($listMeta)
{
$p = null;
if (isset($listMeta) && $listMeta != null)
{
foreach ($listMeta as $meta)
{
$p .= "\n" . $meta;
}
}
return $p;
}
#--------------------------------------------------------------------------------------------------
function diatas($title = 'List Folder')
{
$meta = ulangMeta(metaList());
$style = ulangCss(senaraiCss(1));
print <<<END
<!doctype html>
<html lang="en">
<head>$meta
<title>$title</title>$style
<style type="text/css">
table.excel {
border-style:ridge;
border-width:1;
border-collapse:collapse;
font-family:sans-serif;
font-size:11px;
}
table.excel thead th, table.excel tbody th {
background:#cccccc;
border-style:ridge;
border-width:1;
text-align: center;
vertical-align: top;
}
table.excel tbody th { text-align:center; vertical-align: top; }
table.excel tbody td { vertical-align:bottom; }
table.excel tbody td
{
padding: 0 3px; border: 1px solid #aaaaaa;
background:#ffffff;
}
</style>
</head>
<body>
END;
}
#--------------------------------------------------------------------------------------------------
function dibawah()
{
$jsDaa = ulangJS(listJS());
print <<<END
<!-- Footer
=============================================================================================== -->
<!-- footer class="footer">
<div class="container">
<span class="label label-info">
&copy; Hak Cipta Terperihara 2016. Theme Asal Bootstrap Twitter
</span>
</div>
</footer -->
<!-- khas untuk jquery dan js2 lain
=============================================================================================== -->
$jsDaa
</body>
</html>
END;
}
#--------------------------------------------------------------------------------------------------
function listJS($pilih = 'B')
{
$pA[] = 'https://code.jquery.com/jquery-3.3.1.slim.min.js';
$pA[] = 'https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js';
$pA[] = 'https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js';
//$pA[] = 'https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js';
$pB[] = 'https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/js/bootstrap.bundle.min.js';
# pilih tatasusunan
$p = ($pilih === 'B') ? $pB : $pA;
return $p;
}
#--------------------------------------------------------------------------------------------------
function ulangJS($listJS)
{
$p = null;
if (isset($listJS) && $listJS != null)
{
foreach ($listJS as $js)
{
$p .= '<script type="text/javascript" src="' . $js . '"></script>' . "\n";
}
}
return $p;
}
#--------------------------------------------------------------------------------------------------
if ( ! function_exists('paparJadual')):
function paparJadual($row,$jadual)
{
$output = null;
$bil_baris = count($row);
$printed_headers = false;# mula bina jadual
#-----------------------------------------------------------------
for ($kira=0; $kira < $bil_baris; $kira++)
{ # print the headers once:
if ( !$printed_headers )
{##===========================================================
$output .= "\r\t<thead><tr>";
foreach ( array_keys($row[$kira]) as $kunci ) :
$tajuk = tajukMedanJadual($jadual,$kunci);
$output .= "\r\t" . '<th>' . $tajuk . '</th>';
endforeach;
$output .= "\r\t" . '</tr></thead>';
$output .= "\r\t" . '<tbody>';
##============================================================
$printed_headers = true;
}
#-----------------------------------------------------------------
# print the data row
$output .= "\r\t<tr>";
foreach ( $row[$kira] as $key=>$data ) :
$output .= "\r\t" . '<td>' . $data . '</td>';
endforeach;
$output .= "\r\t" . '</tr></tbody>';
}#----------------------------------------------------------------
return $output;
}
endif;
#--------------------------------------------------------------------------------------------------
/* contoh database daa
-- Adminer 4.8.1 MySQL 5.7.19 dump
SET NAMES utf8;
SET time_zone = '+00:00';
SET foreign_key_checks = 0;
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';
DROP TABLE IF EXISTS `item`;
CREATE TABLE `item` (
`item_id` int(11) NOT NULL AUTO_INCREMENT,
`item_pic` varchar(255) NOT NULL,
`item_name` varchar(100) NOT NULL,
`item_quantity` int(11) NOT NULL,
PRIMARY KEY (`item_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `item` (`item_id`, `item_pic`, `item_name`, `item_quantity`) VALUES
(1, 'gambar001', 'gambar001', 5),
(2, 'gambar002', 'gambar002', 10);
-- 2022-05-22 06:37:43//*/
#--------------------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment