php pagination and search - search

i have a while loop with pagination i have i idea how to make a search just don't know how to insert it here's my code the pagination and output commands are working just don't know how to put the code for search and it's messing my head.
//Count the total number of row in your table*/
$count_query = mysql_query("SELECT COUNT(personid) AS numrows FROM persons");
$row = mysql_fetch_array($count_query);
$numrows = $row['numrows'];
$total_pages = ceil($numrows/$per_page);
$reload = 'index.php';
//main query to fetch the data
$query = mysql_query("SELECT * FROM persons ORDER by RAND() LIMIT $offset,$per_page");
//loop through fetched data
while($result = mysql_fetch_array($query)){
$id = $result['PersonID'];
echo "<div class= content > ";
echo"<img height=100 width=100 src='upload/". $result['Image'] ."'/>";
echo "<font color='black'>". $result['FirstName']. "</font></br>";
echo "</div>";
so as i do trial and error i think the part i got error is the row here's my whole code
> <?php include_once('includes/dbConnect.php');
>
>
> ?>
>
> <?php
>
> // now you can display the results returned. But first we will display
> the search form on the top of the page
>
> $searchText = $_POST["q"];
>
>
>
>
>
> $action = (isset($_REQUEST['action'])&& $_REQUEST['action']
> !=NULL)?$_REQUEST['action']:'';
>
> if($action == 'ajax'){
>
> include 'pagination.php'; //include pagination file
>
>
> //pagination variables $page = (isset($_REQUEST['page']) &&
> !empty($_REQUEST['page']))?$_REQUEST['page']:1; $per_page = 5; //how
> much records you want to show $adjacents = 5; //gap between pages
> after number of adjacents $offset = ($page - 1) * $per_page;
>
> //Count the total number of row in your table*/ $count_query =
> mysql_query("SELECT COUNT(personid) AS numrows FROM persons"); $row
> = mysql_fetch_array($count_query); $numrows = $row['numrows']; $total_pages = ceil($numrows/$per_page); $reload = 'index.php';
>
> //search
> // basic SQL-injection protection
>
> $searchText = $_REQUEST["q"]; //main query to fetch the data
> // query with simple search criteria $query = mysql_query("SELECT * FROM persons WHERE FirstName LIKE '%"
> . $searchText . "%' ORDER by RAND() LIMIT $offset,$per_page");
>
> //loop through fetched data
>
>
> while($result = mysql_fetch_array($query)){
> $id = $result['PersonID'];
>
>
> echo "<div class= content > ";
>
> echo"<img height=100 width=100 src='upload/". $result['Image'] ."'/>";
> echo "<font color='black'>". $result['FirstName']. "</font></br>";
>
>
>
> echo "</div>";
>
>
> } echo paginate($reload, $page, $total_pages, $adjacents); } else{ ?>
>
>
> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Simple Ajax
> Pagination With PHP And MySql</title> <script type="text/javascript"
> src="jquery-1.5.2.min.js"></script> <link media="screen"
> href="style.css" type="text/css" rel="stylesheet"> <script
> type="text/javascript"> $(document).ready(function(){ load(1); });
>
> function load(page){ $("#loader").fadeIn('slow'); $.ajax({
> url:'index.php?action=ajax&page='+page, success:function(data){
> $(".outer_div").html(data).fadeIn('slow');
> $("#loader").fadeOut('slow'); } })
>
> }
>
> </script>
>
> </head> <body>
>
>
>
> <div id="loader"><img src="loader.gif"></div>
>
>
>
> <div class="outer_div"></div>
>
> <div class="content"><form action='' method='POST'> <input type='text' name='q' /> <INPUT TYPE="button" onClick="history.go(0)"
> VALUE="Refresh"/> </p> </form></div> </body> </html> <?php
>
> }?>
this is the output i want
1: http://i.stack.imgur.com/l8MMA.png
2: http://i.stack.imgur.com/p47UI.png

