Using csv format such as (Mcdonalds, cheeseburger), (Taco Bell, Beef Softshell),
ID: 2247051 • Letter: U
Question
Using csv format such as (Mcdonalds, cheeseburger), (Taco Bell, Beef Softshell), create a page that has a place for a user to type. As the user is typing, a list of restaurant names should be shown to the user. The results should include any restaurant whose name starts with the letters the user has typed so far and any restaurant whose cuisine starts with the letters typed so far. The page should be written in PHP and jQuery.
EDIT: The data will just be a text file in csv format RESTAURANT NAME, FOOD ITEM
Explanation / Answer
CREATE TABLE IF NOT EXISTS `Item`` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`Item_name` varchar(255) NOT NULL COMMENT 'Item name',
`Item_salary` double NOT NULL COMMENT 'Item Price',
`Item_age` int(11) NOT NULL COMMENT 'Item quantity',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='datatable demo table' AUTO_INCREMENT=58 ;
INSERT INTO `Item` (`id`, `Item_name`, `Item_salary`, `Item_age`) VALUES
(1, 'Tiger Nixon', 320800, 61),
(2, 'Garrett Winters', 170750, 63),
(3, 'Ashton Cox', 86000, 66),
(4, 'Cedric Kelly', 433060, 22),
(5, 'Airi Satou', 162700, 33),
(6, 'Brielle Williamson', 372000, 61),
(7, 'Herrod Chandler', 137500, 59),
(8, 'Rhona Davidson', 327900, 55),
(9, 'Colleen Hurst', 205500, 39),
(10, 'Sonya Frost', 103600, 23);
<div class="well well-sm col-md-2 pull-right">
<input class="btn btn-primary center-block" type="button" id="export-to-csv" name="export-to-csv" value="Export To CSV" />
</div>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" id="export-form">
<input type="hidden" value='export-to-csv' id='hidden-type' name='ExportType'/>
</form>
<table id="Item_grid"class="table table-striped table-bordered" width="100%" cellspacing="0">
<thead>
<tr>
<th>Emp Id</th>
<th>Emp Name</th>
<th>Salary</th>
<th>Age</th
></tr>
</thead>
<?php
$sql = "SELECT id, Item_name, Item_salary, Item_age FROM Item";
$resultset = mysqli_query($conn, $sql) or die("database error:". mysqli_error($conn));
?>
<tfoot>
<?php
while( $rows = mysqli_fetch_assoc($resultset) ) {
?> <tr>
<th><?php echo $rows["id"]; ?></th>
<th><?php echo $rows["Item_name"]; ?></th>
<th><?php echo $rows["Item_Price"]; ?></th>
<th><?php echo $rows["Item_Quantity"]; ?></th>
</tr>
<?php } ?>
</tfoot>
</table>
<?php
function exportCSV($resultset) {
$fh = fopen( 'php://output', 'w' );
$heading = false;
while( $rows = mysqli_fetch_assoc($resultset) ) {
if(!$heading) {
fputcsv($fh, array_keys($rows));
$heading = true;
}
fputcsv($fh, $rows);
}
fclose($fh);
}
?>
$(document).ready(function() {
jQuery('#export-to-csv').bind("click", function() {
var target = $(this).attr('id');
switch(target) {
case 'export-to-csv' :
$('#hidden-type').val(target);
$('#export-form').submit();
$('#hidden-type').val('');
break
}
});
});
if(isset($_POST["ExportType"])){
switch($_POST["ExportType"]){
case "export-to-csv" :
$filename = $_POST["ExportType"] . ".csv";
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename="$filename"");
$sql = "SELECT id, employee_name, employee_salary, employee_age FROM employee";
$resultset = mysqli_query($conn, $sql) or die("database error:". mysqli_error($conn));
// function to export data to CSV
exportCSV($resultset);
exit();
default :
die("Unknown action : ".$_POST["action"]);
break;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.