Where do I edit to change the search results design? - php

I have created a simple search engine who gets data from a PHP table and post the results. But as of right now it posts the results in a single line and nothing to show that they're in fact 4 different columns. What I'm looking for is a way to create a table so it looks more presentable. I have the search engine divided into 2 .php files.
1st file:
<?php
error_reporting(1);
include('system_admin.inc.php');
$title = 'Leonard';
make_header($title,array('enable_ajax' => true));
?>
<html>
<head>
<title>Leonard</title>
<script type=text/javascript src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type=text/javascript ">
function searchq() {
var searchTxt = $("input[name='search']").val();
$.post("testsearch.php", {searchVal: searchTxt}, function(output) {
$("#output") .html(output);
});
}
</script>
</head>
<body>
<h1>Search</h1>
<form class="odd" action="leonard.php" method="post">
<input type="text" name="search" placeholder="Search" onkeyup="searchq();" />
<div id="output">
</div>
</form>
</body>
</html>
2nd file:
mysql_connect ("localhost","root","xxxxxx") or die ("Connectionissues");
mysql_select_db ("xxxxxx") or die("Can't find DB");
$output = '';
if(isset($_POST['searchVal'])) {
$searchq = $_POST['searchVal'];
$searchq = preg_replace ("#^0-9#"," ",$searchq);
$query = mysql_query("SELECT * FROM ds_OrderItem WHERE idProduct LIKE '%$searchq%' LIMIT 100") or die("Search invalid!");
$count = mysql_num_rows ($query);
if($count == 0){
$output = 'Order have never been made before';
}else{
while($row = mysql_fetch_array($query)) {
$idproduct = $row['idProduct'];
$idorder = $row['idOrder'];
$title = $row['title'];
$qty = $row['qty'];
$output .= '<div> '.$idproduct.' '.$idorder.' '.$title.' '.$qty.' </div>';
}
if($_POST['searchVal'] == NULL) {
$output = "";
}
}
}
I want these four columns to be displayed in a presentable manner:
$idproduct = $row['idProduct'];
$idorder = $row['idOrder'];
$title = $row['title'];
$qty = $row['qty'];
Can anyone help me with this? :-)
Thanks in advance for reading and any advice given!

Change this portion
while($row = mysql_fetch_array($query)) {
$idproduct = $row['idProduct'];
$idorder = $row['idOrder'];
$title = $row['title'];
$qty = $row['qty'];
$output .= '<div> '.$idproduct.' '.$idorder.' '.$title.' '.$qty.' </div>';
}
to
$output .='<table>';
while($row = mysql_fetch_array($query)) {
$output .= '<tr><td>'.$row['idProduct'].'</td><td>'.$row['idOrder'].'</td><td>'.$row['title'].'</td><td>'.$row['qty'].'</td></tr>';
}
$output .='</table>';

Related

PHP/Ajax page navigation url change