In your case you can simply add WHERE clause with pattern condition and receive desired effect quickly.
// basic SQL-injection protection
$searchText = htmlspecialchars ($_POST['searchText']);
// query with simple search criteria
$query = mysql_query("SELECT * FROM persons WHERE FirstName LIKE '%"
. $searchText . "%' ORDER by RAND() LIMIT $offset,$per_page");
But this approache have several disadvatages:
Your request is very slow (you will see it with more data in your DB) because you use ORDER BY RAND() construction which sorts randomly all entries in DB table and then returns small amount specified in your LIMIT clause;
It is neseccary to reload page every time you want to search something. If you want to implement dynamic update of search results list you should use AJAX queries from Javascript.
P.S.: Try not to use deprecated mysql_ functions, use PDO or mysqli indstead (they provide built-in SQL-injection protection trough the prepared statments).
UPDATE:
Ok, you are already using AJAX.
So you don't need form at all. Use 2 elements: text input and button.
HTML:
<input id="q" type='text' name='q' />
<input type="button" onClick="load(1)" value="Refresh"/>
Javascript:
function load(page){
$("#loader").fadeIn('slow');
var searchText = $('#q').val();
$.ajax({
url: 'index.php?action=ajax&page='+page+'&q='+searchText,
success: function(data){
$(".outer_div").html(data).fadeIn('slow');
$("#loader").fadeOut('slow');
}
});
}

So one approach might be to explode the string into tokens and then place those into the query as like commands:
$sql = "SELECT * FROM persons WHERE ";
$tokens = explode(" ", $searchText);
if (count($tokens) == 0) {
$sql += "1 = 1"
}
else {
$i = 0;
foreach ($tokens as $val) {
if ($i > 0) {
$query += " OR ";
}
$i++;
$query += "(firstname LIKE '%$val%' OR lastname LIKE '%$val%')";
}
}
$sql += " ORDER by RAND() LIMIT $offset, $per_page";
$query = mysql_query($sql);
NOTE: I left your query open to SQL injection. Primarily because I don't want to rewrite it to use mysqli. That's something you need to do. You'd need a counter for the number of tokens that exist and you'd name your parameters something like $token1, $token2, etc.

Related

puppeteer text box with no id

I want to use Puppeteer to enter a value in an input field. Ive done it for most of a web page but having a real problem with a specific field that doesn't have an id or a good label.
here is the inspect elements
<div class="retype-security-code"><input type="text" class="form-text mask-cvv-4" aria-label="Security code" placeholder="CVV2" value=""><img src="https://c1.neweggimages.com/WebResource/Themes/Nest/images/cvv2_sm.gif" alt="cvv2"></div>
<input type="text" class="form-text mask-cvv-4" aria-label="Security code" placeholder="CVV2" value="">
image of code above
here is some code that Ive been playing with
while (true) {
try {
await page.waitForSelector('#cvv2Code' , {timeout: 500})
await page.type('#cvv2Code', config.cv2)
break
}
catch (err) {}
try {
await page.waitForSelector('#creditCardCVV2' , {timeout: 500})
await page.type('#creditCardCVV2', config.cv2)
break
}
catch (err) {}
try {
await page.waitForSelector('#app > div > section > div > div > form > div.row-inner > div.row-body > div > div:nth-child(3) > div > div.checkout-step-body > div.checkout-step-done > div.card > div.retype-security-code > input' , {timeout: 500})
await page.focus('#app > div > section > div > div > form > div.row-inner > div.row-body > div > div:nth-child(3) > div > div.checkout-step-body > div.checkout-step-done > div.card > div.retype-security-code > input')
await page.keyboard.type('###')
break
}
catch (err) {}
}
Why are you using #cvv2Code and #creditCardCVV2 as selectors when they are not in your html code, nor in the picture?
This class form-text mask-cvv-4 seems like a reasonable option for the field:
await page.waitForSelector('.form-text.mask-cvv-4');
Those selectors in the last try block are too long, that's unmaintainable and hard to read, avoid writing such selectors.
Also, please add all relevant error messages to your question, it's hard(er) to help you without it.
What I found you can do is find the element using querySelector and then use setAttribute to give the elemtent an id.
So something like
await page.evaluate(() => {
const inputBox = document.querySelector(".form-text mask-cvv-4");
inputBoxes.setAttribute('id', 'inputBox1');
});
await page.type("#inputBox1", "yourText");
If the element has children then you'd just have to get the children from the element. If there is multiple elements with the class tag you can use querySelectorAll and loop through that list.

select option from Bot response

I am new to API.ai. I want to functionality such that when a Bot response says something to select from various options, user will select one of the options.
e.g.,
If Bot response says 'Which type of websites do you want to build?'
Select one of the given options.
1) Static
2) Dynamic
3) One page.
I have gone though various helps from the documentation , but I do not know how to set this up.
Please help.
This is your answer :
$arr = [];
if ($var == "Which type of websites do you want to build?") {
$arr[] = "Static";
$arr[] = "Dynamic";
$arr[] = "One page";
} else {
$arr[] = "option1";
$arr[] = "option1";
$arr[] = "option1";
}
Html Code:
<select name="website">
<?php
foreach ($arr as $key => $value) {
?>
<option value="<?php echo $value ?>"><?php echo $value; ?></option>
<?php
}
?>
</select>
Let me know if you require more details.

