Looking for some help please. I am using the following code to filter and show results which works fine. My problem is the results aren't passing on to the second page so the page becomes blank no errors. very new to this so all help is welcome.
$num_rec_per_page=2;
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("searchresults") or die("ERROR");
if (!isset($_POST['submit'])) exit();
$vars = array('companyname','location');
$verified = TRUE;
foreach ($vars as $v) {
if (!isset($_POST[$v]) || empty($_POST[$v])) {
$verified = FALSE;
}
}
if (!$verified) {
echo "Sorry no results found, please enter your search";
exit();
}
// isset function is also used in checking for the existence of data in $_POST
if (isset($_POST["companyname"]) && $_POST["companyname"] != '') {
$companyname = mysql_real_escape_string($_POST["companyname"]);
$companyname = " AND (companyname LIKE '%$companyname%')";
}
if (isset($_POST["location"]) && $_POST["location"] != '') {
$location = mysql_real_escape_string($_POST["location"]);
$location = " AND (location LIKE '%$location%')";
}
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $num_rec_per_page;
$sql = "SELECT * FROM reviewsandtips WHERE member_id > 0". $companyname . $location . "LIMIT $start_from, $num_rec_per_page";
$sql_result = mysql_query($sql) or die (mysql_error());
// now echo the code for the table heading
if (mysql_num_rows($sql_result)>0) {
while ($row = mysql_fetch_assoc($sql_result)) {
}
$sql = "SELECT * FROM reviewsandtips WHERE member_id > 0". $companyname . $location;
$sql_result = mysql_query($sql); //run the query
$total_records = mysql_num_rows($sql_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='codetest.php?page=1'>".'|<'."</a> "; // Goto 1st page
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='codetest.php?page=".$i."'>".$i."</a> ";
}
echo "<a href='codetest.php?page=$total_pages'>".'>|'."</a> "; // Goto last page
} else {
echo '<tr><td colspan="5">No results found.</td></tr>';
}
Related
I have a C# code and a PHP that gets info I send from C#.
So I need to have such code:
<?php
$link = mysqli_connect('localhost','root','');
$database = mysqli_select_db($link,'serial');
$serial = $_GET['serial'];
$hwid = $_GET['hwidin'];
$sql = "SELECT * FROM serial WHERE serial = '". mysqli_real_escape_string($link,$serial) ."'" ;
$result = $link->query($sql);
/*
0 = Wrong HWID
1 = HWID is correct
2 = HWID left empty
3 = No serial with that key
*/
if(strlen($hwid) < 1)
{
echo "2";
}
else
{
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if (strlen($row['hwid']) > 1)
{
if ($hwid != $row['hwid'])
{
echo "0";
}
else
{
echo "1";
}
}
else
{
$sql = "UPDATE serial SET hwid='$hwid' WHERE serial='$serial'";
echo "1";
if(mysqli_query($link, $sql))
{
echo $row['hwid'];
}
else
{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}
}
}
else
{
echo "3";
}
}
So what I want to do is add some checking, for example:
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if (strlen($row['hwid']) > 1)
{
if ($hwid != $row['hwid'])
{
echo "0";
}
else
{
echo "1";
}
If echo "1" {
$sql = "SELECT time FROM serial Where serial = '". mysqli_real_escape_string($link,$serial) ."'" ;
$result = $link->query($sql);
if $result > 1 {
echo "55";
}
I need somehow to combine it with hwid checker and serial checker.
I want to check "time" column variable and use > < 1 to decide what to echo.
How can I add pagination to a search engine results page?
I have build a search engine but there are hundreds of thousands of results for every search so I want to add pages to it.
The results of the search engine are outputted in a table.
I have started to learn php and sql recently...
How can I add those pages?
I have tried this so far but with no success:
<?php
$con = mysqli_connect(xxxx);
mysqli_select_db($con, 'Data') or die("could not find the database!");
$output = '';
$results_per_page = 1000;
//positioning
if(isset($_GET['search']))
{
$starttime = microtime(true); //TIME
$searchkey = $_GET['search'];
$query = mysqli_query($con, "SELECT * FROM table1 WHERE email LIKE '%$searchkey%'") or die("Could not search") ;
$count = mysqli_num_rows($query);
// count number of pages for the search
$number_of_pages = ceil($count/$results_per_page);
// determine which page number visitor is currently on
if (!isset($_GET['page']))
{
$page = 1;
}
else
{
$page = $_GET['page'];
}
// LIMIT
$this_page_first_result = ($page-1)*$results_per_page;
if ($count == 0)
{
echo "</br>";
$output = 'There are no search results !' ;
}
else
{
echo '<table class="myTable">';
echo "<tr><th>aaa</th><th>bbb</th></tr>";
$query = mysqli_query($con, "SELECT * FROM table1 WHERE email LIKE '%$searchkey%' LIMIT " . $this_page_first_result . ',' . $results_per_page" ") or die("Could not search") ;
while ($row = mysqli_fetch_array($query))
{
$email = preg_replace('/(' . $searchkey . ')/i', '<mark>\1</mark>', $row["aaa"]);
$password = $row['bbb'];
echo "<tr><td>";
echo $aaa;
echo "</td><td>";
echo $bbb;
echo "</td></tr>";
$output = '</table>';
}
//echo "</table>";
$endtime = microtime(true);
$duration = $endtime - $starttime;
echo "</br>";
if ($count == 1)
{
echo "<div class=resinfo>";
echo '<div>'."1 result was found for '$searchkey' in $duration seconds.".'</div>'.'</br>';
echo "</div>";
}
else
{
echo "<div class=resinfo>";
echo '<div>'."$count results were found for '$searchkey' in $duration seconds.".'</div>'.'</br>';
echo "</div>";
}
}
echo $output;
}
//LINKS to other pages
for ($page = 1; $page<=$number_of_pages;$page++){
echo '' . $page . '';
}
?>
What have I done wrong, what can I improve to make it work?
Thanks a lot for your help!
It is not a good idea to build a pagination from scratch, instead use a lib like this: https://github.com/KnpLabs/knp-components/blob/master/doc/pager/intro.md
I have an dymamic PHP file that load Data from MySQL database about some relay card, every user have a different amount of relay card. All card have 8 relays (State 1/0), 8 inputs (State 1/0), and 2 analogue input (State from 0 to 1024) this is for every card, some user will have 1 and some other can have 8 and i would like to update this data as fast as possible. I have done a PHP script that trigger all relays information for a specific user, in this case this is the user 2.
I think the best way to do what i want as what i have read is to call it from jQuery, but i don't understand how-o update many variable.
Here is the code i have to get the latest state from MySQL.
Ok here is an update of the code, i have separated completely the data fetching and the GUI creation, so what i want to do, is show in realtime what the get_states_gui.php file fetch from mysql to the gui.php file. Can someone point me in the good direction since i am completely lost right now! THanx a lot here are those 2 files:
The gui.php:
<?php
require_once('config.php');
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$start = $time;
// We create connection to the MySQL database.
$con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$con) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database\n");
}
$req="SELECT * FROM cartes WHERE utilisateur=$user";
$result = mysql_query($req);
if(!$result) {
die('Query failed: ' . mysql_error());
}
$nb_iplab = mysql_num_rows($result);
$b = "1";
while ($ligne = mysql_fetch_assoc($result))
{
extract($ligne);
${'nom'.$b} = $nom;
${'mac'.$b} = $mac;
${'ip'.$b} = $ip;
for ($i=1; $i <= "8"; $i++ )
{
//We set Relays names for each IPLAB and get their states in the MySQL database.
${'nomR'.$i.$b} = ${'nomR'.$i};
$req = "SELECT * FROM states_log WHERE dev='R$i' AND mac='${'mac'.$b}' ORDER BY date_heure DESC LIMIT 1";
$result2 = mysql_query($req);
if(!$result2)
{
die('Query failed: ' . mysql_error());
}
$ligne = mysql_fetch_assoc($result2);
extract($ligne);
${'stateR'.$i.$b} = $state;
if ( ${'stateR'.$i.$b} == "1" )
{
${'img_linkR'.$i.$b} = "<a href=set_states.php?ip=${'ip'.$b}&cmd=CR$i target=empty> <img src=img/toggle_1.png height=28></a>";
}
else if ( ${'stateR'.$i.$b} == "0" )
{
${'img_linkR'.$i.$b} = "<a href=set_states.php?ip=${'ip'.$b}&cmd=SR$i target=empty> <img src=img/toggle_0.png height=28></a>";
}
else
{
${'img_linkR'.$i.$b} = "<a href=set_states.php?ip=${'ip'.$b}&cmd=CR$i target=empty> <img src=img/LED_yellow.png height=28></a>";
}
//We set Inputs names for each IPLAB and get their states in the MySQL database.
${'nomI'.$i.$b} = ${'nomI'.$i};
${'multI'.$i.$b} = ${'multI'.$i};
${'img_onI'.$i.$b} = ${'img_onI'.$i};
${'img_offI'.$i.$b} = ${'img_offI'.$i};
${'img_naI'.$i.$b} = ${'img_naI'.$i};
$req = "SELECT * FROM states_log WHERE dev='I$i' AND mac='${'mac'.$b}' ORDER BY date_heure DESC LIMIT 1";
$result2 = mysql_query($req);
if(!$result2)
{
die('Query failed: ' . mysql_error());
}
$ligne = mysql_fetch_assoc($result2);
extract($ligne);
${'stateI'.$i.$b} = $state;
if ( ${'stateI'.$i.$b} == "1" )
{
${'img_linkI'.$i.$b} = "<img src=img/${'img_onI'.$i.$b} height=28>";
}
else if ( ${'stateI'.$i.$b} == "0" )
{
${'img_linkI'.$i.$b} = "<img src=img/${'img_offI'.$i.$b} height=28>";
}
else
{
${'img_linkI'.$i.$b} = "<img src=img/${'img_naI'.$i.$b} height=28>";
}
// We check for how many times, the Inputs has changed state to 1, and we set them as a variable for counter.
$req = "SELECT count(*) FROM states_log WHERE dev='I$i' AND state='1' AND mac='${'mac'.$b}'";
$result2 = mysql_query($req);
if(!$result2)
{
die('Query failed: ' . mysql_error());
}
list (${'countI'.$i.$b}) = mysql_fetch_row ($result2);
if (isset(${'multI'.$i.$b}))
{
${'countI'.$i.$b} = ${'countI'.$i.$b} * ${'multI'.$i.$b};
}
}
//We set Analog names fir each IPLAB and get their states in the MySQL database.
for ($i=1; $i <= "3"; $i++ )
{
${'nomA'.$i.$b} = ${'nomA'.$i};
${'unitA'.$i.$b} = ${'unitA'.$i};
$req = "SELECT * FROM states_log WHERE dev='A$i' AND mac='${'mac'.$b}' ORDER BY date_heure DESC LIMIT 1";
$result2 = mysql_query($req);
if(!$result2)
{
die('Query failed: ' . mysql_error());
}
$ligne = mysql_fetch_assoc($result2);
extract($ligne);
${'stateA'.$i.$b} = $state;
}
if ( debug == "1" )
{
echo "${'nom'.$b}<br>";
echo "MAC = ${'mac'.$b}<br>";
for ( $i = 1; $i <= 8; $i++ )
{
echo "${'nomR'.$i.$b} = ${'stateR'.$i.$b}<br>";
echo "${'nomI'.$i.$b} = ${'stateI'.$i.$b}<br>";
}
for ( $i = 1; $i <= 3; $i++ )
{
echo "${'nomA'.$i.$b} = ${'stateA'.$i.$b}<br>";
}
echo "---------------------------------------<br>";
}
$b++;
}
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$finish = $time;
$total_time = round(($finish - $start), 4);
if ( debug == "1" )
{
echo 'Page generated in '.$total_time.' seconds.'."\n";
}
mysql_close($con);
?>
And now the get_states_gui.php:
<?php
require_once('config.php');
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$start = $time;
// We create connection to the MySQL database.
$con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$con) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database\n");
}
$req="SELECT * FROM cartes WHERE utilisateur=$user";
$result = mysql_query($req);
if(!$result) {
die('Query failed: ' . mysql_error());
}
$nb_iplab = mysql_num_rows($result);
$b = "1";
while ($ligne = mysql_fetch_assoc($result))
{
extract($ligne);
${'nom'.$b} = $nom;
${'mac'.$b} = $mac;
${'ip'.$b} = $ip;
for ($i=1; $i <= "8"; $i++ )
{
//We set Relays names for each IPLAB and get their states in the MySQL database.
${'nomR'.$i.$b} = ${'nomR'.$i};
$req = "SELECT * FROM states_log WHERE dev='R$i' AND mac='${'mac'.$b}' ORDER BY date_heure DESC LIMIT 1";
$result2 = mysql_query($req);
if(!$result2)
{
die('Query failed: ' . mysql_error());
}
$ligne = mysql_fetch_assoc($result2);
extract($ligne);
${'stateR'.$i.$b} = $state;
if ( ${'stateR'.$i.$b} == "1" )
{
${'img_linkR'.$i.$b} = "<a href=set_states.php?ip=${'ip'.$b}&cmd=CR$i target=empty> <img src=img/toggle_1.png height=28></a>";
}
else if ( ${'stateR'.$i.$b} == "0" )
{
${'img_linkR'.$i.$b} = "<a href=set_states.php?ip=${'ip'.$b}&cmd=SR$i target=empty> <img src=img/toggle_0.png height=28></a>";
}
else
{
${'img_linkR'.$i.$b} = "<a href=set_states.php?ip=${'ip'.$b}&cmd=CR$i target=empty> <img src=img/LED_yellow.png height=28></a>";
}
//We set Inputs names for each IPLAB and get their states in the MySQL database.
${'nomI'.$i.$b} = ${'nomI'.$i};
${'multI'.$i.$b} = ${'multI'.$i};
${'img_onI'.$i.$b} = ${'img_onI'.$i};
${'img_offI'.$i.$b} = ${'img_offI'.$i};
${'img_naI'.$i.$b} = ${'img_naI'.$i};
$req = "SELECT * FROM states_log WHERE dev='I$i' AND mac='${'mac'.$b}' ORDER BY date_heure DESC LIMIT 1";
$result2 = mysql_query($req);
if(!$result2)
{
die('Query failed: ' . mysql_error());
}
$ligne = mysql_fetch_assoc($result2);
extract($ligne);
${'stateI'.$i.$b} = $state;
if ( ${'stateI'.$i.$b} == "1" )
{
${'img_linkI'.$i.$b} = "<img src=img/${'img_onI'.$i.$b} height=28>";
}
else if ( ${'stateI'.$i.$b} == "0" )
{
${'img_linkI'.$i.$b} = "<img src=img/${'img_offI'.$i.$b} height=28>";
}
else
{
${'img_linkI'.$i.$b} = "<img src=img/${'img_naI'.$i.$b} height=28>";
}
// We check for how many times, the Inputs has changed state to 1, and we set them as a variable for counter.
$req = "SELECT count(*) FROM states_log WHERE dev='I$i' AND state='1' AND mac='${'mac'.$b}'";
$result2 = mysql_query($req);
if(!$result2)
{
die('Query failed: ' . mysql_error());
}
list (${'countI'.$i.$b}) = mysql_fetch_row ($result2);
if (isset(${'multI'.$i.$b}))
{
${'countI'.$i.$b} = ${'countI'.$i.$b} * ${'multI'.$i.$b};
}
}
//We set Analog names fir each IPLAB and get their states in the MySQL database.
for ($i=1; $i <= "3"; $i++ )
{
${'nomA'.$i.$b} = ${'nomA'.$i};
${'unitA'.$i.$b} = ${'unitA'.$i};
$req = "SELECT * FROM states_log WHERE dev='A$i' AND mac='${'mac'.$b}' ORDER BY date_heure DESC LIMIT 1";
$result2 = mysql_query($req);
if(!$result2)
{
die('Query failed: ' . mysql_error());
}
$ligne = mysql_fetch_assoc($result2);
extract($ligne);
${'stateA'.$i.$b} = $state;
}
if ( debug == "1" )
{
echo "${'nom'.$b}<br>";
echo "MAC = ${'mac'.$b}<br>";
for ( $i = 1; $i <= 8; $i++ )
{
echo "${'nomR'.$i.$b} = ${'stateR'.$i.$b}<br>";
echo "${'nomI'.$i.$b} = ${'stateI'.$i.$b}<br>";
}
for ( $i = 1; $i <= 3; $i++ )
{
echo "${'nomA'.$i.$b} = ${'stateA'.$i.$b}<br>";
}
echo "---------------------------------------<br>";
}
$b++;
}
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$finish = $time;
$total_time = round(($finish - $start), 4);
if ( debug == "1" )
{
echo 'Page generated in '.$total_time.' seconds.'."\n";
}
mysql_close($con);
?>
I can give access to the source, MySQL etc if someone can help, i am learning right now, And i really enjoy how it goes for now, but the AJAX Jquery thing, i completely miss it!!
Thanx a lot!
You could wright more of an abstract question ... no need for php code ...
If i understood you right , you need to send ajax reqests on a specific time interval to get your updated data ( js function setInterval() )
Here is a link about setInterval : http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/
Here is link about ajax : http://www.w3schools.com/ajax/
If this answer has nothing to do with your question , please review it and state what you want to do with your data? Show it on each update? or what kind of update?
You could use javascript to trriger ajax script after 1 sec delay use setTimeout() in javascript for delay And in your php page retrieve the data and echo it.
I'm writing a quiz program using PHP and a bit of Javascript. The questions can be answered correctly when IE or Chrome is used but Firefox refreshes the page and increments the session variable.
Here's a code snippet:
if(isset($_GET['answer1']))
{
if($_GET['answer'] == $_GET['answer1'])
{
include 'config.php';
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db($db, $con);
$userId = $_SESSION['userId'];
$wordNow = $_SESSION['word'];
$sql3 = "SELECT * FROM userAccomplishments Where UserId = '$userId' AND
word = '$wordNow'";
$result3 = mysql_query($sql3);
if (mysql_num_rows($result3) == 0) {
$sql4 = "SELECT wordId from allwords WHERE word = '$wordNow'";
$result = mysql_query($sql4);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting from random number";
exit;
}
while ($row = mysql_fetch_assoc($result)) {
$wordId = $row["wordId"];
// echo $row["word"] . " " ."<BR>" ;
}
list($usec, $sec) = explode(' ', microtime());
$script_end = (float) $sec + (float) $usec;
$elapsed_time = (float)($script_end - $script_start);
$elapsed_time = $_GET['formvar'];
$sql2 = "INSERT INTO userAccomplishments (UserId, word, TimeTaken, wordId)
VALUES ('$userId', '$wordNow', '$elapsed_time', '$wordId')";
echo 'You got '. $wordNow . ' in ' .$elapsed_time . ' seconds. <BR><BR>';
$result = mysql_query($sql2);
}
$_SESSION['views'] = $_SESSION['views'] + 1;
}
else
{
$_SESSION['views'] = $_SESSION['views'] + 1;
echo 'Keep studying <BR>';
}
}
else
{
$_SESSION['views'] = 1;
}
Doesn't look like you're calling session_start(). Make sure you call it to get your session in gear! Woo! Yeah!
i'm trying to run this php code which should display a quote from mysql, but can't figure out where is it going wrong. the result variable is null or empty. can someone help me out. thanks!
<?php
include 'config.php';
// 'text' is the name of your table that contains
// the information you want to pull from
$rowcount = mysql_query("select count(*) as rows from quotes");
// Gets the total number of items pulled from database.
while ($row = mysql_fetch_assoc($rowcount))
{
$max = $row["rows"];
//print_r ($max);
}
// Selects an item's index at random
$rand = rand(1,$max)-1;
print_r ($rand);
$result = mysql_query("select * from quotes limit $rand, 1") or die ('Error: '.mysql_error());
if (!$result or mysql_num_rows($result))
{
echo "Empty";
}
else{
while ($row = mysql_fetch_array($result)) {
$randomOutput = $row['cQuotes'];
echo '<p>' . $randomOutput . '</p>';
}
}
$result = mysql_query("SELECT * FROM quotes ORDER BY rand() LIMIT 1") or die ('Error: '.mysql_error());
if (!$result || mysql_num_rows($result) == 0)
echo "Empty";
else {
while ($row = mysql_fetch_array($result)) {
$randomOutput = $row['cQuotes'];
echo '<p>' . $randomOutput . '</p>';
}
}
// your script probably can't go on without this file?
require 'config.php';
// I prefer to always pass the connection resource to mysql_query/mysql_real_escape_string
// assume $mysql = mysql_connect....
$result = mysql_query("SELECT Count(*) AS rows FROM quotes", $mysql)
or die(mysql_error());
// there's only one row with only one column, so mysql_result() is fine
$rowcount = mysql_result($result, 0, 0);
$rand = rand(0,$rowcount-1);
$result = mysql_query("SELECT cQuotes FROM quotes LIMIT $rand, 1", $mysql)
or die ('Error: '.mysql_error());
// there's either one or zero records. Again, no need for a while loop
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if ( !$row ) {
echo "Empty";
}
else{
// do you have to treat $row['cQuotes'] with htmlspecialchars()?
echo '<p>', $row['cQuotes'], '</p>';
}
if ($result && mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
$randomOutput = $row['cQuotes'];
echo '<p>' . $randomOutput . '</p>';
}
} else {
echo "Empty";
}