Ik want to build a ajax/jquery page navigation so when the user clicks on a page, the url changes to so that there are no problems with browser's back button. I found a lot of answers for this but not what I searched for. I saw this code below on a tutorial site and I want to customize it so that the url moves to. Do I have to build that in the ajax script of on the PHP side? How can I achieve this?
My index.php
<html>
<head>
<title>Webslesson Tutorial | Make Pagination using Jquery, PHP, Ajax and MySQL</title>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
</head>
<body>
<br /><br />
<div class="container">
<h3 align="center">Make Pagination using Jquery, PHP, Ajax and MySQL</h3><br />
<div class="table-responsive" id="pagination_data">
</div>
</div>
</body>
</html>
<script>
$(document).ready(function(){
load_data();
function load_data(page)
{
$.ajax({
url:"pagina.php",
method:"POST",
data:{page:page},
success:function(data){
$('#pagination_data').html(data);
history.pushState({ foo: 'bar' }, '', '/bank'); // I tried this line but it don't work
}
})
}
$(document).on('click', '.pagination_link', function(){
var page = $(this).attr("id");
load_data(page);
});
});
</script>
And my pagina.php
<?PHP
$connect = mysqli_connect("hidden", "hidden", "hidden","publiek2") or die("Connection failed: " . mysqli_connect_error());
$record_per_page = 50;
$page = '';
$output = '';
if(isset($_POST["page"]))
{
$page = $_POST["page"];
}
else
{
$page = 1;
}
$start_from = ($page - 1)*$record_per_page;
$query = "SELECT * FROM voertuigen WHERE merk='Chevrolet' AND voorpagina='1' ORDER BY model ASC LIMIT $start_from, $record_per_page";
$result = mysqli_query($connect, $query);
$output .= "
<table class='table table-bordered'>
<tr>
<th width='50%'>Name</th>
<th width='50%'>Phone</th>
</tr>
";
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["merk"].'</td>
<td>'.$row["model"].'</td>
</tr>
';
}
$output .= '</table><br /><div align="center">';
$page_query = "SELECT * FROM voertuigen WHERE merk='Chevrolet' AND voorpagina='1' ORDER BY model ASC";
$page_result = mysqli_query($connect, $page_query);
$total_records = mysqli_num_rows($page_result);
$total_pages = ceil($total_records/$record_per_page);
for($i=1; $i<=$total_pages; $i++)
{
$output .= "<span class='pagination_link' style='cursor:pointer; padding:6px; border:1px solid #ccc;' id='".$i."'>".$i."</span>";
}
$output .= '</div><br /><br />';
echo $output;
?>
Thanks in advance.
I've tried to customize the ajax script.
As a javascript and PHP developer I don't understand much about Jquery but I think the code describes that there is a div called.pagination_link takes id as page number eg: 1. And makes an HTTP POST request to the PHP side. You can simply do this with a sample example in Javascript.
HTML
<div id="1" classname="pagination_link">1</div>
Javascript
const id = document.querySelector('.pagination_link').getAttribute('id');
fetch('pagina.php', {
method : 'POST',
body : id
})
If you want to send data.
I added a hidden text value with the page number as value and called the id 'idd' and recalled that id on the ajax page. It works good now but if I want to go back with browser button, the url moves to the previous page but the results on the page stays te same. How can I resolve that?
My new index.php JS code
<script>
$(document).ready(function(){
var idd;
load_data();
function load_data(page)
{
$.ajax({
url:"pagina.php",
method:"GET",
data:{page:page},
success:function(data){
$('#pagination_data').html(data);
if (idd === undefined) {
alert("ja");
idd = 1;
alert(idd);
history.pushState({}, '', 'http://localhost:4612/pagina/1');
}
else {
idd = $("#idd").val();
history.pushState({}, '', 'http://localhost:4612/pagina/'+idd);
}
}
})
}
$(document).on('click', '.pagination_link', function(){
var page = $(this).attr("id");
load_data(page);
});
});
</script>
and my altered pagina.php
<?PHP
$connect = mysqli_connect("hidden", "hidden", "hidden","publiek2") or die("Connection failed: " . mysqli_connect_error());
$record_per_page = 50;
$page = '';
$output = '';
if(isset($_GET["page"]))
{
$page = $_GET["page"];
echo "<input type='hidden' id='idd' value='".$_GET['page']."'>";
}
else
{
$page = 1;
$idd = 1;
}
$start_from = ($page - 1)*$record_per_page;
$query = "SELECT * FROM voertuigen WHERE merk='Chevrolet' AND voorpagina='1' ORDER BY model ASC LIMIT $start_from, $record_per_page";
$result = mysqli_query($connect, $query);
$output .= "
<table class='table table-bordered'>
<tr>
<th width='50%'>Name</th>
<th width='50%'>Phone</th>
</tr>
";
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["merk"].'</td>
<td>'.$row["model"].'</td>
</tr>
';
}
$output .= '</table><br /><div align="center">';
$page_query = "SELECT * FROM voertuigen WHERE merk='Chevrolet' AND voorpagina='1' ORDER BY model ASC";
$page_result = mysqli_query($connect, $page_query);
$total_records = mysqli_num_rows($page_result);
$total_pages = ceil($total_records/$record_per_page);
for($i=1; $i<=$total_pages; $i++)
{
$output .= "<span class='pagination_link' style='cursor:pointer; padding:6px; border:1px solid #ccc;' id='".$i."'>".$i."</span>";
}
$output .= '</div><br /><br />';
echo $output;
?>