Template for MIGX MODx

I have some PHP foreach that i have moved to MIGX, now can somebody know how to make template for MIGX, here is my PHP
<?php
$i = 0;
$y = 0;
$active = 'active';
echo '<ol class="carousel-indicators">';
foreach(glob($dir) as $file) {
if ($i < 1) {
echo '<li data-target="#myCarousel" data-slide-to="' . $i . '" class="active"></li>';
$i = $i + 1;
}
else {
echo '
<li data-target="#myCarousel" data-slide-to="' . $i . '"></li>';
$i = $i + 1;
}
}
echo '</ol>';
echo '<div class="carousel-inner">';
foreach(glob($dir) as $file) {
$y = $y + 1;
if ($y == 1) {
echo '
<div class="' . $active . ' item">
<img class="img_book" src="' . $file . '" alt="">
</div>
';
}
else {
$active = 'not-active';
echo '
<div class="' . $active . ' item">
<img class="img_book" src="' . $file . '" alt="">
</div>
';
}
}
echo '</div>';
The MIGX Extra comes with a Snippet getImageList that will parse the values in the MIGX TV and return them based on a Chunk template that you specify.
For example, you could put this snippet call in your Template:
<div class="carousel-inner">
[[getImageList?
&tvname=`myMIGXtv`
&tpl=`myTplChunk`
]]
</div>
The snippet would return the values stored in the MIGX TV named myMIGXtv, in the currently requested Resource, and format the output based on the tpl Chunk names myTplChunk. The content of myTplChunk would be something like:
<div class="[[+idx:is=`1`:then=`active`:else=``]] item">
<img class="img_book" src="[[+file]]" alt="[[+another_migx_field]]">
</div>
The syntax of calling the [[+idx]] placeholder with a : after the tag name, invokes the MODX output modifier class, which supports conditionals.
Here are some reference materials for the methods described above:
Documentation for MIGX getImageList
Documentation for MODX
Output Modifiers
The right way and wrong way to use conditional
MODX Output Modifiers

Get data from previous page

I create a popup window using onclick="window.open()" the question is how can i get the data from previous page? here is my code :
<?php
include ("conn.php");
extract($_GET);
$applicantID = $_GET['applicantID'];
$sql = "SELECT * FROM applicant WHERE applicantID ='$applicantID'";
$query = mysql_query($sql);
$data = mysql_fetch_array($query);
echo $data['applicantID'];
?>
When i echo $data['applicantID']; it doesn't show any data.
I use this <input type="button" value="Check" onclick="window.open('checkstatus.php','popup','width=800,height=800,scrollbars=yes,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false" />
Why don't you use window.open('checkstatus.php?applicantID=something')? And retrieve it by $applicantID = $_GET['applicantID'] so that I hope it will work.

Is it possible to 'freeze panes' in SharePoint lists?

I need to keep my columns header, so people can see what data is displayed there, but I can not find if it is possible, or how to do it. Also will be nice to add a navigation bar to the list. Any suggestions?
Thanks,
I had similar challenge recently, and worked out the following code. The code works for IE, but there's a problem with the width of the freeze header in Chrome.
Anyway, hope it helps.
<script type="text/javascript">
$(document).ready(function(){
// Replace 'NameOfList' with the name of the SharePoint list
var $header = $("table[summary^='NameOfList']:first > tbody > tr.ms-viewheadertr");
var headertop = $header.offset().top;
// Replace 'NameOfColumn' with the name of the column that you would like to freeze
var $fzCol= $("tr.ms-viewheadertr th:contains('NameOfColumn')");
// IE has iFrame, Chrome doesn't have, so the 'n-th' count of the column is different in IE than in Chrome
if( $fzCol.siblings().eq(0).children().eq(0).prop("tagName") == "IFRAME"){ var shift = 0} else { var shift = 1};
var nfzCol=$fzCol.index()+shift;
var $mcol=$("table[summary^='NameOfList'] > tbody > tr:not(.ms-viewheadertr) > td:nth-child("+nfzCol+")");
var colleft=$mcol.eq(0).offset().left;
$(window).scroll(function(){
var windowtop = $('body').scrollTop();
if( windowtop > headertop ){
$header.css({"position":"absolute", "top":windowtop});
} else {
$header.css({"position":"static", "top":"0px"});
}
var windowleft = $('body').scrollLeft();
if (windowleft > colleft ){
$mcol.css({"position":"relative", "left": windowleft-colleft});
} else {
$mcol.css({"position":"static", "left":"0px"});
}
});
}

Resources