OK let me be as specific as possible.... I would like to change the way my search bar behaves. Right now the string has to match the MySQL query exactly!!! or it won't show my products.
Here is what I got:
A search for "case iphone"
searchitems.php?tosearch=case+iphone&Search=Search
Now a search for "iphone case"
searchitems.php?tosearch=iphone+case&Search=Search
Now based on how the query would work...if I type in the URL manually to this... it works how I want it to:
searchitems.php?tosearch=%iphone%&%case%&Search=Search
So how do I go about changing the URL from
searchitems.php?tosearch=case+iphone&Search=Search
to
searchitems.php?tosearch=%iphone%&%case%&Search=Search
Here is the code that I have so far:
Search Form In index.php
<form method="GET" action="searchitems.php">
<input size="50" type="text" name="tosearch" value="Search" name="keyword" id="keyword" title="keyword" onfocus="clearText(this)" onblur="clearText(this)" class="txt_field">
<input type="submit" name="Search" value="Search" alt="Search" id="searchbutton" title="Search" class="sub_btn">
</form>
searchitems.php (Sorry if it isn't tabbed correctly just copied and pasted)
<?php
include('core/header2.php');
include ('core/connectdb.php');
if(isset($_GET['tosearch'])) {
$tosearch=$_GET['tosearch'];
$tosearch=urldecode($tosearch);
$tosearch = preg_replace('!\s+!', ' ', trim($tosearch));
$search_terms = explode(' ',$tosearch);
$search_terms[] = $tosearch;
$search_terms=array_unique($search_terms);
$query = "select * from products where ";
$query_fields = Array();
$sql = "SHOW COLUMNS FROM products";
$columnlist = $connect->query($sql);
while($arr = $columnlist->fetch_assoc()){
extract($arr);
$query_fields[] = $Field . " LIKE ('%". $tosearch . "%')";
}
$query .= implode(" OR ", $query_fields);
$results = mysqli_query($connect, $query) or die(mysql_error());
$rows = $results->num_rows;
if ($rows > 0) {
$cols = 5;
$counter = 1;
$nbsp = $cols - ($rows % $cols);
echo '<div id="content" class="float_r">';
echo "<table border=\"0\">";
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
if(($counter % $cols) == 1) { // Check if it's new row
echo '<tr align="center">';
}
extract($row);
echo '<td valign="top" style="padding-right:15px;">';
echo "<a href=itemdetails.php?itemcode=$item_code>";
echo '<img src=' . $imagename . ' style="max-width:120px;max-height:140px;
width:auto;height:auto;"></img><br/>';
echo $item_name .'<br/>';
echo "</a>";
echo '<div class="product_price">$'. $price .'</div>';
echo "<form method=\"POST\" action=\"cart.php?action=add&icode=$item_code&iname=$item_name&iprice=$price&ilocation=$location\">";
echo "<input type=\"submit\" name=\"addtocart\" value=\"Add To Cart\"></form>";
echo "</td>";
if(($counter % $cols) == 0 ){
echo "</tr>";
}
$counter++;
}
if($nbsp > 0) { // Add unused column in last row
for ($i = 0; $i < $nbsp; $i++) {
echo '<td> </td>';
}
echo '</tr>';
}
}
}
echo '</table></div><div class="cleaner"></div>';
include('core/footer.php');
?>
well, brother, it's a bad approach how you doing it. But for learning, its fine. For + sign issue, you can use php urldecode, example:
<?php
$tosearch = 'a+b';
echo urldecode($tosearch);
it has its own pro/con thing but on high-level it will work for you, you can dig more into it if you like.
Related
This is the code that I've got to search and it just appears next to each other. Can I put html within php and how would this be done? Or should I make a table below the form?
<?php
include ('database_conn.php');
$output = '';
if(isset($_POST['search'])) {
$search = $_POST['search'];
$search = preg_replace("#[^0-9a-z]i#","", $search);
$query = mysqli_query($conn, "SELECT * FROM attendance WHERE stud_id LIKE '%$search%'") or die ("Could not search");
$count = mysqli_num_rows($query);
if($count == 0){
$output = "There was no search results!";
}else{
while ($row = mysqli_fetch_array($query)) {
$stud_id = $row ['stud_id'];
$module = $row ['module'];
$attendance_status = $row ['attendance_status'];
$output .='<div> '.$stud_id.''.$module.''.$attendance_status.'</div>';
}
}
}
?>
This is the form that I've got in the HTML to search
<form action ="CM0671_attendance.php" method = "post">
<input name="search" type="text" size="30" placeholder="Student ID"/>
<input class="btn btn-primary" type="submit" value="Search"/>
</form>
Replace your while with this:
echo "<table>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Module</th>";
echo "<th>Status</th>";
echo "</tr>";
while ($row = mysqli_fetch_array($query)) {
$stud_id = $row ['stud_id'];
$module = $row ['module'];
$attendance_status = $row ['attendance_status'];
echo "<tr>";
echo "<td>{$stud_id}</td>";
echo "<td>{$module}</td>";
echo "<td>{$attendance_status}</td>";
echo "</tr>";
}
echo "</table><br>";
I can't figure out why my values aren't being passed from the form. I can't spot an error.
The Form Code:
$table = $_POST['table'];
$id = $_POST['id'];
$count = 0;
$query = "SELECT * FROM `" . $table . "` WHERE id = " . $id;
$result1 = mysqli_query($link, $query);
echo '<center><table style="text-align:center">';
echo '<form action="edit-process.php" method="post">';
while($row = mysqli_fetch_assoc($result1)){
foreach($row as $key => $val){
if ($count > 0){
echo "<tr>";
echo "<td>" . $key . "</td>";
echo '<td><input type="text" name="' . $key . '" value="' . $val . '"></td>';
echo "</tr>";
$count++;
}
else $count++;
}
}
echo '<input type="hidden" name="table" value="' . $table . '" />';
echo '<input type="hidden" name="id" value="' . $id . '" />';
echo '<tr><td><input type="submit" value="Save Changes" /></td></tr>';
echo "</form>";
echo "</table>";
The php file:
$table = $_POST['table'];
$id = $_POST['id'];
$count1 = 0;
$count2 = 0;
$result = mysqli_query($link, "SHOW COLUMNS FROM `" . $table . "`");
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$keyNames[$count2] = $row['Field'];
$count2++;
}
}
while ($count1 < $count2){
if ($count1 > 0) {
$value = mysqli_real_escape_string($_POST[$keyNames[$count1]]);
$query2 = "UPDATE `" . $table . "` SET `" . $keyNames[$count1] . "` = '" . $value . "' WHERE id = " . $id;
echo $query2 . "<br>";
$result2 = mysqli_query($link, $query2);
$count1++;
}
else $count1++;
}
I am avoiding displaying the id column with all the counts. The output of the echo-ed queries are:
Any ideas?
EDIT
I'll take care of changing everything over to procedural style once I figure out this issue. If I get rid of the mysqli_real_escape_string, it passes all the data except those columns with spaces in them. I thought that's what backticks were for? Is there something else I can do to make the columns with two words pass data like those with one word?
You need to switch these rows -
echo '<center><table style="text-align:center">';
echo '<form action="edit-process.php" method="post">';
....
echo "</form>";
echo "</table>";
to
echo '<form action="edit-process.php" method="post">';
echo '<center><table style="text-align:center">';
....
echo "</table>";
echo "</form>";
Having the <form> inside the <table> is invalid code. It either needs to wrap the <table> or be inside <td></td>.
see also -
form inside table
Form inside a table
Update #1-
On your Edit
Spaces in <input name=""> will be replaced with _ so your $_POST[] name will not match your <input name="">. from the manual - http://www.php.net/manual/en/language.variables.external.php
Note:
Dots and spaces in variable names are converted to underscores. For example <input name="a.b" /> becomes $_REQUEST["a_b"].
see also -
Can <input> elements have capital letters and spaces in PHP
I am building this function which echo-s all table fields and has a checkbox along with it.
I need to print this fields after submission, i have this code, and no i haven't started something, not because i don't want but because i don't have any idea how to.. so i'm asking your help.
I need to print information in report.php please help.
<form action='report.php' method='post'>
<?php // Script 12.7 - sopping.php
$db = mysql_connect('localhost', 'root', '');
mysql_select_db('db_up', $db);
echo "<table border='1' class='tabtext'>";
$result = mysql_query("SELECT * FROM hostess");
$numrows = mysql_num_rows($result);
$numfields = mysql_num_fields($result);
// show headers
echo '<thead><tr>';
for ($field = 0; $field < $numfields; $field++) {
$field_name = mysql_field_name($result, $field); // instead of $i
echo '<th><label><input type="checkbox" name="checkbox[' . $field_name . '][]" value="1"/> ' . $field_name . '</label></th>';
}
echo '</tr></thead>';
echo '<tbody>';
for ($row = 0; $row < $numrows; $row++) {
$data = mysql_fetch_assoc($result);
echo '<tr>';
for ($field = 0; $field < $numfields; $field++) {
$field_name = mysql_field_name($result, $field);
if (isset($_POST['checkbox'][$field_name])) {
echo '<td>' . $data[$field_name] . '</td>';
}
}
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
?>
<input type='submit' value='Submit' />
</form>
The report.php
<?php // Script 12.7 - sopping.php
$db = mysql_connect('localhost', 'root', '');
mysql_select_db('db_up', $db);
foreach($_POST['checkbox'] as $key => $value)
?>
Try this one:
if(isset($_POST['Submit'])) {
echo "<pre>";
$checked = implode(',', $_POST['checkbox']);
echo $checked;
}
I need to display a photo gallery in a table, I want five pictures on each line, but can't find the way to insert </tr><tr> after each fifth picture.
Here's my code:
<?php
// table name
$tbl_name=gallery1;
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
while($rows= mysql_fetch_assoc($result)){
$id = $rows['id'];
$path = $rows['path'];
$image_name = $rows['image_name'];
$title = $rows['title'];
?>
<img src="<?php echo $path."/".$image_name;?>" height="120"/>Name:<?php echo $title;?>
<?php
echo "<form action='pictry.php' enctype='multipart/form-data' method='post'>
<input name='file[]' type='hidden' value='".$image_name."' />
<input name='id' type='hidden' id='id' value='".$id."'/>
<input type='submit' name='button' id='button' value='Delete Picture' /></form>";
}
?>
Replace the line
while($rows= mysql_fetch_assoc($result)){
with
for($i = 0; $rows= mysql_fetch_assoc($result); ++$i) {
Then you place something like this into the for loop.
if($i % 5 == 0) { /* insert stuff */ }
Untested Code
<?php
$perrow = 5;
$i = 0;
echo '<table>';
echo '<tr>';
while($rows = mysql_fetch_assoc($result)) {
echo '<td><img src=[grapresultfromrows] /></td>';
++$i;
if($i == $perrow) {
$i = 0;
echo '</tr>';
echo '<tr>';
}
}
// If not a multiple of $perrow you need to add extra cells
for($i; $i < $perrow; ++$i) {
echo '<td></td>';
}
echo '</tr>';
echo '</table>';
?>
I want to select particular fields from a database using a checkbox in PHP.
My code is:
<?php
echo "<table border='1' class='tabtext'>";
$result = mysql_query("SELECT * FROM orders");
$numfields = mysql_num_fields($result);
echo "<table>\n<tr>";
for ($i=0; $i < $numfields; $i++) // Header
{
echo '<th>'.mysql_field_name($result, $i).'</th>';
echo '<tr><td><input type="hidden" name="checkbox[]" value="false"/></td></tr>';
echo '<tr><td><input type="checkbox" name="checkbox[]" value="true"/></td></tr>';
$checkbox[$i] = isset($_POST['checkbox'][$i]) ? true : false;
if(isset($checkbox))
{
foreach($checkbox as $value)
{
echo $value."<br>"; //it will print the value of your checkbox that you checked
}
}
}
I think something like this will give you better results.
<?php
echo "<table border='1' class='tabtext'>";
$result = mysql_query("SELECT * FROM orders");
$numrows = mysql_num_rows($result);
$numfields = mysql_num_fields($result);
// show headers
echo '<thead><tr>';
for ($field = 0; $field < $numfields; $field++) {
$field_name = mysql_field_name($result, $i);
echo '<th><label><input type="checkbox" name="checkbox[' . $field_name . ']" value="1"/> ' . $field_name . '</label></th>';
}
echo '</tr></thead>';
echo '<tbody>';
for ($row = 0; $row < $numrows; $row++) {
$data = mysql_fetch_assoc($result);
echo '<tr>';
for ($field = 0; $field < $numfields; $field++) {
$field_name = mysql_field_name($result, $i);
if (isset($_POST['checkbox'][$field_name])) {
echo '<td>' . $data[$field_name] . '</td>';
}
}
echo '</tr>';
}
echo '</tbody>';
echo '</table>';