Search doesn't work when there's an apostrophe

I'm trying to make an OPAC website. Everything works fine since it's mostly just selecting from the database and displaying it. I noticed that when the book title i'm trying to search has an apostrophe, it displays nothing. If the book title doesn't contain any apostrophe it all works. I'm using mysql for my database.
<!-- {this is how i connect my datatbase} -->
<?php
include 'includes/dbh.inc.php';
?>
<!DOCTYPE <!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body>
<div id= "wrapper">
<img class="cpclogo" src="cpc.png">
<header>
<h1 class="CPC"> Colegio de la Purisima Concepcion </h1>
<h3 class="Saying"> The School of the Archdiocese of Capiz </h3>
</header>
</div>
header.php file
<?php
$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "library";
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
?>
search.php file
<?php
include 'header.php'
?>
<h1 class="searchresults">Search Results:</h1>
<div class="search-container">
<?php
if (isset($_POST['submit']))
{
$search = mysqli_real_escape_string($conn, $_POST['search']);
$sql = "SELECT * FROM book WHERE Book_Title LIKE '%$search%' OR Author LIKE '%$search%' OR Call_Number LIKE '%$search%' OR Book_ID LIKE '%$search%'";
$result = mysqli_query($conn, $sql);
$queryResult = mysqli_num_rows($result);
$search = mysqli_real_escape_string($conn, $_POST['search']);
echo "<h3 class='resultcount'>There are ".$queryResult." results!</h3>";
if ($queryResult > 0)
{
while ($row = mysqli_fetch_assoc($result))
{
echo "<a href='article.php?Book_Title=".$row['Book_Title']."&id=".$row['Book_ID']."&call=".$row['Call_Number']."' class= 'search-ref'><div class=search-box>
<tr><td>".$row['Book_Title']." </td>
<td>/ ".$row['Author']."</td>
<p>".$row['Call_Number']."</p>
</div></tr><br>";
}
}
}
?>
<input class="backbtn" type="button" value="Back" onclick="history.back(-1)" />
</div>
article.php file
<?php
include 'header.php';
?>
<div class="article-container">
<?php
//Declairing Variables
$Author = "Authors: ";
$Edition = "Edition: ";
$Subject ="Subject: ";
$Summary = "Summary: ";
$Notes = "Notes: ";
$Publisher ="Publisher: ";
$Phys_Desc ="Physical Description: ";
$Call_Number ="Call Number: ";
$Book_ID = "Book ID: ";
$Title= mysqli_real_escape_string($conn, $_GET['Book_Title']);
$sql ="SELECT * FROM book WHERE Book_Title='$Title'";
$result = mysqli_query($conn, $sql);
$queryResult = mysqli_num_rows($result);
if ($queryResult > 0)
while ($row = mysqli_fetch_assoc($result))
{
echo "<div class= 'article-box'>
<h3 class='booktitle'><b>".$row['Book_Title']."</h3></b>
<p><b>$Author</b>".$row['Author']."</p>
<p><b>$Edition</b>".$row['Edition']."</p>
<p><b>$Subject</b>".$row['Subject']."</p>
<p><b>$Summary</b>".$row['Summary']."</p>
<p><b>$Notes</b>".$row['Notes']."</p>
<p><b>$Publisher</b>".$row['Publisher']."</p>
<p><b>$Phys_Desc</b>".$row['Phys_Desc']."</p>
<p><b>$Call_Number</b>".$row['Call_Number']."</p>
</div>";
}
?>
<div class="btns">
<input class="backbtn" type="button" value="Back" onclick="history.back(-1)" />
<button type="submit" id="copybtn" class= "copybtn">Copies</button>
</div>
<!-- POP-UP WINDOW -->
<div class="bg-modal">
<div class="modal-content">
<div class="close"></div>
<table class = "table">
<tr>
<th>Copy</th>
<th>Status</th>
<th>Accession Number</th>
<th>Call Number</th>
<th>Location</th>
<th>Format</th>
<th>Cost</th>
<th>Vendor</th>
<th>Fund</th>
<th>Date Acquired</th>
</tr>
<?php
$id = mysqli_real_escape_string($conn, $_GET['id']);
$call = mysqli_real_escape_string($conn, $_GET['call']);
$sql = "SELECT Copy, Status, Accession_Number, l.Location, f.Format, Cost, Vendor, u.Fund, Date_Acq
FROM copy c
INNER JOIN location l ON l.Location_Acronym = c.Location
INNER JOIN format f ON f.Format_ID = c.Format
INNER JOIN fund u ON u.Fund_ID = c.Fund
WHERE Book_ID='$id'";
$result = mysqli_query($conn, $sql);
$queryResult = mysqli_num_rows($result);
if ($queryResult > 0)
{
while ($row = mysqli_fetch_assoc($result))
{
echo "
<tr><td>".$row['Copy']."</td>
<td>".$row['Status']."</td>
<td>".$row['Accession_Number']."</td>
<td>".$call."</td>
<td>".$row['Location']."</td>
<td>".$row['Format']."</td>
<td>₱".$row['Cost']."</td>
<td>".$row['Vendor']."</td>
<td>".$row['Fund']."</td>
<td>".$row['Date_Acq']."</td></tr>
";
}
}
?>
</table>
</div>
</div>
<script src="popup.js"></script>
</body>
</html>
copies.php file
<?php
include 'header.php'
?>
<h1 class="copyresults">Copy Results:</h1>
<div class="article-container">
<table class = "table">
<tr>
<th>Barcode</th>
<th>Copy</th>
<th>Status</th>
<th>Location</th>
<th>Format</th>
<th>Vendor</th>
</tr>
<?php
{
$id = mysqli_real_escape_string($conn, $_GET['id']);
$sql = "SELECT * FROM copy WHERE Book_ID='$id'";
$result = mysqli_query($conn, $sql);
$queryResult = mysqli_num_rows($result);
if ($queryResult > 0)
{
while ($row = mysqli_fetch_assoc($result))
{
echo "
<tr><td>".$row['Barcode']."</td>
<td>".$row['Copy']."</td>
<td>".$row['Status']."</td>
<td>".$row['Location']."</td>
<td>".$row['Format']."</td>
<td>".$row['Vendor']."</td></tr>
";
}
}
}
?>
</table>
I reckon there is something fishy about this:
echo "<a href='article.php?Book_Title=".$row['Book_Title']."&id=".$row['Book_ID']."&call=".$row['Call_Number']."' class= 'search-ref'><div class=search-box>...
See that your href value is wrapped in single quotes? When you click on that link, the entire querystring will be truncated at the first single quote and this is the likely culprit.
Use: urlencode() on $row['Book_Title'] if it is the only trouble maker.
echo "<a href='article.php?Book_Title=" . urlencode($row['Book_Title']) . "&id=" . $row['Book_ID'] . "&call=".$row['Call_Number'] . "' class= 'search-ref'><div class=search-box>...
Or this might make your code more attractive (certainly more robust):
$data = [
'Book_Title' => $row['Book_Title'],
'id' => $row['Book_ID'],
'call' => $row['Call_Number']
];
echo "<a href='article.php?" . http_build_query($data) . "' class='search-ref'><div class=search-box>...

