Originally Posted by
Aevis
Nice! You can further reduce your script if you remove all the elseif for the similar multidimensional arrays (genre, publisher, rarity) and change the first else if (console) from
Code:
else if ($key == 'console')
to
Code:
else if(in_array($key, array('console','genre','publisher','rarity')))
awesome thank you. Finally got the date working! so happy. Wasnt able to figure out a way to do between but got it working with > and <. Right now I only have a few issues left to fix
1) I cant do combinations of searches any more. When I run the query the last array i run is replaceing all the arrays. So for instance if i search nintendo, super nintendo, with a release year between 1980 and 1990 My query output looks like this:
Code:
array(2) { [0]=> string(4) "1980" [1]=> string(4) "1990" } SELECT rowid, title, console, genre, publisher, releaseyear, rarity, details, image FROM Games WHERE console = ? OR console = ? AND releaseYear > ? AND releaseYear < ?
UPDATE: Looks like it is something to do with $param. If using title or the else statement it works fine. I belive its because its not storing in an array (both of which use $param[] = $value instead of just $param = $value)
so its trying to say that where console = 1980 or console = 1990. Not really sure how to fix this
2) it is forcing me to use the release year fields now for some reason. If I don't fill in the release year blanks i get the following output.
Code:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1 near "releaseYear": syntax error' in E:\XAMPP\htdocs\NesList-CSS\Slider\database.php:87 Stack trace: #0 E:\XAMPP\htdocs\NesList-CSS\Slider\database.php(87): PDO->prepare('SELECT rowid, t...') #1 {main} thrown in E:\XAMPP\htdocs\NesList-CSS\Slider\database.php on line 87
3) I need to be able to search by pieces of the title. For instance if I search Super Bros it should actually search for title like "super" and title like "bros" instead of title like "super bros", I'm just not really sure how to break the string into diffrent parts.
Current code:
Code:
<html>
<?php
// Specify your sqlite database name and path
$dir = 'sqlite:GameCollection';
// Instantiate PDO connection object and failure msg
$dbh = new PDO($dir) or die('cannot open database');
// Set PDO error handling to throw exceptions
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Define your SQL statement, myTable = table name in your DB
$sql = 'SELECT rowid, title, console, genre, publisher, releaseyear, rarity, details, image FROM Games';
// If there is no POST data, create empty $param array
if (empty($_POST)) { $param = array(); }
// Append POST data dynamically to the query
else {
//Reset counter
$i = 0;
//Column whitelist
$columns = array('title', 'console', 'genre', 'publisher', 'dateRange', 'releaseYear', 'rarity',);
//Loop over the $_POST["game"] array
foreach($_POST["game"] as $key => $value) {
//Check if key value is a proper column name
if(in_array($key, $columns)) {
//Only add condition if it's not empty
if(!empty($value)) {
//If at first loop, use WHERE, otherwise AND
if($i == 0) { $sql .= " WHERE "; }
else { $sql .= " AND "; }
//SQL LIKE syntax for title
if($key == 'title'){
$sql .= "$key LIKE ?";
$param[] = "%{$value}%";
}
//Console is a multidimensional array due to multiple checkboxes
else if(in_array($key, array('console','genre','publisher','rarity'))) {
foreach($value as $placeholderName) {
$sql .= "$key = ?";
if($placeholderName != end($value)) { $sql .= " OR "; }
}
$param = array_values($value);
}
//handles the release year statments
else if ($key == 'releaseYear') {
$a = 0;
foreach($value as $releaseName) {
if($a == 0) {
$sql .= "$key > ?";
$a++;
}
else { $sql .= "$key < ?"; }
if($releaseName != end ($value)) {
$sql .= " AND ";
}
}
$param = array_values($value);
}
//Standard SQL = syntax
else {
$sql .= "$key = ?";
$param[] = "$value";
}
//Increase counter
$i++;
}
}
else { exit("Error! $key is not in whitelist"); }
}
}
//Prepare the query
$sth = $dbh->prepare($sql);
//Execute the query with the place holders populated by the data from the array
$sth->execute($param);
// Fetch the results //
$rowset = $sth->fetchAll(PDO::FETCH_ASSOC);
var_dump($param);
echo $sql;
?>
<head>
<title>NES Collection</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<link id="pagestyle" href="style.css" rel="stylesheet" type="text/css">
<script type="text/javascript">
$(function() {
$("#startdate").datepicker({ dateFormat: "yy" }).val()
$("#enddate").datepicker({ dateFormat: "yy" }).val()
});
</script>
<body>
<div id="header">
<h1>Retro Game Collection</h1><br><br><br>
<a href = "database.php">Home</a>
</div>
<div id="leftNav">
<form action = "database.php" method = "post">
Search By: <br><br>
<b> Title: </b> <br> <input type="Text" name="game[title]"> <br>
______________________<br>
<script>
$(document).ready(function() {
$('#selecctall-1').click(function(event) { //on click
if(this.checked) { // check select status
$('.checkbox1').each(function() { //loop through each checkbox
this.checked = true; //select all checkboxes with class "checkbox1"
});
}else{
$('.checkbox1').each(function() { //loop through each checkbox
this.checked = false; //deselect all checkboxes with class "checkbox1"
});
}
});
});
</script>
<script>
$(document).ready(function() {
$('#selecctall-2').click(function(event) { //on click
if(this.checked) { // check select status
$('.checkbox2').each(function() { //loop through each checkbox
this.checked = true; //select all checkboxes with class "checkbox1"
});
}else{
$('.checkbox2').each(function() { //loop through each checkbox
this.checked = false; //deselect all checkboxes with class "checkbox1"
});
}
});
});
</script>
<script>
$(document).ready(function() {
$('#selecctall-3').click(function(event) { //on click
if(this.checked) { // check select status
$('.checkbox3').each(function() { //loop through each checkbox
this.checked = true; //select all checkboxes with class "checkbox1"
});
}else{
$('.checkbox3').each(function() { //loop through each checkbox
this.checked = false; //deselect all checkboxes with class "checkbox1"
});
}
});
});
</script>
<script>
$(document).ready(function() {
$('#selecctall-4').click(function(event) { //on click
if(this.checked) { // check select status
$('.checkbox4').each(function() { //loop through each checkbox
this.checked = true; //select all checkboxes with class "checkbox1"
});
}else{
$('.checkbox4').each(function() { //loop through each checkbox
this.checked = false; //deselect all checkboxes with class "checkbox1"
});
}
});
});
</script>
<b>Console</b><br>
<input type="checkbox" id="selecctall-1"/> All <br>
<input class="checkbox1" type = "checkbox" name = "game[console][0]" value = "Nintendo"> Nintendo<br>
<input class="checkbox1" type = "checkbox" name = "game[console][1]" value = "Super Nintendo"> Super Nintendo<br>
<input class="checkbox1" type = "checkbox" name = "game[console][2]" value = "Nintendo 64"> Nintendo 64<br>
<input class="checkbox1" type = "checkbox" name = "game[console][3]" value = "Game Cube"> Game Cube<br>
<input class="checkbox1" type = "checkbox" name = "game[console][4]" value = "Playstation"> Playstation<br>
<input class="checkbox1" type = "checkbox" name = "game[console][5]" value = "Playstation2"> Playstation2<br>
<input class="checkbox1" type = "checkbox" name = "game[console][6]" value = "Xbox"> Xbox<br>
______________________<br>
<b>Genre:</b> <br>
<input type="checkbox" id="selecctall-2"/> All <br>
<input class="checkbox2" type = "checkbox" name = "game[genre][0]" value = "Action"> Action<br>
<input class="checkbox2" type = "checkbox" name = "game[genre][1]" value = "Beatup"> Beat'em Up<br>
<input class="checkbox2" type = "checkbox" name = "game[genre][2]" value = "Platform"> Platformer<br>
<input class="checkbox2" type = "checkbox" name = "game[genre][3]" value = "Shooter"> Shooter<br>
<input class="checkbox2" type = "checkbox" name = "game[genre][4]" value = "Adventure"> Adventure<br>
<input class="checkbox2" type = "checkbox" name = "game[genre][5]" value = "rpg"> Role-Playing<br>
<input class="checkbox2" type = "checkbox" name = "game[genre][6]" value = "Sim"> Simulation<br>
<input class="checkbox2" type = "checkbox" name = "game[genre][7]" value = "Strategy"> Strategy<br>
<input class="checkbox2" type = "checkbox" name = "game[genre][8]" value = "Sports"> Sports <br>
______________________<br>
<b>Publisher: </b> <br>
<input type="checkbox" id="selecctall-3"/> All <br>
<input class="checkbox3" type = "checkbox" name = "game[publisher][0]" value = "Aklaim"> Aklaim<br>
<input class="checkbox3" type = "checkbox" name = "game[publisher][1]" value = "Capcom"> Capcom<br>
<input class="checkbox3" type = "checkbox" name = "game[publisher][2]" value = "EA"> EA<br>
<input class="checkbox3" type = "checkbox" name = "game[publisher][3]" value = "KOEI"> KOEI<br>
<input class="checkbox3" type = "checkbox" name = "game[publisher][4]" value = "Konami"> Konami<br>
<input class="checkbox3" type = "checkbox" name = "game[publisher][5]" value = "Nintendo"> Nintendo<br>
<input class="checkbox3" type = "checkbox" name = "game[publisher][6]" value = "Rare"> Rare<br>
<input class="checkbox3" type = "checkbox" name = "game[publisher][7]" value = "SNK"> SNK<br>
<input class="checkbox3" type = "checkbox" name = "game[publisher][8]" value = "SunSoft"> SunSoft<br>
<input class="checkbox3" type = "checkbox" name = "game[publisher][9]" value = "Square Enix"> Square Enix<br>
<input class="checkbox3" type = "checkbox" name = "game[publisher][10]" value = "Tecmo"> Tecmo<br>
<input class="checkbox3" type = "checkbox" name = "game[publisher][11]" value = "TradeWest"> TradeWest<br>
<input class="checkbox3" type = "checkbox" name = "game[publisher][12]" value = "Broderbund"> Broderbund<br>
______________________<br>
<b>Release Year: </b>
<br>Between</br>
<input type="text" id="startdate" name="game[releaseYear][0]" size="20"/>
and<br>
<input type="text" id="enddate" name="game[releaseYear][1]" size="20"/> <br>
______________________<br>
<b>Rarity: </b><br>
<input type="checkbox" id="selecctall-4"/> All <br>
<input class="checkbox4" type = "checkbox" name = "game[rarity][0]" value = "Common"> Common<br>
<input class="checkbox4" type = "checkbox" name = "game[rarity][1]" value = "Uncommon"> Uncommon<br>
<input class="checkbox4" type = "checkbox" name = "game[rarity][2]" value = "Rare"> Rare<br>
<input class="checkbox4" type = "checkbox" name = "game[rarity][3]" value = "veryRare"> Very Rare<br>
<input class="checkbox4" type = "checkbox" name = "game[rarity][4]" value = "SuperRare"> Super Rare<br>
<input class="checkbox4" type = "checkbox" name = "game[rarity][5]" value = "Ultrarare"> Ultra Rare<br>
______________________<br><br><br>
<input type = "submit" value = "Submit" name = "searchSubmit" style="width: 100px; height: 20px;" />
</form>
</div>
<div id="body">
<h1>NES Titles</h1>
<table border ="1" width: "100%">
<tr>
<th>Title</th>
<th>Console</th>
<th>Genre</th>
<th>Publisher</th>
<th>Release Year</th>
<th>Rarity</th>
<th>Details</th>
<th>img Path</th>
</tr>
<tr>
<?php foreach($rowset as $row): ?>
<tr>
<td>
<a href="gameDetails.php?rowid=<?php echo $row['rowid']; ?>">
<?php echo $row['title']; ?>
</a>
</td>
<td><?php echo $row['console']; ?></td>
<td><?php echo $row['genre']; ?></td>
<td><?php echo $row['publisher']; ?></td>
<td><?php echo $row['releaseYear']; ?></td>
<td><?php echo $row['rarity']; ?></td>
<td><?php echo $row['details']; ?></td>
<td><?php echo $row['image']; ?></td>
</tr>
<?php endforeach; ?>
</table>
</div>
</body>
</html>
4) for simplicity I would like to be able to combine the swicthes for my checkboxes all buttons into 1 jquery statment but not on the top of my list atm.
Thanks for all the help! and as always i'll post updates on my progress as I make any.