I am getting " Notice: Undefined index:k" in the PHP code. K is the name of the text field and I am using $_GET[] method to get the value of k. In the below mentioned example I am trying to keep the value available even after the form is submitted. This Code runs fine for the first time but the second time it gives the above error.
<form name="keywordquery" method="get" action="keywordsearch.php">
<fieldset class="fieldsetclass"><legend class="legendclass">Search by
Keywords</legend>
<div id="searchbox">
<input type="text" name="k" value="<?php if(isset($_GET['k'])){echo
htmlentities($_GET['k']);} ?>" style="border: 1px, thin; width:92%; "/> <input
type="image" style="margin-bottom: 0; margin-top: 2px;" src="search.png"
value="submit" />
</div>
</fieldset>
</form>
</div>
<table id="dataTable" cellpadding="0" cellspacing="0" border="1" style="border-
radius:20px; box-shadow: 9px 5px 8px #7E9044;">
<tbody>
<?php
// PAGINATION Code. check if the starting row variable was passed in the
URL or not
if (!isset($_GET['startrow']) or !is_numeric($_GET['startrow'])) {
//we give the value of the starting row to 0 because nothing was found in URL
$startrow = 0;
//otherwise we take the value from the URL
} else {
$startrow = (int)$_GET['startrow'];
}
$k1 = $_GET['k'];
$term = explode(" ", $k1);
$query = "SELECT * FROM data ";
foreach ($term as $each) {
$i++;
if($i==1)
{
$query .= "WHERE keywords LIKE '%$each%' ";
}
else {
$query .= " OR WHERE keywords LIKE '%$each%' ";
}
}
$query .= "LIMIT $startrow, 1";
$connection = mysql_connect("xxxx", "xxxxx","");
if(!$connection)
echo "No database connected";
$dbase = mysql_select_db("xxxxxxxx", $connection);
if(!$dbase)
echo "No datatable connected";
$ourquery1 = mysql_query ($query);
if(!$ourquery1)
echo "No query found";
$row1 = mysql_num_rows ($ourquery1);
if ($row1 > 0)
{
while($result = mysql_fetch_assoc($ourquery1))
{
echo "<tr>";
$title = $result['title'];
$link = $result['link'];
$region = $result['region'];
$sector = $result['sector'];
$theme = $result['theme'];
echo "<td> <a href=$link><h3>$title<h3></a>";
echo "<h4>Sector: $sector
Theme: $theme
<br> Region: $region </td>";
}
}
echo "</tbody>";
echo 'Next';
echo 'Prev';
Replace the line:
$k1 = $_GET['k'];
with something like:
$k1 = isset($_GET['k'])? $_GET['k'] : $default_k_value;
You don't show the complete form so it is tough to tell what is wrong but here is a hint. Swap $_GET with $_REQUEST.
Example:
<?php if(isset($_REQUEST['k'])){echo
htmlentities($_REQUEST['k']);} ?>
If the form is using a POST method, the value would be in $_POST. If the form uses a GET method, the value would be in $_GET. However $_REQUEST will contain the form fields whether the form used POST or GET.
Related
I am trying to keep my pagination based on the query I am using, but my problem is, it only works on the first page of pagination, and after that the query reverts to the standard one without filters (page one shows my filter, but page two show all results). I am wondering if there is an effective method that will carry over my filtered query when I click my pages, I am just at a loss right now as to how to accomplish this. Here is my code currently:
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<style type="text/css">
#win-range, #gaa-range, #sv-range{
width: 160px;
font-size: 10px;
margin: 0 auto;
}
#win-range a, #gaa-range a, #sv-range a{
margin-top: 0px !important;
padding: 0 !important;
}
</style>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>
$(function(){
$("#win-range").slider({
range: true,
min: 1,
max: 1000,
values: [1, 1000],
slide: function(event, ui) {
// in order to pass the user selected values to your app, we will use jQuery to prepopulate certain hidden form elements, then grab those values from the $_POST
$("#minwins").val(ui.values[0]);
$("#maxwins").val(ui.values[1]);
$("#winamount").val(ui.values[0] + " - " + ui.values[1]);
}
});
$("#winamount").val($("#win-range").slider("values", 0) + " - " + $("#win-range").slider("values", 1));
});
$(function(){
$("#gaa-range").slider({
range: true,
min: 0,
max: 10,
values: [0, 10],
slide: function(event, ui) {
// in order to pass the user selected values to your app, we will use jQuery to prepopulate certain hidden form elements, then grab those values from the $_POST
$("#mingaa").val(ui.values[0]);
$("#maxgaa").val(ui.values[1]);
$("#gaaamount").val(ui.values[0] + " - " + ui.values[1]);
}
});
$("#gaaamount").val($("#gaa-range").slider("values", 0) + " - " + $("#gaa-range").slider("values", 1));
});
$(function(){
$("#sv-range").slider({
range: true,
min: 750,
max: 1000,
values: [750, 1000],
slide: function(event, ui) {
// in order to pass the user selected values to your app, we will use jQuery to prepopulate certain hidden form elements, then grab those values from the $_POST
$("#minsv").val(ui.values[0]);
$("#maxsv").val(ui.values[1]);
$("#svamount").val(ui.values[0] + " - " + ui.values[1]);
}
});
$("#svamount").val($("#sv-range").slider("values", 0) + " - " + $("#sv-range").slider("values", 1));
});
</script>
<?php
include("includes/header.php");
include("includes/mysqli_connect.php");
$minwins = $_POST['minwins'];
$maxwins = $_POST['maxwins'];
$mingaa = $_POST['mingaa'];
$maxgaa = $_POST['maxgaa'];
$minsv = $_POST['minsv'];
$maxsv = $_POST['maxsv'];
$querySelection = $_POST['q'];
// FILTERING YOUR DB
$sortstats = $_GET['sortstats'];
$sortstatslow = $_GET['sortstatslow'];
// pagination
$getcount = mysqli_query ($con,"SELECT COUNT(*) FROM Player");
$postnum = mysqli_result($getcount,0);// this needs a fix for MySQLi upgrade; see custom function below
$limit = 6; //how many blog posts per page you will see.
if($postnum > $limit){
$tagend = round($postnum % $limit,0);
$splits = round(($postnum - $tagend)/$limit,0);
if($tagend == 0){
$num_pages = $splits;
}else{
$num_pages = $splits + 1;
}
if(isset($_GET['pg'])){
$pg = $_GET['pg'];
}else{
$pg = 1;
}
$startpos = ($pg*$limit)-$limit;
$limstring = "LIMIT $startpos,$limit";
}else{
$limstring = "LIMIT 0,$limit";
}
// MySQLi upgrade: we need this for mysql_result() equivalent
function mysqli_result($res, $row, $field=0) {
$res->data_seek($row);
$datarow = $res->fetch_array();
return $datarow[$field];
}
?>
<div class="listingcontainer">
<div class="sidebar">
<h3>Sort By:</h3>
Most Wins
Best Goals Against
Best Save %
<hr/>
<h3>Custom Filter</h3>
<br/>
<div class="custom-filter">
<form name="filters" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="filters">
<label for="winamount">Win Range:</label>
<input type="text" id="winamount" />
<div style="clear:both;"></div>
<input type="hidden" id="minwins" name="minwins" value="0" />
<input type="hidden" id="maxwins" name="maxwins" value="1000" />
<div id="win-range"></div>
<br/>
<label for="gaaamount">GAA:</label>
<input type="text" id="gaaamount" /><br />
<div style="clear:both;"></div>
<input type="hidden" id="mingaa" name="mingaa" value="0" />
<input type="hidden" id="maxgaa" name="maxgaa" value="10" />
<div id="gaa-range"></div>
<br/>
<label for="svamount">SV %:</label>
<input type="text" id="svamount" /><br />
<div style="clear:both;"></div>
<input type="hidden" id="minsv" name="minsv" value="750" />
<input type="hidden" id="maxsv" name="maxsv" value="1000" />
<div id="sv-range"></div>
<input type="submit" name="submit" id="submit"/>
</form>
</div>
</div>
<div class="main-listings">
<h1>Current NHL Goaltenders</h1>
<?php
$result = mysqli_query($con, "SELECT * FROM Player ORDER BY PlayerID ASC $limstring");
if(isset($sortstats)) {
$result = mysqli_query($con,"SELECT * FROM Player ORDER BY $sortstats DESC $limstring ") or die (mysql_error());
}
if(isset($sortstatslow)) {
$result = mysqli_query($con,"SELECT * FROM Player ORDER BY $sortstatslow ASC $limstring ") or die (mysql_error());
}
if(isset($_POST['submit']))
{
$result = mysqli_query($con, "SELECT * FROM Player WHERE Wins BETWEEN '$minwins' AND '$maxwins' AND
GAA BETWEEN '$mingaa' AND '$maxgaa' AND SavePerc BETWEEN '$minsv' AND '$maxsv'
ORDER BY PlayerID ASC $limstring") or die (mysql_error());
}
while($row = mysqli_fetch_array($result)){
$name = $row['LastName'] . ", " . $row['FirstName'];
$wins = $row['Wins'];
$pid = $row['PlayerID'];
$image = $row['Picture'];
$gaa = $row['GAA'];
$sv = $row['SavePerc'];
echo "<div class=\"player-listing\">";
echo "<div class=\"image-holder\">";
echo "<span class=\"helper\"></span>";
echo "<img src=\"admin/thumbs/$image\" alt=\"$name\">";
echo "</div>";
echo "<div style=\"clear:both;\"></div>";
echo "$name";
echo "<table align=\"center\">";
echo "<tr>";
echo "<td style=\"border-bottom: 1px solid #212121;\">Wins</td>";
echo "<td style=\"border-bottom: 1px solid #212121;\">GAA</td>";
echo "<td style=\"border-bottom: 1px solid #212121;\">SV%</td>";
echo "</tr>";
echo "<tr>";
echo "<td>$wins</td>";
echo "<td>$gaa</td>";
echo "<td>.$sv</td>";
echo "</tr>";
echo "</table>";
echo "</div>";
}
// paging links:
echo "<div class=\"paging\">";
if($postnum > $limit){
echo "<span class=\"page-numbers\"><strong>Pages:</strong> </span>";
$n = $pg + 1;
$p = $pg - 1;
$thisroot = $_SERVER['PHP_SELF'];
if($pg > 1){
echo "<< prev ";
}
for($i=1; $i<=$num_pages; $i++){
if($i!= $pg){
echo "$i ";
}else{
echo "$i ";
}
}
if($pg < $num_pages){
// INSERT QUERY STRING VARIBLE TO CARRY OVER DB QUERY
echo "next >>";
}
echo " ";
}
// end paging
echo "</div>";
?>
</div>
<div style="clear:both;"></div>
Save you search filters in session and always check the session value is set for any search or not and pass that data into you query.until and unless nobody reset that search criteria and when your user reset that search criteria to null then put the value blank in the session for that session search variable.
You need to pass all filter conditions to the paginating link, for example
$filter = "minwins={$minwins}&maxwins={$maxwins}&mingaa={$mingaa}&minsv={$minsv}&maxsv={$maxsv}&q={$querySelection}";
if($pg > 1){
echo "<< prev ";
}
for($i=1; $i<=$num_pages; $i++){
if($i!= $pg){
echo "$i ";
}else{
echo "$i ";
}
}
if($pg < $num_pages){
// INSERT QUERY STRING VARIBLE TO CARRY OVER DB QUERY
echo "next >>";
}
In the code for getting filter conditions from $_POST you are using, change it to $_REQUEST because it should get from URL ($_GET).
Ex
$minwins = $_REQUEST['minwins'];
You could always use the below to create a list of URL keys (filters).
foreach($_GET as $key => $value){
$url_keys .= $key . "=" . $value . "&";
}
And then add that into your URL for next/prev page.
echo "next >>";
You will then be passing your filters through the URL and have access to them on the next page.
I made some little translator.
And if word exist in database all is ok. But if word didnt exist in database, word isnt printed. I add some code to print if result is empty but didnt work.
Any idea why word that not exist in database isnt printed?
Here is code:
<p><form method="post" action="prevedi.php">
<textarea id="prevedi" name="prevedi" style="margin: 2px; height: 137px; width: 380px;">
<?php
echo htmlentities($_POST['prevedi']);
?>
</textarea>
<textarea id="prevod" disabled="disabled" name="prevod" style="margin: 2px; height: 137px; width: 380px; border: 0px;" readonly>
<?
if (isset($_POST['prevedi'])) {
//Kreci
$prevedi = htmlentities($_POST['prevedi']);
$prevedi = explode(" ",$prevedi);
foreach ($prevedi as $word) {
$slovo = $word[0];
$result = mysqli_query($con,"SELECT * FROM $slovo WHERE srpski='$word'");
if (!empty($result)) {
while($row = mysqli_fetch_array($result))
{
$prevod .= $row['romski']." ";
}
}
else
{
$prevod .= "".$word." ";
}
}
echo $prevod;
//Kraj isset
}
?>
</textarea><br>
<input name="translate" type="submit" value="Translate"/>
</form>
</p>
Because your result is not empty. You should check if $result number of rows is > 0
if ($result->num_rows > 0) {
while($row = mysqli_fetch_array($result)){
$prevod .= $row['romski']." ";
}
}else{
$prevod .= $word." ";
}
i think you have to initialize $prevod before assigning it the way you did.
$prevod = "";
if (!empty($result)) {
while($row = mysqli_fetch_array($result))
{
$prevod .= $row['romski']." ";
}`enter code here`
}
else
{
$prevod .= "".$word." ";
}
}
echo $prevod;
here is my code. actually i am displaying some data from mysql on the page and creating dynamic link.i want started a session with session_start() in the very begining of code before starting any code. i want to store the value of the link that is to be display on other pagepage..
page1.php
<a style="color:#F00; font-family:Arial, Helvetica, sans-serif; margin-left:33px; font-weight:bold">
No. of registered students:
</a>
<table border='1' align="center" style="font-size:14px" width="95%" cellspacing="3" class="db_table">
<tr class="db_table_tr" >
<th class="db_table_th" name="submit">USN</th>
</tr>
<?php
include('includes/login_connection.php');
$query = "select p.usn, p.name from personal_details p, course_codes c where p.usn = c.usn order by p.usn";
$run = mysql_query($query) or die($query."<br/><br/>".mysql_error());
$num = mysql_numrows($run);
echo $num;
while($row = mysql_fetch_assoc($run)){
echo "<tr>";
echo "<td>" . $row['usn'] . "" . "</td>";
echo "<td>" . $row['name'] . " </td>";
if(isset($_GET['submit'])){
$_SESSION['session_usn'] = $_GET['usn'];
}
}
echo "</tr>";
mysql_close($bd);
?>
</table>
page2.php
<?php
session_start();
if(isset($_SESSION['session_usn']))
{
$_POST['usn'] = $_SESSION['session_usn'];
echo $_POST['usn'];
}
?>
You need to provide a fall-back, in case the URL provided does not contain the proper variables in the $_GET section.
You have:
if(isset($_GET['submit'])){
$_SESSION['session_usn'] = $_GET['usn'];
}
You should do something else if $_GET['submit'] isn't set:
if(isset($_GET['submit'])){
$_SESSION['session_usn'] = $_GET['usn'];
} else {
$_SESSION['session_usn'] = "unset";
// or set a warning flag like "unset"
}
You should be feeding your php file a url like:
http://yoururl.com/page1.php?usn='333'
Where 333 is the value you want to store.
I am working on a very basic random name generator. The generator works great, and is called by an HTML form submit button. Now, in the case that a user wanted to save the name, I will have a second button that will call a PHP mail action to email the name. Before I add the mail function, I'm just trying to store the variable somewhere so that I can alert it and validate that I have control over it. In the following code $field2 is the name which is located by $id_num which equals $search_str. I then try and assign $field2 to the global variable $nameContainer:
<body>
<?
$nameContainer = "Mary";
?>
<?
function loadName() {
global $nameContainer;
if(!empty($_POST['act'])) {
$lineNum = rand(1, 5189);
$search_str = $lineNum;
$lines = file('names_girls_5189_origin.csv');
foreach($lines as $line){
list($id_num, $field2, $field3, $field4)=explode(",",$line);
$nameContainer = ($id_num == $search_str) ? "$field2" : "";
echo ($id_num == $search_str) ?
"<table style= 'height: 100%;'>
<tr style='width: 100%; height= '200px';'>
</tr>
<center><span class='myText' style='color: white; font-size: 48px;'>$field2</span>
<span style='color: #E8C8D5; font-size: 36px'>Smith</span><br/>
<span class='myText' style='color: #2E7B4D; font-size: 24px;'>Origin: $field3</span><br/>
<span class='myText' style='color: #2E7B4D; font-size: 24px;'>Meaning: $field4</span></center>
</table>" : "";
}
}
}
loadName();
?>
<script type="text/javascript">
function alertName() {
alert("<? getName(); ?>");
}
</script>
<?
function getName() {
global $nameContainer;
echo $nameContainer;
}
?>
<center><form action="index.php" method="post">
<input type="hidden" name="act" value="run" />
<p><input type="submit" value="New" /></p>
</form>
</center>
<center><button onclick="alertName()">Alert</button></center>
</body>
It appears my problem stems from $nameContainer = ($id_num == $search_str) ? "$field2" : ""; because $nameContainer = $field2; works but assigns the very last name in the csv rather than the one that matches the search string. I realize there's probably some basic logic I'm missing here, so any help is appreciated!
Update (sample csv):
ID,Name,Origin,Meaning
1,Aaliyah,"Arabic, Hebrew","high exalted, to ascend"
2,Akala,Aboriginal,A parrot.
3,Alba,Aboriginal,A sand hill. Also see Albina.
5188,Zelma,,A divine helmet. From the name Anselma. Also see Salima.
5189,Zola,,Life.`
I think, you need to check if $nameContainer has content and break the loop:
$nameContainer = ($id_num == $search_str) ? "$field2" : "";
if(!empty($nameContainer)){
break;
}
But, to make it work, you need to initialize $nameContainer with empty string:
$nameContainer = "";
It seems to me you're using $search_str to pick a line (an element of the $lines array) at random. You should use array_rand for this:
function loadName() {
global $nameContainer;
if(!empty($_POST['act'])) {
list($id_num, $field2, $field3, $field4)=explode(",",array_rand(file('names_girls_5189_origin.csv')));
$nameContainer = $field2;
echo "<table style= 'height: 100%;'>...</table>" : "";
}
}
In a site I'm making, I need a search engine to find songs for people to listen to. I have it working to the point that it can get info from the database and display them on the page. The problem comes when there are two songs with the same name. I have a system so the results will go to separate links, but when I search them they display the same image even though there is two separate sources for them. It also will make extra results for some reason. Here's my code:
<?php
if (isset($_GET['q'])) {
$q = $_GET['q'];
mysql_connect('********', '********', '********');
mysql_select_db('********');
$query = mysql_query("SELECT * FROM ******** WHERE title LIKE '$q'");
$numrows = mysql_num_rows($query);
} else {
echo "
<span style='font-family: Helvetica, Arial;font-weight: bold;font-size: 25px;'>Search</span>
<form action='http://www.example.com/search' method='get'>
<input placeholder='Search for music' type='text' name='q' style='font-weight:bold;padding:5px;width:300px;border-top-left-radius: 4px;border-top-right-radius: 10px;border-bottom-left-radius: 10px;border-bottom-right-radius: 4px;border: 3px solid gray;background-color:#000000;color:#FFFFFF;' />
</form>
";
}
if ($numrows != 0) {
$index = 0;
$results = array();
while($row = mysql_fetch_assoc($query)) {
$results[$index] = $row;
$index++;
foreach ($results as $result) {
$url = "http://www.example.com?id=" . $row['id'];
$title = $row['title'];
$arturl = $row['art_url'];
if ($_GET['q'] != "") {
echo "
<a href='$url'>
<table>
<tr style='text-align:left;'>
<td><img src='$arturl' style='width:100px;height:100px;'></td>
<td>
<span class='songTitle'>$title</span>
<br/>
<span class='songArtist'>By: Unknown</span>
</td>
</tr>
</table>
</a>
<br />
";
}
}
}
} else {
if ($_GET['q'] != "") {
echo "
<span style='font-family: Helvetica, Arial;font-weight: bold;font-size: 25px;'>Search</span>
<form action='********' method='get'>
<input placeholder='Search for music' type='text' name='q' style='font-weight:bold;padding:5px;width:300px;border-top-left-radius: 4px;border-top-right-radius: 10px;border-bottom-left-radius: 10px;border-bottom-right-radius: 4px;border: 3px solid gray;background-color:#000000;color:#FFFFFF;' />
</form>
";
echo "<br />No results where found.";
}
}
?>
while($row = mysql_fetch_array($result))Try $result['title'] instead of $row['title'];. Same goes to $url and $arturl
This should work.
if ($numrows != 0) {
$index = 0;
$results = array();
while($row = mysql_fetch_array($query)) {
$url = "http://www.example.com?id=" . $row['id'];
$title = $row['title'];
$arturl = $row['art_url'];
if ($_GET['q'] != "") {
echo "
<a href='$url'>
<table>
<tr style='text-align:left;'>
<td><img src='$arturl' style='width:100px;height:100px;'></td>
<td>
<span class='songTitle'>$title</span>
<br/>
<span class='songArtist'>By: Unknown</span>
</td>
</tr>
</table>
</a>
<br />
";
}
}
}
your codes seem right. However, to clarify, try to echo $arturl and see if it is getting the right source name. And also check the value in the database. Last thing is try to check whether the source image is in the correct folder. Try and give the feedback.