Search in PHP not showing message when there's no result

I had set a search button in my website. It works fine if there's a result but if there's none, it doesn't show the "no results.....".
What's wrong with my code?
<html !DOCTYPE HTML>
<body>
<?php
$title = "DenTEETH";
include('header.html');
//connect to database
$db = mysqli_connect("127.0.0.1", "root", "", "authentication");
if(isset($_GET['q']) && $_GET['q'] !== '')
{
$searchq = $_GET['q'];
$sql = "SELECT * FROM search WHERE keyword LIKE '%$searchq%' OR title LIKE '%$searchq%'";
$output='';
$results = mysqli_query($db, $sql);
if (count($results) == 0){
$output .= 'No search results for <b>"' . $searchq . '"</b>';
}
else{
while ($row = mysqli_fetch_array($results)){
$id = $row['search_id'];
$title = $row['title'];
$desc = $row['description'];
$link = $row['link'];
$img = '<img src="images/thumbnail/'.$row['search_id'].'.jpg" class="thumbnail">';
$output .= '<div class="search_thumb">
<p class="search_cap">' . $img . '<h3>' . $title . '</h3>' . $desc .
'<div class="clear"></div></p></div>';
}
}
}
else{
header("location: ./");
}
print($output);
?>
<div class="row">
</div>
<?php
include('footer.html');
?>
</body>
</html>
Use mysqli_num_rows() try this
<html !DOCTYPE HTML>
<body>
<?php
$title = "DenTEETH";
include('header.html');
//connect to database
$db = mysqli_connect("127.0.0.1", "root", "", "authentication");
if(isset($_GET['q']) && $_GET['q'] !== '')
{
$searchq = $_GET['q'];
$sql = "SELECT * FROM search WHERE keyword LIKE '%$searchq%' OR title LIKE '%$searchq%'";
$output='';
$results = mysqli_query($db, $sql);
if (mysqli_num_rows($results ) == 0){
$output .= 'No search results for <b>"' . $searchq . '"</b>';
}
else{
while ($row = mysqli_fetch_array($results)){
$id = $row['search_id'];
$title = $row['title'];
$desc = $row['description'];
$link = $row['link'];
$img = '<img src="images/thumbnail/'.$row['search_id'].'.jpg" class="thumbnail">';
$output .= '<div class="search_thumb">
<p class="search_cap">' . $img . '<h3>' . $title . '</h3>' . $desc .
'<div class="clear"></div></p></div>';
}
}
}
else{
header("location: ./");
}
print($output);
?>
<div class="row">
</div>
<?php
include('footer.html');
?>
</body>
</html>

while background change php/css

i've created a php script that connects to my database (mysql) this will be changed to a more secure enviorment.
But for now i want to change the bg color of my out come
'echo "<h2><a href='$Link'>$title</a></h2>
<b>$description</b><br /> ";
}
}
else
echo "Geen resultaat gevonden voor \"<b>$s</b>\""; '
im not sure how to do so with a while loop.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
<title> Snuffelen</title>
</head>
<body>
<a href="index.php">
<img src="image/logo_klein.jpg" alt="Snuffelen logo" style="width:250px;height:75px;border:0;">
</a>
<form action='./search.php' method='get'>
<input type='text' name='s'size='50' value='<?php echo $_GET['s']; ?>' />
<input type='submit' value='Zoek'/>
</form>
<hr />
<?php
$s = $_GET['s'];
$terms = explode (" ", $s);
$query = "SELECT * FROM ID WHERE ";
foreach ($terms as $each){
$i++;
if ($i ==1)
$query .= "keywords LIKE '%$each%' ";
else
$query .= "OR keywords LIKE '%$each%' ";
}
//connect to database
mysql_connect("server", "user", "pw");
mysql_select_db("DB");
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if($numrows > 0){
while($row = mysql_fetch_assoc($query)){
$id = $row['id'];
$photo = $row['photo'];
$title = $row['title'];
$description = $row['description'];
$price = $row['price'];
$Link = $row['Link'];
$keywords = $row['keywords'];
$euro = $row['euro'];
$line = $row ["__________________________________________________________________________________________________"];
echo "<h2><a href='$Link'>$title</a></h2>
<b>$description</b><br /> ";
}
}
else
echo "Geen resultaat gevonden voor \"<b>$s</b>\"";
//disconect
mysql_close();
?>
</body>
</html>
i whould like to add to colors 1 light grey and 2 white and then the next one light grey white light grey etc etc
does anybody know how to do so??
Please try this
$bgColor = "style='background-color: green;'";
echo "<div ".$bgColor.">";
if($numrows > 0){
while($row = mysql_fetch_assoc($query)){
$id = $row['id'];
$photo = $row['photo'];
$title = $row['title'];
$description = $row['description'];
$price = $row['price'];
$Link = $row['Link'];
$keywords = $row['keywords'];
$euro = $row['euro'];
$line = $row ["__________________________________________________________________________________________________"];
echo "<h2><a href='$Link'>$title</a></h2>
<b>$description</b><br /> ";
}
}
else
echo "Geen resultaat gevonden voor \"<b>$s</b>\"";
echo "</div>";
Please try this
$bgColorGreen = "style='border: 1px solid green;'";
$bgColorBlue = "style='border: 1px solid blue;'";
if($numrows > 0){
$i = 0;
while($row = mysql_fetch_assoc($query)){
$id = $row['id'];
$photo = $row['photo'];
$title = $row['title'];
$description = $row['description'];
$price = $row['price'];
$Link = $row['Link'];
$keywords = $row['keywords'];
$euro = $row['euro'];
$line = $row ["__________________________________________________________________________________________________"];
if($i % 2 == 0){
echo "<hr ".$bgColorGreen.">";
}else{
echo "<hr ".$bgColorBlue.">";
}
echo "<h2><a href='$Link'>$title</a></h2>
<b>$description</b><br /> ";
$i++;
}
}
else
echo "Geen resultaat gevonden voor \"<b>$s</b>\"";

How to print a record from a MySQL table if a field value = a preset value with PHP

I want to filter records being displayed from a MySQL table if the value of a field = one of multiple predeclared values. I am already running a query on my table which displays records based on which option from a dropdown menu the user selects. However before the user selects an option from the dropdown box when the page has just loaded I want the table to display only some records.
<?php
require_once("includes/session.php");
include_once("includes/masterinclude.php");
$preferences = getPreferences();
$category = "";
$attribute1 = ""; $attribute2 = ""; $attriute3 = ""; $attribute4 = "";
$top_level="0";
$name = $_GET['member'];
$name = $_GET['countries'];
$information = getInformationPage($name);
$infopagename=$information->IN_NAME;
//meta data for information pages now taken from the information table
$pageTitle = $information->IN_NAME . html_entity_decode($information->IN_TITLE);
$pageMetaDescription = html_entity_decode($information->IN_META_DESC);
$pageMetaKeywords = html_entity_decode($information->IN_META_KEYWORDS);
$pageCustomHead = html_entity_decode($information->IN_CUSTOM_HEAD, ENT_QUOTES);
//initialise screen fields
$selected_member = "";
$id = "";
$username = ""; $username_original = "";
$password = ""; $password_original = "";
$password_test = "";
$title = "MR"; $first_name = ""; $last_name = ""; $company_name = "";
$address1 = ""; $address2 = ""; $town = ""; $county = ""; $country = ""; $postcode = ""; $phone = ""; $mobile = ""; $email = "";
$member_confirmed = "N";
$ast_first = 0; $ast_last = 0; $ast_company = 0; $ast_add1 = 0; $ast_add2 = 0; $ast_town = 0; $ast_county = 0; $ast_country = 0; $ast_post = 0; $ast_phone = 0;
$ast_mobile = 0; $ast_email = 0;
$ast_user = 0; $ast_pass = 0; $ast_passconf = 0;
$selected_product = "";
$members = Get_All_Members("ALL");
$counties = Get_All_Counties("ALL");
$_GET['searchdata'] = $_POST['SEARCH_DATA']; $_GET['searchmember'] = $_POST['MEMBER'];
$selected_county = $counties->CTY_COUNTY;
$_GET['searchdata'] = $_POST['SEARCH_DATA']; $_GET['searchcounty'] = $_POST['COUNTY'];
$selected_country = $countries->CTY_COUNTRY;
$_GET['searchdata'] = $_POST['SEARCH_DATA']; $_GET['searchcountry'] = $_POST['COUNTRY'];
include_once("includes/header.php");
?>
<!-- start: Page header / Breadcrumbs -->
<div id="breadcrumbs">
<div class="container">
<div class="breadcrumbs">
Home<i class="icon-angle-right"></i>Export Stockists
</div>
</div>
</div>
<!-- end: Page header / Breadcrumbs -->
<!-- start: Container -->
<div id="container">
<div class="container">
<div class="row-fluid">
<!-- start: Page section -->
<section class="span12">
<div class="row-fluid shop-result">
<div class="inner darken clearfix">
<h1>Export Stockists</h1>
</div>
</div>
<div class="row-fluid">
<?php
$sql = "SELECT * FROM member ";
if (isset($_POST['search'])) {
$search_term = mysql_real_escape_string($_POST['search_box']);
$sql .= "WHERE MB_COUNTRY = '{$search_term}' ";
$sql .=" OR MB_COMPANY = '{$search_term}' ";
}
$query = mysql_query($sql) or die(mysql_error());
?>
<form name="search_form" method="POST" action="stockists_country.php" enctype="multipart/form-data" class="form-search">
<div class="input-append">
<select name="all_countries" onchange="MM_jumpMenu('parent',this,1)" type="submit">
<option disabled="disabled" selected="selected" hidden="hidden">Select County</option>
<option value="103">Republic of Ireland</option>
<option value="39">Canada</option>
<option value="149">Netherlands</option>
<option value="193">South Africa</option>
<option value="194">Spain</option>
<option value="228">United States</option>
</select>
<input type="submit" name="search" value="Search for Stockist" class="btn btn-primary">
</div>
</form>
<?php
$sql = "SELECT * FROM member ";
if (isset($_POST['search'])) {
$search_term = mysql_real_escape_string($_POST['all_counties']);
$sql .= "WHERE MB_COUNTRY = '{$search_term}' ";
//$sql .= "WHERE MB_COUNTRY IN ('103','39','149','193','194','228')";
$sql .=" OR MB_COMPANY = '{$search_term}' ";
}
$query2 = mysql_query($sql) or die(mysql_error());
?>
<table width="70%" cellpadding="5" cellspace="5" class="table table-hover table-striped">
<tr>
<td><strong>Company Name</strong></td>
<td><strong>Website</strong></td>
<td><strong>Phone</strong></td>
<td><strong>Address</strong></td>
</tr>
<?php
$a = "MB_COUNTRY";
$b = "103";
$c = "39";
$d = "149";
$e = "193";
$f = "194";
$g = "228";
if( in_array($a, array($b,$c,$d,$e,$f,$g)) ){
?>
<?php while ($row = mysql_fetch_array($query)) { ?>
<tr>
<td><?php echo $row['MB_COMPANY'];?></td>
<td><?php echo $row['MB_MOBILE'];?></td>
<td><?php echo $row['MB_PHONE'];?></td>
<td><?php echo $row['MB_ADDRESS1'];?>, <?php echo $row['MB_ADDRESS2'];?>, <?php echo $row['MB_TOWN'];?>, <?php echo $row['MB_COUNTY'];?></td>
</tr>
<?php } ?>
<?php } ?>
</table>
<a class="btn btn-primary" href="login_member.php">Member Login</a>
</section>
<!-- end: Page section -->
</div>
</div>
</div>
<!-- end: Container -->
<?php
include_once("includes/footer.php");
?>
I have tried putting an IF around my table that says if MB_COUTRY = $b,$c,$d,$e,$f,$g then print records. However when using that my table displays no records however there are no errors.
I want to run both queries on the same field (MB_COUNTRY) but at different times.
If anyone knows of a query that would help and how to run them at the right times that would be a massive help.
You need to do the if on every row for it to work. Also, you need to test on a value from $row and not an arbitrary value. Your if will need another instruction to test on the user_search to make sure it is not already filtered.
<?php
$a = "MB_COUNTRY";
$b = "103";
$c = "39";
$d = "149";
$e = "193";
$f = "194";
$g = "228";
while ($row = mysql_fetch_array($query)) {
if( in_array($row[$a], array($b,$c,$d,$e,$f,$g)) ){?>
<tr>
<td><?php echo $row['MB_COMPANY'];?></td>
<td><?php echo $row['MB_MOBILE'];?></td>
<td><?php echo $row['MB_PHONE'];?></td>
<td><?php echo $row['MB_ADDRESS1'];?>, <?php echo $row['MB_ADDRESS2'];?>, <?php echo $row['MB_TOWN'];?>, <?php echo $row['MB_COUNTY'];?></td>
</tr>
<?php } ?>
<?php } ?>

Categories